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