BitMagic-C++
svsample04.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 svsample04.cpp
20 Example how to use NULL-enabled sparse vector
21
22 \sa bm::sparse_vector
23 \sa bm::sparse_vector::clear
24 \sa bm::sparse_vector::set_null
25 \sa bm::sparse_vector::push_back
26 \sa bm::sparse_vector::decode
27 \sa bm::sparse_vector::resize
28 \sa bm::sparse_vector::join
29 \sa bm::bvector::enumerator
30
31*/
32
33/*! \file svsample04.cpp
34 \brief Example: sparse_vector<> with NULL (unset) values
35*/
36
37#include <iostream>
38#include <vector>
39
40#include "bm.h"
41#include "bmsparsevec.h"
42#include "bmundef.h" /* clear the pre-proc defines from BM */
43
44using namespace std;
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
73int main(void)
74{
75 try
76 {
77 // sparse vector can support unassigned value semantics
78 // (also known as NULL value in databases)
79 //
80 // to enable this function sparse_vector<> needs to be constructed using
81 // special flag ( bm::use_null )
82 //
84
85 sv1.resize(10);
86 sv1.set(2, 25);
87 sv1.set(3, 35);
88 sv1.set(7, 75);
89
90 print_svector(sv1); // 10: [ NULL, NULL, 25, 35, NULL, NULL, NULL, 75, NULL, NULL ]
91
92 sv1.set_null(7);
93
94 print_svector(sv1); // 10: [ NULL, NULL, 25, 35, NULL, NULL, NULL, NULL, NULL, NULL ]
95
96 unsigned arr[3] = {1,2,3};
97 sv1.import(arr, 3, 9); // import from a C-style array (by index 9)
98 print_svector(sv1); // 12: [ NULL, NULL, 25, 35, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
99
100 sv1.clear(2, true); // clear element and set to NULL
101 print_svector(sv1); // 12: [ NULL, NULL, NULL, 35, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
102
103 sv1.clear(2, false); // clear element (not NULL anymore)
104 print_svector(sv1); // 12: [ NULL, NULL, 0, 35, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
105
106 sv1.clear(3, false); // clear element (it stays NOT NULL)
107 print_svector(sv1); // 12: [ NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
108
109 sv1.clear();
110 print_svector(sv1); // 0: [ EMPTY ]
111
112 sv1.resize(3);
113 print_svector(sv1); // 3: [ NULL, NULL, NULL ]
114
115 sv1.push_back(10);
116 sv1.push_back(20);
117 sv1.push_back(30);
118
119 print_svector(sv1); // 5: [ NULL, NULL, NULL, 10, 20, 30 ]
120
121 // bulk clear the vector (can be a lot faster than random element access)
122 {
123 bm::bvector<> bv_clear { 3, 5 }; // clear sv1[3]=0 and sv1[5]=0
124 sv1.clear(bv_clear); // clear multiple elemets at once
125 print_svector(sv1); // 5: [ NULL, NULL, NULL, 0, 20, 0 ]
126 // please note, that clear does not set elements to NULL (but to 0)
127 }
128
129 sv1.set(1, 10);
130 sv1.set(2, 20);
131 sv1.push_back(40);
132
133 print_svector(sv1); // 5: [ NULL, 10, 20, 0, 20, 0, 40 ]
134 // bulk set_null the vector
135 {
136 bm::bvector<> bv_set_null { 0, 1, 3, 5 }; // NULLs sv1[3] and sv1[5]
137 sv1.set_null(bv_set_null); // clear multiple elemets at once
138 print_svector(sv1); // 5: [ NULL, NULL, 20, NULL, 20, NULL, 40]
139 // please note, that clear both sets to zero and sets it to NULL
140 }
141
142
143
144
145 // construct not-null-able sparse vector
146 //
148 sv2.push_back(100);
149 sv2.push_back(200);
150
151 sv1.join(sv2); // merge two vectors together
152
153 print_svector(sv1); // 5: [ 100, 200, NULL, 10, 20 ]
154
155 // construct another NULL-able vector for another join
157 sv3.resize(9);
158 sv3.push_back(300);
159
160 // this join will fully respect the NULL flags assigned in both sparse vectors
161 sv1.join(sv3);
162
163 print_svector(sv1); // 10: [ 100, 200, NULL, 10, 20, NULL, NULL, NULL, NULL, 300 ]
164
165 // traverse and print the non-NULL values
166 // using bit-vector of assigned values
167 //
168 // [0] = 100, [1] = 200, [3] = 10, [4] = 20, [9] = 300
169 //
170 {
171 const bm::bvector<>* bv_non_null = sv1.get_null_bvector();
172 if (bv_non_null)
173 {
174 bm::bvector<>::enumerator en = bv_non_null->first();
175
176 for (; en.valid(); ++en)
177 {
178 unsigned idx = *en;
179 unsigned v = sv1[idx];
180 std::cout << "[" << idx << "] = " << v << ", ";
181 }
182 std::cout << endl;
183 }
184 }
185
186
187 // decode sparse_vector with NULL values
188 // decode ignores NULLs (in our case replaces values with '0s'
189 //
190 {
191 std::vector<unsigned> v1(sv1.size());
192 sv1.decode(&v1[0], 0, sv1.size()); // extract elements starting from 0
193
194 // 100,200,0,10,20,0,0,0,0,300,
195 for (unsigned i = 0; i < v1.size(); ++i)
196 {
197 cout << v1[i] << ",";
198 }
199 cout << endl;
200 }
201 }
202 catch(std::exception& ex)
203 {
204 std::cerr << ex.what() << std::endl;
205 return 1;
206 }
207
208 return 0;
209}
210
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
pre-processor un-defines to avoid global space pollution (internal)
const bvector_type * get_null_bvector() const BMNOEXCEPT
Get bit-vector of assigned values or NULL (if not constructed that way)
Definition: bmbmatrix.h:430
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
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1849
succinct sparse vector with runtime compression using bit-slicing / transposition method
Definition: bmsparsevec.h:87
void set_null(size_type idx)
set specified element to unassigned value (NULL)
Definition: bmsparsevec.h:1146
void push_back(value_type v)
push value back into vector
Definition: bmsparsevec.h:1838
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
void resize(size_type sz)
resize vector
Definition: bmsparsevec.h:721
void clear(size_type idx, bool set_null)
clear specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1820
sparse_vector< Val, BV > & join(const sparse_vector< Val, BV > &sv)
join all with another sparse vector using OR operation
Definition: bmsparsevec.h:2127
size_type decode(value_type *arr, size_type idx_from, size_type dec_size, bool zero_mem=true) const
Bulk export list of elements to a C-style array.
Definition: bmsparsevec.h:1338
void import(const value_type *arr, size_type arr_size, size_type offset=0, bool set_not_null=true)
Import list of elements from a C-style array.
Definition: bmsparsevec.h:1154
@ use_null
support "non-assigned" or "NULL" logic
Definition: bmconst.h:229
static void print_svector(const bm::sparse_vector< unsigned, bm::bvector<> > &sv)
Definition: svsample04.cpp:47
int main(void)
Definition: svsample04.cpp:73