1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>BitMagic: sample4.cpp</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<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>
<h1>sample4.cpp</h1>Exmaple demonstrates bitvector serialization/deserialization.<p>
For more information please visit: <a href="http://bmagic.sourceforge.net">http://bmagic.sourceforge.net</a><p>
<dl compact><dt><b>See also:</b></dt><dd>bm::serialize <p>
bm::deserialize</dd></dl>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
<span class="comment"></span>
<span class="comment">Permission is hereby granted, free of charge, to any person </span>
<span class="comment">obtaining a copy of this software and associated documentation </span>
<span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
<span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
<span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
<span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
<span class="comment">subject to the following conditions:</span>
<span class="comment"></span>
<span class="comment">The above copyright notice and this permission notice shall be included </span>
<span class="comment">in all copies or substantial portions of the Software.</span>
<span class="comment"></span>
<span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
<span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
<span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
<span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
<span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
<span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
<span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
<span class="comment">*/</span>
<span class="comment"></span>
<span class="comment">/** \example sample4.cpp</span>
<span class="comment"> Exmaple demonstrates bitvector serialization/deserialization.</span>
<span class="comment"> </span>
<span class="comment">For more information please visit: http://bmagic.sourceforge.net</span>
<span class="comment"></span>
<span class="comment"> \sa bm::serialize </span>
<span class="comment"> \sa bm::deserialize </span>
<span class="comment"></span>
<span class="comment">*/</span>
<span class="preprocessor">#include <stdlib.h></span>
<span class="preprocessor">#include <iostream></span>
<span class="preprocessor">#include "<a class="code" href="a00074.html">bm.h</a>"</span>
<span class="preprocessor">#include "bmserial.h"</span>
<span class="keyword">using</span> <span class="keyword">namespace </span>std;
<span class="comment">// This exmaple demonstrates bitvector serialization/deserialization.</span>
<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="a00085.html#a0">MAX_VALUE</a> = 1000000;
<span class="comment">// This procedure creates very dense bitvector.</span>
<span class="comment">// The resulting set will consists mostly from ON (1) bits</span>
<span class="comment">// interrupted with small gaps of 0 bits.</span>
<span class="keywordtype">void</span> <a name="a18"></a><a class="code" href="a00085.html#a1">fill_bvector</a>(<a name="_a19"></a><a class="code" href="a00048.html">bm::bvector<></a>* bv)
{
<span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < MAX_VALUE; ++i)
{
<span class="keywordflow">if</span> (rand() % 2500)
{
bv->set_bit(i);
}
}
}
<span class="keywordtype">void</span> <a name="a20"></a><a class="code" href="a00085.html#a2">print_statistics</a>(<span class="keyword">const</span> <a class="code" href="a00048.html">bm::bvector<></a>& bv)
{
<a class="code" href="a00048.html">bm::bvector<></a>::statistics st;
bv.calc_stat(&st);
cout << <span class="stringliteral">"Bits count:"</span> << bv.count() << endl;
cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl << endl;;
}
<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* <a name="a21"></a><a class="code" href="a00086.html#a3">serialize_bvector</a>(<a class="code" href="a00048.html">bm::bvector<></a>& bv)
{
<span class="comment">// It is reccomended to optimize vector before serialization.</span>
bv.<a name="a22"></a><a class="code" href="a00048.html#a53">optimize</a>();
<a class="code" href="a00048.html">bm::bvector<></a>::statistics st;
bv.<a name="a23"></a><a class="code" href="a00048.html#a45">calc_stat</a>(&st);
cout << <span class="stringliteral">"Bits count:"</span> << bv.<a name="a24"></a><a class="code" href="a00048.html#a26">count</a>() << endl;
cout << <span class="stringliteral">"Bit blocks:"</span> << st.bit_blocks << endl;
cout << <span class="stringliteral">"GAP blocks:"</span> << st.gap_blocks << endl;
cout << <span class="stringliteral">"Memory used:"</span><< st.memory_used << endl;
cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.max_serialize_mem << endl;
<span class="comment">// Allocate serialization buffer.</span>
<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];
<span class="comment">// Serialization to memory.</span>
<span class="keywordtype">unsigned</span> len = bm::serialize(bv, buf);
cout << <span class="stringliteral">"Serialized size:"</span> << len << endl << endl;
<span class="keywordflow">return</span> buf;
}
<span class="keywordtype">int</span> <a name="a25"></a><a class="code" href="a00083.html#a0">main</a>(<span class="keywordtype">void</span>)
{
<a class="code" href="a00048.html">bm::bvector<></a> bv1;
<a class="code" href="a00048.html">bm::bvector<></a> bv2;
bv2.<a name="a26"></a><a class="code" href="a00048.html#a50">set_new_blocks_strat</a>(bm::BM_GAP); <span class="comment">// set DGAP compression mode ON</span>
<a class="code" href="a00085.html#a1">fill_bvector</a>(&bv1);
<a class="code" href="a00085.html#a1">fill_bvector</a>(&bv2);
<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf1 = <a class="code" href="a00086.html#a3">serialize_bvector</a>(bv1);
<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>* buf2 = <a class="code" href="a00086.html#a3">serialize_bvector</a>(bv2);
<span class="comment">// Serialized bvectors (buf1 and buf2) now ready to be</span>
<span class="comment">// saved to a database, file or send over a network.</span>
<span class="comment">// ...</span>
<span class="comment">// Deserialization.</span>
<a class="code" href="a00048.html">bm::bvector<></a> bv3;
<span class="comment">// As a result of desrialization bv3 will contain all bits from</span>
<span class="comment">// bv1 and bv3:</span>
<span class="comment">// bv3 = bv1 OR bv2</span>
bm::deserialize(bv3, buf1);
bm::deserialize(bv3, buf2);
<a class="code" href="a00085.html#a2">print_statistics</a>(bv3);
<span class="comment">// After a complex operation we can try to optimize bv3.</span>
bv3.<a name="a27"></a><a class="code" href="a00048.html#a53">optimize</a>();
<a class="code" href="a00085.html#a2">print_statistics</a>(bv3);
<span class="keyword">delete</span> [] buf1;
<span class="keyword">delete</span> [] buf2;
<span class="keywordflow">return</span> 0;
}
</pre></div> <hr size="1"><address style="align: right;"><small>Generated on Thu Apr 20 13:28:45 2006 for BitMagic by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>
</body>
</html>
|