BitMagic-C++
sample3.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 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#include <stdlib.h>
35#include <iostream>
36
37#include "bm.h"
38#include "bmundef.h" /* clear the pre-proc defines from BM */
39
40using namespace std;
41
42const unsigned MAX_VALUE = 1000000;
43
44// This procedure creates very dense bitvectors.
45// The resulting set will consists mostly from ON (1) bits
46// interrupted with small gaps of 0 bits.
47static
49{
50 for (unsigned i = 0; i < MAX_VALUE; ++i)
51 {
52 if (rand() % 2500)
53 {
54 bv1->set_bit(i);
55 bv2->set_bit(i);
56 }
57 }
58}
59
60static
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
74int main(void)
75{
76 try
77 {
78 bm::bvector<> bv1;
79 bm::bvector<> bv2;
80
81 bv2.set_new_blocks_strat(bm::BM_GAP); // set DGAP compression mode ON
82
83 fill_bvector(&bv1, &bv2); // Fill both bvectors with the same values
84
85 // For a given distrubution statistics should demonstrate
86 // lower memory consumption for the vector with compression
87
90
91 // Now run optimization procedure for bv1 and see statistics.
92 bv1.optimize();
93
95 }
96 catch(std::exception& ex)
97 {
98 std::cerr << ex.what() << std::endl;
99 }
100
101 return 0;
102}
103
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
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
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const BMNOEXCEPT
Calculates bitvector statistics.
Definition: bm.h:3943
@ BM_GAP
GAP compression is ON.
Definition: bmconst.h:147
static void print_statistics(const bm::bvector<> &bv)
Definition: sample3.cpp:61
static void fill_bvector(bm::bvector<> *bv1, bm::bvector<> *bv2)
Definition: sample3.cpp:48
int main(void)
Definition: sample3.cpp:74
const unsigned MAX_VALUE
Definition: sample3.cpp:42
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