File: sample15_8cpp-example.html

package info (click to toggle)
bmagic 6.3.0-1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 49,956 kB
  • sloc: cpp: 84,298; ansic: 9,703; sh: 1,664; makefile: 742
file content (177 lines) | stat: -rwxr-xr-x 16,078 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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://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"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.17"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>BitMagic-C++: sample15.cpp</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">BitMagic-C++
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.17 -->
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',false,false,'search.php','Search');
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">sample15.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Example for finding first and last bits in bit-vector (dynamic range).Ranges of bit-vectors can be used to find probability of intersection. For instance, in some corner cases AND product can be predicted empty if vectors belong to different ranges. </p><pre class="fragment">@sa bm::bvector&lt;&gt;::find()
@sa bm::bvector&lt;&gt;::find_reverse()
@sa bm::bvector&lt;&gt;::find_range()
</pre><div class="fragment"><div class="line"><span class="comment">/*</span></div>
<div class="line"><span class="comment">Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span></div>
<div class="line"><span class="comment">you may not use this file except in compliance with the License.</span></div>
<div class="line"><span class="comment">You may obtain a copy of the License at</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">    http://www.apache.org/licenses/LICENSE-2.0</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">Unless required by applicable law or agreed to in writing, software</span></div>
<div class="line"><span class="comment">distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span></div>
<div class="line"><span class="comment">WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div>
<div class="line"><span class="comment">See the License for the specific language governing permissions and</span></div>
<div class="line"><span class="comment">limitations under the License.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">For more information please visit:  http://bitmagic.io</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">/** \example sample15.cpp</span></div>
<div class="line"><span class="comment">Example for finding first and last bits in bit-vector (dynamic range).</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">Ranges of bit-vectors can be used to find probability of intersection.</span></div>
<div class="line"><span class="comment">For instance, in some corner cases AND product can be predicted empty if vectors</span></div>
<div class="line"><span class="comment">belong to different ranges.</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">    @sa bm::bvector&lt;&gt;::find()</span></div>
<div class="line"><span class="comment">    @sa bm::bvector&lt;&gt;::find_reverse()</span></div>
<div class="line"><span class="comment">    @sa bm::bvector&lt;&gt;::find_range()</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">/*! \file sample15.cpp</span></div>
<div class="line"><span class="comment">    \brief Example: bvector&lt;&gt; methods to find last bit and bit-vectors effective range</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &lt;stdlib.h&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;vector&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="bm_8h.html">bm.h</a>&quot;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keyword">const</span> <span class="keywordtype">unsigned</span> <a name="a0"></a><a class="code" href="sample15_8cpp.html#a8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a> = 10000000;</div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span></div>
<div class="line"><span class="keywordtype">void</span> <a name="a1"></a><a class="code" href="sample15_8cpp.html#a20e0b4b93aca798208bbc2cda0ea1db4">fill_bvector</a>(<a name="_a2"></a><a class="code" href="classbm_1_1bvector.html">bm::bvector&lt;&gt;</a>* bv)</div>
<div class="line">{</div>
<div class="line">    <span class="keywordtype">unsigned</span> start = <a class="code" href="sample15_8cpp.html#a8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a> / (rand()%10);</div>
<div class="line">    <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = start; i &lt; <a class="code" href="sample15_8cpp.html#a8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a>; ++i)</div>
<div class="line">    {</div>
<div class="line">        <span class="keywordflow">if</span> ((rand() % 10))</div>
<div class="line">        {</div>
<div class="line">            bv-&gt;<a name="a3"></a><a class="code" href="classbm_1_1bvector.html#a52b4259a5cc4ebd8953832c696e24f02">set</a>(i);</div>
<div class="line">        }</div>
<div class="line">    }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> <a name="a4"></a><a class="code" href="sample15_8cpp.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    <span class="keywordflow">try</span></div>
<div class="line">    {</div>
<div class="line">        <a class="code" href="classbm_1_1bvector.html">bm::bvector&lt;&gt;</a>   bv1;</div>
<div class="line">        <a class="code" href="classbm_1_1bvector.html">bm::bvector&lt;&gt;</a>   bv2;</div>
<div class="line"> </div>
<div class="line">        <a class="code" href="sample15_8cpp.html#a20e0b4b93aca798208bbc2cda0ea1db4">fill_bvector</a>(&amp;bv1);</div>
<div class="line">        <a class="code" href="sample15_8cpp.html#a20e0b4b93aca798208bbc2cda0ea1db4">fill_bvector</a>(&amp;bv2);</div>
<div class="line">        </div>
<div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;bv1 count = &quot;</span> &lt;&lt; bv1.<a name="a5"></a><a class="code" href="classbm_1_1bvector.html#a3009ba9b116f13f4933a48641f3a695d">count</a>() &lt;&lt; endl;</div>
<div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;bv2 count = &quot;</span> &lt;&lt; bv2.<a class="code" href="classbm_1_1bvector.html#a3009ba9b116f13f4933a48641f3a695d">count</a>() &lt;&lt; endl;</div>
<div class="line"> </div>
<div class="line">        <span class="keywordtype">bool</span> found;</div>
<div class="line">        <a class="code" href="classbm_1_1bvector.html#a405dd9ed048c935c84a5086ec8ba3649">bm::bvector&lt;&gt;::size_type</a> first, last;</div>
<div class="line">        </div>
<div class="line">        found = bv1.<a name="a6"></a><a class="code" href="classbm_1_1bvector.html#a08093c98bb6dfe27708357801aae7280">find</a>(first);</div>
<div class="line">        <span class="keywordflow">if</span> (found)</div>
<div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;bv1 first = &quot;</span> &lt;&lt; first &lt;&lt; endl;</div>
<div class="line">        </div>
<div class="line">        found = bv1.<a name="a7"></a><a class="code" href="classbm_1_1bvector.html#ada8a47cb4a23e1402a7a6d728e35d50f">find_reverse</a>(last);</div>
<div class="line">        <span class="keywordflow">if</span> (found)</div>
<div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;bv1 last = &quot;</span> &lt;&lt; last &lt;&lt; endl;</div>
<div class="line"> </div>
<div class="line">        found = bv2.<a class="code" href="classbm_1_1bvector.html#a08093c98bb6dfe27708357801aae7280">find</a>(first);</div>
<div class="line">        <span class="keywordflow">if</span> (found)</div>
<div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;bv2 first = &quot;</span> &lt;&lt; first &lt;&lt; endl;</div>
<div class="line">        </div>
<div class="line">        found = bv2.<a class="code" href="classbm_1_1bvector.html#ada8a47cb4a23e1402a7a6d728e35d50f">find_reverse</a>(last);</div>
<div class="line">        <span class="keywordflow">if</span> (found)</div>
<div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;bv2 last = &quot;</span> &lt;&lt; last &lt;&lt; endl;</div>
<div class="line">        </div>
<div class="line">        found = bv1.<a name="a8"></a><a class="code" href="classbm_1_1bvector.html#a509670c79426e6388396c7720519f5c1">find_range</a>(first, last);</div>
<div class="line">        <span class="keywordflow">if</span> (found)</div>
<div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;bv1 range = [&quot;</span> &lt;&lt; first &lt;&lt; <span class="stringliteral">&quot;, &quot;</span> &lt;&lt; last &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; endl;</div>
<div class="line">        </div>
<div class="line">        found = bv2.<a class="code" href="classbm_1_1bvector.html#a509670c79426e6388396c7720519f5c1">find_range</a>(first, last);</div>
<div class="line">        <span class="keywordflow">if</span> (found)</div>
<div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;bv2 range = [&quot;</span> &lt;&lt; first &lt;&lt; <span class="stringliteral">&quot;, &quot;</span> &lt;&lt; last &lt;&lt; <span class="stringliteral">&quot;]&quot;</span> &lt;&lt; endl;</div>
<div class="line"> </div>
<div class="line">    }</div>
<div class="line">    <span class="keywordflow">catch</span>(std::exception&amp; ex)</div>
<div class="line">    {</div>
<div class="line">        std::cerr &lt;&lt; ex.what() &lt;&lt; std::endl;</div>
<div class="line">        <span class="keywordflow">return</span> 1;</div>
<div class="line">    }</div>
<div class="line">    </div>
<div class="line"> </div>
<div class="line">    <span class="keywordflow">return</span> 0;</div>
<div class="line">}</div>
<div class="line"> </div>
</div><!-- fragment --> </div><!-- contents -->
<div class="ttc" id="asample15_8cpp_html_a20e0b4b93aca798208bbc2cda0ea1db4"><div class="ttname"><a href="sample15_8cpp.html#a20e0b4b93aca798208bbc2cda0ea1db4">fill_bvector</a></div><div class="ttdeci">static void fill_bvector(bm::bvector&lt;&gt; *bv)</div><div class="ttdef"><b>Definition:</b> <a href="sample15_8cpp_source.html#l00047">sample15.cpp:47</a></div></div>
<div class="ttc" id="asample15_8cpp_html_a840291bc02cba5474a4cb46a9b9566fe"><div class="ttname"><a href="sample15_8cpp.html#a840291bc02cba5474a4cb46a9b9566fe">main</a></div><div class="ttdeci">int main(void)</div><div class="ttdef"><b>Definition:</b> <a href="sample15_8cpp_source.html#l00060">sample15.cpp:60</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html"><div class="ttname"><a href="classbm_1_1bvector.html">bm::bvector&lt;&gt;</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a52b4259a5cc4ebd8953832c696e24f02"><div class="ttname"><a href="classbm_1_1bvector.html#a52b4259a5cc4ebd8953832c696e24f02">bm::bvector::set</a></div><div class="ttdeci">bvector&lt; Alloc &gt; &amp; set(size_type n, bool val=true)</div><div class="ttdoc">Sets bit n if val is true, clears bit n if val is false.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l03581">bm.h:3581</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a3009ba9b116f13f4933a48641f3a695d"><div class="ttname"><a href="classbm_1_1bvector.html#a3009ba9b116f13f4933a48641f3a695d">bm::bvector::count</a></div><div class="ttdeci">size_type count() const BMNOEXCEPT</div><div class="ttdoc">population cout (count of ON bits)</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l02194">bm.h:2194</a></div></div>
<div class="ttc" id="asample15_8cpp_html_a8652d848be89c1f9b0a77327ee32fc0f"><div class="ttname"><a href="sample15_8cpp.html#a8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a></div><div class="ttdeci">const unsigned MAX_VALUE</div><div class="ttdef"><b>Definition:</b> <a href="sample15_8cpp_source.html#l00044">sample15.cpp:44</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a08093c98bb6dfe27708357801aae7280"><div class="ttname"><a href="classbm_1_1bvector.html#a08093c98bb6dfe27708357801aae7280">bm::bvector::find</a></div><div class="ttdeci">bool find(size_type &amp;pos) const BMNOEXCEPT</div><div class="ttdoc">Finds index of first 1 bit.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l04035">bm.h:4035</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a509670c79426e6388396c7720519f5c1"><div class="ttname"><a href="classbm_1_1bvector.html#a509670c79426e6388396c7720519f5c1">bm::bvector::find_range</a></div><div class="ttdeci">bool find_range(size_type &amp;first, size_type &amp;last) const BMNOEXCEPT</div><div class="ttdoc">Finds dynamic range of bit-vector [first, last].</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l04081">bm.h:4081</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a405dd9ed048c935c84a5086ec8ba3649"><div class="ttname"><a href="classbm_1_1bvector.html#a405dd9ed048c935c84a5086ec8ba3649">bm::bvector::size_type</a></div><div class="ttdeci">bm::id_t size_type</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l00117">bm.h:117</a></div></div>
<div class="ttc" id="abm_8h_html"><div class="ttname"><a href="bm_8h.html">bm.h</a></div><div class="ttdoc">Compressed bit-vector bvector&lt;&gt; container, set algebraic methods, traversal iterators.</div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_ada8a47cb4a23e1402a7a6d728e35d50f"><div class="ttname"><a href="classbm_1_1bvector.html#ada8a47cb4a23e1402a7a6d728e35d50f">bm::bvector::find_reverse</a></div><div class="ttdeci">bool find_reverse(size_type &amp;pos) const BMNOEXCEPT</div><div class="ttdoc">Finds last index of 1 bit.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l03978">bm.h:3978</a></div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.17
</small></address>
</body>
</html>