BitMagic-C++
sample3.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 sample3.cpp
20  Exmaple demonstrates using bitvectors with different initial
21  block allocation strategy.
22  Bitvector 1 (bv1) by default working without RLE compression option
23  (best performance, maximum memory consumption).
24  Bitvector 2 (bv2) will be working in compression mode and use less memory.
25 
26  \sa bm::bvector<>::set_new_blocks_strat()
27 
28  For more information please visit: http://bmagic.sourceforge.net
29 
30 */
31 /*! \file sample3.cpp
32  \brief Example: bvector<> with different allocation/compression strategies
33 */
34 
35 #include <stdlib.h>
36 #include <iostream>
37 #include "bm.h"
38 
39 using namespace std;
40 
41 const unsigned MAX_VALUE = 1000000;
42 
43 // This procedure creates very dense bitvectors.
44 // The resulting set will consists mostly from ON (1) bits
45 // interrupted with small gaps of 0 bits.
46 static
48 {
49  for (unsigned i = 0; i < MAX_VALUE; ++i)
50  {
51  if (rand() % 2500)
52  {
53  bv1->set_bit(i);
54  bv2->set_bit(i);
55  }
56  }
57 }
58 
59 static
61 {
63  bv.calc_stat(&st);
64 
65  cout << "Bits count:" << bv.count() << endl;
66  cout << "Bit blocks:" << st.bit_blocks << endl;
67  cout << "GAP blocks:" << st.gap_blocks << endl;
68  cout << "Memory used:"<< st.memory_used << endl;
69  cout << "Max.serialize mem.:" << st.max_serialize_mem << endl << endl;;
70 }
71 
72 
73 int main(void)
74 {
75  try
76  {
77  bm::bvector<> bv1;
78  bm::bvector<> bv2;
79 
80  bv2.set_new_blocks_strat(bm::BM_GAP); // set DGAP compression mode ON
81 
82  fill_bvector(&bv1, &bv2); // Fill both bvectors with the same values
83 
84  // For a given distrubution statistics should demonstrate
85  // lower memory consumption for the vector with compression
86 
87  print_statistics(bv1);
88  print_statistics(bv2);
89 
90  // Now run optimization procedure for bv1 and see statistics.
91  bv1.optimize();
92 
93  print_statistics(bv1);
94  }
95  catch(std::exception& ex)
96  {
97  std::cerr << ex.what() << std::endl;
98  }
99 
100  return 0;
101 }
102 
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
unsigned gap_blocks
Number of GAP blocks.
Definition: bmfunc.h:59
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:2863
size_t max_serialize_mem
Estimated maximum of memory required for serialization.
Definition: bmfunc.h:61
void set_new_blocks_strat(strategy strat)
Sets new blocks allocation strategy.
Definition: bm.h:2009
GAP compression is ON.
Definition: bmconst.h:113
Statistical information about bitset&#39;s memory allocation details.
Definition: bm.h:145
size_t memory_used
Memory used by bitvector including temp and service blocks.
Definition: bmfunc.h:63
static void print_statistics(const bm::bvector<> &bv)
Definition: sample3.cpp:60
bm::id_t count() const
population cout (count of ON bits)
Definition: bm.h:2401
static void fill_bvector(bm::bvector<> *bv1, bm::bvector<> *bv2)
Definition: sample3.cpp:47
const unsigned MAX_VALUE
Definition: sample3.cpp:41
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const
Calculates bitvector statistics.
Definition: bm.h:3092
unsigned bit_blocks
Number of bit blocks.
Definition: bmfunc.h:57
int main(void)
Definition: sample3.cpp:73
bool set_bit(bm::id_t n, bool val=true)
Sets bit n.
Definition: bm.h:1362