BitMagic-C++
sample14.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 sample14.cpp
20  Exmaple demonstrates bitvector serialization/deserialization and set-operations on
21  searialized BLOBs
22 
23  \sa bm::serializer
24  \sa bm::deserialize
25 */
26 
27 /*! \file sample14.cpp
28  \brief Example: bvector<> set operations on serialized/compressed BLOBs
29 */
30 
31 
32 #include <stdlib.h>
33 #include <iostream>
34 #include <vector>
35 
36 #include "bm.h"
37 #include "bmserial.h"
38 
39 using namespace std;
40 
41 
42 const unsigned MAX_VALUE = 1000000;
43 
44 static
46 {
47  for (unsigned i = 0; i < MAX_VALUE; ++i)
48  {
49  if ((rand() % 10))
50  {
51  bv->set(i);
52  }
53  }
54 }
55 
56 
57 int main(void)
58 {
59  try
60  {
61  bm::bvector<> bv1;
62  bm::bvector<> bv2;
63 
64  fill_bvector(&bv1);
65  fill_bvector(&bv2);
66 
67  cout << "bv1 count = " << bv1.count() << endl;
68  cout << "bv2 count = " << bv2.count() << endl;
69 
70 
71  // Prepare a serializer class
72  // for best performance - create serilizer once and reuse it
73  //
76  bvs.set_compression_level(4);
77 
80 
81  // compress bit-vectors and compute statistics
82  // (later re-used in serialization)
83  //
86 
87  // declare serialization buffers
88  bm::serializer<bm::bvector<> >::buffer sbuf1;
89  bm::serializer<bm::bvector<> >::buffer sbuf2;
90 
91  // perform serialization
92  //
93  bvs.serialize(bv1, sbuf1, &st1);
94  bvs.serialize(bv2, sbuf2, &st2);
95 
96 
97  // Serialized bvectors (sbuf1 and sbuf2) now ready to be
98  // saved to a database, file or send over a network.
99  // to simulate this we just copy content to std::vector<>
100  //
101 
102  std::vector<unsigned char> vect1;
103  std::vector<unsigned char> vect2;
104 
105  vect1.resize(sbuf1.size());
106  vect2.resize(sbuf2.size());
107 
108  ::memcpy(vect1.data(), sbuf1.buf(), sbuf1.size());
109  ::memcpy(vect2.data(), sbuf2.buf(), sbuf2.size());
110 
111 
112  // Simple deserialization.
113  //
114  bm::bvector<> bv3;
115 
116  // As a result of desrialization bv3 will contain all bits from
117  // bv1 and bv3:
118  // bv3 = bv1 OR bv2
119 
120  bm::deserialize(bv3, sbuf1.buf());
121  bm::deserialize(bv3, sbuf2.buf());
122 
123  bv3.optimize(tb);
124 
125  // A few examples of operation deserializations
126  // set algebraic operation over bit-vector and a BLOB
127  //
128  bm::bvector<> bv4(bv3);
129 
130  // bv4 = (bv1 OR bv2) AND bv1
131  // this must be equal to bv1 ?
132  //
134  vect1.data(),
135  tb,
136  bm::set_AND);
137 
138  cout << "bv4 count = " << bv4.count() << endl;
139  int cmp = bv1.compare(bv4);
140  if (cmp != 0)
141  {
142  cerr << "Logical error detected!" << endl;
143  return 1;
144  }
145  else
146  cout << "bv4 is equal to bv1" << endl;
147 
148  bm::bvector<> bv5(bv3);
149 
150 
151  // if we need just set count, we can get it faster
152  // via set_COUNT_ operations
153  // use of COUNT operations does not materialize a target vector
154  //
155  // POPCNT((bv1 OR bv2) MINUS bv1)
156  auto cnt_sub =
158  sbuf1.buf(),
159  tb,
161  cout << "minus count = " << cnt_sub << endl;
162 
163 
164  // or we can actually perform the operation and get the full vector
165  // bv5 = (bv1 OR bv2) MINUS bv1
166  //
168  sbuf1.buf(),
169  tb,
170  bm::set_SUB);
171  auto bv5_cnt = bv5.count();
172  cout << "bv5 count = " << bv5_cnt << endl;
173 
174  if (cnt_sub != bv5_cnt)
175  {
176  cerr << "Logical error!" << endl;
177  return 1;
178  }
179 
180 
181  }
182  catch(std::exception& ex)
183  {
184  std::cerr << ex.what() << std::endl;
185  return 1;
186  }
187 
188 
189  return 0;
190 }
191 
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
size_t deserialize(BV &bv, const unsigned char *buf, bm::word_t *temp_block=0)
Bitvector deserialization from memory.
Definition: bmserial.h:1900
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector&#39;s memory allocation.
Definition: bm.h:3136
Bit-vector serialization class.
Definition: bmserial.h:77
int compare(const bvector< Alloc > &bvect) const
Lexicographical comparison with a bitvector.
Definition: bm.h:3224
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
Statistical information about bitset&#39;s memory allocation details.
Definition: bm.h:121
bvector< Alloc > & set(size_type n, bool val=true)
Sets bit n if val is true, clears bit n if val is false.
Definition: bm.h:3539
size_type serialize(const BV &bv, unsigned char *buf, size_t buf_size)
Bitvector serialization into memory block.
Definition: bmserial.h:1608
size_type count() const
population cout (count of ON bits)
Definition: bm.h:2487
const unsigned MAX_VALUE
Definition: sample14.cpp:42
int main(void)
Definition: sample14.cpp:57
static void fill_bvector(bm::bvector<> *bv)
Definition: sample14.cpp:45
Serialization / compression of bvector<>. Set theoretical operations on compressed BLOBs...
void set_compression_level(unsigned clevel)
Set compression level.
Definition: bmserial.h:790
Deserializer, performs logical operations between bit-vector and serialized bit-vector.
Definition: bmserial.h:622