1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
3
<title>BitMagic: sample5.cpp Source File</title>
4
<link href="doxygen.css" rel="stylesheet" type="text/css">
5
<link href="tabs.css" rel="stylesheet" type="text/css">
7
<!-- Generated by Doxygen 1.5.5 -->
8
<div class="navigation" id="top">
11
<li><a href="index.html"><span>Main Page</span></a></li>
12
<li><a href="modules.html"><span>Modules</span></a></li>
13
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
14
<li><a href="classes.html"><span>Data Structures</span></a></li>
15
<li class="current"><a href="files.html"><span>Files</span></a></li>
16
<li><a href="examples.html"><span>Examples</span></a></li>
19
<h1>sample5.cpp</h1><a href="a00101.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
20
<a name="l00002"></a>00002 <span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
21
<a name="l00003"></a>00003 <span class="comment"></span>
22
<a name="l00004"></a>00004 <span class="comment">Permission is hereby granted, free of charge, to any person </span>
23
<a name="l00005"></a>00005 <span class="comment">obtaining a copy of this software and associated documentation </span>
24
<a name="l00006"></a>00006 <span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
25
<a name="l00007"></a>00007 <span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
26
<a name="l00008"></a>00008 <span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
27
<a name="l00009"></a>00009 <span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
28
<a name="l00010"></a>00010 <span class="comment">subject to the following conditions:</span>
29
<a name="l00011"></a>00011 <span class="comment"></span>
30
<a name="l00012"></a>00012 <span class="comment">The above copyright notice and this permission notice shall be included </span>
31
<a name="l00013"></a>00013 <span class="comment">in all copies or substantial portions of the Software.</span>
32
<a name="l00014"></a>00014 <span class="comment"></span>
33
<a name="l00015"></a>00015 <span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
34
<a name="l00016"></a>00016 <span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
35
<a name="l00017"></a>00017 <span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
36
<a name="l00018"></a>00018 <span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
37
<a name="l00019"></a>00019 <span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
38
<a name="l00020"></a>00020 <span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
39
<a name="l00021"></a>00021 <span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
40
<a name="l00022"></a>00022 <span class="comment">*/</span>
41
<a name="l00023"></a>00023 <span class="comment"></span>
42
<a name="l00024"></a>00024 <span class="comment">/** \example sample5.cpp</span>
43
<a name="l00025"></a>00025 <span class="comment"> Example demonstrates using enumerators - the fastest way to retrieve </span>
44
<a name="l00026"></a>00026 <span class="comment"> indexes of 1 bits from the bitvector. This approach works faster than</span>
45
<a name="l00027"></a>00027 <span class="comment"> get_first/get_next functions.</span>
46
<a name="l00028"></a>00028 <span class="comment"> </span>
47
<a name="l00029"></a>00029 <span class="comment"> \sa bm::bvector<>::enumerator </span>
48
<a name="l00030"></a>00030 <span class="comment"></span>
49
<a name="l00031"></a>00031 <span class="comment">For more information please visit: http://bmagic.sourceforge.net</span>
50
<a name="l00032"></a>00032 <span class="comment"></span>
51
<a name="l00033"></a>00033 <span class="comment">*/</span>
52
<a name="l00034"></a>00034
53
<a name="l00035"></a>00035 <span class="preprocessor">#include <iostream></span>
54
<a name="l00036"></a>00036 <span class="preprocessor">#include <algorithm></span>
55
<a name="l00037"></a>00037 <span class="preprocessor">#include "<a class="code" href="a00082.html">bm.h</a>"</span>
56
<a name="l00038"></a>00038
57
<a name="l00039"></a>00039 <span class="keyword">using namespace </span>std;
58
<a name="l00040"></a>00040
59
<a name="l00041"></a><a class="code" href="a00101.html#80085d7c877f4c6f36d10c67dc163845">00041</a> <span class="keywordtype">void</span> <a class="code" href="a00101.html#80085d7c877f4c6f36d10c67dc163845">Print</a>(<span class="keywordtype">unsigned</span> n)
60
<a name="l00042"></a>00042 {
61
<a name="l00043"></a>00043 cout << n << endl;;
62
<a name="l00044"></a>00044 }
63
<a name="l00045"></a>00045
64
<a name="l00046"></a><a class="code" href="a00101.html#840291bc02cba5474a4cb46a9b9566fe">00046</a> <span class="keywordtype">int</span> <a class="code" href="a00097.html#840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
65
<a name="l00047"></a>00047 {
66
<a name="l00048"></a>00048 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv;
67
<a name="l00049"></a>00049
68
<a name="l00050"></a>00050 bv[10] = <span class="keyword">true</span>;
69
<a name="l00051"></a>00051 bv[100] = <span class="keyword">true</span>;
70
<a name="l00052"></a>00052 bv[10000] = <span class="keyword">true</span>;
71
<a name="l00053"></a>00053
72
<a name="l00054"></a>00054 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<>::enumerator</a> en = bv.<a class="code" href="a00040.html#ad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>();
73
<a name="l00055"></a>00055 <a class="code" href="a00040.html" title="bitvector with runtime compression of bits.">bm::bvector<>::enumerator</a> en_end = bv.<a class="code" href="a00040.html#2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>();
74
<a name="l00056"></a>00056
75
<a name="l00057"></a>00057 <span class="keywordflow">while</span> (en < en_end)
76
<a name="l00058"></a>00058 {
77
<a name="l00059"></a>00059 cout << *en << endl;
78
<a name="l00060"></a>00060 ++en; <span class="comment">// Fastest way to increment enumerator</span>
79
<a name="l00061"></a>00061 }
80
<a name="l00062"></a>00062
81
<a name="l00063"></a>00063 en = bv.<a class="code" href="a00040.html#ad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>();
82
<a name="l00064"></a>00064
83
<a name="l00065"></a>00065 <span class="comment">// This is not the fastest way to do the job, because for_each </span>
84
<a name="l00066"></a>00066 <span class="comment">// often will try to calculate difference between iterators,</span>
85
<a name="l00067"></a>00067 <span class="comment">// which is expensive for enumerators.</span>
86
<a name="l00068"></a>00068 <span class="comment">// But it can be useful for some STL loyal applications. </span>
87
<a name="l00069"></a>00069
88
<a name="l00070"></a>00070 std::for_each(en, en_end, <a class="code" href="a00101.html#80085d7c877f4c6f36d10c67dc163845">Print</a>);
89
<a name="l00071"></a>00071
90
<a name="l00072"></a>00072 <span class="keywordflow">return</span> 0;
91
<a name="l00073"></a>00073 }
93
<hr size="1"><address style="text-align: right;"><small>Generated on Mon Oct 12 12:16:39 2009 for BitMagic by
94
<a href="http://www.doxygen.org/index.html">
95
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.5 </small></address>