~ubuntu-branches/ubuntu/jaunty/bmagic/jaunty

« back to all changes in this revision

Viewing changes to html/a00037.html

  • Committer: Bazaar Package Importer
  • Author(s): Andres Salomon
  • Date: 2008-01-05 23:58:56 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080105235856-2kmxhxkz14qjy9ia
Tags: 3.5.0-1
* New upstream release.
* Add tcpp.dpatch.  This stops tests/stress/t.cpp from including
  ncbi_pch.hpp.  As far as I can tell, NCBI is not used at all, I have
  no idea where that came from..
* Silence some lintian warnings; binary-arch-rules-but-pkg-is-arch-indep
  and ancient-standards-version.

Show diffs side-by-side

added added

removed removed

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