1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml">
4
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5
<title>BitMagic: sample8.cpp Source File</title>
6
<link href="tabs.css" rel="stylesheet" type="text/css"/>
7
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
10
<!-- Generated by Doxygen 1.6.1 -->
11
<div class="navigation" id="top">
14
<li><a href="index.html"><span>Main Page</span></a></li>
15
<li><a href="modules.html"><span>Modules</span></a></li>
16
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
17
<li><a href="annotated.html"><span>Data Structures</span></a></li>
18
<li class="current"><a href="files.html"><span>Files</span></a></li>
19
<li><a href="examples.html"><span>Examples</span></a></li>
24
<li><a href="files.html"><span>File List</span></a></li>
25
<li><a href="globals.html"><span>Globals</span></a></li>
28
<h1>sample8.cpp</h1><a href="a00113.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
29
<a name="l00002"></a>00002 <span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
30
<a name="l00003"></a>00003 <span class="comment"></span>
31
<a name="l00004"></a>00004 <span class="comment">Permission is hereby granted, free of charge, to any person </span>
32
<a name="l00005"></a>00005 <span class="comment">obtaining a copy of this software and associated documentation </span>
33
<a name="l00006"></a>00006 <span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
34
<a name="l00007"></a>00007 <span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
35
<a name="l00008"></a>00008 <span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
36
<a name="l00009"></a>00009 <span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
37
<a name="l00010"></a>00010 <span class="comment">subject to the following conditions:</span>
38
<a name="l00011"></a>00011 <span class="comment"></span>
39
<a name="l00012"></a>00012 <span class="comment">The above copyright notice and this permission notice shall be included </span>
40
<a name="l00013"></a>00013 <span class="comment">in all copies or substantial portions of the Software.</span>
41
<a name="l00014"></a>00014 <span class="comment"></span>
42
<a name="l00015"></a>00015 <span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
43
<a name="l00016"></a>00016 <span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
44
<a name="l00017"></a>00017 <span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
45
<a name="l00018"></a>00018 <span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
46
<a name="l00019"></a>00019 <span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
47
<a name="l00020"></a>00020 <span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
48
<a name="l00021"></a>00021 <span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
49
<a name="l00022"></a>00022 <span class="comment">*/</span>
50
<a name="l00023"></a>00023 <span class="comment"></span>
51
<a name="l00024"></a>00024 <span class="comment">/** \example sample8.cpp</span>
52
<a name="l00025"></a>00025 <span class="comment"></span>
53
<a name="l00026"></a>00026 <span class="comment"> Example demonstrates some STL compatability set operations using</span>
54
<a name="l00027"></a>00027 <span class="comment"> set iterators.</span>
55
<a name="l00028"></a>00028 <span class="comment"> </span>
56
<a name="l00029"></a>00029 <span class="comment"> \sa bm::bvector<>::enumerator </span>
57
<a name="l00030"></a>00030 <span class="comment"> \sa bm::bvector<>::insert_iterator</span>
58
<a name="l00031"></a>00031 <span class="comment"></span>
59
<a name="l00032"></a>00032 <span class="comment"> For more information please visit: http://bmagic.sourceforge.net</span>
60
<a name="l00033"></a>00033 <span class="comment"></span>
61
<a name="l00034"></a>00034 <span class="comment">*/</span>
62
<a name="l00035"></a>00035
63
<a name="l00036"></a>00036 <span class="preprocessor">#include <iostream></span>
64
<a name="l00037"></a>00037 <span class="preprocessor">#include <algorithm></span>
65
<a name="l00038"></a>00038 <span class="preprocessor">#include <vector></span>
66
<a name="l00039"></a>00039 <span class="preprocessor">#include <list></span>
67
<a name="l00040"></a>00040
68
<a name="l00041"></a>00041 <span class="keyword">using</span> std::vector;
69
<a name="l00042"></a>00042 <span class="keyword">using</span> std::list;
70
<a name="l00043"></a>00043
71
<a name="l00044"></a>00044 <span class="comment">// This example requires STL compatibility</span>
72
<a name="l00045"></a>00045 <span class="preprocessor">#ifdef BM_NO_STL</span>
73
<a name="l00046"></a>00046 <span class="preprocessor"></span><span class="preprocessor"># undef BM_NO_STL</span>
74
<a name="l00047"></a>00047 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
75
<a name="l00048"></a>00048 <span class="preprocessor"></span>
76
<a name="l00049"></a>00049 <span class="preprocessor">#include "<a class="code" href="a00087.html">bm.h</a>"</span>
77
<a name="l00050"></a>00050
78
<a name="l00051"></a>00051 <span class="keyword">using namespace </span>std;
79
<a name="l00052"></a>00052
80
<a name="l00053"></a><a class="code" href="a00113.html#a80085d7c877f4c6f36d10c67dc163845">00053</a> <span class="keywordtype">void</span> <a class="code" href="a00110.html#a80085d7c877f4c6f36d10c67dc163845">Print</a>(<span class="keywordtype">unsigned</span> n)
81
<a name="l00054"></a>00054 {
82
<a name="l00055"></a>00055 cout << n << endl;;
83
<a name="l00056"></a>00056 }
84
<a name="l00057"></a>00057
85
<a name="l00058"></a>00058 <span class="comment">// Utility template function used to print container</span>
86
<a name="l00059"></a><a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">00059</a> <span class="keyword">template</span><<span class="keyword">class</span> T> <span class="keywordtype">void</span> <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(T first, T last)
87
<a name="l00060"></a>00060 {
88
<a name="l00061"></a>00061 <span class="keywordflow">if</span> (first == last)
89
<a name="l00062"></a>00062 cout << <span class="stringliteral">"<EMPTY SET>"</span>;
90
<a name="l00063"></a>00063 <span class="keywordflow">else</span>
91
<a name="l00064"></a>00064 <span class="keywordflow">for</span>(;first != last; ++first)
92
<a name="l00065"></a>00065 cout << *first << <span class="stringliteral">";"</span>;
93
<a name="l00066"></a>00066 cout << endl;
94
<a name="l00067"></a>00067 }
95
<a name="l00068"></a>00068
96
<a name="l00069"></a><a class="code" href="a00113.html#a840291bc02cba5474a4cb46a9b9566fe">00069</a> <span class="keywordtype">int</span> <a class="code" href="a00105.html#a840291bc02cba5474a4cb46a9b9566fe">main</a>(<span class="keywordtype">void</span>)
97
<a name="l00070"></a>00070 {
98
<a name="l00071"></a>00071 <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv;
99
<a name="l00072"></a>00072
100
<a name="l00073"></a>00073 bv[10] = <span class="keyword">true</span>;
101
<a name="l00074"></a>00074 bv[100] = <span class="keyword">true</span>;
102
<a name="l00075"></a>00075 bv[10000] = <span class="keyword">true</span>;
103
<a name="l00076"></a>00076
104
<a name="l00077"></a>00077 cout << <span class="stringliteral">"Source set:"</span>;
105
<a name="l00078"></a>00078 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(bv.<a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>(), bv.<a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>());
106
<a name="l00079"></a>00079
107
<a name="l00080"></a>00080 <span class="comment">// copy all bitset information into STL vector using copy algorithm</span>
108
<a name="l00081"></a>00081 {
109
<a name="l00082"></a>00082 vector<unsigned> vect;
110
<a name="l00083"></a>00083 vect.resize(bv.<a class="code" href="a00044.html#a71077c1315555301532e24d4d2b95ea2" title="Returns count of bits which are 1.">count</a>());
111
<a name="l00084"></a>00084 std::copy(bv.<a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>(), bv.<a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>(), vect.begin());
112
<a name="l00085"></a>00085 cout << <span class="stringliteral">"Vector:"</span>;
113
<a name="l00086"></a>00086 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(vect.begin(), vect.end());
114
<a name="l00087"></a>00087 }
115
<a name="l00088"></a>00088
116
<a name="l00089"></a>00089 <span class="comment">// doing the same with the help of back_inserter</span>
117
<a name="l00090"></a>00090
118
<a name="l00091"></a>00091 {
119
<a name="l00092"></a>00092 list<unsigned> lst;
120
<a name="l00093"></a>00093 std::copy(bv.<a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>(), bv.<a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>(), std::back_inserter(lst));
121
<a name="l00094"></a>00094 cout << <span class="stringliteral">"List:"</span>;
122
<a name="l00095"></a>00095 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(lst.begin(), lst.end());
123
<a name="l00096"></a>00096 }
124
<a name="l00097"></a>00097
125
<a name="l00098"></a>00098 {
126
<a name="l00099"></a>00099 vector<unsigned> vect;
127
<a name="l00100"></a>00100 vector<unsigned> res1, res2, res3;
128
<a name="l00101"></a>00101
129
<a name="l00102"></a>00102 vect.push_back(100);
130
<a name="l00103"></a>00103 vect.push_back(15);
131
<a name="l00104"></a>00104 vect.push_back(150);
132
<a name="l00105"></a>00105
133
<a name="l00106"></a>00106 cout << <span class="stringliteral">"Argument vector for set operations:"</span>;
134
<a name="l00107"></a>00107 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(vect.begin(), vect.end());
135
<a name="l00108"></a>00108
136
<a name="l00109"></a>00109 <span class="comment">// set should be ordered by < to make set algorithms possible</span>
137
<a name="l00110"></a>00110 std::sort(vect.begin(), vect.end());
138
<a name="l00111"></a>00111 cout << endl;
139
<a name="l00112"></a>00112
140
<a name="l00113"></a>00113 std::set_union(bv.<a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>(), bv.<a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>(),
141
<a name="l00114"></a>00114 vect.begin(), vect.end(),
142
<a name="l00115"></a>00115 std::back_inserter(res1)); <span class="comment">//10;15;100;150;10000</span>
143
<a name="l00116"></a>00116 cout << <span class="stringliteral">"Set union:"</span> << endl;
144
<a name="l00117"></a>00117 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(res1.begin(), res1.end());
145
<a name="l00118"></a>00118
146
<a name="l00119"></a>00119 std::set_intersection(bv.<a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>(), bv.<a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>(),
147
<a name="l00120"></a>00120 vect.begin(), vect.end(),
148
<a name="l00121"></a>00121 std::back_inserter(res2)); <span class="comment">// 100</span>
149
<a name="l00122"></a>00122 cout << <span class="stringliteral">"Set intersection:"</span> << endl;
150
<a name="l00123"></a>00123 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(res2.begin(), res2.end());
151
<a name="l00124"></a>00124
152
<a name="l00125"></a>00125 vector<unsigned>::const_iterator it1 = vect.begin();
153
<a name="l00126"></a>00126 vector<unsigned>::const_iterator it2 = vect.end();
154
<a name="l00127"></a>00127 <a class="code" href="a00063.html" title="Constant input iterator designed to enumerate &quot;ON&quot; bits.">bm::bvector<>::enumerator</a> en = bv.<a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>();
155
<a name="l00128"></a>00128 <a class="code" href="a00063.html" title="Constant input iterator designed to enumerate &quot;ON&quot; bits.">bm::bvector<>::enumerator</a> en2= bv.<a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>();
156
<a name="l00129"></a>00129
157
<a name="l00130"></a>00130 std::set_difference(en, en2,
158
<a name="l00131"></a>00131 it1, it2,
159
<a name="l00132"></a>00132 std::back_inserter(res3)); <span class="comment">// 10;10000</span>
160
<a name="l00133"></a>00133
161
<a name="l00134"></a>00134 cout << <span class="stringliteral">"Set diff:"</span> << endl;
162
<a name="l00135"></a>00135 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(res3.begin(), res3.end());
163
<a name="l00136"></a>00136
164
<a name="l00137"></a>00137 }
165
<a name="l00138"></a>00138
166
<a name="l00139"></a>00139 <span class="comment">// Using bvector<>::insert_iterator to set bits</span>
167
<a name="l00140"></a>00140 {
168
<a name="l00141"></a>00141 <a class="code" href="a00044.html" title="bitvector with runtime compression of bits.">bm::bvector<></a> bv1;
169
<a name="l00142"></a>00142 std::vector<unsigned> vect;
170
<a name="l00143"></a>00143
171
<a name="l00144"></a>00144 vect.push_back(300);
172
<a name="l00145"></a>00145 vect.push_back(200);
173
<a name="l00146"></a>00146 vect.push_back(275);
174
<a name="l00147"></a>00147 vect.push_back(200);
175
<a name="l00148"></a>00148
176
<a name="l00149"></a>00149 cout << endl << <span class="stringliteral">"Source vector:"</span>;
177
<a name="l00150"></a>00150 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(vect.begin(), vect.end()); <span class="comment">// 300;200;275;200;</span>
178
<a name="l00151"></a>00151
179
<a name="l00152"></a>00152 <span class="comment">// The "side effect" of this operation is that we sorted</span>
180
<a name="l00153"></a>00153 <span class="comment">// the input sequence and eliminated duplicates</span>
181
<a name="l00154"></a>00154
182
<a name="l00155"></a>00155 std::copy(vect.begin(), vect.end(), bv1.<a class="code" href="a00044.html#ada0d32bbd1a135722a7790b13d866451">inserter</a>());
183
<a name="l00156"></a>00156 cout << <span class="stringliteral">"Bitset:"</span>;
184
<a name="l00157"></a>00157
185
<a name="l00158"></a>00158 <a class="code" href="a00113.html#ab7f435443f8dbf9f50e4ad0efea8b1f8">PrintContainer</a>(bv1.<a class="code" href="a00044.html#aad48c661f843e32c9893438a6f27a8f0" title="Returns enumerator pointing on the first non-zero bit.">first</a>(), bv1.<a class="code" href="a00044.html#a2c6e522f858fdc466765d2c0f269f590" title="Returns enumerator pointing on the next bit after the last.">end</a>()); <span class="comment">// 200;275;300</span>
186
<a name="l00159"></a>00159 }
187
<a name="l00160"></a>00160
188
<a name="l00161"></a>00161
189
<a name="l00162"></a>00162 <span class="keywordflow">return</span> 0;
190
<a name="l00163"></a>00163 }
191
<a name="l00164"></a>00164
193
<hr size="1"/><address style="text-align: right;"><small>Generated on Fri Jan 8 20:09:19 2010 for BitMagic by
194
<a href="http://www.doxygen.org/index.html">
195
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.1 </small></address>