BitMagic-C++: svsample04.cpp Source File

BitMagic-C++

svsample04.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15
16 For 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 */
24
25 /*! \file svsample04.cpp
26 \brief Example: sparse_vector<> with NULL (unset) values
27 */
28
29 #include <iostream>
30 #include <vector>
31
32 #include "bm.h"
33 #include "bmsparsevec.h"
34
35 using namespace std;
36
37 static
38 void print_svector(const bm::sparse_vector<unsigned, bm::bvector<> >& sv)
39 {
40 if (sv.size() == 0)
41 {
42 cout << sv.size() << ": [ EMPTY ]" << endl;
43 return;
44 }
45 cout << sv.size() << ": [ ";
46 for (unsigned i = 0; i < sv.size(); ++i)
47 {
48 unsigned v = sv.at(i);
49 bool is_null = sv.is_null(i);
50
51 if (is_null)
52 cout << "NULL";
53 else
54 cout << v << "";
55
56 if (i == sv.size()-1)
57 cout << " ]";
58 else
59 cout << ", ";
60 }
61 cout << endl;
62 }
63
64 int main(void)
65 {
66 try
67 {
68 // sparse vector can support unassigned value semantics
69 // (also known as NULL value in databases)
70 //
71 // to enable this function sparse_vector<> needs to be constructed using
72 // special flag ( bm::use_null )
73 //
74 bm::sparse_vector<unsigned, bm::bvector<> > sv1(bm::use_null);
75
76 sv1.resize(10);
77 sv1.set(2, 25);
78 sv1.set(3, 35);
79 sv1.set(7, 75);
80
81 print_svector(sv1); // 10: [ NULL, NULL, 25, 35, NULL, NULL, NULL, 75, NULL, NULL ]
82
83 sv1.set_null(7);
84
85 print_svector(sv1); // 10: [ NULL, NULL, 25, 35, NULL, NULL, NULL, NULL, NULL, NULL ]
86
87 unsigned arr[3] = {1,2,3};
88 sv1.import(arr, 3, 9); // import from a C-style array (by index 9)
89 print_svector(sv1); // 12: [ NULL, NULL, 25, 35, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
90
91 sv1.clear(2, true); // clear element and set to NULL
92 print_svector(sv1); // 12: [ NULL, NULL, NULL, 35, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
93
94 sv1.clear(2); // clear element (not NULL anymore)
95 print_svector(sv1); // 12: [ NULL, NULL, 0, 35, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
96
97 sv1.clear(3); // clear element (it stays NOT NULL)
98 print_svector(sv1); // 12: [ NULL, NULL, NULL, 0, NULL, NULL, NULL, NULL, NULL, 1, 2, 3 ]
99
100 sv1.clear();
101 print_svector(sv1); // 0: [ EMPTY ]
102
103 sv1.resize(3);
104 print_svector(sv1); // 3: [ NULL, NULL, NULL ]
105
106 sv1.push_back(10);
107 sv1.push_back(20);
108
109 print_svector(sv1); // 5: [ NULL, NULL, NULL, 10, 20 ]
110
111
112 // construct not-null-able sparse vector
113 //
114 bm::sparse_vector<unsigned, bm::bvector<> > sv2;
115 sv2.push_back(100);
116 sv2.push_back(200);
117
118 sv1.join(sv2); // merge two vectors together
119
120 print_svector(sv1); // 5: [ 100, 200, NULL, 10, 20 ]
121
122 // construct another NULL-able vector for another join
123 bm::sparse_vector<unsigned, bm::bvector<> > sv3(bm::use_null);
124 sv3.resize(9);
125 sv3.push_back(300);
126
127 // this join will fully respect the NULL flags assigned in both sparse vectors
128 sv1.join(sv3);
129
130 print_svector(sv1); // 10: [ 100, 200, NULL, 10, 20, NULL, NULL, NULL, NULL, 300 ]
131
132 // traverse and print the non-NULL values
133 // using bit-vector of assigned values
134 //
135 // [0] = 100, [1] = 200, [3] = 10, [4] = 20, [9] = 300
136 //
137 {
138 const bm::bvector<>* bv_non_null = sv1.get_null_bvector();
139 if (bv_non_null)
140 {
141 bm::bvector<>::enumerator en = bv_non_null->first();
142
143 for (; en.valid(); ++en)
144 {
145 unsigned idx = *en;
146 unsigned v = sv1[idx];
147 std::cout << "[" << idx << "] = " << v << ", ";
148 }
149 std::cout << endl;
150 }
151 }
152
153
154 // decode sparse_vector with NULL values
155 // decode ignores NULLs (in our case replaces values with '0s'
156 //
157 {
158 std::vector<unsigned> v1(sv1.size());
159 sv1.decode(&v1[0], 0, sv1.size()); // extract elements starting from 0
160
161 // 100,200,0,10,20,0,0,0,0,300,
162 for (unsigned i = 0; i < v1.size(); ++i)
163 {
164 cout << v1[i] << ",";
165 }
166 cout << endl;
167 }
168 }
169 catch(std::exception& ex)
170 {
171 std::cerr << ex.what() << std::endl;
172 return 1;
173 }
174
175 return 0;
176 }
177
bm::sparse_vector
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
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::sparse_vector::set_null
void set_null(size_type idx)
set specified element to unassigned value (NULL)
Definition: bmsparsevec.h:986
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::sparse_vector::import
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:994
bm::sparse_vector::clear
void clear(size_type idx, bool set_null=false)
clear specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1481
main
int main(void)
Definition: svsample04.cpp:64
bm::sparse_vector::size
size_type size() const BMNOEXCEPT
return size of the vector
Definition: bmsparsevec.h:644
bm::sparse_vector::push_back
void push_back(value_type v)
push value back into vector
Definition: bmsparsevec.h:1498
print_svector
static void print_svector(const bm::sparse_vector< unsigned, bm::bvector<> > &sv)
Definition: svsample04.cpp:38
bm::sparse_vector::decode
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:1083
bm::bvector::first
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1770
bm::sparse_vector::join
sparse_vector< Val, BV > & join(const sparse_vector< Val, BV > &sv)
join all with another sparse vector using OR operation
Definition: bmsparsevec.h:1764
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
bm::base_sparse_vector< Val, BV, 1 >::get_null_bvector
const bvector_type * get_null_bvector() const BMNOEXCEPT
Get bit-vector of assigned values or NULL (if not constructed that way)
Definition: bmbmatrix.h:329
bm::sparse_vector::resize
void resize(size_type sz)
resize vector
Definition: bmsparsevec.h:654