~ubuntu-branches/ubuntu/trusty/bmagic/trusty

« back to all changes in this revision

Viewing changes to html/a00149.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 Source File</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
<div class="nav">
 
9
<a class="el" href="dir_000001.html">samples</a>&nbsp;/&nbsp;<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>
 
42
00033 
 
43
00034 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
 
44
00035 <span class="preprocessor">#include &lt;iostream&gt;</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>
 
47
00038 
 
48
00039 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
 
49
00040 
 
50
00041 
 
51
00042 <span class="comment">// This exmaple demonstrates bitvector serialization/deserialization.</span>
 
52
00043 
 
53
00044 
 
54
00045 
 
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;
 
56
00047 
 
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>
 
60
00051 
 
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&lt;&gt;</a>* bv)
 
62
00053 {
 
63
00054     <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; <a class="code" href="a00122.html#a0">MAX_VALUE</a>; ++i)
 
64
00055     {
 
65
00056         <span class="keywordflow">if</span> (rand() % 2500)
 
66
00057         {
 
67
00058             bv-&gt;set_bit(i);
 
68
00059         }
 
69
00060     }
 
70
00061 }
 
71
00062 
 
72
00063 
 
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&lt;&gt;</a>&amp; bv)
 
74
00065 {
 
75
00066     <a class="code" href="a00084.html">bm::bvector&lt;&gt;</a>::statistics st;
 
76
00067     bv.calc_stat(&amp;st);
 
77
00068 
 
78
00069     cout &lt;&lt; <span class="stringliteral">"Bits count:"</span> &lt;&lt; bv.count() &lt;&lt; endl;
 
79
00070     cout &lt;&lt; <span class="stringliteral">"Bit blocks:"</span> &lt;&lt; st.bit_blocks &lt;&lt; endl;
 
80
00071     cout &lt;&lt; <span class="stringliteral">"GAP blocks:"</span> &lt;&lt; st.gap_blocks &lt;&lt; endl;
 
81
00072     cout &lt;&lt; <span class="stringliteral">"Memory used:"</span>&lt;&lt; st.memory_used &lt;&lt; endl;
 
82
00073     cout &lt;&lt; <span class="stringliteral">"Max.serialize mem.:"</span> &lt;&lt; st.max_serialize_mem &lt;&lt; endl &lt;&lt; endl;;
 
83
00074 }
 
84
00075 
 
85
00076 
 
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&lt;&gt;</a>&amp; bv)
 
87
00078 {
 
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>();  
 
90
00081 
 
91
00082     <a class="code" href="a00084.html">bm::bvector&lt;&gt;</a>::statistics st;
 
92
00083     bv.<a class="code" href="a00084.html#a47">calc_stat</a>(&amp;st);
 
93
00084 
 
94
00085     cout &lt;&lt; <span class="stringliteral">"Bits count:"</span> &lt;&lt; bv.<a class="code" href="a00084.html#a28">count</a>() &lt;&lt; endl;
 
95
00086     cout &lt;&lt; <span class="stringliteral">"Bit blocks:"</span> &lt;&lt; st.bit_blocks &lt;&lt; endl;
 
96
00087     cout &lt;&lt; <span class="stringliteral">"GAP blocks:"</span> &lt;&lt; st.gap_blocks &lt;&lt; endl;
 
97
00088     cout &lt;&lt; <span class="stringliteral">"Memory used:"</span>&lt;&lt; st.memory_used &lt;&lt; endl;
 
98
00089     cout &lt;&lt; <span class="stringliteral">"Max.serialize mem.:"</span> &lt;&lt; st.max_serialize_mem &lt;&lt; endl;
 
99
00090 
 
100
00091     <span class="comment">// Allocate serialization buffer.</span>
 
101
00092 
 
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];
 
103
00094 
 
104
00095     <span class="comment">// Serialization to memory.</span>
 
105
00096 
 
106
00097     <span class="keywordtype">unsigned</span> len = bm::serialize(bv, buf);
 
107
00098 
 
108
00099     cout &lt;&lt; <span class="stringliteral">"Serialized size:"</span> &lt;&lt; len &lt;&lt; endl &lt;&lt; endl;
 
109
00100 
 
110
00101     <span class="keywordflow">return</span> buf;
 
111
00102 }
 
112
00103 
 
113
00104 
 
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>)
 
115
00106 {
 
116
00107     <a class="code" href="a00084.html">bm::bvector&lt;&gt;</a>   bv1;    
 
117
00108     <a class="code" href="a00084.html">bm::bvector&lt;&gt;</a>   bv2;
 
118
00109 
 
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>
 
120
00111 
 
121
00112     <a class="code" href="a00122.html#a1">fill_bvector</a>(&amp;bv1);
 
122
00113     <a class="code" href="a00122.html#a1">fill_bvector</a>(&amp;bv2);
 
123
00114     
 
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);
 
126
00117 
 
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>
 
129
00120 
 
130
00121     <span class="comment">// ...</span>
 
131
00122 
 
132
00123     <span class="comment">// Deserialization.</span>
 
133
00124 
 
134
00125     <a class="code" href="a00084.html">bm::bvector&lt;&gt;</a>  bv3;
 
135
00126 
 
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>
 
139
00130 
 
140
00131     bm::deserialize(bv3, buf1);
 
141
00132     bm::deserialize(bv3, buf2);
 
142
00133 
 
143
00134     <a class="code" href="a00122.html#a2">print_statistics</a>(bv3);
 
144
00135 
 
145
00136     <span class="comment">// After a complex operation we can try to optimize bv3.</span>
 
146
00137 
 
147
00138     bv3.<a class="code" href="a00084.html#a55">optimize</a>();
 
148
00139 
 
149
00140     <a class="code" href="a00122.html#a2">print_statistics</a>(bv3);
 
150
00141 
 
151
00142     <span class="keyword">delete</span> [] buf1;
 
152
00143     <span class="keyword">delete</span> [] buf2;
 
153
00144 
 
154
00145     <span class="keywordflow">return</span> 0;
 
155
00146 }
 
156
00147 
 
157
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Aug 5 14:12:26 2007 for BitMagic by&nbsp;
 
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>
 
160
</body>
 
161
</html>