File: svsample03_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 (162 lines) | stat: -rwxr-xr-x 16,040 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
<!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++: svsample03.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">svsample03.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>Example of how to merge sparse vectors and extract values fast</p><dl class="section see"><dt>See also</dt><dd><a class="el" href="classbm_1_1sparse__vector.html#a56101ce9a559b30f8e14153133063ca7" title="set specified element with bounds checking and automatic resize">bm::sparse_vector&lt;&gt;::set</a> </dd>
<dd>
<a class="el" href="classbm_1_1sparse__vector.html#ae7a505562b0f056868aa4b11864eae55" title="Import list of elements from a C-style array.">bm::sparse_vector&lt;&gt;::import</a> </dd>
<dd>
<a class="el" href="classbm_1_1sparse__vector.html#ac2f8506f78fcf0ebfc573e98ef387876" title="Bulk export list of elements to a C-style array.">bm::sparse_vector&lt;&gt;::decode</a> </dd>
<dd>
<a class="el" href="classbm_1_1sparse__vector.html#a91ae9e6f8d5e9bce06ce514072be9eb0" title="join all with another sparse vector using OR operation">bm::sparse_vector&lt;&gt;::join</a></dd></dl>
<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 svsample03.cpp</span></div>
<div class="line"><span class="comment">  Example of how to merge sparse vectors and extract values fast</span></div>
<div class="line"><span class="comment"> </span></div>
<div class="line"><span class="comment">  \sa bm::sparse_vector&lt;&gt;::set</span></div>
<div class="line"><span class="comment">  \sa bm::sparse_vector&lt;&gt;::import</span></div>
<div class="line"><span class="comment">  \sa bm::sparse_vector&lt;&gt;::decode</span></div>
<div class="line"><span class="comment">  \sa bm::sparse_vector&lt;&gt;::join</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 svsample03.cpp</span></div>
<div class="line"><span class="comment">    \brief Example: sparse_vector&lt;&gt; merge and fast extraction of content</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"> </div>
<div class="line"> </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"><span class="preprocessor">#include &quot;<a class="code" href="bm_8h.html">bm.h</a>&quot;</span></div>
<div class="line"><span class="preprocessor">#include &quot;<a class="code" href="bmsparsevec_8h.html">bmsparsevec.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"><span class="keywordtype">int</span> <a name="a0"></a><a class="code" href="svsample03_8cpp.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)</div>
<div class="line">{</div>
<div class="line">    <a name="_a1"></a><a class="code" href="classbm_1_1sparse__vector.html">bm::sparse_vector&lt;unsigned, bm::bvector&lt;&gt;</a> &gt; sv1;</div>
<div class="line">    <a class="code" href="classbm_1_1sparse__vector.html">bm::sparse_vector&lt;unsigned, bm::bvector&lt;&gt;</a> &gt; sv2;</div>
<div class="line">    <span class="keywordtype">unsigned</span> i;</div>
<div class="line">    <span class="keywordtype">unsigned</span> arr[3] = {1,2,3};</div>
<div class="line">    </div>
<div class="line">    sv1.<a name="a2"></a><a class="code" href="classbm_1_1sparse__vector.html#ae7a505562b0f056868aa4b11864eae55">import</a>(arr, 3); <span class="comment">// import from a C-style array (fastest way to populate)</span></div>
<div class="line"> </div>
<div class="line">    <span class="comment">// optimize memory allocation of sparse vector</span></div>
<div class="line">    {</div>
<div class="line">        <a name="a3"></a><a class="code" href="bm_8h.html#ad9736d9521d713fa5d48859aa720bfdd">BM_DECLARE_TEMP_BLOCK</a>(tb)</div>
<div class="line">        sv1.<a name="a4"></a><a class="code" href="classbm_1_1sparse__vector.html#af9f218aa3af25cdfc9088cc0affdaa8f">optimize</a>(tb);</div>
<div class="line">    }</div>
<div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;sv1.size() = &quot;</span> &lt;&lt; sv1.<a name="a5"></a><a class="code" href="classbm_1_1sparse__vector.html#a468ddeec517a36a4a9333008703acfab">size</a>() &lt;&lt; <span class="stringliteral">&quot;: &quot;</span>;</div>
<div class="line">    <span class="keywordflow">for</span> (i = 0; i &lt; sv1.<a class="code" href="classbm_1_1sparse__vector.html#a468ddeec517a36a4a9333008703acfab">size</a>(); ++i)</div>
<div class="line">    {</div>
<div class="line">        cout &lt;&lt; sv1.<a name="a6"></a><a class="code" href="classbm_1_1sparse__vector.html#a13e498908f6a3c2361d451a97f50b5ed">at</a>(i) &lt;&lt; <span class="stringliteral">&quot;,&quot;</span>;</div>
<div class="line">    }</div>
<div class="line">    cout &lt;&lt; endl;</div>
<div class="line"> </div>
<div class="line">    <span class="comment">// populate second sparse vector using dunamic resize assignment</span></div>
<div class="line">    <span class="comment">//</span></div>
<div class="line">    <span class="keywordflow">for</span> (i = 65536; i &lt; 65536+10; ++i)</div>
<div class="line">    {</div>
<div class="line">        sv2.<a name="a7"></a><a class="code" href="classbm_1_1sparse__vector.html#a56101ce9a559b30f8e14153133063ca7">set</a>(i, 256+i);</div>
<div class="line">    }</div>
<div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;sv2.size() = &quot;</span> &lt;&lt; sv2.<a class="code" href="classbm_1_1sparse__vector.html#a468ddeec517a36a4a9333008703acfab">size</a>() &lt;&lt; endl;</div>
<div class="line"> </div>
<div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Perform sparse_vector&lt;&gt;::join()&quot;</span> &lt;&lt; endl;</div>
<div class="line">    <span class="comment">// join sv2 into sv1</span></div>
<div class="line">    <span class="comment">// operation implemented using bitset OR on all bit-planes.</span></div>
<div class="line">    sv1.<a name="a8"></a><a class="code" href="classbm_1_1sparse__vector.html#a91ae9e6f8d5e9bce06ce514072be9eb0">join</a>(sv2);</div>
<div class="line">    sv1.<a class="code" href="classbm_1_1sparse__vector.html#af9f218aa3af25cdfc9088cc0affdaa8f">optimize</a>(); <span class="comment">// please note, we use default optimize here (it is a bit slower)</span></div>
<div class="line">    </div>
<div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Now sv1.size() = &quot;</span> &lt;&lt; sv1.<a class="code" href="classbm_1_1sparse__vector.html#a468ddeec517a36a4a9333008703acfab">size</a>() &lt;&lt; endl;</div>
<div class="line">    </div>
<div class="line">    </div>
<div class="line">    std::vector&lt;unsigned&gt; v1(16);</div>
<div class="line">    sv1.<a name="a9"></a><a class="code" href="classbm_1_1sparse__vector.html#ac2f8506f78fcf0ebfc573e98ef387876">decode</a>(&amp;v1[0], 65530, 16); <span class="comment">// extract 16 elements starting from 65530</span></div>
<div class="line">    <span class="keywordflow">for</span> (i = 0; i &lt; 16; ++i)</div>
<div class="line">    {</div>
<div class="line">        cout &lt;&lt; v1[i] &lt;&lt; <span class="stringliteral">&quot;,&quot;</span>;</div>
<div class="line">    }</div>
<div class="line">    cout &lt;&lt; endl;</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="aclassbm_1_1sparse__vector_html_af9f218aa3af25cdfc9088cc0affdaa8f"><div class="ttname"><a href="classbm_1_1sparse__vector.html#af9f218aa3af25cdfc9088cc0affdaa8f">bm::sparse_vector::optimize</a></div><div class="ttdeci">void optimize(bm::word_t *temp_block=0, typename bvector_type::optmode opt_mode=bvector_type::opt_compress, typename sparse_vector&lt; Val, BV &gt;::statistics *stat=0)</div><div class="ttdoc">run memory optimization for all vector plains</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l01728">bmsparsevec.h:1728</a></div></div>
<div class="ttc" id="aclassbm_1_1sparse__vector_html"><div class="ttname"><a href="classbm_1_1sparse__vector.html">bm::sparse_vector</a></div><div class="ttdoc">sparse vector with runtime compression using bit transposition method</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l00081">bmsparsevec.h:81</a></div></div>
<div class="ttc" id="abm_8h_html_ad9736d9521d713fa5d48859aa720bfdd"><div class="ttname"><a href="bm_8h.html#ad9736d9521d713fa5d48859aa720bfdd">BM_DECLARE_TEMP_BLOCK</a></div><div class="ttdeci">#define BM_DECLARE_TEMP_BLOCK(x)</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l00047">bm.h:47</a></div></div>
<div class="ttc" id="abmsparsevec_8h_html"><div class="ttname"><a href="bmsparsevec_8h.html">bmsparsevec.h</a></div><div class="ttdoc">Sparse constainer sparse_vector&lt;&gt; for integer types using bit-transposition transform.</div></div>
<div class="ttc" id="aclassbm_1_1sparse__vector_html_ae7a505562b0f056868aa4b11864eae55"><div class="ttname"><a href="classbm_1_1sparse__vector.html#ae7a505562b0f056868aa4b11864eae55">bm::sparse_vector::import</a></div><div class="ttdeci">void import(const value_type *arr, size_type arr_size, size_type offset=0, bool set_not_null=true)</div><div class="ttdoc">Import list of elements from a C-style array.</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l00994">bmsparsevec.h:994</a></div></div>
<div class="ttc" id="aclassbm_1_1sparse__vector_html_a468ddeec517a36a4a9333008703acfab"><div class="ttname"><a href="classbm_1_1sparse__vector.html#a468ddeec517a36a4a9333008703acfab">bm::sparse_vector::size</a></div><div class="ttdeci">size_type size() const BMNOEXCEPT</div><div class="ttdoc">return size of the vector</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l00644">bmsparsevec.h:644</a></div></div>
<div class="ttc" id="asvsample03_8cpp_html_a840291bc02cba5474a4cb46a9b9566fe"><div class="ttname"><a href="svsample03_8cpp.html#a840291bc02cba5474a4cb46a9b9566fe">main</a></div><div class="ttdeci">int main(void)</div><div class="ttdef"><b>Definition:</b> <a href="svsample03_8cpp_source.html#l00040">svsample03.cpp:40</a></div></div>
<div class="ttc" id="aclassbm_1_1sparse__vector_html_a13e498908f6a3c2361d451a97f50b5ed"><div class="ttname"><a href="classbm_1_1sparse__vector.html#a13e498908f6a3c2361d451a97f50b5ed">bm::sparse_vector::at</a></div><div class="ttdeci">value_type at(size_type idx) const</div><div class="ttdoc">access specified element with bounds checking</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l01434">bmsparsevec.h:1434</a></div></div>
<div class="ttc" id="aclassbm_1_1sparse__vector_html_ac2f8506f78fcf0ebfc573e98ef387876"><div class="ttname"><a href="classbm_1_1sparse__vector.html#ac2f8506f78fcf0ebfc573e98ef387876">bm::sparse_vector::decode</a></div><div class="ttdeci">size_type decode(value_type *arr, size_type idx_from, size_type dec_size, bool zero_mem=true) const</div><div class="ttdoc">Bulk export list of elements to a C-style array.</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l01083">bmsparsevec.h:1083</a></div></div>
<div class="ttc" id="aclassbm_1_1sparse__vector_html_a91ae9e6f8d5e9bce06ce514072be9eb0"><div class="ttname"><a href="classbm_1_1sparse__vector.html#a91ae9e6f8d5e9bce06ce514072be9eb0">bm::sparse_vector::join</a></div><div class="ttdeci">sparse_vector&lt; Val, BV &gt; &amp; join(const sparse_vector&lt; Val, BV &gt; &amp;sv)</div><div class="ttdoc">join all with another sparse vector using OR operation</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l01764">bmsparsevec.h:1764</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_1sparse__vector_html_a56101ce9a559b30f8e14153133063ca7"><div class="ttname"><a href="classbm_1_1sparse__vector.html#a56101ce9a559b30f8e14153133063ca7">bm::sparse_vector::set</a></div><div class="ttdeci">void set(size_type idx, value_type v)</div><div class="ttdoc">set specified element with bounds checking and automatic resize</div><div class="ttdef"><b>Definition:</b> <a href="bmsparsevec_8h_source.html#l01469">bmsparsevec.h:1469</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>