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: sample4.cpp Source File</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 -->
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
4
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5
<title>BitMagic: Memory Allocation</title>
6
<link href="tabs.css" rel="stylesheet" type="text/css"/>
7
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
10
<!-- Generated by Doxygen 1.6.1 -->
8
11
<div class="navigation" id="top">
11
14
<li><a href="index.html"><span>Main Page</span></a></li>
12
15
<li><a href="modules.html"><span>Modules</span></a></li>
13
16
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
14
<li><a href="classes.html"><span>Data Structures</span></a></li>
15
<li class="current"><a href="files.html"><span>Files</span></a></li>
17
<li><a href="annotated.html"><span>Data Structures</span></a></li>
18
<li><a href="files.html"><span>Files</span></a></li>
16
19
<li><a href="examples.html"><span>Examples</span></a></li>
19
<h1>sample4.cpp</h1><a href="a00100.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
20
<a name="l00002"></a>00002 <span class="comment">Copyright(c) 2002-2009 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
21
<a name="l00003"></a>00003 <span class="comment"></span>
22
<a name="l00004"></a>00004 <span class="comment">Permission is hereby granted, free of charge, to any person </span>
23
<a name="l00005"></a>00005 <span class="comment">obtaining a copy of this software and associated documentation </span>
24
<a name="l00006"></a>00006 <span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
25
<a name="l00007"></a>00007 <span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
26
<a name="l00008"></a>00008 <span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
27
<a name="l00009"></a>00009 <span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
28
<a name="l00010"></a>00010 <span class="comment">subject to the following conditions:</span>
29
<a name="l00011"></a>00011 <span class="comment"></span>
30
<a name="l00012"></a>00012 <span class="comment">The above copyright notice and this permission notice shall be included </span>
31
<a name="l00013"></a>00013 <span class="comment">in all copies or substantial portions of the Software.</span>
32
<a name="l00014"></a>00014 <span class="comment"></span>
33
<a name="l00015"></a>00015 <span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
34
<a name="l00016"></a>00016 <span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
35
<a name="l00017"></a>00017 <span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
36
<a name="l00018"></a>00018 <span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
37
<a name="l00019"></a>00019 <span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
38
<a name="l00020"></a>00020 <span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
39
<a name="l00021"></a>00021 <span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
40
<a name="l00022"></a>00022 <span class="comment">*/</span>
41
<a name="l00023"></a>00023 <span class="comment"></span>
42
<a name="l00024"></a>00024 <span class="comment">/** \example sample4.cpp</span>
43
<a name="l00025"></a>00025 <span class="comment"> Exmaple demonstrates bitvector serialization/deserialization.</span>
44
<a name="l00026"></a>00026 <span class="comment"> </span>
45
<a name="l00027"></a>00027 <span class="comment">For more information please visit: http://bmagic.sourceforge.net</span>
46
<a name="l00028"></a>00028 <span class="comment"></span>
47
<a name="l00029"></a>00029 <span class="comment"> \sa bm::serializer</span>
48
<a name="l00030"></a>00030 <span class="comment"> \sa bm::deserialize </span>
49
<a name="l00031"></a>00031 <span class="comment"></span>
50
<a name="l00032"></a>00032 <span class="comment">*/</span>
51
<a name="l00033"></a>00033
52
<a name="l00034"></a>00034 <span class="preprocessor">#include <stdlib.h></span>
53
<a name="l00035"></a>00035 <span class="preprocessor">#include <iostream></span>
54
<a name="l00036"></a>00036 <span class="preprocessor">#include "<a class="code" href="a00082.html">bm.h</a>"</span>
55
<a name="l00037"></a>00037 <span class="preprocessor">#include "<a class="code" href="a00090.html">bmserial.h</a>"</span>
56
<a name="l00038"></a>00038
57
<a name="l00039"></a>00039 <span class="keyword">using namespace </span>std;
58
<a name="l00040"></a>00040
59
<a name="l00041"></a>00041
60
<a name="l00042"></a>00042 <span class="comment">// This exmaple demonstrates bitvector serialization/deserialization.</span>
61
<a name="l00043"></a>00043
62
<a name="l00044"></a>00044
63
<a name="l00045"></a>00045
64
<a name="l00046"></a><a class="code" href="a00100.html#8652d848be89c1f9b0a77327ee32fc0f">00046</a> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="a00099.html#8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a> = 1000000;
65
<a name="l00047"></a>00047
66
<a name="l00048"></a>00048 <span class="comment">// This procedure creates very dense bitvector.</span>
67
<a name="l00049"></a>00049 <span class="comment">// The resulting set will consists mostly from ON (1) bits</span>
68
<a name="l00050"></a>00050 <span class="comment">// interrupted with small gaps of 0 bits.</span>
69
<a name="l00051"></a>00051
70
<a name="l00052"></a><a class="code" href="a00100.html#663e1bc930c9ade56c0bc436b8860a1e">00052</a> <span class="keywordtype">void</span> <a class="code" href="a00099.html#6c4e3c21afef90ce9111f8f5e60d4c13">fill_bvector</a>(<a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a>* bv)
71
<a name="l00053"></a>00053 {
72
<a name="l00054"></a>00054 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < <a class="code" href="a00099.html#8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a>; ++i)
73
<a name="l00055"></a>00055 {
74
<a name="l00056"></a>00056 <span class="keywordflow">if</span> (rand() % 2500)
75
<a name="l00057"></a>00057 {
76
<a name="l00058"></a>00058 bv-><a class="code" href="a00040.html#1769d9ed3a4ee41b03fc3ca4a0e5a58e" title="Sets bit n.">set_bit</a>(i);
77
<a name="l00059"></a>00059 }
78
<a name="l00060"></a>00060 }
79
<a name="l00061"></a>00061 }
80
<a name="l00062"></a>00062
81
<a name="l00063"></a>00063
82
<a name="l00064"></a><a class="code" href="a00100.html#eb4ca6f0a986ad6ae67d1f7124fdc400">00064</a> <span class="keywordtype">void</span> <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)
83
<a name="l00065"></a>00065 {
84
<a name="l00066"></a>00066 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<>::statistics</a> st;
85
<a name="l00067"></a>00067 bv.<a class="code" href="a00040.html#1eaaac76a97b9eb1d3c11f21cf9385ac" title="Calculates bitvector statistics.">calc_stat</a>(&st);
86
<a name="l00068"></a>00068
87
<a name="l00069"></a>00069 cout << <span class="stringliteral">"Bits count:"</span> << bv.<a class="code" href="a00040.html#71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
88
<a name="l00070"></a>00070 cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
89
<a name="l00071"></a>00071 cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
90
<a name="l00072"></a>00072 cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
91
<a name="l00073"></a>00073 cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl << endl;;
92
<a name="l00074"></a>00074 }
93
<a name="l00075"></a>00075
94
<a name="l00076"></a>00076
95
<a name="l00077"></a><a class="code" href="a00100.html#2ba5ce11270f84d292466449521b695f">00077</a> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* <a class="code" href="a00100.html#2ba5ce11270f84d292466449521b695f">serialize_bvector</a>(<a class="code" href="a00079.html" title="Bit-vector serialization class.">bm::serializer</a><<a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> >& bvs,
96
<a name="l00078"></a>00078 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a>& bv)
97
<a name="l00079"></a>00079 {
98
<a name="l00080"></a>00080 <span class="comment">// It is reccomended to optimize vector before serialization.</span>
99
<a name="l00081"></a>00081 bv.<a class="code" href="a00040.html#bd522c13e26c2b8f84636fe8b6648dcb" title="Optimize memory bitvector&#39;s memory allocation.">optimize</a>();
100
<a name="l00082"></a>00082
101
<a name="l00083"></a>00083 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<>::statistics</a> st;
102
<a name="l00084"></a>00084 bv.<a class="code" href="a00040.html#1eaaac76a97b9eb1d3c11f21cf9385ac" title="Calculates bitvector statistics.">calc_stat</a>(&st);
103
<a name="l00085"></a>00085
104
<a name="l00086"></a>00086 cout << <span class="stringliteral">"Bits count:"</span> << bv.<a class="code" href="a00040.html#71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
105
<a name="l00087"></a>00087 cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
106
<a name="l00088"></a>00088 cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
107
<a name="l00089"></a>00089 cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
108
<a name="l00090"></a>00090 cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl;
109
<a name="l00091"></a>00091
110
<a name="l00092"></a>00092 <span class="comment">// Allocate serialization buffer.</span>
111
<a name="l00093"></a>00093
112
<a name="l00094"></a>00094 <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];
113
<a name="l00095"></a>00095
114
<a name="l00096"></a>00096 <span class="comment">// Serialization to memory.</span>
115
<a name="l00097"></a>00097
116
<a name="l00098"></a>00098 <span class="keywordtype">unsigned</span> len = bvs.serialize(bv, buf, 0);
117
<a name="l00099"></a>00099 <span class="comment">//unsigned len = bm::serialize(bv, buf);</span>
118
<a name="l00100"></a>00100
119
<a name="l00101"></a>00101 cout << <span class="stringliteral">"Serialized size:"</span> << len << endl << endl;
120
<a name="l00102"></a>00102
121
<a name="l00103"></a>00103 <span class="keywordflow">return</span> buf;
122
<a name="l00104"></a>00104 }
123
<a name="l00105"></a>00105
124
<a name="l00106"></a>00106
125
<a name="l00107"></a><a class="code" href="a00100.html#840291bc02cba5474a4cb46a9b9566fe">00107</a> <span class="keywordtype">int</span> <a class="code" href="a00097.html#840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
126
<a name="l00108"></a>00108 {
127
<a name="l00109"></a>00109 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv1;
128
<a name="l00110"></a>00110 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv2;
129
<a name="l00111"></a>00111
130
<a name="l00112"></a>00112 bv2.<a class="code" href="a00040.html#b1094180d03cbc961d62f0f453097452" title="Sets new blocks allocation strategy.">set_new_blocks_strat</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>
131
<a name="l00113"></a>00113
132
<a name="l00114"></a>00114 <a class="code" href="a00099.html#6c4e3c21afef90ce9111f8f5e60d4c13">fill_bvector</a>(&bv1);
133
<a name="l00115"></a>00115 <a class="code" href="a00099.html#6c4e3c21afef90ce9111f8f5e60d4c13">fill_bvector</a>(&bv2);
134
<a name="l00116"></a>00116
135
<a name="l00117"></a>00117 <span class="comment">// Prepare a serializer class </span>
136
<a name="l00118"></a>00118 <span class="comment">// for best performance it is best to create serilizer once and reuse it</span>
137
<a name="l00119"></a>00119 <span class="comment">// (saves a lot of memory allocations)</span>
138
<a name="l00120"></a>00120 <span class="comment">//</span>
139
<a name="l00121"></a>00121 <a class="code" href="a00079.html" title="Bit-vector serialization class.">bm::serializer<bm::bvector<></a> > bvs;
140
<a name="l00122"></a>00122
141
<a name="l00123"></a>00123 <span class="comment">// next settings provide lowest serilized size </span>
142
<a name="l00124"></a>00124 bvs.<a class="code" href="a00079.html#892b124d42bb939485e47d197b8dc1ac" title="Set byte-order serialization (for cross platform compatibility).">byte_order_serialization</a>(<span class="keyword">false</span>);
143
<a name="l00125"></a>00125 bvs.<a class="code" href="a00079.html#5b19026c2e928c639f09da4a4e1235ab" title="Set GAP length serialization (serializes GAP levels of the original vector).">gap_length_serialization</a>(<span class="keyword">false</span>);
144
<a name="l00126"></a>00126 bvs.<a class="code" href="a00079.html#d433d790f07103d39c5f15a6caaf2823" title="Set compression level.">set_compression_level</a>(4);
145
<a name="l00127"></a>00127
146
<a name="l00128"></a>00128
147
<a name="l00129"></a>00129 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf1 = <a class="code" href="a00100.html#2ba5ce11270f84d292466449521b695f">serialize_bvector</a>(bvs, bv1);
148
<a name="l00130"></a>00130 <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf2 = <a class="code" href="a00100.html#2ba5ce11270f84d292466449521b695f">serialize_bvector</a>(bvs, bv2);
149
<a name="l00131"></a>00131
150
<a name="l00132"></a>00132 <span class="comment">// Serialized bvectors (buf1 and buf2) now ready to be</span>
151
<a name="l00133"></a>00133 <span class="comment">// saved to a database, file or send over a network.</span>
152
<a name="l00134"></a>00134
153
<a name="l00135"></a>00135 <span class="comment">// ...</span>
154
<a name="l00136"></a>00136
155
<a name="l00137"></a>00137 <span class="comment">// Deserialization.</span>
156
<a name="l00138"></a>00138
157
<a name="l00139"></a>00139 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv3;
158
<a name="l00140"></a>00140
159
<a name="l00141"></a>00141 <span class="comment">// As a result of desrialization bv3 will contain all bits from</span>
160
<a name="l00142"></a>00142 <span class="comment">// bv1 and bv3:</span>
161
<a name="l00143"></a>00143 <span class="comment">// bv3 = bv1 OR bv2</span>
162
<a name="l00144"></a>00144
163
<a name="l00145"></a>00145 <a class="code" href="a00116.html#gfbe633db8da3d867f098468269fd8d3d" title="Bitvector deserialization from memory.">bm::deserialize</a>(bv3, buf1);
164
<a name="l00146"></a>00146 <a class="code" href="a00116.html#gfbe633db8da3d867f098468269fd8d3d" title="Bitvector deserialization from memory.">bm::deserialize</a>(bv3, buf2);
165
<a name="l00147"></a>00147
166
<a name="l00148"></a>00148 <a class="code" href="a00099.html#eb4ca6f0a986ad6ae67d1f7124fdc400">print_statistics</a>(bv3);
167
<a name="l00149"></a>00149
168
<a name="l00150"></a>00150 <span class="comment">// After a complex operation we can try to optimize bv3.</span>
169
<a name="l00151"></a>00151
170
<a name="l00152"></a>00152 bv3.<a class="code" href="a00040.html#bd522c13e26c2b8f84636fe8b6648dcb" title="Optimize memory bitvector&#39;s memory allocation.">optimize</a>();
171
<a name="l00153"></a>00153
172
<a name="l00154"></a>00154 <a class="code" href="a00099.html#eb4ca6f0a986ad6ae67d1f7124fdc400">print_statistics</a>(bv3);
173
<a name="l00155"></a>00155
174
<a name="l00156"></a>00156 <span class="keyword">delete</span> [] buf1;
175
<a name="l00157"></a>00157 <span class="keyword">delete</span> [] buf2;
176
<a name="l00158"></a>00158
177
<a name="l00159"></a>00159 <span class="keywordflow">return</span> 0;
178
<a name="l00160"></a>00160 }
179
<a name="l00161"></a>00161
181
<hr size="1"><address style="text-align: right;"><small>Generated on Mon Oct 12 12:16:39 2009 for BitMagic by
23
<div class="contents">
24
<h1>Memory Allocation<br/>
26
[<a class="el" href="a00112.html">BitMagic C++ Library</a>]</small>
28
<p><div class="dynheader">
29
Collaboration diagram for Memory Allocation:</div>
30
<div class="dynsection">
31
<center><table><tr><td><img src="a00122.png" border="0" alt="" usemap="#a00122_map"/>
32
<map name="a00122_map" id="a00122">
33
<area shape="rect" href="a00112.html" title="For more information please visit: http://bmagic.sourceforge.net." alt="" coords="5,5,149,35"/></map></td></tr></table></center>
36
<table border="0" cellpadding="0" cellspacing="0">
37
<tr><td colspan="2"><h2>Data Structures</h2></td></tr>
38
<tr><td class="memItemLeft" align="right" valign="top">class </td><td class="memItemRight" valign="bottom"><a class="el" href="a00038.html">bm::block_allocator</a></td></tr>
39
<tr><td class="mdescLeft"> </td><td class="mdescRight">Default malloc based bitblock allocator class. <a href="a00038.html#_details">More...</a><br/></td></tr>
40
<tr><td class="memItemLeft" align="right" valign="top">class </td><td class="memItemRight" valign="bottom"><a class="el" href="a00077.html">bm::ptr_allocator</a></td></tr>
41
<tr><td class="mdescLeft"> </td><td class="mdescRight">Default malloc based bitblock allocator class. <a href="a00077.html#_details">More...</a><br/></td></tr>
42
<tr><td class="memItemLeft" align="right" valign="top">class </td><td class="memItemRight" valign="bottom"><a class="el" href="a00073.html">bm::mem_alloc< BA, PA ></a></td></tr>
43
<tr><td class="mdescLeft"> </td><td class="mdescRight">BM style allocator adapter. <a href="a00073.html#_details">More...</a><br/></td></tr>
44
<tr><td colspan="2"><h2>Typedefs</h2></td></tr>
45
<tr><td class="memItemLeft" align="right" valign="top">typedef mem_alloc<br class="typebreak"/>
46
< block_allocator, <br class="typebreak"/>
47
ptr_allocator > </td><td class="memItemRight" valign="bottom"><a class="el" href="a00122.html#gab9059bfb40a6588cdbacfd93901593c1">bm::standard_allocator</a></td></tr>
49
<hr/><a name="_details"></a><h2>Detailed Description</h2>
50
<p>Memory allocation related units </p>
51
<hr/><h2>Typedef Documentation</h2>
52
<a class="anchor" id="gab9059bfb40a6588cdbacfd93901593c1"></a><!-- doxytag: member="bm::standard_allocator" ref="gab9059bfb40a6588cdbacfd93901593c1" args="" -->
54
<div class="memproto">
55
<table class="memname">
57
<td class="memname">typedef mem_alloc<block_allocator, ptr_allocator> <a class="el" href="a00073.html">bm::standard_allocator</a></td>
63
<p>Definition at line <a class="el" href="a00087_source.html#l00234">234</a> of file <a class="el" href="a00087_source.html">bmalloc.h</a>.</p>
68
<hr size="1"/><address style="text-align: right;"><small>Generated on Sun Nov 22 10:49:44 2009 for BitMagic by
182
69
<a href="http://www.doxygen.org/index.html">
183
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.5 </small></address>
70
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>