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
|
<!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++: sample3.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&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">sample3.cpp</div> </div>
</div><!--header-->
<div class="contents">
<p>Exmaple demonstrates using bitvectors with different initial block allocation strategy.Bitvector 1 (bv1) by default working without RLE compression option (best performance, maximum memory consumption). Bitvector 2 (bv2) will be working in compression mode and use less memory.</p>
<dl class="section see"><dt>See also</dt><dd><a class="el" href="classbm_1_1bvector.html#a653ee052b297a49c35344704df67a452" title="Sets new blocks allocation strategy.">bm::bvector<>::set_new_blocks_strat()</a></dd></dl>
<p>For more information please visit: <a href="http://bmagic.sourceforge.net">http://bmagic.sourceforge.net</a></p>
<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 "License");</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 "AS IS" 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 sample3.cpp</span></div>
<div class="line"><span class="comment"> Exmaple demonstrates using bitvectors with different initial</span></div>
<div class="line"><span class="comment"> block allocation strategy. </span></div>
<div class="line"><span class="comment"> Bitvector 1 (bv1) by default working without RLE compression option</span></div>
<div class="line"><span class="comment"> (best performance, maximum memory consumption). </span></div>
<div class="line"><span class="comment"> Bitvector 2 (bv2) will be working in compression mode and use less memory.</span></div>
<div class="line"><span class="comment"> </span></div>
<div class="line"><span class="comment"> \sa bm::bvector<>::set_new_blocks_strat() </span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment"> For more information please visit: http://bmagic.sourceforge.net</span></div>
<div class="line"><span class="comment"></span> </div>
<div class="line"><span class="comment">*/</span><span class="comment"></span></div>
<div class="line"><span class="comment">/*! \file sample3.cpp</span></div>
<div class="line"><span class="comment"> \brief Example: bvector<> with different allocation/compression strategies</span></div>
<div class="line"><span class="comment">*/</span></div>
<div class="line"><span class="preprocessor">#include <stdlib.h></span></div>
<div class="line"><span class="preprocessor">#include <iostream></span></div>
<div class="line"><span class="preprocessor">#include "<a class="code" href="bm_8h.html">bm.h</a>"</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="keyword">const</span> <span class="keywordtype">unsigned</span> <a name="a0"></a><a class="code" href="sample3_8cpp.html#a8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a> = 1000000;</div>
<div class="line"> </div>
<div class="line"><span class="comment">// This procedure creates very dense bitvectors.</span></div>
<div class="line"><span class="comment">// The resulting set will consists mostly from ON (1) bits</span></div>
<div class="line"><span class="comment">// interrupted with small gaps of 0 bits.</span></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="sample3_8cpp.html#a6ab5952664863268e8109fd11218815f">fill_bvector</a>(<a name="_a2"></a><a class="code" href="classbm_1_1bvector.html">bm::bvector<></a>* bv1, <a class="code" href="classbm_1_1bvector.html">bm::bvector<></a>* bv2)</div>
<div class="line">{</div>
<div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < <a class="code" href="sample3_8cpp.html#a8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a>; ++i)</div>
<div class="line"> {</div>
<div class="line"> <span class="keywordflow">if</span> (rand() % 2500)</div>
<div class="line"> {</div>
<div class="line"> bv1-><a name="a3"></a><a class="code" href="classbm_1_1bvector.html#a6dfc9af9e9041405cbd29394121c4eb6">set_bit</a>(i);</div>
<div class="line"> bv2-><a class="code" href="classbm_1_1bvector.html#a6dfc9af9e9041405cbd29394121c4eb6">set_bit</a>(i);</div>
<div class="line"> }</div>
<div class="line"> }</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"><span class="keyword">static</span></div>
<div class="line"><span class="keywordtype">void</span> <a name="a4"></a><a class="code" href="sample3_8cpp.html#a67503e5a21cea06b95ae7a0dd18f602e">print_statistics</a>(<span class="keyword">const</span> <a class="code" href="classbm_1_1bvector.html">bm::bvector<></a>& bv)</div>
<div class="line">{</div>
<div class="line"> <a name="_a5"></a><a class="code" href="structbm_1_1bvector_1_1statistics.html">bm::bvector<>::statistics</a> st;</div>
<div class="line"> bv.<a name="a6"></a><a class="code" href="classbm_1_1bvector.html#af30532c371bfdc8e2ba02bc2115b004d">calc_stat</a>(&st);</div>
<div class="line"> </div>
<div class="line"> cout << <span class="stringliteral">"Bits count:"</span> << bv.<a name="a7"></a><a class="code" href="classbm_1_1bvector.html#a3009ba9b116f13f4933a48641f3a695d">count</a>() << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Bit blocks:"</span> << st.<a name="a8"></a><a class="code" href="structbm_1_1bv__statistics.html#a7be7eba0cd58b422085b2c8c75c7e188">bit_blocks</a> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"GAP blocks:"</span> << st.<a name="a9"></a><a class="code" href="structbm_1_1bv__statistics.html#a68582594950732906ddea7a99f8cd0b7">gap_blocks</a> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Memory used:"</span><< st.<a name="a10"></a><a class="code" href="structbm_1_1bv__statistics.html#aeb3cae5be872eed7fbe4191a8d710e42">memory_used</a> << endl;</div>
<div class="line"> cout << <span class="stringliteral">"Max.serialize mem.:"</span> << st.<a name="a11"></a><a class="code" href="structbm_1_1bv__statistics.html#acf1547fe85bf8562dde286488d567f35">max_serialize_mem</a> << endl << endl;;</div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> <a name="a12"></a><a class="code" href="sample3_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<></a> bv1;</div>
<div class="line"> <a class="code" href="classbm_1_1bvector.html">bm::bvector<></a> bv2;</div>
<div class="line"> </div>
<div class="line"> bv2.<a name="a13"></a><a class="code" href="classbm_1_1bvector.html#a653ee052b297a49c35344704df67a452">set_new_blocks_strat</a>(<a name="a14"></a><a class="code" href="group__bvector.html#ggad0b87b3b7292383a864d27feaf1c9effa7fe09d1d7c62a813db922c396c0cbc9c">bm::BM_GAP</a>); <span class="comment">// set DGAP compression mode ON</span></div>
<div class="line"> </div>
<div class="line"> <a class="code" href="sample3_8cpp.html#a6ab5952664863268e8109fd11218815f">fill_bvector</a>(&bv1, &bv2); <span class="comment">// Fill both bvectors with the same values</span></div>
<div class="line"> </div>
<div class="line"> <span class="comment">// For a given distrubution statistics should demonstrate</span></div>
<div class="line"> <span class="comment">// lower memory consumption for the vector with compression</span></div>
<div class="line"> </div>
<div class="line"> <a class="code" href="sample3_8cpp.html#a67503e5a21cea06b95ae7a0dd18f602e">print_statistics</a>(bv1);</div>
<div class="line"> <a class="code" href="sample3_8cpp.html#a67503e5a21cea06b95ae7a0dd18f602e">print_statistics</a>(bv2);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Now run optimization procedure for bv1 and see statistics.</span></div>
<div class="line"> bv1.<a name="a15"></a><a class="code" href="classbm_1_1bvector.html#a622e6827d799a561e2f0db28f7ac19d2">optimize</a>();</div>
<div class="line"> </div>
<div class="line"> <a class="code" href="sample3_8cpp.html#a67503e5a21cea06b95ae7a0dd18f602e">print_statistics</a>(bv1);</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">catch</span>(std::exception& ex)</div>
<div class="line"> {</div>
<div class="line"> std::cerr << ex.what() << std::endl;</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_1bvector_html_af30532c371bfdc8e2ba02bc2115b004d"><div class="ttname"><a href="classbm_1_1bvector.html#af30532c371bfdc8e2ba02bc2115b004d">bm::bvector::calc_stat</a></div><div class="ttdeci">void calc_stat(struct bm::bvector< Alloc >::statistics *st) const BMNOEXCEPT</div><div class="ttdoc">Calculates bitvector statistics.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l03393">bm.h:3393</a></div></div>
<div class="ttc" id="astructbm_1_1bv__statistics_html_acf1547fe85bf8562dde286488d567f35"><div class="ttname"><a href="structbm_1_1bv__statistics.html#acf1547fe85bf8562dde286488d567f35">bm::bv_statistics::max_serialize_mem</a></div><div class="ttdeci">size_t max_serialize_mem</div><div class="ttdoc">estimated maximum memory for serialization</div><div class="ttdef"><b>Definition:</b> <a href="bmfunc_8h_source.html#l00060">bmfunc.h:60</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a653ee052b297a49c35344704df67a452"><div class="ttname"><a href="classbm_1_1bvector.html#a653ee052b297a49c35344704df67a452">bm::bvector::set_new_blocks_strat</a></div><div class="ttdeci">void set_new_blocks_strat(strategy strat)</div><div class="ttdoc">Sets new blocks allocation strategy.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l01810">bm.h:1810</a></div></div>
<div class="ttc" id="asample3_8cpp_html_a8652d848be89c1f9b0a77327ee32fc0f"><div class="ttname"><a href="sample3_8cpp.html#a8652d848be89c1f9b0a77327ee32fc0f">MAX_VALUE</a></div><div class="ttdeci">const unsigned MAX_VALUE</div><div class="ttdef"><b>Definition:</b> <a href="sample3_8cpp_source.html#l00040">sample3.cpp:40</a></div></div>
<div class="ttc" id="astructbm_1_1bv__statistics_html_a7be7eba0cd58b422085b2c8c75c7e188"><div class="ttname"><a href="structbm_1_1bv__statistics.html#a7be7eba0cd58b422085b2c8c75c7e188">bm::bv_statistics::bit_blocks</a></div><div class="ttdeci">size_t bit_blocks</div><div class="ttdoc">Number of bit blocks.</div><div class="ttdef"><b>Definition:</b> <a href="bmfunc_8h_source.html#l00056">bmfunc.h:56</a></div></div>
<div class="ttc" id="astructbm_1_1bv__statistics_html_a68582594950732906ddea7a99f8cd0b7"><div class="ttname"><a href="structbm_1_1bv__statistics.html#a68582594950732906ddea7a99f8cd0b7">bm::bv_statistics::gap_blocks</a></div><div class="ttdeci">size_t gap_blocks</div><div class="ttdoc">Number of GAP blocks.</div><div class="ttdef"><b>Definition:</b> <a href="bmfunc_8h_source.html#l00057">bmfunc.h:57</a></div></div>
<div class="ttc" id="asample3_8cpp_html_a6ab5952664863268e8109fd11218815f"><div class="ttname"><a href="sample3_8cpp.html#a6ab5952664863268e8109fd11218815f">fill_bvector</a></div><div class="ttdeci">static void fill_bvector(bm::bvector<> *bv1, bm::bvector<> *bv2)</div><div class="ttdef"><b>Definition:</b> <a href="sample3_8cpp_source.html#l00046">sample3.cpp:46</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html"><div class="ttname"><a href="classbm_1_1bvector.html">bm::bvector<></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="asample3_8cpp_html_a67503e5a21cea06b95ae7a0dd18f602e"><div class="ttname"><a href="sample3_8cpp.html#a67503e5a21cea06b95ae7a0dd18f602e">print_statistics</a></div><div class="ttdeci">static void print_statistics(const bm::bvector<> &bv)</div><div class="ttdef"><b>Definition:</b> <a href="sample3_8cpp_source.html#l00059">sample3.cpp:59</a></div></div>
<div class="ttc" id="agroup__bvector_html_ggad0b87b3b7292383a864d27feaf1c9effa7fe09d1d7c62a813db922c396c0cbc9c"><div class="ttname"><a href="group__bvector.html#ggad0b87b3b7292383a864d27feaf1c9effa7fe09d1d7c62a813db922c396c0cbc9c">bm::BM_GAP</a></div><div class="ttdeci">@ BM_GAP</div><div class="ttdoc">GAP compression is ON.</div><div class="ttdef"><b>Definition:</b> <a href="bmconst_8h_source.html#l00144">bmconst.h:144</a></div></div>
<div class="ttc" id="asample3_8cpp_html_a840291bc02cba5474a4cb46a9b9566fe"><div class="ttname"><a href="sample3_8cpp.html#a840291bc02cba5474a4cb46a9b9566fe">main</a></div><div class="ttdeci">int main(void)</div><div class="ttdef"><b>Definition:</b> <a href="sample3_8cpp_source.html#l00072">sample3.cpp:72</a></div></div>
<div class="ttc" id="astructbm_1_1bv__statistics_html_aeb3cae5be872eed7fbe4191a8d710e42"><div class="ttname"><a href="structbm_1_1bv__statistics.html#aeb3cae5be872eed7fbe4191a8d710e42">bm::bv_statistics::memory_used</a></div><div class="ttdeci">size_t memory_used</div><div class="ttdoc">memory usage for all blocks and service tables</div><div class="ttdef"><b>Definition:</b> <a href="bmfunc_8h_source.html#l00061">bmfunc.h:61</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<> container, set algebraic methods, traversal iterators.</div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a6dfc9af9e9041405cbd29394121c4eb6"><div class="ttname"><a href="classbm_1_1bvector.html#a6dfc9af9e9041405cbd29394121c4eb6">bm::bvector::set_bit</a></div><div class="ttdeci">bool set_bit(size_type n, bool val=true)</div><div class="ttdoc">Sets bit n.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l03617">bm.h:3617</a></div></div>
<div class="ttc" id="aclassbm_1_1bvector_html_a622e6827d799a561e2f0db28f7ac19d2"><div class="ttname"><a href="classbm_1_1bvector.html#a622e6827d799a561e2f0db28f7ac19d2">bm::bvector::optimize</a></div><div class="ttdeci">void optimize(bm::word_t *temp_block=0, optmode opt_mode=opt_compress, statistics *stat=0)</div><div class="ttdoc">Optimize memory bitvector's memory allocation.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l03071">bm.h:3071</a></div></div>
<div class="ttc" id="astructbm_1_1bvector_1_1statistics_html"><div class="ttname"><a href="structbm_1_1bvector_1_1statistics.html">bm::bvector::statistics</a></div><div class="ttdoc">Statistical information about bitset's memory allocation details.</div><div class="ttdef"><b>Definition:</b> <a href="bm_8h_source.html#l00121">bm.h:121</a></div></div>
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.17
</small></address>
</body>
</html>
|