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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>BitMagic: sample7.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.1 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Data&nbsp;Structures</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
</div>
<div class="contents">
<h1>sample7.cpp</h1><p>This example demonstrates using of memory save mode of bitset operations.</p>
<p>For more information please visit: <a href="http://bmagic.sourceforge.net">http://bmagic.sourceforge.net</a></p>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
<span class="comment"></span>
<span class="comment">Permission is hereby granted, free of charge, to any person </span>
<span class="comment">obtaining a copy of this software and associated documentation </span>
<span class="comment">files (the &quot;Software&quot;), to deal in the Software without restriction, </span>
<span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
<span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
<span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
<span class="comment">subject to the following conditions:</span>
<span class="comment"></span>
<span class="comment">The above copyright notice and this permission notice shall be included </span>
<span class="comment">in all copies or substantial portions of the Software.</span>
<span class="comment"></span>
<span class="comment">THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, </span>
<span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
<span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
<span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
<span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
<span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
<span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
<span class="comment">*/</span>
<span class="comment"></span>
<span class="comment">/*! @example sample7.cpp</span>
<span class="comment"> This example demonstrates using of memory save mode of bitset operations.</span>
<span class="comment"></span>
<span class="comment">For more information please visit:  http://bmagic.sourceforge.net</span>
<span class="comment"></span>
<span class="comment">*/</span>
<span class="comment"></span>
<span class="comment">/*! @file</span>
<span class="comment">    @ingroup mset</span>
<span class="comment">*/</span>

<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<span class="preprocessor">#include &lt;stdio.h&gt;</span>
<span class="preprocessor">#include &lt;time.h&gt;</span>


<span class="comment">// BM library version 3.1.3 and later can keep internal bit flags in pointers.</span>
<span class="comment">// This is efficient but not completely portable hack. </span>
<span class="comment">// For compatibility it can be disabled it by defining BM_DISBALE_BIT_IN_PTR.</span>
<span class="comment">// If you do not disable bits in pointer the second template parameter of bvector</span>
<span class="comment">// is simply ignored and portion of this example is becoming irrelevant.</span>
<span class="preprocessor">#define BM_DISBALE_BIT_IN_PTR</span>
<span class="preprocessor"></span><span class="preprocessor">#include &quot;<a class="code" href="a00088.html">bm.h</a>&quot;</span>

<span class="keyword">using namespace </span>std;

<span class="comment">// Customized bitvector uses standard memory allocator and uses</span>
<span class="comment">// an alternative implementation of internal set. Saves memory when we </span>
<span class="comment">// work with sparse or dense bitsets.</span>
<span class="keyword">typedef</span> <a name="_a0"></a><a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bm::bvector</a>&lt;<a name="_a1"></a><a class="code" href="a00074.html" title="BM style allocator adapter.">bm::standard_allocator</a>, 
                    <a name="_a2"></a><a class="code" href="a00075.html" title="Template class implements memory saving set functionality.">bm::miniset&lt;bm::block_allocator, bm::set_total_blocks&gt;</a> &gt; <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bvect</a>;


<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a name="a3"></a><a class="code" href="a00113.html#a2508b4033cfced61642ad9358d67dd2a">setscount</a> = 10000;
<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a name="a4"></a><a class="code" href="a00113.html#a0be29ed34efa43589f6c14e4e9f5dbc4">randombits</a> = 150;
<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a name="a5"></a><a class="code" href="a00113.html#a1b239e2f0f190ff4df0b359f8b601644">maxbit</a> = 100000000;

<a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bvect</a>*  <a name="a6"></a><a class="code" href="a00113.html#aef9a3bedb74930ffc25d49f7ac4ba1eb">bitsets</a>[<a class="code" href="a00113.html#a2508b4033cfced61642ad9358d67dd2a">setscount</a>];

<span class="comment">// ---------------------------------------------------------</span>

