1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3
<title>BitMagic: sample3.cpp</title>
4
<link href="doxygen.css" rel="stylesheet" type="text/css">
5
<link href="tabs.css" rel="stylesheet" type="text/css">
7
<!-- Generated by Doxygen 1.5.5 -->
8
<div class="navigation" id="top">
11
<li><a href="index.html"><span>Main Page</span></a></li>
12
<li><a href="modules.html"><span>Modules</span></a></li>
13
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
14
<li><a href="classes.html"><span>Data Structures</span></a></li>
15
<li><a href="files.html"><span>Files</span></a></li>
16
<li><a href="examples.html"><span>Examples</span></a></li>
20
<div class="contents">
21
<h1>sample3.cpp</h1>Exmaple demonstrates using bitvectors with different initial block allocation strategy. Bitvector 1 (bv1) by default working without RLE compression option (best performance, maximum memory consumption). Bitvector 2 (bv2) will be working in compression mode and use less memory.<p>
22
<dl class="see" compact><dt><b>See also:</b></dt><dd><a class="el" href="a00040.html#b1094180d03cbc961d62f0f453097452" title="Sets new blocks allocation strategy.">bm::bvector<>::set_new_blocks_strat()</a></dd></dl>
23
For more information please visit: <a href="http://bmagic.sourceforge.net">http://bmagic.sourceforge.net</a><p>
24
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
25
<span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
26
<span class="comment"></span>
27
<span class="comment">Permission is hereby granted, free of charge, to any person </span>
28
<span class="comment">obtaining a copy of this software and associated documentation </span>
29
<span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
30
<span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
31
<span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
32
<span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
33
<span class="comment">subject to the following conditions:</span>
34
<span class="comment"></span>
35
<span class="comment">The above copyright notice and this permission notice shall be included </span>
36
<span class="comment">in all copies or substantial portions of the Software.</span>
37
<span class="comment"></span>
38
<span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
39
<span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
40
<span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
41
<span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
42
<span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
43
<span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
44
<span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
45
<span class="comment">*/</span>
46
<span class="comment"></span>
47
<span class="comment">/** \example sample3.cpp</span>
48
<span class="comment"> Exmaple demonstrates using bitvectors with different initial</span>
49
<span class="comment"> block allocation strategy. </span>
50
<span class="comment"> Bitvector 1 (bv1) by default working without RLE compression option</span>
51
<span class="comment"> (best performance, maximum memory consumption). </span>
52
<span class="comment"> Bitvector 2 (bv2) will be working in compression mode and use less memory.</span>
53
<span class="comment"> </span>
54
<span class="comment"> \sa bm::bvector<>::set_new_blocks_strat() </span>
55
<span class="comment"></span>
56
<span class="comment"> For more information please visit: http://bmagic.sourceforge.net</span>
57
<span class="comment"></span>
58
<span class="comment">*/</span>
60
<span class="preprocessor">#include <stdlib.h></span>
61
<span class="preprocessor">#include <iostream></span>
62
<span class="preprocessor">#include "<a class="code" href="a00082.html">bm.h</a>"</span>
64
<span class="keyword">using namespace </span>std;
66
<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a name="a0"></a><a class="code" href="a00099.html#8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a> = 1000000;
68
<span class="comment">// This procedure creates very dense bitvectors.</span>
69
<span class="comment">// The resulting set will consists mostly from ON (1) bits</span>
70
<span class="comment">// interrupted with small gaps of 0 bits.</span>
72
<span class="keywordtype">void</span> <a name="a1"></a><a class="code" href="a00099.html#6c4e3c21afef90ce9111f8f5e60d4c13">fill_bvector</a>(<a name="_a2"></a><a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a>* bv1, <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a>* bv2)
74
<span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < <a class="code" href="a00099.html#8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a>; ++i)
76
<span class="keywordflow">if</span> (rand() % 2500)
78
bv1-><a name="a3"></a><a class="code" href="a00040.html#1769d9ed3a4ee41b03fc3ca4a0e5a58e" title="Sets bit n.">set_bit</a>(i);
79
bv2-><a class="code" href="a00040.html#1769d9ed3a4ee41b03fc3ca4a0e5a58e" title="Sets bit n.">set_bit</a>(i);
85
<span class="keywordtype">void</span> <a name="a4"></a><a class="code" href="a00099.html#eb4ca6f0a986ad6ae67d1f7124fdc400">print_statistics</a>(<span class="keyword">const</span> <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a>& bv)
87
<a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<>::statistics</a> st;
88
bv.<a name="a5"></a><a class="code" href="a00040.html#1eaaac76a97b9eb1d3c11f21cf9385ac" title="Calculates bitvector statistics.">calc_stat</a>(&st);
90
cout << <span class="stringliteral">"Bits count:"</span> << bv.<a name="a6"></a><a class="code" href="a00040.html#71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
91
cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
92
cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
93
cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
94
cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl << endl;;
98
<span class="keywordtype">int</span> <a name="a7"></a><a class="code" href="a00097.html#840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
100
<a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv1;
101
<a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv2;
103
bv2.<a name="a8"></a><a class="code" href="a00040.html#b1094180d03cbc961d62f0f453097452" title="Sets new blocks allocation strategy.">set_new_blocks_strat</a>(<a name="a9"></a><a class="code" href="a00109.html#ggd0b87b3b7292383a864d27feaf1c9eff7fe09d1d7c62a813db922c396c0cbc9c" title="GAP compression is ON.">bm::BM_GAP</a>); <span class="comment">// set DGAP compression mode ON</span>
105
<a class="code" href="a00099.html#6c4e3c21afef90ce9111f8f5e60d4c13">fill_bvector</a>(&bv1, &bv2); <span class="comment">// Fill both bvectors with the same values</span>
107
<span class="comment">// For a given distrubution statistics should demonstrate</span>
108
<span class="comment">// lower memory consumption for the vector with compression</span>
110
<a class="code" href="a00099.html#eb4ca6f0a986ad6ae67d1f7124fdc400">print_statistics</a>(bv1);
111
<a class="code" href="a00099.html#eb4ca6f0a986ad6ae67d1f7124fdc400">print_statistics</a>(bv2);
113
<span class="comment">// Now run optimization procedure for bv1 and see statistics.</span>
115
bv1.<a name="a10"></a><a class="code" href="a00040.html#bd522c13e26c2b8f84636fe8b6648dcb" title="Optimize memory bitvector&#39;s memory allocation.">optimize</a>();
117
<a class="code" href="a00099.html#eb4ca6f0a986ad6ae67d1f7124fdc400">print_statistics</a>(bv1);
119
<span class="keywordflow">return</span> 0;
123
<hr size="1"><address style="text-align: right;"><small>Generated on Mon Oct 12 12:16:38 2009 for BitMagic by
124
<a href="http://www.doxygen.org/index.html">
125
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.5 </small></address>