23
23
<div class="contents">
24
24
<h1>sample1.cpp</h1><p>Example of how to use bvector template class to set bits and then retrieve indexes of ON bits. For more information please visit: <a href="http://bmagic.sourceforge.net">http://bmagic.sourceforge.net</a></p>
25
<dl class="see"><dt><b>See also:</b></dt><dd><a class="el" href="a00043.html#adef9c095a682a9857068c280e37c8240" title="Finds the number of the next bit ON.">bm::bvector<>::get_next()</a> </dd>
27
<a class="el" href="a00043.html#af3129b35cabf0554d85711c085a8616d" title="Gets number of first bit which is ON.">bm::bvector<>::get_first()</a> </dd>
29
<a class="el" href="a00043.html#aa745480d136c8b0e6bc41afe951577c5" title="Sets every bit in this bitset to 1.">bm::bvector<>::set()</a> </dd>
31
<a class="el" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">bm::bvector<>::count()</a> </dd>
33
<a class="el" href="a00043.html#afe98c658c4ee54155e61f9bdfa7b9019" title="Clears every bit in the bitvector.">bm::bvector<>::clear()</a></dd></dl>
25
<dl class="see"><dt><b>See also:</b></dt><dd><a class="el" href="a00044.html#adef9c095a682a9857068c280e37c8240" title="Finds the number of the next bit ON.">bm::bvector<>::get_next()</a> </dd>
27
<a class="el" href="a00044.html#af3129b35cabf0554d85711c085a8616d" title="Gets number of first bit which is ON.">bm::bvector<>::get_first()</a> </dd>
29
<a class="el" href="a00044.html#aa745480d136c8b0e6bc41afe951577c5" title="Sets every bit in this bitset to 1.">bm::bvector<>::set()</a> </dd>
31
<a class="el" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">bm::bvector<>::count()</a> </dd>
33
<a class="el" href="a00044.html#afe98c658c4ee54155e61f9bdfa7b9019" title="Clears every bit in the bitvector.">bm::bvector<>::clear()</a></dd></dl>
34
34
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
35
35
<span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
36
36
<span class="comment"></span>
72
72
<span class="preprocessor">#include <iostream></span>
73
<span class="preprocessor">#include "<a class="code" href="a00084.html">bm.h</a>"</span>
73
<span class="preprocessor">#include "<a class="code" href="a00087.html">bm.h</a>"</span>
75
75
<span class="keyword">using namespace </span>std;
77
<span class="keywordtype">int</span> <a name="a0"></a><a class="code" href="a00101.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
77
<span class="keywordtype">int</span> <a name="a0"></a><a class="code" href="a00105.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
79
<a name="_a1"></a><a class="code" href="a00043.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv; <span class="comment">// Bitvector variable declaration.</span>
79
<a name="_a1"></a><a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv; <span class="comment">// Bitvector variable declaration.</span>
81
cout << bv.<a name="a2"></a><a class="code" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
81
cout << bv.<a name="a2"></a><a class="code" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
83
83
<span class="comment">// Set some bits.</span>
85
bv.<a name="a3"></a><a class="code" href="a00043.html#abc85ec9da74de03ff7e2a6c836656dd8" title="Sets bit n if val is true, clears bit n if val is false.">set</a>(10);
86
bv.<a class="code" href="a00043.html#abc85ec9da74de03ff7e2a6c836656dd8" title="Sets bit n if val is true, clears bit n if val is false.">set</a>(100);
87
bv.<a class="code" href="a00043.html#abc85ec9da74de03ff7e2a6c836656dd8" title="Sets bit n if val is true, clears bit n if val is false.">set</a>(1000000);
85
bv.<a name="a3"></a><a class="code" href="a00044.html#abc85ec9da74de03ff7e2a6c836656dd8" title="Sets bit n if val is true, clears bit n if val is false.">set</a>(10);
86
bv.<a class="code" href="a00044.html#abc85ec9da74de03ff7e2a6c836656dd8" title="Sets bit n if val is true, clears bit n if val is false.">set</a>(100);
87
bv.<a class="code" href="a00044.html#abc85ec9da74de03ff7e2a6c836656dd8" title="Sets bit n if val is true, clears bit n if val is false.">set</a>(1000000);
89
89
<span class="comment">// New bitvector's count.</span>
91
cout << bv.<a class="code" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
91
cout << bv.<a class="code" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
94
94
<span class="comment">// Print the bitvector.</span>
96
<span class="keywordtype">unsigned</span> value = bv.<a name="a4"></a><a class="code" href="a00043.html#af3129b35cabf0554d85711c085a8616d" title="Gets number of first bit which is ON.">get_first</a>();
96
<span class="keywordtype">unsigned</span> value = bv.<a name="a4"></a><a class="code" href="a00044.html#af3129b35cabf0554d85711c085a8616d" title="Gets number of first bit which is ON.">get_first</a>();
97
97
<span class="keywordflow">do</span>
99
99
cout << value;
100
value = bv.<a name="a5"></a><a class="code" href="a00043.html#adef9c095a682a9857068c280e37c8240" title="Finds the number of the next bit ON.">get_next</a>(value);
100
value = bv.<a name="a5"></a><a class="code" href="a00044.html#adef9c095a682a9857068c280e37c8240" title="Finds the number of the next bit ON.">get_next</a>(value);
101
101
<span class="keywordflow">if</span> (value)
103
103
cout << <span class="stringliteral">","</span>;
111
111
cout << endl;
113
bv.<a name="a6"></a><a class="code" href="a00043.html#afe98c658c4ee54155e61f9bdfa7b9019" title="Clears every bit in the bitvector.">clear</a>(); <span class="comment">// Clean up.</span>
113
bv.<a name="a6"></a><a class="code" href="a00044.html#afe98c658c4ee54155e61f9bdfa7b9019" title="Clears every bit in the bitvector.">clear</a>(); <span class="comment">// Clean up.</span>
115
cout << bv.<a class="code" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
115
cout << bv.<a class="code" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
117
117
<span class="comment">// We also can use operators to set-clear bits;</span>
120
120
bv[100] = <span class="keyword">true</span>;
121
121
bv[10000] = <span class="keyword">true</span>;
123
cout << bv.<a class="code" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
123
cout << bv.<a class="code" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
125
125
<span class="keywordflow">if</span> (bv[10])
127
127
bv[10] = <span class="keyword">false</span>;
130
cout << bv.<a class="code" href="a00043.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
130
cout << bv.<a class="code" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() << endl;
132
132
<span class="keywordflow">return</span> 0;
135
135
</pre></div> </div>
136
<hr size="1"/><address style="text-align: right;"><small>Generated on Sun Nov 22 10:49:33 2009 for BitMagic by
136
<hr size="1"/><address style="text-align: right;"><small>Generated on Fri Jan 8 20:09:19 2010 for BitMagic by
137
137
<a href="http://www.doxygen.org/index.html">
138
138
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>