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  //
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  //
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
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 
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Sparse constainer sparse_vector<> for integer types using bit-transposition transform.
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:1039
void set(size_type idx, value_type v)
set specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1460
const bvector_type * get_null_bvector() const
Get bit-vector of assigned values or NULL (if not constructed that way)
Definition: bmbmatrix.h:319
sparse_vector< Val, BV > & join(const sparse_vector< Val, BV > &sv)
join all with another sparse vector using OR operation
Definition: bmsparsevec.h:1754
void import(const value_type *arr, size_type arr_size, size_type offset=0)
Import list of elements from a C-style array.
Definition: bmsparsevec.h:953
sparse vector with runtime compression using bit transposition method
Definition: bmsparsevec.h:81
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:2122
void push_back(value_type v)
push value back into vector
Definition: bmsparsevec.h:1489
support "non-assigned" or "NULL" logic
Definition: bmconst.h:216
void resize(size_type sz)
resize vector
Definition: bmsparsevec.h:623
bool valid() const
Checks if iterator is still valid.
Definition: bm.h:277
void set_null(size_type idx)
set specified element to unassigned value (NULL)
Definition: bmsparsevec.h:945
int main(void)
Definition: svsample04.cpp:64
size_type size() const
return size of the vector
Definition: bmsparsevec.h:613
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:590
static void print_svector(const bm::sparse_vector< unsigned, bm::bvector<> > &sv)
Definition: svsample04.cpp:38
void clear(size_type idx, bool set_null=false)
clear specified element with bounds checking and automatic resize
Definition: bmsparsevec.h:1472