BitMagic-C++
sample26.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 sample26.cpp
20 Immutable bit-vectors.
21 This example generates sparse bit-vectors and shows how to make them use less memory
22 via optimization (after optimization bit-vector remains mutable) and freezing it into immutable,
23 where memory all edit reservations are getting dropped.
24 Freezing also allocates memory blocks together, reducing the heap fragmentation.
25
26 \sa bm::bvector::optimize
27 \sa bm::bvector::freeze
28 \sa bm::bvector::is_ro
29*/
30
31/*! \file sample26.cpp
32 \brief Example: bvector<> with immutability (read-only)
33*/
34#include <stdlib.h>
35#include <iostream>
36#include <cassert>
37
38#include "bm.h"
39#include "bmundef.h" /* clear the pre-proc defines from BM */
40
41using namespace std;
42
43const unsigned MAX_VALUE = 1000000;
44
45/// Fill bit-vectors with values using dense and sparse distrubutions
46///
47static
49{
50 const unsigned fill_factor1 = 2500;
51 const unsigned fill_factor2 = 150;
52 for (unsigned i = 0; i < MAX_VALUE; ++i)
53 if (unsigned(rand()) % fill_factor1)
54 bv1->set(i);
55 for (unsigned i = 0; i < MAX_VALUE; i+=fill_factor2)
56 bv2->set(i);
57}
58
59static
61{
63 bv.calc_stat(&st);
64
65 cout << "Bit blocks :" << st.bit_blocks << endl;
66 cout << "GAP blocks :" << st.gap_blocks << endl;
67 cout << "Memory used :" << st.memory_used << endl;
68 cout << "Memory overhead :" << st.gap_cap_overhead << endl;
69 cout << "Max.serialize mem :" << st.max_serialize_mem << endl << endl;;
70}
71
72
73int main(void)
74{
75 try
76 {
77 bm::bvector<> bv1;
78 bm::bvector<> bv2;
79
80 fill_bvector(&bv1, &bv2); // Fill bvectors (dense and sparse)
81
82 cout << "Statistics before memory optimization" << endl;
83 cout << "-------------------------------------" << endl << endl;
86
87
88 {
89 // optimization scratch buffer on stack to avoid dynamic allocation
91
92 bv1.optimize(tb);
93 bv2.optimize(tb);
94 }
95
96 cout << "Statistics after memory optimization" << endl;
97 cout << "-------------------------------------" << endl;
100
101 // two ways to turn vector read-only:
102 // 1. construct a new vector as a READ_ONLY copy (drop the old one)
103 // 2. Use bm::bvector<>::freeze()
104
106 assert(bv1_ro.is_ro());
107 bv2.freeze();
108 cout << bv2.is_ro() << endl; // 1 - yes it is now READ_ONLY
109
110
111 cout << "Statistics after freezing" << endl;
112 cout << "-------------------------------------" << endl;
113 print_statistics(bv1_ro);
114 print_statistics(bv2);
115
116 // interface-wise, immutable bit-vectors remain just the same bvector<>
117 // bit you CANNOT call non-cost modification functions on it
118 // (it would be undefined behavior) don't shoot yourself in the leg
119 //
120
121 // READ-ONLY status is preserved over the copy construction and
122 // assignment
123 {
124 // copy construct of RO bit-vector (result is read-only vector vector)
125 bm::bvector<> bv11(bv1_ro);
126 assert(bv11.is_ro());
127 bool eq = bv11.equal(bv1_ro);
128 assert(eq);
129 }
130
131
132 // if you can rewet it back here is how to do it
133 // construct bv11 as READ-WRITE from READONLY
134 {
135 assert(bv2.is_ro()); // bv2 is immutable
136
138 bv2.swap(bv21);
139 assert(!bv2.is_ro()); // bv2 is now a mutable vector again
140
141 bv2.set(0); // we can do chnage operations at this point
142 }
143
144
145
146
147 }
148 catch(std::exception& ex)
149 {
150 std::cerr << ex.what() << std::endl;
151 }
152
153 return 0;
154}
155
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
#define BM_DECLARE_TEMP_BLOCK(x)
Definition: bm.h:47
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
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
void swap(bvector< Alloc > &bvect) BMNOEXCEPT
Exchanges content of bv and this bvector.
Definition: bm.h:3931
void freeze()
Turn current vector to read-only (immutable vector).
Definition: bm.h:7820
bool is_ro() const BMNOEXCEPT
Returns true if vector is read-only.
Definition: bm.h:1046
void calc_stat(struct bm::bvector< Alloc >::statistics *st) const BMNOEXCEPT
Calculates bitvector statistics.
Definition: bm.h:3943
@ READWRITE
mutable (read-write object)
@ READONLY
immutable (read-only object)
static void print_statistics(const bm::bvector<> &bv)
Definition: sample26.cpp:60
static void fill_bvector(bm::bvector<> *bv1, bm::bvector<> *bv2)
Fill bit-vectors with values using dense and sparse distrubutions.
Definition: sample26.cpp:48
int main(void)
Definition: sample26.cpp:73
const unsigned MAX_VALUE
Definition: sample26.cpp:43
size_t gap_cap_overhead
gap memory overhead between length and capacity
Definition: bmfunc.h:63
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