1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3
<title>BitMagic: sample3.cpp Source File</title>
4
<link href="doxygen.css" rel="stylesheet" type="text/css">
6
<!-- Generated by Doxygen 1.4.1 -->
7
<div class="qindex"><a class="qindex" href="index.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="examples.html">Examples</a></div>
9
<a class="el" href="dir_000001.html">samples</a> / <a class="el" href="dir_000004.html">sample3</a></div>
10
<h1>sample3.cpp</h1><a href="a00122.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
11
00002 <span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
12
00003 <span class="comment"></span>
13
00004 <span class="comment">Permission is hereby granted, free of charge, to any person </span>
14
00005 <span class="comment">obtaining a copy of this software and associated documentation </span>
15
00006 <span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
16
00007 <span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
17
00008 <span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
18
00009 <span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
19
00010 <span class="comment">subject to the following conditions:</span>
20
00011 <span class="comment"></span>
21
00012 <span class="comment">The above copyright notice and this permission notice shall be included </span>
22
00013 <span class="comment">in all copies or substantial portions of the Software.</span>
23
00014 <span class="comment"></span>
24
00015 <span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
25
00016 <span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
26
00017 <span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
27
00018 <span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
28
00019 <span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
29
00020 <span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
30
00021 <span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
31
00022 <span class="comment">*/</span>
32
00023 <span class="comment"></span>
33
00024 <span class="comment">/** \example sample3.cpp</span>
34
00025 <span class="comment"> Exmaple demonstrates using bitvectors with different initial</span>
35
00026 <span class="comment"> block allocation strategy. </span>
36
00027 <span class="comment"> Bitvector 1 (bv1) by default working without RLE compression option</span>
37
00028 <span class="comment"> (best performance, maximum memory consumption). </span>
38
00029 <span class="comment"> Bitvector 2 (bv2) will be working in compression mode and use less memory.</span>
39
00030 <span class="comment"> </span>
40
00031 <span class="comment"> \sa bm::bvector<>::set_new_blocks_strat() </span>
41
00032 <span class="comment"></span>
42
00033 <span class="comment"> For more information please visit: http://bmagic.sourceforge.net</span>
43
00034 <span class="comment"></span>
44
00035 <span class="comment">*/</span>
46
00037 <span class="preprocessor">#include <stdlib.h></span>
47
00038 <span class="preprocessor">#include <iostream></span>
48
00039 <span class="preprocessor">#include "<a class="code" href="a00111.html">bm.h</a>"</span>
50
00041 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
52
<a name="l00043"></a><a class="code" href="a00122.html#a0">00043</a> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="a00122.html#a0">MAX_VALUE</a> = 1000000;
54
00045 <span class="comment">// This procedure creates very dense bitvectors.</span>
55
00046 <span class="comment">// The resulting set will consists mostly from ON (1) bits</span>
56
00047 <span class="comment">// interrupted with small gaps of 0 bits.</span>
58
<a name="l00049"></a><a class="code" href="a00122.html#a1">00049</a> <span class="keywordtype">void</span> <a class="code" href="a00122.html#a1">fill_bvector</a>(<a class="code" href="a00084.html">bm::bvector<></a>* bv1, <a class="code" href="a00084.html">bm::bvector<></a>* bv2)
60
00051 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < <a class="code" href="a00122.html#a0">MAX_VALUE</a>; ++i)
62
00053 <span class="keywordflow">if</span> (rand() % 2500)
64
00055 bv1->set_bit(i);
65
00056 bv2->set_bit(i);
71
<a name="l00062"></a><a class="code" href="a00122.html#a2">00062</a> <span class="keywordtype">void</span> <a class="code" href="a00122.html#a2">print_statistics</a>(<span class="keyword">const</span> <a class="code" href="a00084.html">bm::bvector<></a>& bv)
73
00064 <a class="code" href="a00084.html">bm::bvector<></a>::statistics st;
74
00065 bv.calc_stat(&st);
76
00067 cout << <span class="stringliteral">"Bits count:"</span> << bv.count() << endl;
77
00068 cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
78
00069 cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
79
00070 cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
80
00071 cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl << endl;;
84
<a name="l00075"></a><a class="code" href="a00122.html#a3">00075</a> <span class="keywordtype">int</span> <a class="code" href="a00120.html#a0">main</a>(<span class="keywordtype">void</span>)
86
00077 <a class="code" href="a00084.html">bm::bvector<></a> bv1;
87
00078 <a class="code" href="a00084.html">bm::bvector<></a> bv2;
89
00080 bv2.<a class="code" href="a00084.html#a52">set_new_blocks_strat</a>(bm::BM_GAP); <span class="comment">// set DGAP compression mode ON</span>
91
00082 <a class="code" href="a00122.html#a1">fill_bvector</a>(&bv1, &bv2); <span class="comment">// Fill both bvectors with the same values</span>
93
00084 <span class="comment">// For a given distrubution statistics should demonstrate</span>
94
00085 <span class="comment">// lower memory consumption for the vector with compression</span>
96
00087 <a class="code" href="a00122.html#a2">print_statistics</a>(bv1);
97
00088 <a class="code" href="a00122.html#a2">print_statistics</a>(bv2);
99
00090 <span class="comment">// Now run optimization procedure for bv1 and see statistics.</span>
101
00092 bv1.<a class="code" href="a00084.html#a55">optimize</a>();
103
00094 <a class="code" href="a00122.html#a2">print_statistics</a>(bv1);
105
00096 <span class="keywordflow">return</span> 0;
108
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Aug 5 14:12:26 2007 for BitMagic by
109
<a href="http://www.doxygen.org/index.html">
110
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>