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: sample4.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_000005.html">sample4</a></div>
10
<h1>sample4.cpp</h1><a href="a00123.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 sample4.cpp</span>
34
00025 <span class="comment"> Exmaple demonstrates bitvector serialization/deserialization.</span>
35
00026 <span class="comment"> </span>
36
00027 <span class="comment">For more information please visit: http://bmagic.sourceforge.net</span>
37
00028 <span class="comment"></span>
38
00029 <span class="comment"> \sa bm::serialize </span>
39
00030 <span class="comment"> \sa bm::deserialize </span>
40
00031 <span class="comment"></span>
41
00032 <span class="comment">*/</span>
43
00034 <span class="preprocessor">#include <stdlib.h></span>
44
00035 <span class="preprocessor">#include <iostream></span>
45
00036 <span class="preprocessor">#include "<a class="code" href="a00111.html">bm.h</a>"</span>
46
00037 <span class="preprocessor">#include "bmserial.h"</span>
48
00039 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
51
00042 <span class="comment">// This exmaple demonstrates bitvector serialization/deserialization.</span>
55
<a name="l00046"></a><a class="code" href="a00123.html#a0">00046</a> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="a00122.html#a0">MAX_VALUE</a> = 1000000;
57
00048 <span class="comment">// This procedure creates very dense bitvector.</span>
58
00049 <span class="comment">// The resulting set will consists mostly from ON (1) bits</span>
59
00050 <span class="comment">// interrupted with small gaps of 0 bits.</span>
61
<a name="l00052"></a><a class="code" href="a00123.html#a1">00052</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>* bv)
63
00054 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < <a class="code" href="a00122.html#a0">MAX_VALUE</a>; ++i)
65
00056 <span class="keywordflow">if</span> (rand() % 2500)
67
00058 bv->set_bit(i);
73
<a name="l00064"></a><a class="code" href="a00123.html#a2">00064</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)
75
00066 <a class="code" href="a00084.html">bm::bvector<></a>::statistics st;
76
00067 bv.calc_stat(&st);
78
00069 cout << <span class="stringliteral">"Bits count:"</span> << bv.count() << endl;
79
00070 cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
80
00071 cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
81
00072 cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
82
00073 cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl << endl;;
86
<a name="l00077"></a><a class="code" href="a00123.html#a3">00077</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* <a class="code" href="a00123.html#a3">serialize_bvector</a>(<a class="code" href="a00084.html">bm::bvector<></a>& bv)
88
00079 <span class="comment">// It is reccomended to optimize vector before serialization.</span>
89
00080 bv.<a class="code" href="a00084.html#a55">optimize</a>();
91
00082 <a class="code" href="a00084.html">bm::bvector<></a>::statistics st;
92
00083 bv.<a class="code" href="a00084.html#a47">calc_stat</a>(&st);
94
00085 cout << <span class="stringliteral">"Bits count:"</span> << bv.<a class="code" href="a00084.html#a28">count</a>() << endl;
95
00086 cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
96
00087 cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
97
00088 cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
98
00089 cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl;
100
00091 <span class="comment">// Allocate serialization buffer.</span>
102
00093 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf = <span class="keyword">new</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>[st.max_serialize_mem];
104
00095 <span class="comment">// Serialization to memory.</span>
106
00097 <span class="keywordtype">unsigned</span> len = bm::serialize(bv, buf);
108
00099 cout << <span class="stringliteral">"Serialized size:"</span> << len << endl << endl;
110
00101 <span class="keywordflow">return</span> buf;
114
<a name="l00105"></a><a class="code" href="a00123.html#a4">00105</a> <span class="keywordtype">int</span> <a class="code" href="a00120.html#a0">main</a>(<span class="keywordtype">void</span>)
116
00107 <a class="code" href="a00084.html">bm::bvector<></a> bv1;
117
00108 <a class="code" href="a00084.html">bm::bvector<></a> bv2;
119
00110 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>
121
00112 <a class="code" href="a00122.html#a1">fill_bvector</a>(&bv1);
122
00113 <a class="code" href="a00122.html#a1">fill_bvector</a>(&bv2);
124
00115 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf1 = <a class="code" href="a00123.html#a3">serialize_bvector</a>(bv1);
125
00116 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf2 = <a class="code" href="a00123.html#a3">serialize_bvector</a>(bv2);
127
00118 <span class="comment">// Serialized bvectors (buf1 and buf2) now ready to be</span>
128
00119 <span class="comment">// saved to a database, file or send over a network.</span>
130
00121 <span class="comment">// ...</span>
132
00123 <span class="comment">// Deserialization.</span>
134
00125 <a class="code" href="a00084.html">bm::bvector<></a> bv3;
136
00127 <span class="comment">// As a result of desrialization bv3 will contain all bits from</span>
137
00128 <span class="comment">// bv1 and bv3:</span>
138
00129 <span class="comment">// bv3 = bv1 OR bv2</span>
140
00131 bm::deserialize(bv3, buf1);
141
00132 bm::deserialize(bv3, buf2);
143
00134 <a class="code" href="a00122.html#a2">print_statistics</a>(bv3);
145
00136 <span class="comment">// After a complex operation we can try to optimize bv3.</span>
147
00138 bv3.<a class="code" href="a00084.html#a55">optimize</a>();
149
00140 <a class="code" href="a00122.html#a2">print_statistics</a>(bv3);
151
00142 <span class="keyword">delete</span> [] buf1;
152
00143 <span class="keyword">delete</span> [] buf2;
154
00145 <span class="keywordflow">return</span> 0;
157
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Aug 5 14:12:26 2007 for BitMagic by
158
<a href="http://www.doxygen.org/index.html">
159
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>