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: sample5.cpp</title>
4
<link href="doxygen.css" rel="stylesheet" type="text/css">
6
<!-- Generated by Doxygen 1.4.1 -->
7
<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>
8
<h1>sample5.cpp</h1>Example demonstrates using enumerators - the fastest way to retrieve indexes of 1 bits from the bitvector. This approach works faster than get_first/get_next functions.<p>
9
<dl compact><dt><b>See also:</b></dt><dd><a class="el" href="a00084.html#n1">bm::bvector<>::enumerator</a></dd></dl>
10
For more information please visit: <a href="http://bmagic.sourceforge.net">http://bmagic.sourceforge.net</a><p>
11
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
12
<span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
13
<span class="comment"></span>
14
<span class="comment">Permission is hereby granted, free of charge, to any person </span>
15
<span class="comment">obtaining a copy of this software and associated documentation </span>
16
<span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
17
<span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
18
<span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
19
<span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
20
<span class="comment">subject to the following conditions:</span>
21
<span class="comment"></span>
22
<span class="comment">The above copyright notice and this permission notice shall be included </span>
23
<span class="comment">in all copies or substantial portions of the Software.</span>
24
<span class="comment"></span>
25
<span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
26
<span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
27
<span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
28
<span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
29
<span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
30
<span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
31
<span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
32
<span class="comment">*/</span>
33
<span class="comment"></span>
34
<span class="comment">/** \example sample5.cpp</span>
35
<span class="comment"> Example demonstrates using enumerators - the fastest way to retrieve </span>
36
<span class="comment"> indexes of 1 bits from the bitvector. This approach works faster than</span>
37
<span class="comment"> get_first/get_next functions.</span>
38
<span class="comment"> </span>
39
<span class="comment"> \sa bm::bvector<>::enumerator </span>
40
<span class="comment"></span>
41
<span class="comment">For more information please visit: http://bmagic.sourceforge.net</span>
42
<span class="comment"></span>
43
<span class="comment">*/</span>
45
<span class="preprocessor">#include <iostream></span>
46
<span class="preprocessor">#include <algorithm></span>
47
<span class="preprocessor">#include "<a class="code" href="a00111.html">bm.h</a>"</span>
49
<span class="keyword">using</span> <span class="keyword">namespace </span>std;
51
<span class="keywordtype">void</span> <a name="a28"></a><a class="code" href="a00124.html#a0">Print</a>(<span class="keywordtype">unsigned</span> n)
53
cout << n << endl;;
56
<span class="keywordtype">int</span> <a name="a29"></a><a class="code" href="a00120.html#a0">main</a>(<span class="keywordtype">void</span>)
58
<a name="_a30"></a><a class="code" href="a00084.html">bm::bvector<></a> bv;
60
bv[10] = <span class="keyword">true</span>;
61
bv[100] = <span class="keyword">true</span>;
62
bv[10000] = <span class="keyword">true</span>;
64
<a class="code" href="a00084.html">bm::bvector<></a>::enumerator en = bv.<a name="a31"></a><a class="code" href="a00084.html#a61">first</a>();
65
<a class="code" href="a00084.html">bm::bvector<></a>::enumerator en_end = bv.<a name="a32"></a><a class="code" href="a00084.html#a62">end</a>();
67
<span class="keywordflow">while</span> (en < en_end)
69
cout << *en << endl;
70
++en; <span class="comment">// Fastest way to increment enumerator</span>
73
en = bv.<a class="code" href="a00084.html#a61">first</a>();
75
<span class="comment">// This is not the fastest way to do the job, because for_each </span>
76
<span class="comment">// often will try to calculate difference between iterators,</span>
77
<span class="comment">// which is expensive for enumerators.</span>
78
<span class="comment">// But it can be useful for some STL loyal applications. </span>
80
std::for_each(en, en_end, Print);
82
<span class="keywordflow">return</span> 0;
84
</pre></div> <hr size="1"><address style="align: right;"><small>Generated on Sun Aug 5 14:12:25 2007 for BitMagic by
85
<a href="http://www.doxygen.org/index.html">
86
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>