BitMagic-C++
svsample05.cpp

Example how to use for bvector re-mapping / transformation based on sparse_vector as a translation table.

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"
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() << ")" << ": [";
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)
//
// 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.
bool is_null(size_type idx) const BMNOEXCEPT
test if specified element is NULL
Definition bmbmatrix.h:1285
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition bm.h:1770
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.
sparse vector with runtime compression using bit transposition method
Definition bmsparsevec.h:82
value_type at(size_type idx) const
access specified element with bounds checking
size_type size() const BMNOEXCEPT
return size of the vector
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
@ use_null
support "non-assigned" or "NULL" logic
Definition bmconst.h:215
unsigned int id_t
Definition bmconst.h:37
int main(void)
Definition sample1.cpp:38
static void print_bvector(const bm::bvector<> &bv)
Definition sample2.cpp:34
static void print_svector(const bm::sparse_vector< unsigned, bm::bvector<> > &sv)
bm::sparse_vector< unsigned, bm::bvector<> > sparse_vector_u32
Definition xsample02.cpp:66