File: a00040.html

package info (click to toggle)
bmagic 3.4.0-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 4,208 kB
  • ctags: 2,500
  • sloc: cpp: 9,399; ansic: 3,142; sh: 2,430; makefile: 340
file content (211 lines) | stat: -rw-r--r-- 12,736 bytes parent folder | download
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>BitMagic: sample7.cpp</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<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>
<h1>sample7.cpp</h1>This example demonstrates using of memory save mode of bitset operations.<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 "Software"), 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 "AS IS", 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 "<a class="code" href="a00074.html">bm.h</a>"</span>

<span class="keyword">using</span> <span class="keyword">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="_a37"></a><a class="code" href="a00048.html">bm::bvector</a>&lt;bm::standard_allocator, 
                    <a name="_a38"></a><a class="code" href="a00072.html">bm::miniset&lt;bm::block_allocator, bm::set_total_blocks&gt;</a> &gt; <a class="code" href="a00088.html#a1">bvect</a>;


<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="a00089.html#a2">setscount</a> = 10000;
<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="a00089.html#a3">randombits</a> = 150;
<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a class="code" href="a00089.html#a4">maxbit</a> = 100000000;

<a class="code" href="a00088.html#a1">bvect</a>*  <a class="code" href="a00089.html#a5">bitsets</a>[<a class="code" href="a00089.html#a2">setscount</a>];

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

<span class="keywordtype">void</span> <a name="a39"></a><a class="code" href="a00089.html#a6">CreateSets</a>()
{
    <span class="keywordtype">unsigned</span> mu = 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">"."</span>; cout.flush(); }
        <span class="comment">// create bitvector using in GAP mode using an alternative</span>
        <span class="comment">// GAP levels table (minimalistic).</span>
        <a class="code" href="a00089.html#a5">bitsets</a>[i] = 
            <span class="keyword">new</span> <a name="a40"></a><a class="code" href="a00088.html#a1">bvect</a>(bm::BM_GAP, <a name="_a41"></a><a class="code" href="a00069.html">bm::gap_len_table_min&lt;true&gt;</a>::_len, maxbit);
        <a class="code" href="a00088.html#a1">bvect</a>&amp; bv = *<a class="code" href="a00089.html#a5">bitsets</a>[i];
        bvect::statistics st;
        bv.<a name="a42"></a><a class="code" href="a00048.html#a45">calc_stat</a>(&amp;st);
        mu += st.memory_used;
    }
    cout &lt;&lt; endl &lt;&lt; <span class="stringliteral">"Created "</span> &lt;&lt; <a class="code" href="a00089.html#a2">setscount</a> &lt;&lt; <span class="stringliteral">" sets."</span> &lt;&lt; endl; 
    cout &lt;&lt; <span class="stringliteral">"Used "</span> &lt;&lt; mu / (1024*1024)&lt;&lt; <span class="stringliteral">" MB."</span> &lt;&lt; endl;
}

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

<span class="keywordtype">void</span> <a name="a43"></a><a class="code" href="a00089.html#a7">FillSets</a>()
{
    <span class="keywordtype">unsigned</span> mu, bit_blocks, gap_blocks;
    cout &lt;&lt; <span class="stringliteral">"Filling sets..."</span>;
    mu = bit_blocks = gap_blocks = 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">"."</span>; cout.flush(); }
        <span class="keywordflow">if</span> ((i % 3) == 0) continue;
        bvect&amp; bv = *bitsets[i];
        <span class="keywordtype">unsigned</span> bn = 0;
        for (<span class="keywordtype">unsigned</span> j = 0; j &lt; randombits; j+=3)
        {
            bn += (<a class="code" href="a00089.html#a4">maxbit</a> / <a class="code" href="a00089.html#a3">randombits</a>) + rand() % 10;
            <span class="keywordflow">if</span> (bn &gt; maxbit) bn = rand() % maxbit;

            bv[bn] = true;
            bv[bn+1] = true;
            bv[bn+2] = true;
        }
        bvect::statistics st;
        bv.calc_stat(&amp;st);
        mu += st.memory_used;
        bit_blocks += st.bit_blocks;
        gap_blocks += st.gap_blocks;
    }
    cout &lt;&lt; endl &lt;&lt; "Used " &lt;&lt; mu / (1024*1024)&lt;&lt; " MB." &lt;&lt; endl;

    cout &lt;&lt; "BIT Blocks=" &lt;&lt; bit_blocks &lt;&lt; endl;
    cout &lt;&lt; "GAP Blocks=" &lt;&lt; gap_blocks &lt;&lt; endl;
}

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

<span class="keywordtype">void</span> EnumerateSets()
{
    cout &lt;&lt; <span class="stringliteral">"Enumerating sets..."</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">"."</span>; cout.flush(); }
        <a class="code" href="a00088.html#a1">bvect</a>&amp; bv = *<a class="code" href="a00089.html#a5">bitsets</a>[i];

        bvect::enumerator en = bv.<a name="a44"></a><a class="code" href="a00048.html#a59">first</a>();
        bvect::enumerator en_end = bv.<a name="a45"></a><a class="code" href="a00048.html#a60">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="a46"></a><a class="code" href="a00089.html#a9">DestroySets</a>()
{
    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i &lt; setscount; ++i)
    {
        <span class="keyword">delete</span> <a class="code" href="a00089.html#a5">bitsets</a>[i];
    }
}

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

<span class="keywordtype">void</span> <a name="a47"></a><a class="code" href="a00089.html#a10">OrSets</a>()
{
    <a class="code" href="a00088.html#a1">bvect</a> res;
    cout &lt;&lt; <span class="stringliteral">"Calculating Or..."</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">"."</span>; cout.flush(); }
        <span class="keyword">const</span> <a class="code" href="a00088.html#a1">bvect</a>&amp; bv = *<a class="code" href="a00089.html#a5">bitsets</a>[i];

        res |= bv;
    }
    cout &lt;&lt; endl &lt;&lt; res.<a name="a48"></a><a class="code" href="a00048.html#a26">count</a>() &lt;&lt; endl;
}

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



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

    <a class="code" href="a00089.html#a6">CreateSets</a>();
    <a class="code" href="a00089.html#a7">FillSets</a>();
    <a name="a50"></a><a class="code" href="a00089.html#a8">EnumerateSets</a>();
    <a class="code" href="a00089.html#a10">OrSets</a>();

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

    cout &lt;&lt; "Time = " &lt;&lt; (end_time - start_time) &lt;&lt; endl;
    cout &lt;&lt; "Operations per second:" &lt;&lt; ops &lt;&lt; endl;

    DestroySets();

    return 0;
}

  
</pre></div> <hr size="1"><address style="align: right;"><small>Generated on Thu Apr 20 13:28:45 2006 for BitMagic by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>
</body>
</html>