BitMagic-C++
sample14.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 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#include "bmundef.h" /* clear the pre-proc defines from BM */
39
40using namespace std;
41
42
43const unsigned MAX_VALUE = 1000000;
44
45static
47{
48 for (unsigned i = 0; i < MAX_VALUE; ++i)
49 {
50 if ((rand() % 10))
51 {
52 bv->set(i);
53 }
54 }
55}
56
57
58int main(void)
59{
61 try
62 {
63 bm::bvector<> bv1;
64 bm::bvector<> bv2;
65
66 fill_bvector(&bv1);
67 fill_bvector(&bv2);
68
69 cout << "bv1 count = " << bv1.count() << endl;
70 cout << "bv2 count = " << bv2.count() << endl;
71
72
73 // Prepare a serializer class
74 // for best performance - create serilizer once and reuse it
75 //
79
82
83 // compress bit-vectors and compute statistics
84 // (later re-used in serialization)
85 //
88
89 // declare serialization buffers
90 bm::serializer<bm::bvector<> >::buffer sbuf1;
91 bm::serializer<bm::bvector<> >::buffer sbuf2;
92
93 // perform serialization
94 //
95 bvs.serialize(bv1, sbuf1, &st1);
96 bvs.serialize(bv2, sbuf2, &st2);
97
98
99 // Serialized bvectors (sbuf1 and sbuf2) now ready to be
100 // saved to a database, file or send over a network.
101 // to simulate this we just copy content to std::vector<>
102 //
103
104 std::vector<unsigned char> vect1;
105 std::vector<unsigned char> vect2;
106
107 vect1.resize(sbuf1.size());
108 vect2.resize(sbuf2.size());
109
110 ::memcpy(vect1.data(), sbuf1.buf(), sbuf1.size());
111 ::memcpy(vect2.data(), sbuf2.buf(), sbuf2.size());
112
113
114 // Simple deserialization.
115 //
116 bm::bvector<> bv3;
117
118 // As a result of desrialization bv3 will contain all bits from
119 // bv1 and bv3:
120 // bv3 = bv1 OR bv2
121
122 bm::deserialize(bv3, sbuf1.buf());
123 bm::deserialize(bv3, sbuf2.buf());
124
125 bv3.optimize(tb);
126
127 // A few examples of operation deserializations
128 // set algebraic operation over bit-vector and a BLOB
129 //
130 bm::bvector<> bv4(bv3);
131
132 // bv4 = (bv1 OR bv2) AND bv1
133 // this must be equal to bv1 ?
134 //
135 od.deserialize(bv4, vect1.data(), bm::set_AND);
136
137 cout << "bv4 count = " << bv4.count() << endl;
138 bool eq = bv1.equal(bv4);
139 if (!eq)
140 {
141 cerr << "Logical error detected!" << endl;
142 return 1;
143 }
144 else
145 cout << "bv4 is equal to bv1" << endl;
146
147 bm::bvector<> bv5(bv3);
148
149
150 // if we need just set count, we can get it faster
151 // via set_COUNT_ operations
152 // use of COUNT operations does not materialize a target vector
153 //
154 // POPCNT((bv1 OR bv2) MINUS bv1)
155 auto cnt_sub =
156 od.deserialize(bv3, sbuf1.buf(), bm::set_COUNT_SUB_AB);
157 cout << "minus count = " << cnt_sub << endl;
158
159
160 // or we can actually perform the operation and get the full vector
161 // bv5 = (bv1 OR bv2) MINUS bv1
162 //
163 od.deserialize(bv5, sbuf1.buf(), tb, bm::set_SUB);
164 auto bv5_cnt = bv5.count();
165 cout << "bv5 count = " << bv5_cnt << endl;
166
167 if (cnt_sub != bv5_cnt)
168 {
169 cerr << "Logical error!" << endl;
170 return 1;
171 }
172 }
173 catch(std::exception& ex)
174 {
175 std::cerr << ex.what() << std::endl;
176 return 1;
177 }
178
179
180 return 0;
181}
182
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
Serialization / compression of bvector<>. Set theoretical operations on compressed BLOBs.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bool equal(const bvector< Alloc > &bvect) const BMNOEXCEPT
Equal comparison with an agr bit-vector.
Definition: bm.h:1995
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
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:4153
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:3600
Deserializer, performs logical operations between bit-vector and serialized bit-vector.
Definition: bmserial.h:930
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:6579
Bit-vector serialization class.
Definition: bmserial.h:76
void set_compression_level(unsigned clevel) BMNOEXCEPT
Set compression level.
Definition: bmserial.h:1254
size_type serialize(const BV &bv, unsigned char *buf, size_t buf_size)
Bitvector serialization into memory block.
Definition: bmserial.h:2706
@ set_COUNT_SUB_AB
Definition: bmconst.h:177
@ set_SUB
Definition: bmconst.h:170
@ set_AND
Definition: bmconst.h:168
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:3140
static void fill_bvector(bm::bvector<> *bv)
Definition: sample14.cpp:46
int main(void)
Definition: sample14.cpp:58
const unsigned MAX_VALUE
Definition: sample14.cpp:43
Statistical information about bitset's memory allocation details.
Definition: bm.h:125