BitMagic-C++
sample8.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16For more information please visit: http://bitmagic.io
17*/
18
19/** \example sample8.cpp
20
21 Example for STL interoperability and set operations with iterators.
22
23 \sa bm::bvector<>::enumerator
24 \sa bm::bvector<>::insert_iterator
25*/
26
27/*! \file sample8.cpp
28 \brief Example: bvector<> - STL interoperability
29*/
30
31#include <iostream>
32#include <algorithm>
33#include <vector>
34#include <list>
35
36using std::vector;
37using std::list;
38
39// This example requires STL compatibility
40#ifdef BM_NO_STL
41# undef BM_NO_STL
42#endif
43
44#include "bm.h"
45#include "bmundef.h" /* clear the pre-proc defines from BM */
46
47using namespace std;
48
49inline
51{
52 cout << n << endl;;
53}
54
55// Utility template function used to print container
56template<class T> void PrintContainer(T first, T last)
57{
58 if (first == last)
59 cout << "<EMPTY SET>";
60 else
61 for(;first != last; ++first)
62 cout << *first << ";";
63 cout << endl;
64}
65
66int main(void)
67{
69 try
70 {
72
73 bv[10] = true;
74 bv[100] = true;
75 bv[10000] = true;
76
77 cout << "Source set:";
78 PrintContainer(bv.first(), bv.end());
79
80 // copy all bitset information into STL vector using copy algorithm
81 {
82 vector<bm_size_type> vect;
83 vect.resize(bv.count());
84 std::copy(bv.first(), bv.end(), vect.begin());
85 cout << "Vector:";
86 PrintContainer(vect.begin(), vect.end());
87 }
88
89 // doing the same with the help of back_inserter
90
91 {
92 list<bm_size_type> lst;
93 std::copy(bv.first(), bv.end(), std::back_inserter(lst));
94 cout << "List:";
95 PrintContainer(lst.begin(), lst.end());
96 }
97
98 {
99 vector<bm_size_type> vect;
100 vector<bm_size_type> res1, res2, res3;
101
102 vect.push_back(100);
103 vect.push_back(15);
104 vect.push_back(150);
105
106 cout << "Argument vector for set operations:";
107 PrintContainer(vect.begin(), vect.end());
108
109 // set should be ordered by < to make set algorithms possible
110 std::sort(vect.begin(), vect.end());
111 cout << endl;
112
113 std::set_union(bv.first(), bv.end(),
114 vect.begin(), vect.end(),
115 std::back_inserter(res1)); //10;15;100;150;10000
116 cout << "Set union:" << endl;
117 PrintContainer(res1.begin(), res1.end());
118
119 std::set_intersection(bv.first(), bv.end(),
120 vect.begin(), vect.end(),
121 std::back_inserter(res2)); // 100
122 cout << "Set intersection:" << endl;
123 PrintContainer(res2.begin(), res2.end());
124
125 vector<bm_size_type>::const_iterator it1 = vect.begin();
126 vector<bm_size_type>::const_iterator it2 = vect.end();
129
130 std::set_difference(en, en2,
131 it1, it2,
132 std::back_inserter(res3)); // 10;10000
133
134 cout << "Set diff:" << endl;
135 PrintContainer(res3.begin(), res3.end());
136
137 }
138
139 // Using bvector<>::insert_iterator to set bits
140 {
141 bm::bvector<> bv1;
142 std::vector<bm_size_type> vect;
143
144 vect.push_back(300);
145 vect.push_back(200);
146 vect.push_back(275);
147 vect.push_back(200);
148
149 cout << endl << "Source vector:";
150 PrintContainer(vect.begin(), vect.end()); // 300;200;275;200;
151
152 // The "side effect" of this operation is that we sorted
153 // the input sequence and eliminated duplicates
154
155 std::copy(vect.begin(), vect.end(), bv1.inserter());
156 cout << "Bitset:";
157
158 PrintContainer(bv1.first(), bv1.end()); // 200;275;300
159 }
160 }
161 catch(std::exception& ex)
162 {
163 std::cerr << ex.what() << std::endl;
164 return 1;
165 }
166
167
168 return 0;
169}
170
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
size_type count() const BMNOEXCEPT
population count (count of ON bits)
Definition: bm.h:2366
insert_iterator inserter()
Definition: bm.h:1265
bvector_size_type size_type
Definition: bm.h:121
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1849
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition: bm.h:1855
bm::bvector ::size_type bm_size_type
Definition: sample12.cpp:56
void PrintContainer(T first, T last)
Definition: sample8.cpp:56
void Print(bm::bvector<>::size_type n)
Definition: sample8.cpp:50
int main(void)
Definition: sample8.cpp:66