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
178
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>BitMagic: sample8.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<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>
<div class="nav">
<a class="el" href="dir_000001.html">samples</a> / <a class="el" href="dir_000009.html">sample8</a></div>
<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>
00002 <span class="comment">Copyright(c) 2002-2005 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)</span>
00003 <span class="comment"></span>
00004 <span class="comment">Permission is hereby granted, free of charge, to any person </span>
00005 <span class="comment">obtaining a copy of this software and associated documentation </span>
00006 <span class="comment">files (the "Software"), to deal in the Software without restriction, </span>
00007 <span class="comment">including without limitation the rights to use, copy, modify, merge, </span>
00008 <span class="comment">publish, distribute, sublicense, and/or sell copies of the Software, </span>
00009 <span class="comment">and to permit persons to whom the Software is furnished to do so, </span>
00010 <span class="comment">subject to the following conditions:</span>
00011 <span class="comment"></span>
00012 <span class="comment">The above copyright notice and this permission notice shall be included </span>
00013 <span class="comment">in all copies or substantial portions of the Software.</span>
00014 <span class="comment"></span>
00015 <span class="comment">THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, </span>
00016 <span class="comment">EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES </span>
00017 <span class="comment">OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. </span>
00018 <span class="comment">IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, </span>
00019 <span class="comment">DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, </span>
00020 <span class="comment">ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR </span>
00021 <span class="comment">OTHER DEALINGS IN THE SOFTWARE.</span>
00022 <span class="comment">*/</span>
00023 <span class="comment"></span>
00024 <span class="comment">/** \example sample8.cpp</span>
00025 <span class="comment"></span>
00026 <span class="comment"> Example demonstrates some STL compatability set operations using</span>
00027 <span class="comment"> set iterators.</span>
00028 <span class="comment"> </span>
00029 <span class="comment"> \sa bm::bvector<>::enumerator </span>
00030 <span class="comment"> \sa bm::bvector<>::insert_iterator</span>
00031 <span class="comment"></span>
00032 <span class="comment"> For more information please visit: http://bmagic.sourceforge.net</span>
00033 <span class="comment"></span>
00034 <span class="comment">*/</span>
00035
00036 <span class="preprocessor">#include <iostream></span>
00037 <span class="preprocessor">#include <algorithm></span>
00038 <span class="preprocessor">#include <vector></span>
00039 <span class="preprocessor">#include <list></span>
00040
00041 <span class="keyword">using</span> std::vector;
00042 <span class="keyword">using</span> std::list;
00043
00044 <span class="comment">// This example requires STL compatibility</span>
00045 <span class="preprocessor">#ifdef BM_NO_STL</span>
00046 <span class="preprocessor"></span><span class="preprocessor"># undef BM_NO_STL</span>
00047 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
00048 <span class="preprocessor"></span>
00049 <span class="preprocessor">#include "<a class="code" href="a00111.html">bm.h</a>"</span>
00050
00051 <span class="keyword">using</span> <span class="keyword">namespace </span>std;
00052
<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)
00054 {
00055 cout << n << endl;;
00056 }
00057
00058 <span class="comment">// Utility template function used to print container</span>
<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)
00060 {
00061 <span class="keywordflow">if</span> (first == last)
00062 cout << <span class="stringliteral">"<EMPTY SET>"</span>;
00063 <span class="keywordflow">else</span>
00064 <span class="keywordflow">for</span>(;first != last; ++first)
00065 cout << *first << <span class="stringliteral">";"</span>;
00066 cout << endl;
00067 }
00068
<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>)
00070 {
00071 <a class="code" href="a00084.html">bm::bvector<></a> bv;
00072
00073 bv[10] = <span class="keyword">true</span>;
00074 bv[100] = <span class="keyword">true</span>;
00075 bv[10000] = <span class="keyword">true</span>;
00076
00077 cout << <span class="stringliteral">"Source set:"</span>;
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>());
00079
00080 <span class="comment">// copy all bitset information into STL vector using copy algorithm</span>
00081 {
00082 vector<unsigned> vect;
00083 vect.<a class="code" href="a00084.html#a31">resize</a>(bv.<a class="code" href="a00084.html#a28">count</a>());
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());
00085 cout << <span class="stringliteral">"Vector:"</span>;
00086 <a class="code" href="a00127.html#a1">PrintContainer</a>(vect.begin(), vect.end());
00087 }
00088
00089 <span class="comment">// doing the same with the help of back_inserter</span>
00090
00091 {
00092 list<unsigned> lst;
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));
00094 cout << <span class="stringliteral">"List:"</span>;
00095 <a class="code" href="a00127.html#a1">PrintContainer</a>(lst.begin(), lst.end());
00096 }
00097
00098 {
00099 vector<unsigned> vect;
00100 vector<unsigned> res1, res2, res3;
00101
00102 vect.push_back(100);
00103 vect.push_back(15);
00104 vect.push_back(150);
00105
00106 cout << <span class="stringliteral">"Argument vector for set operations:"</span>;
00107 <a class="code" href="a00127.html#a1">PrintContainer</a>(vect.begin(), vect.end());
00108
00109 <span class="comment">// set should be ordered by < to make set algorithms possible</span>
00110 std::sort(vect.begin(), vect.end());
00111 cout << endl;
00112
00113 std::set_union(bv.<a class="code" href="a00084.html#a61">first</a>(), bv.<a class="code" href="a00084.html#a62">end</a>(),
00114 vect.begin(), vect.<a class="code" href="a00084.html#a62">end</a>(),
00115 std::back_inserter(res1)); <span class="comment">//10;15;100;150;10000</span>
00116 cout << <span class="stringliteral">"Set union:"</span> << endl;
00117 <a class="code" href="a00127.html#a1">PrintContainer</a>(res1.begin(), res1.end());
00118
00119 std::set_intersection(bv.<a class="code" href="a00084.html#a61">first</a>(), bv.<a class="code" href="a00084.html#a62">end</a>(),
00120 vect.begin(), vect.<a class="code" href="a00084.html#a62">end</a>(),
00121 std::back_inserter(res2)); <span class="comment">// 100</span>
00122 cout << <span class="stringliteral">"Set intersection:"</span> << endl;
00123 <a class="code" href="a00127.html#a1">PrintContainer</a>(res2.begin(), res2.end());
00124
00125 vector<unsigned>::const_iterator it1 = vect.begin();
00126 vector<unsigned>::const_iterator it2 = vect.<a class="code" href="a00084.html#a62">end</a>();
00127 <a class="code" href="a00084.html">bm::bvector<></a>::enumerator en = bv.<a class="code" href="a00084.html#a61">first</a>();
00128 <a class="code" href="a00084.html">bm::bvector<></a>::enumerator en2= bv.<a class="code" href="a00084.html#a62">end</a>();
00129
00130 std::set_difference(en, en2,
00131 it1, it2,
00132 std::back_inserter(res3)); <span class="comment">// 10;10000</span>
00133
00134 cout << <span class="stringliteral">"Set diff:"</span> << endl;
00135 <a class="code" href="a00127.html#a1">PrintContainer</a>(res3.begin(), res3.end());
00136
00137 }
00138
00139 <span class="comment">// Using bvector<>::insert_iterator to set bits</span>
00140 {
00141 <a class="code" href="a00084.html">bm::bvector<></a> bv1;
00142 std::vector<unsigned> vect;
00143
00144 vect.push_back(300);
00145 vect.push_back(200);
00146 vect.push_back(275);
00147 vect.push_back(200);
00148
00149 cout << endl << <span class="stringliteral">"Source vector:"</span>;
00150 <a class="code" href="a00127.html#a1">PrintContainer</a>(vect.begin(), vect.end()); <span class="comment">// 300;200;275;200;</span>
00151
00152 <span class="comment">// The "side effect" of this operation is that we sorted</span>
00153 <span class="comment">// the input sequence and eliminated duplicates</span>
00154
00155 std::copy(vect.begin(), vect.end(), bv1.<a class="code" href="a00084.html#a24">inserter</a>());
00156 cout << <span class="stringliteral">"Bitset:"</span>;
00157
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>
00159 }
00160
00161
00162 <span class="keywordflow">return</span> 0;
00163 }
00164
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Sun Aug 5 14:12:26 2007 for BitMagic by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>
</body>
</html>
|