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 "bmundef.h" /* clear the pre-proc defines from BM */
using namespace std;
static
{
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
{
cout << "( count = " << bv.count() << ")" << ": [";
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)
//
// 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;
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;
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;
}
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
Algorithms for bm::sparse_vector.
pre-processor un-defines to avoid global space pollution (internal)
bool is_null(size_type idx) const BMNOEXCEPT
test if specified element is NULL
Definition: bmbmatrix.h:1709
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
bool valid() const BMNOEXCEPT
Checks if iterator is still valid.
Definition: bm.h:283
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1849
Integer set to set transformation (functional image in groups theory) https://en.wikipedia....
void remap(const bvector_type &bv_in, bvector_type &bv_out)
Perform remapping (Image function) (based on attached translation table)
void run(const bvector_type &bv_in, const SV &sv_brel, bvector_type &bv_out)
Run remap transformation.
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
value_type at(size_type idx) const
access specified element with bounds checking
Definition: bmsparsevec.h:1730
size_type size() const BMNOEXCEPT
return size of the vector
Definition: bmsparsevec.h:711
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1804
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:229
unsigned int id_t
Definition: bmconst.h:38
void print_svector(const SV &sv, bool show_nulls=false)
Prints the vector using is_null() and get() Please note, that random access is not the fastest,...
Definition: rscsample01.cpp:61
bm::sparse_vector< bm::id_t, bm::bvector<> > sparse_vector_u32
Definition: svsample05.cpp:45
int main(void)
Definition: svsample05.cpp:87
void print_bvector(const bm::bvector<> &bv)
Definition: svsample05.cpp:75