1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
3
<title>BitMagic: sample8.cpp Source File</title>
4
<link href="doxygen.css" rel="stylesheet" type="text/css">
6
<!-- Generated by Doxygen 1.4.1 -->
7
<div class="qindex"><a class="qindex" href="index.html">Main 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 Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Data Structures</a> | <a class="qindex" href="dirs.html">Directories</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Data Fields</a> | <a class="qindex" href="globals.html">Globals</a> | <a class="qindex" href="examples.html">Examples</a></div>
9
<a class="el" href="dir_000001.html">samples</a> / <a class="el" href="dir_000009.html">sample8</a></div>
10
<h1>sample8.cpp</h1><a href="a00127.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
11
00002 <span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
12
00003 <span class="comment"></span>
13
00004 <span class="comment">Permission is hereby granted, free of charge, to any person </span>
14
00005 <span class="comment">obtaining a copy of this software and associated documentation </span>
15
00006 <span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
16
00007 <span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
17
00008 <span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
18
00009 <span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
19
00010 <span class="comment">subject to the following conditions:</span>
20
00011 <span class="comment"></span>
21
00012 <span class="comment">The above copyright notice and this permission notice shall be included </span>
22
00013 <span class="comment">in all copies or substantial portions of the Software.</span>
23
00014 <span class="comment"></span>
24
00015 <span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
25
00016 <span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
26
00017 <span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
27
00018 <span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
28
00019 <span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
29
00020 <span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
30
00021 <span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
31
00022 <span class="comment">*/</span>
32
00023 <span class="comment"></span>
33
00024 <span class="comment">/** \example sample8.cpp</span>
34
00025 <span class="comment"></span>
35
00026 <span class="comment"> Example demonstrates some STL compatability set operations using</span>
36
00027 <span class="comment"> set iterators.</span>
37
00028 <span class="comment"> </span>
38
00029 <span class="comment"> \sa bm::bvector<>::enumerator </span>
39
00030 <span class="comment"> \sa bm::bvector<>::insert_iterator</span>
40
00031 <span class="comment"></span>
41
00032 <span class="comment"> For more information please visit: http://bmagic.sourceforge.net</span>
42
00033 <span class="comment"></span>
43
00034 <span class="comment">*/</span>
45
00036 <span class="preprocessor">#include <iostream></span>
46
00037 <span class="preprocessor">#include <algorithm></span>
47
00038 <span class="preprocessor">#include <vector></span>
48
00039 <span class="preprocessor">#include <list></span>
50
00041 <span class="keyword">using</span> std::vector;
51
00042 <span class="keyword">using</span> std::list;
53
00044 <span class="comment">// This example requires STL compatibility</span>
54
00045 <span class="preprocessor">#ifdef BM_NO_STL</span>
55
00046 <span class="preprocessor"></span><span class="preprocessor"># undef BM_NO_STL</span>
56
00047 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
57
00048 <span class="preprocessor"></span>
58
00049 <span class="preprocessor">#include "<a class="code" href="a00111.html">bm.h</a>"</span>
60
00051 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
62
<a name="l00053"></a><a class="code" href="a00127.html#a0">00053</a> <span class="keywordtype">void</span> <a class="code" href="a00124.html#a0">Print</a>(<span class="keywordtype">unsigned</span> n)
64
00055 cout << n << endl;;
67
00058 <span class="comment">// Utility template function used to print container</span>
68
<a name="l00059"></a><a class="code" href="a00127.html#a1">00059</a> <span class="keyword">template</span><<span class="keyword">class</span> T> <span class="keywordtype">void</span> <a class="code" href="a00127.html#a1">PrintContainer</a>(T first, T last)
70
00061 <span class="keywordflow">if</span> (first == last)
71
00062 cout << <span class="stringliteral">"<EMPTY SET>"</span>;
72
00063 <span class="keywordflow">else</span>
73
00064 <span class="keywordflow">for</span>(;first != last; ++first)
74
00065 cout << *first << <span class="stringliteral">";"</span>;
75
00066 cout << endl;
78
<a name="l00069"></a><a class="code" href="a00127.html#a2">00069</a> <span class="keywordtype">int</span> <a class="code" href="a00120.html#a0">main</a>(<span class="keywordtype">void</span>)
80
00071 <a class="code" href="a00084.html">bm::bvector<></a> bv;
82
00073 bv[10] = <span class="keyword">true</span>;
83
00074 bv[100] = <span class="keyword">true</span>;
84
00075 bv[10000] = <span class="keyword">true</span>;
86
00077 cout << <span class="stringliteral">"Source set:"</span>;
87
00078 <a class="code" href="a00127.html#a1">PrintContainer</a>(bv.<a class="code" href="a00084.html#a61">first</a>(), bv.<a class="code" href="a00084.html#a62">end</a>());
89
00080 <span class="comment">// copy all bitset information into STL vector using copy algorithm</span>
91
00082 vector<unsigned> vect;
92
00083 vect.<a class="code" href="a00084.html#a31">resize</a>(bv.<a class="code" href="a00084.html#a28">count</a>());
93
00084 std::copy(bv.<a class="code" href="a00084.html#a61">first</a>(), bv.<a class="code" href="a00084.html#a62">end</a>(), vect.begin());
94
00085 cout << <span class="stringliteral">"Vector:"</span>;
95
00086 <a class="code" href="a00127.html#a1">PrintContainer</a>(vect.begin(), vect.end());
98
00089 <span class="comment">// doing the same with the help of back_inserter</span>
101
00092 list<unsigned> lst;
102
00093 std::copy(bv.<a class="code" href="a00084.html#a61">first</a>(), bv.<a class="code" href="a00084.html#a62">end</a>(), std::back_inserter(lst));
103
00094 cout << <span class="stringliteral">"List:"</span>;
104
00095 <a class="code" href="a00127.html#a1">PrintContainer</a>(lst.begin(), lst.end());
108
00099 vector<unsigned> vect;
109
00100 vector<unsigned> res1, res2, res3;
111
00102 vect.push_back(100);
112
00103 vect.push_back(15);
113
00104 vect.push_back(150);
115
00106 cout << <span class="stringliteral">"Argument vector for set operations:"</span>;
116
00107 <a class="code" href="a00127.html#a1">PrintContainer</a>(vect.begin(), vect.end());
118
00109 <span class="comment">// set should be ordered by < to make set algorithms possible</span>
119
00110 std::sort(vect.begin(), vect.end());
120
00111 cout << endl;
122
00113 std::set_union(bv.<a class="code" href="a00084.html#a61">first</a>(), bv.<a class="code" href="a00084.html#a62">end</a>(),
123
00114 vect.begin(), vect.<a class="code" href="a00084.html#a62">end</a>(),
124
00115 std::back_inserter(res1)); <span class="comment">//10;15;100;150;10000</span>
125
00116 cout << <span class="stringliteral">"Set union:"</span> << endl;
126
00117 <a class="code" href="a00127.html#a1">PrintContainer</a>(res1.begin(), res1.end());
128
00119 std::set_intersection(bv.<a class="code" href="a00084.html#a61">first</a>(), bv.<a class="code" href="a00084.html#a62">end</a>(),
129
00120 vect.begin(), vect.<a class="code" href="a00084.html#a62">end</a>(),
130
00121 std::back_inserter(res2)); <span class="comment">// 100</span>
131
00122 cout << <span class="stringliteral">"Set intersection:"</span> << endl;
132
00123 <a class="code" href="a00127.html#a1">PrintContainer</a>(res2.begin(), res2.end());
134
00125 vector<unsigned>::const_iterator it1 = vect.begin();
135
00126 vector<unsigned>::const_iterator it2 = vect.<a class="code" href="a00084.html#a62">end</a>();
136
00127 <a class="code" href="a00084.html">bm::bvector<></a>::enumerator en = bv.<a class="code" href="a00084.html#a61">first</a>();
137
00128 <a class="code" href="a00084.html">bm::bvector<></a>::enumerator en2= bv.<a class="code" href="a00084.html#a62">end</a>();
139
00130 std::set_difference(en, en2,
141
00132 std::back_inserter(res3)); <span class="comment">// 10;10000</span>
143
00134 cout << <span class="stringliteral">"Set diff:"</span> << endl;
144
00135 <a class="code" href="a00127.html#a1">PrintContainer</a>(res3.begin(), res3.end());
148
00139 <span class="comment">// Using bvector<>::insert_iterator to set bits</span>
150
00141 <a class="code" href="a00084.html">bm::bvector<></a> bv1;
151
00142 std::vector<unsigned> vect;
153
00144 vect.push_back(300);
154
00145 vect.push_back(200);
155
00146 vect.push_back(275);
156
00147 vect.push_back(200);
158
00149 cout << endl << <span class="stringliteral">"Source vector:"</span>;
159
00150 <a class="code" href="a00127.html#a1">PrintContainer</a>(vect.begin(), vect.end()); <span class="comment">// 300;200;275;200;</span>
161
00152 <span class="comment">// The "side effect" of this operation is that we sorted</span>
162
00153 <span class="comment">// the input sequence and eliminated duplicates</span>
164
00155 std::copy(vect.begin(), vect.end(), bv1.<a class="code" href="a00084.html#a24">inserter</a>());
165
00156 cout << <span class="stringliteral">"Bitset:"</span>;
167
00158 <a class="code" href="a00127.html#a1">PrintContainer</a>(bv1.<a class="code" href="a00084.html#a61">first</a>(), bv1.<a class="code" href="a00084.html#a62">end</a>()); <span class="comment">// 200;275;300</span>
171
00162 <span class="keywordflow">return</span> 0;
174
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Aug 5 14:12:26 2007 for BitMagic by
175
<a href="http://www.doxygen.org/index.html">
176
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>