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 {
60  try
61  {
62  bm::bvector<> bv1;
63  bm::bvector<> bv2;
64 
65  fill_bvector(&bv1);
66  fill_bvector(&bv2);
67 
68  cout << "bv1 count = " << bv1.count() << endl;
69  cout << "bv2 count = " << bv2.count() << endl;
70 
71 
72  // Prepare a serializer class
73  // for best performance - create serilizer once and reuse it
74  //
77  bvs.set_compression_level(4);
78 
81 
82  // compress bit-vectors and compute statistics
83  // (later re-used in serialization)
84  //
87 
88  // declare serialization buffers
89  bm::serializer<bm::bvector<> >::buffer sbuf1;
90  bm::serializer<bm::bvector<> >::buffer sbuf2;
91 
92  // perform serialization
93  //
94  bvs.serialize(bv1, sbuf1, &st1);
95  bvs.serialize(bv2, sbuf2, &st2);
96 
97 
98  // Serialized bvectors (sbuf1 and sbuf2) now ready to be
99  // saved to a database, file or send over a network.
100  // to simulate this we just copy content to std::vector<>
101  //
102 
103  std::vector<unsigned char> vect1;
104  std::vector<unsigned char> vect2;
105 
106  vect1.resize(sbuf1.size());
107  vect2.resize(sbuf2.size());
108 
109  ::memcpy(vect1.data(), sbuf1.buf(), sbuf1.size());
110  ::memcpy(vect2.data(), sbuf2.buf(), sbuf2.size());
111 
112 
113  // Simple deserialization.
114  //
115  bm::bvector<> bv3;
116 
117  // As a result of desrialization bv3 will contain all bits from
118  // bv1 and bv3:
119  // bv3 = bv1 OR bv2
120 
121  bm::deserialize(bv3, sbuf1.buf());
122  bm::deserialize(bv3, sbuf2.buf());
123 
124  bv3.optimize(tb);
125 
126  // A few examples of operation deserializations
127  // set algebraic operation over bit-vector and a BLOB
128  //
129  bm::bvector<> bv4(bv3);
130 
131  // bv4 = (bv1 OR bv2) AND bv1
132  // this must be equal to bv1 ?
133  //
134  od.deserialize(bv4, vect1.data(), bm::set_AND);
135 
136  cout << "bv4 count = " << bv4.count() << endl;
137  bool eq = bv1.equal(bv4);
138  if (!eq)
139  {
140  cerr << "Logical error detected!" << endl;
141  return 1;
142  }
143  else
144  cout << "bv4 is equal to bv1" << endl;
145 
146  bm::bvector<> bv5(bv3);
147 
148 
149  // if we need just set count, we can get it faster
150  // via set_COUNT_ operations
151  // use of COUNT operations does not materialize a target vector
152  //
153  // POPCNT((bv1 OR bv2) MINUS bv1)
154  auto cnt_sub =
155  od.deserialize(bv3, sbuf1.buf(), bm::set_COUNT_SUB_AB);
156  cout << "minus count = " << cnt_sub << endl;
157 
158 
159  // or we can actually perform the operation and get the full vector
160  // bv5 = (bv1 OR bv2) MINUS bv1
161  //
162  od.deserialize(bv5, sbuf1.buf(), tb, bm::set_SUB);
163  auto bv5_cnt = bv5.count();
164  cout << "bv5 count = " << bv5_cnt << endl;
165 
166  if (cnt_sub != bv5_cnt)
167  {
168  cerr << "Logical error!" << endl;
169  return 1;
170  }
171  }
172  catch(std::exception& ex)
173  {
174  std::cerr << ex.what() << std::endl;
175  return 1;
176  }
177 
178 
179  return 0;
180 }
181 
bm::deserialize
size_t deserialize(BV &bv, const unsigned char *buf, bm::word_t *temp_block=0, const bm::bv_ref_vector< BV > *ref_vect=0)
Bitvector deserialization from a memory BLOB.
Definition: bmserial.h:2688
bm::set_AND
Definition: bmconst.h:154
bm::set_SUB
Definition: bmconst.h:156
BM_DECLARE_TEMP_BLOCK
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
bm::serializer::serialize
size_type serialize(const BV &bv, unsigned char *buf, size_t buf_size)
Bitvector serialization into memory block.
Definition: bmserial.h:2264
main
int main(void)
Definition: sample14.cpp:57
fill_bvector
static void fill_bvector(bm::bvector<> *bv)
Definition: sample14.cpp:45
bm::serializer::set_compression_level
void set_compression_level(unsigned clevel) BMNOEXCEPT
Set compression level.
Definition: bmserial.h:1119
bm::bvector<>
bm::bvector::set
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:3581
MAX_VALUE
const unsigned MAX_VALUE
Definition: sample14.cpp:42
bm::bvector::count
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition: bm.h:2194
bm::bvector::equal
bool equal(const bvector< Alloc > &bvect) const BMNOEXCEPT
Equal comparison with an agr bit-vector.
Definition: bm.h:1883
bm::operation_deserializer
Deserializer, performs logical operations between bit-vector and serialized bit-vector.
Definition: bmserial.h:824
bm::serializer
Bit-vector serialization class.
Definition: bmserial.h:75
bm::set_COUNT_SUB_AB
Definition: bmconst.h:163
bm::operation_deserializer::deserialize
size_type deserialize(bvector_type &bv, const unsigned char *buf, set_operation op, bool exit_on_one=false)
Deserialize bvector using buffer as set operation argument.
Definition: bmserial.h:5772
bmserial.h
Serialization / compression of bvector<>. Set theoretical operations on compressed BLOBs.
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::bvector::optimize
void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)
Optimize memory bitvector's memory allocation.
Definition: bm.h:3071
bm::bvector::statistics
Statistical information about bitset's memory allocation details.
Definition: bm.h:121