<span class="keywordtype">void</span> <a name="a7"></a><a class="code" href="a00113.html#adb34fb3c04bbc949324b069298b6ad60">CreateSets</a>()
{
    <span class="keywordtype">unsigned</span> mu = 0;
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; <a class="code" href="a00113.html#a2508b4033cfced61642ad9358d67dd2a">setscount</a>; ++i)
    {
        <span class="keywordflow">if</span> ((i % 100) == 0) { cout &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>; cout.flush(); }
        <span class="comment">// create bitvector using in GAP mode using an alternative</span>
        <span class="comment">// GAP levels table (minimalistic).</span>
        bitsets[i] = 
            <span class="keyword">new</span> <a name="a8"></a><a class="code" href="a00112.html#ac07bb102df543a0c581493c5f35b2129">bvect</a>(<a name="a9"></a><a class="code" href="a00119.html#ggad0b87b3b7292383a864d27feaf1c9effa7fe09d1d7c62a813db922c396c0cbc9c" title="GAP compression is ON.">bm::BM_GAP</a>, <a name="_a10"></a><a class="code" href="a00068.html" title="Alternative GAP lengths table. Good for for memory saver mode and very sparse bitsets...">bm::gap_len_table_min&lt;true&gt;::_len</a>, <a class="code" href="a00113.html#a1b239e2f0f190ff4df0b359f8b601644">maxbit</a>);
        <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bvect</a>&amp; bv = *bitsets[i];
        <a name="_a11"></a><a class="code" href="a00086.html" title="Statistical information about bitset&amp;#39;s memory allocation details.">bvect::statistics</a> st;
        bv.<a name="a12"></a><a class="code" href="a00044.html#a1eaaac76a97b9eb1d3c11f21cf9385ac" title="Calculates bitvector statistics.">calc_stat</a>(&amp;st);
        mu += st.<a name="a13"></a><a class="code" href="a00043.html#ad3b2190eb4afe24ecd56323cbc0a7666" title="Memory used by bitvector including temp and service blocks.">memory_used</a>;
    }
    cout &lt;&lt; endl &lt;&lt; <span class="stringliteral">&quot;Created &quot;</span> &lt;&lt; setscount &lt;&lt; <span class="stringliteral">&quot; sets.&quot;</span> &lt;&lt; endl; 
    cout &lt;&lt; <span class="stringliteral">&quot;Used &quot;</span> &lt;&lt; mu / (1024*1024)&lt;&lt; <span class="stringliteral">&quot; MB.&quot;</span> &lt;&lt; endl;
}

<span class="comment">// ---------------------------------------------------------</span>

<span class="keywordtype">void</span> <a name="a14"></a><a class="code" href="a00113.html#a8b36b27c3c40d9e559c078673a3a4dc9">FillSets</a>()
{
    <span class="keywordtype">unsigned</span> mu, bit_blocks, gap_blocks;
    cout &lt;&lt; <span class="stringliteral">&quot;Filling sets...&quot;</span>;
    mu = bit_blocks = gap_blocks = 0;
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; <a class="code" href="a00113.html#a2508b4033cfced61642ad9358d67dd2a">setscount</a>; ++i)
    {
        <span class="keywordflow">if</span> ((i % 100) == 0) { cout &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>; cout.flush(); }
        <span class="keywordflow">if</span> ((i % 3) == 0) <span class="keywordflow">continue</span>;
        <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bvect</a>&amp; bv = *bitsets[i];
        <span class="keywordtype">unsigned</span> bn = 0;
        <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j &lt; <a class="code" href="a00113.html#a0be29ed34efa43589f6c14e4e9f5dbc4">randombits</a>; j+=3)
        {
            bn += (<a class="code" href="a00113.html#a1b239e2f0f190ff4df0b359f8b601644">maxbit</a> / randombits) + rand() % 10;
            <span class="keywordflow">if</span> (bn &gt; <a class="code" href="a00113.html#a1b239e2f0f190ff4df0b359f8b601644">maxbit</a>) bn = rand() % <a class="code" href="a00113.html#a1b239e2f0f190ff4df0b359f8b601644">maxbit</a>;

            bv[bn] = <span class="keyword">true</span>;
            bv[bn+1] = <span class="keyword">true</span>;
            bv[bn+2] = <span class="keyword">true</span>;
        }
        <a class="code" href="a00086.html" title="Statistical information about bitset&amp;#39;s memory allocation details.">bvect::statistics</a> st;
        bv.<a class="code" href="a00044.html#a1eaaac76a97b9eb1d3c11f21cf9385ac" title="Calculates bitvector statistics.">calc_stat</a>(&amp;st);
        mu += st.<a class="code" href="a00043.html#ad3b2190eb4afe24ecd56323cbc0a7666" title="Memory used by bitvector including temp and service blocks.">memory_used</a>;
        bit_blocks += st.<a name="a15"></a><a class="code" href="a00043.html#a4ec8ba190b4ba31c431c008402881c62" title="Number of bit blocks.">bit_blocks</a>;
        gap_blocks += st.<a name="a16"></a><a class="code" href="a00043.html#a4dcfd64e9ab0be5472e3c2b04e551f81" title="Number of GAP blocks.">gap_blocks</a>;
    }
    cout &lt;&lt; endl &lt;&lt; <span class="stringliteral">&quot;Used &quot;</span> &lt;&lt; mu / (1024*1024)&lt;&lt; <span class="stringliteral">&quot; MB.&quot;</span> &lt;&lt; endl;

    cout &lt;&lt; <span class="stringliteral">&quot;BIT Blocks=&quot;</span> &lt;&lt; bit_blocks &lt;&lt; endl;
    cout &lt;&lt; <span class="stringliteral">&quot;GAP Blocks=&quot;</span> &lt;&lt; gap_blocks &lt;&lt; endl;
}

