BitMagic-C++
sample4.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 sample4.cpp
20 Exmaple demonstrates bitvector serialization/deserialization.
21
22For more information please visit: http://bmagic.sourceforge.net
23
24 \sa bm::serializer
25 \sa bm::deserialize
26*/
27
28/*! \file sample4.cpp
29 \brief Example: bvector<> serialization/deserialization.
30*/
31#include <stdlib.h>
32#include <iostream>
33
34#include "bm.h"
35#include "bmserial.h"
36#include "bmundef.h" /* clear the pre-proc defines from BM */
37
38using namespace std;
39
40
41// This exmaple demonstrates bitvector serialization/deserialization.
42
43
44const unsigned MAX_VALUE = 1000000;
45
46// This procedure creates very dense bitvector.
47// The resulting set will consists mostly from ON (1) bits
48// interrupted with small gaps of 0 bits.
49
50static
52{
53 for (unsigned i = 0; i < MAX_VALUE; ++i)
54 {
55 if (rand() % 2500)
56 {
57 bv->set_bit(i);
58 }
59 }
60}
61
62static
64{
66 bv.calc_stat(&st);
67
68 cout << "Bits count:" << bv.count() << endl;
69 cout << "Bit blocks:" << st.bit_blocks << endl;
70 cout << "GAP blocks:" << st.gap_blocks << endl;
71 cout << "Memory used:"<< st.memory_used << endl;
72 cout << "Max.serialize mem.:" << st.max_serialize_mem << endl << endl;;
73}
74
75// This is fairly low level serialization method, saves into a buffer
76//
77static
79 bm::bvector<>& bv)
80{
81 // It is good to optimize vector before serialization.
82
83 // scratch memory block
87
88 cout << "Bits count:" << bv.count() << endl;
89 cout << "Bit blocks:" << st.bit_blocks << endl;
90 cout << "GAP blocks:" << st.gap_blocks << endl;
91 cout << "Memory used:"<< st.memory_used << endl;
92 cout << "Max.serialize mem.:" << st.max_serialize_mem << endl;
93
94 // Allocate serialization buffer.
95 unsigned char* buf = new unsigned char[st.max_serialize_mem];
96
97 // Serialization to memory.
98 size_t len = bvs.serialize(bv, buf, st.max_serialize_mem);
99
100 cout << "Serialized size:" << len << endl << endl;
101 return buf;
102}
103
104
105
106int main(void)
107{
108 unsigned char* buf1 = 0;
109 unsigned char* buf2 = 0;
110
111 try
112 {
113 bm::bvector<> bv1;
114 bm::bvector<> bv2;
115
116 bv2.set_new_blocks_strat(bm::BM_GAP); // set DGAP compression mode ON
117
118 fill_bvector(&bv1);
119 fill_bvector(&bv2);
120
121 // Prepare a serializer class
122 // for best performance it is best to create serilizer once and reuse it
123 // (saves a memory allocations). Do NOT use it concurrently.
124 //
126
127 // next settings provide lowest size
128 bvs.byte_order_serialization(false);
129 bvs.gap_length_serialization(false);
130
131 // 1: serializes into a new allocated buffer (needs to be deallocated)
132 buf1 = serialize_bvector(bvs, bv1);
133
134 // 2: use serialization buffer class (automatic RAI, freed on destruction)
135 bm::serializer<bm::bvector<> >::buffer sbuf;
136 {
137 bvs.serialize(bv2, sbuf);
138 buf2 = sbuf.data();
139 auto sz = sbuf.size();
140 cout << "BV2 Serialized size:" << sz << endl;
141 }
142
143 // Serialized bvectors (buf1 and buf2) now ready to be
144 // saved to a database, file or send over a network.
145
146 // ...
147
148 // Deserialization.
149
150 bm::bvector<> bv3;
151
152 // As a result of desrialization bv3 will contain all bits from
153 // bv1 and bv3:
154 // bv3 = bv1 OR bv2
155
156 bm::deserialize(bv3, buf1);
157 bm::deserialize(bv3, buf2);
158
159 print_statistics(bv3);
160
161 // After a complex operation we can try to optimize bv3.
162
163 bv3.optimize();
164
165 print_statistics(bv3);
166
167 // control check using OR on the original vectors
168 {
169 bm::bvector<> bv_c;
170 bv_c.bit_or(bv1, bv2, bm::bvector<>::opt_compress);
171 int cmp = bv_c.compare(bv3);
172 if (cmp != 0)
173 {
174 std::cerr << "Error: bug in serialization" << std::endl;
175 }
176 bm::serializer<bm::bvector<> >::buffer sbuf2;
177
178 // destructive serialization, "bv_c" should not be used after
179 bvs.optimize_serialize_destroy(bv_c, sbuf2);
180
181 cout << "BV_C Serialized size:" << sbuf2.size() << endl;
182 }
183
184 }
185 catch(std::exception& ex)
186 {
187 std::cerr << ex.what() << std::endl;
188 delete [] buf1;
189 return 1;
190 }
191
192 delete [] buf1;
193
194 return 0;
195}
196
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
bm::bvector< Alloc > & bit_or(const bm::bvector< Alloc > &bv1, const bm::bvector< Alloc > &bv2, typename bm::bvector< Alloc >::optmode opt_mode=opt_none)
3-operand OR : this := bv1 OR bv2
Definition: bm.h:5668
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
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
void set_new_blocks_strat(strategy strat)
Sets new blocks allocation strategy.
Definition: bm.h:1890
bool set_bit(size_type n, bool val=true)
Sets bit n.
Definition: bm.h:4192
int compare(const bvector< Alloc > &bvect) const BMNOEXCEPT
Lexicographical comparison with a bitvector.
Definition: bm.h:3709
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const BMNOEXCEPT
Calculates bitvector statistics.
Definition: bm.h:3943
Bit-vector serialization class.
Definition: bmserial.h:76
void gap_length_serialization(bool value) BMNOEXCEPT
Set GAP length serialization (serializes GAP levels of the original vector)
Definition: bmserial.h:1275
void byte_order_serialization(bool value) BMNOEXCEPT
Set byte-order serialization (for cross platform compatibility)
Definition: bmserial.h:1281
void optimize_serialize_destroy(BV &bv, typename serializer< BV >::buffer &buf)
Bitvector serialization into buffer object (resized automatically) Input bit-vector gets optimized an...
Definition: bmserial.h:2268
size_type serialize(const BV &bv, unsigned char *buf, size_t buf_size)
Bitvector serialization into memory block.
Definition: bmserial.h:2706
@ BM_GAP
GAP compression is ON.
Definition: bmconst.h:147
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: sample4.cpp:51
static void print_statistics(const bm::bvector<> &bv)
Definition: sample4.cpp:63
int main(void)
Definition: sample4.cpp:106
const unsigned MAX_VALUE
Definition: sample4.cpp:44
static unsigned char * serialize_bvector(bm::serializer< bm::bvector<> > &bvs, bm::bvector<> &bv)
Definition: sample4.cpp:78
size_t gap_blocks
Number of GAP blocks.
Definition: bmfunc.h:58
size_t bit_blocks
Number of bit blocks.
Definition: bmfunc.h:57
size_t max_serialize_mem
estimated maximum memory for serialization
Definition: bmfunc.h:61
size_t memory_used
memory usage for all blocks and service tables
Definition: bmfunc.h:62
Statistical information about bitset's memory allocation details.
Definition: bm.h:125