BitMagic-C++
svsample05.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For more information please visit: http://bitmagic.io
17*/
18
19/** \example svsample05.cpp
20 Example how to use for bvector re-mapping / transformation
21 based on sparse_vector as a translation table.
22
23 Example discusses how use of NULL (unassigned) sparse_vector<> semantics affects
24 behavior of image transformation algorithm.
25
26 \sa bm::sparse_vector<>
27 \sa bm::set2set_11_transform
28*/
29
30/*! \file svsample05.cpp
31 \brief Example: sparse_vector<> used for set 2 set remapping (theory of groups Image)
32*/
33
34
35#include <iostream>
36#include <vector>
37
38#include "bm.h"
39#include "bmsparsevec.h"
40#include "bmsparsevec_algo.h"
41
42using namespace std;
43
45
46static
48{
49 if (sv.size() == 0)
50 {
51 cout << sv.size() << ": [ EMPTY ]" << endl;
52 return;
53 }
54 cout << sv.size() << ": [ ";
55 for (unsigned i = 0; i < sv.size(); ++i)
56 {
57 unsigned v = sv.at(i);
58 bool is_null = sv.is_null(i);
59
60 if (is_null)
61 cout << "NULL";
62 else
63 cout << v << "";
64
65 if (i == sv.size()-1)
66 cout << " ]";
67 else
68 cout << ", ";
69 }
70 cout << endl;
71}
72
73inline
75{
76 cout << "( count = " << bv.count() << ")" << ": [";
77 bm::bvector<>::enumerator en = bv.first();
78 for (; en.valid(); ++en)
79 {
80 cout << *en << ", ";
81 }
82 cout << "]" << endl;
83}
84
85
86int main(void)
87{
88 try
89 {
90 // initialize sparse_vector as a binary relation (transform function)
91 // to translate from one set to another (please note it uses NULL values)
92 //
93 sparse_vector_u32 sv_transform(bm::use_null);
94
95 // transformation function defined as a table:
96 // 2 -> 25
97 // 3 -> 35
98 // 7 -> 75
99 // 1000 -> 2000
100 // 256 -> 2001
101
102 sv_transform.set(2, 25);
103 sv_transform.set(3, 35);
104 sv_transform.set(7, 75);
105 sv_transform.set(1000, 2000);
106 sv_transform.set(256, 2001);
107
108 print_svector(sv_transform);
109
110 // initialize input bit-vector
111 //
112 bm::bvector<> bv_in { 1, 2, 3, 1000, 256 };
113
114 bm::bvector<> bv_out;
115
117 func.run(bv_in, sv_transform, bv_out);
118
119 std::cout << "re-mapped vector:" << endl;
120 print_bvector(bv_out); // ( count = 4): [25, 35, 2000, 2001, ]
121
122 // Please note, that remapping request for "1" cannot be satisfied
123 // as sv_transform vector did not assign this element so it has
124 // special unassigned, NULL value
125
126 bm::id_t from = 1;
127 bm::id_t to;
128 bool found = func.remap(1, sv_transform, to);
129 if (!found)
130 std::cout << from << " not mapped" << std::endl; // THIS works!
131 else
132 std::cout << from << " mapped to " << to << std::endl;
133
134
135 // now lets try another experiment here, construct image transform
136 // function as default NOT using NULL values
137
138 sparse_vector_u32 sv_transform2;
139
140 sv_transform2.set(2, 25);
141 sv_transform2.set(3, 35);
142 sv_transform2.set(7, 75);
143 sv_transform2.set(1000, 2000);
144 sv_transform2.set(256, 2001);
145
146 found = func.remap(1, sv_transform2, to);
147 if (!found)
148 std::cout << from << " not mapped" << std::endl;
149 else
150 std::cout << from << " mapped to " << to << std::endl; // Now THIS works!
151
152 // Please note that remapping for unassigned value - works and maps to 0
153 // because transformation sparse vector does not keep information about
154 // unassigned values, all unassigned are now implicitly assumed to be "0"
155
156 func.run(bv_in, sv_transform2, bv_out);
157
158 std::cout << "re-mapped vector2:" << endl;
159 print_bvector(bv_out); // ( count = 5): [0, 25, 35, 2000, 2001, ]
160
161 }
162 catch(std::exception& ex)
163 {
164 std::cerr << ex.what() << std::endl;
165 return 1;
166 }
167
168 return 0;
169}
170
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
bm::sparse_vector< bm::id_t, bm::bvector<> > sparse_vector_u32
int main(void)
void print_bvector(const bm::bvector<> &bv)
static void print_svector(const sparse_vector_u32 &sv)