<span class="comment">// ---------------------------------------------------------</span>

<span class="keywordtype">void</span> <a name="a17"></a><a class="code" href="a00113.html#a5cb27a7766b9e7c1ea79b2741714b404">EnumerateSets</a>()
{
    cout &lt;&lt; <span class="stringliteral">&quot;Enumerating sets...&quot;</span>;
    <span class="keywordtype">unsigned</span> bitcnt = 0;
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; setscount; ++i)
    {
        <span class="keywordflow">if</span> ((i % 100) == 0) { cout &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>; cout.flush(); }
        <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bvect</a>&amp; bv = *bitsets[i];

        <a name="_a18"></a><a class="code" href="a00063.html" title="Constant input iterator designed to enumerate &amp;quot;ON&amp;quot; bits.">bvect::enumerator</a> en = bv.<a name="a19"></a><a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>();
        <a class="code" href="a00063.html" title="Constant input iterator designed to enumerate &amp;quot;ON&amp;quot; bits.">bvect::enumerator</a> en_end = bv.<a name="a20"></a><a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>();

        <span class="keywordflow">for</span> (;en &lt; en_end; ++en)
        {
            ++bitcnt;
        }
    }
    cout &lt;&lt; endl &lt;&lt; bitcnt &lt;&lt; endl;
}

<span class="comment">// ---------------------------------------------------------</span>

<span class="keywordtype">void</span> <a name="a21"></a><a class="code" href="a00113.html#aa777f579687be6096cdbb8b2d70baf6e">DestroySets</a>()
{
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; setscount; ++i)
    {
        <span class="keyword">delete</span> bitsets[i];
    }
}

<span class="comment">// ---------------------------------------------------------</span>

<span class="keywordtype">void</span> <a name="a22"></a><a class="code" href="a00113.html#a1bdd608971ec7ea3d5c76cf21c7d92e7">OrSets</a>()
{
    <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bvect</a> res;
    cout &lt;&lt; <span class="stringliteral">&quot;Calculating Or...&quot;</span>;
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; setscount; ++i)
    {
        <span class="keywordflow">if</span> ((i % 100) == 0) { cout &lt;&lt; <span class="stringliteral">&quot;.&quot;</span>; cout.flush(); }
        <span class="keyword">const</span> <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bvect</a>&amp; bv = *bitsets[i];

        res |= bv;
    }
    cout &lt;&lt; endl &lt;&lt; res.<a name="a23"></a><a class="code" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>() &lt;&lt; endl;
}

<span class="comment">// ---------------------------------------------------------</span>



<span class="keywordtype">int</span> <a name="a24"></a><a class="code" href="a00106.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
{
    time_t start_time = time(0);

    <a class="code" href="a00113.html#adb34fb3c04bbc949324b069298b6ad60">CreateSets</a>();
    <a class="code" href="a00113.html#a8b36b27c3c40d9e559c078673a3a4dc9">FillSets</a>();
    <a class="code" href="a00113.html#a5cb27a7766b9e7c1ea79b2741714b404">EnumerateSets</a>();
    <a class="code" href="a00113.html#a1bdd608971ec7ea3d5c76cf21c7d92e7">OrSets</a>();

    time_t end_time = time(0);
    <span class="keywordtype">unsigned</span> elapsed = unsigned(end_time - start_time);
    cout &lt;&lt; <span class="stringliteral">&quot;elapsed=&quot;</span> &lt;&lt; elapsed &lt;&lt; endl;
    <span class="keywordtype">unsigned</span> ops;
    <span class="keywordflow">if</span> (elapsed)
        ops = setscount / elapsed;
    <span class="keywordflow">else</span>
        ops = setscount;

    cout &lt;&lt; <span class="stringliteral">&quot;Time = &quot;</span> &lt;&lt; (end_time - start_time) &lt;&lt; endl;
    cout &lt;&lt; <span class="stringliteral">&quot;Operations per second:&quot;</span> &lt;&lt; ops &lt;&lt; endl;

    <a class="code" href="a00113.html#aa777f579687be6096cdbb8b2d70baf6e">DestroySets</a>();

    <span class="keywordflow">return</span> 0;
}

  
</pre></div> </div>
<hr size="1"/><address style="text-align: right;"><small>Generated on Tue Mar 23 20:47:54 2010 for BitMagic by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>
</body>
</html>