BitMagic-C++: svsample05.cpp

BitMagic-C++

svsample05.cpp

Example how to use for bvector re-mapping / transformation based on sparse_vector as a translation table.Example discusses how use of NULL (unassigned) sparse_vector<> semantics affects behavior of image transformation algorithm.

See also
  bm::sparse_vector<>
  bm::set2set_11_transform

/*
Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For more information please visit: http://bitmagic.io
*/
/** \example svsample05.cpp
Example how to use for bvector re-mapping / transformation
based on sparse_vector as a translation table.
Example discusses how use of NULL (unassigned) sparse_vector<> semantics affects
behavior of image transformation algorithm.
\sa bm::sparse_vector<>
\sa bm::set2set_11_transform
*/
/*! \file svsample05.cpp
\brief Example: sparse_vector<> used for set 2 set remapping (theory of groups Image)
*/
#include <iostream>
#include <vector>
#include "bm.h"
#include "bmsparsevec.h"
#include "bmsparsevec_algo.h"
using namespace std;
typedef bm::sparse_vector<bm::id_t, bm::bvector<> > sparse_vector_u32;
static
void print_svector(const sparse_vector_u32& sv)
{
if (sv. size() == 0)
{
cout << sv.size() << ": [ EMPTY ]" << endl;
return;
}
cout << sv.size() << ": [ ";
for (unsigned i = 0; i < sv.size(); ++i)
{
unsigned v = sv. at(i);
bool is_null = sv. is_null(i);
if (is_null)
cout << "NULL";
else
cout << v << "";
if (i == sv.size()-1)
cout << " ]";
else
cout << ", ";
}
cout << endl;
}
inline
void print_bvector(const bm::bvector<>& bv)
{
cout << "( count = " << bv. count() << ")" << ": [";
bm::bvector<>::enumerator en = bv. first();
for (; en. valid(); ++en)
{
cout << *en << ", ";
}
cout << "]" << endl;
}
int main(void)
{
try
{
// initialize sparse_vector as a binary relation (transform function)
// to translate from one set to another (please note it uses NULL values)
//
sparse_vector_u32 sv_transform(bm::use_null);
// transformation function defined as a table:
// 2 -> 25
// 3 -> 35
// 7 -> 75
// 1000 -> 2000
// 256 -> 2001
sv_transform.set(2, 25);
sv_transform.set(3, 35);
sv_transform.set(7, 75);
sv_transform.set(1000, 2000);
sv_transform.set(256, 2001);
print_svector(sv_transform);
// initialize input bit-vector
//
bm::bvector<> bv_in { 1, 2, 3, 1000, 256 };
bm::bvector<> bv_out;
bm::set2set_11_transform<sparse_vector_u32> func;
func. run(bv_in, sv_transform, bv_out);
std::cout << "re-mapped vector:" << endl;
print_bvector(bv_out); // ( count = 4): [25, 35, 2000, 2001, ]
// Please note, that remapping request for "1" cannot be satisfied
// as sv_transform vector did not assign this element so it has
// special unassigned, NULL value
bm::id_t from = 1;
bm::id_t to;
bool found = func. remap(1, sv_transform, to);
if (!found)
std::cout << from << " not mapped" << std::endl; // THIS works!
else
std::cout << from << " mapped to " << to << std::endl;
// now lets try another experiment here, construct image transform
// function as default NOT using NULL values
sparse_vector_u32 sv_transform2;
sv_transform2. set(2, 25);
sv_transform2.set(3, 35);
sv_transform2.set(7, 75);
sv_transform2.set(1000, 2000);
sv_transform2.set(256, 2001);
found = func.remap(1, sv_transform2, to);
if (!found)
std::cout << from << " not mapped" << std::endl;
else
std::cout << from << " mapped to " << to << std::endl; // Now THIS works!
// Please note that remapping for unassigned value - works and maps to 0
// because transformation sparse vector does not keep information about
// unassigned values, all unassigned are now implicitly assumed to be "0"
func.run(bv_in, sv_transform2, bv_out);
std::cout << "re-mapped vector2:" << endl;
print_bvector(bv_out); // ( count = 5): [0, 25, 35, 2000, 2001, ]
}
catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
return 1;
}
return 0;
}
bm::set2set_11_transform::remap
void remap(const bvector_type &bv_in, bvector_type &bv_out)
Perform remapping (Image function) (based on attached translation table)
Definition: bmsparsevec_algo.h:982
bmsparsevec_algo.h
Algorithms for bm::sparse_vector.
bm::sparse_vector
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
bm::set2set_11_transform
Integer set to set transformation (functional image in groups theory) https://en.wikipedia....
Definition: bmsparsevec_algo.h:773
bmsparsevec.h
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
bm::bvector::enumerator
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:599
bm::bvector<>
bm::use_null
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:215
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2194
print_bvector
void print_bvector(const bm::bvector<> &bv)
Definition: svsample05.cpp:74
bm::bvector::iterator_base::valid
bool valid() const BMNOEXCEPT
Checks if iterator is still valid. Analog of != 0 comparison for pointers.
Definition: bm.h:280
bm::set2set_11_transform::run
void run(const bvector_type &bv_in, const SV &sv_brel, bvector_type &bv_out)
Run remap transformation.
Definition: bmsparsevec_algo.h:831
bm::sparse_vector::size
size_type size() const BMNOEXCEPT
return size of the vector
Definition: bmsparsevec.h:644
main
int main(void)
Definition: svsample05.cpp:86
bm::base_sparse_vector::is_null
bool is_null(size_type idx) const BMNOEXCEPT
test if specified element is NULL
Definition: bmbmatrix.h:1285
bm::id_t
unsigned int id_t
Definition: bmconst.h:37
sparse_vector_u32
bm::sparse_vector< bm::id_t, bm::bvector<> > sparse_vector_u32
Definition: svsample05.cpp:44
bm::sparse_vector::at
value_type at(size_type idx) const
access specified element with bounds checking
Definition: bmsparsevec.h:1434
print_svector
void print_svector(const SV &sv, bool show_nulls=false)
Definition: rscsample01.cpp:56
bm::bvector::first
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1770
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::sparse_vector::set
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1469