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
46using namespace std;
47
48inline
50{
51 cout << n << endl;;
52}
53
54// Utility template function used to print container
55template<class T> void PrintContainer(T first, T last)
56{
57 if (first == last)
58 cout << "<EMPTY SET>";
59 else
60 for(;first != last; ++first)
61 cout << *first << ";";
62 cout << endl;
63}
64
65int main(void)
66{
68 try
69 {
71
72 bv[10] = true;
73 bv[100] = true;
74 bv[10000] = true;
75
76 cout << "Source set:";
77 PrintContainer(bv.first(), bv.end());
78
79 // copy all bitset information into STL vector using copy algorithm
80 {
81 vector<bm_size_type> vect;
82 vect.resize(bv.count());
83 std::copy(bv.first(), bv.end(), vect.begin());
84 cout << "Vector:";
85 PrintContainer(vect.begin(), vect.end());
86 }
87
88 // doing the same with the help of back_inserter
89
90 {
91 list<bm_size_type> lst;
92 std::copy(bv.first(), bv.end(), std::back_inserter(lst));
93 cout << "List:";
94 PrintContainer(lst.begin(), lst.end());
95 }
96
97 {
98 vector<bm_size_type> vect;
99 vector<bm_size_type> res1, res2, res3;
100
101 vect.push_back(100);
102 vect.push_back(15);
103 vect.push_back(150);
104
105 cout << "Argument vector for set operations:";
106 PrintContainer(vect.begin(), vect.end());
107
108 // set should be ordered by < to make set algorithms possible
109 std::sort(vect.begin(), vect.end());
110 cout << endl;
111
112 std::set_union(bv.first(), bv.end(),
113 vect.begin(), vect.end(),
114 std::back_inserter(res1)); //10;15;100;150;10000
115 cout << "Set union:" << endl;
116 PrintContainer(res1.begin(), res1.end());
117
118 std::set_intersection(bv.first(), bv.end(),
119 vect.begin(), vect.end(),
120 std::back_inserter(res2)); // 100
121 cout << "Set intersection:" << endl;
122 PrintContainer(res2.begin(), res2.end());
123
124 vector<bm_size_type>::const_iterator it1 = vect.begin();
125 vector<bm_size_type>::const_iterator it2 = vect.end();
128
129 std::set_difference(en, en2,
130 it1, it2,
131 std::back_inserter(res3)); // 10;10000
132
133 cout << "Set diff:" << endl;
134 PrintContainer(res3.begin(), res3.end());
135
136 }
137
138 // Using bvector<>::insert_iterator to set bits
139 {
140 bm::bvector<> bv1;
141 std::vector<bm_size_type> vect;
142
143 vect.push_back(300);
144 vect.push_back(200);
145 vect.push_back(275);
146 vect.push_back(200);
147
148 cout << endl << "Source vector:";
149 PrintContainer(vect.begin(), vect.end()); // 300;200;275;200;
150
151 // The "side effect" of this operation is that we sorted
152 // the input sequence and eliminated duplicates
153
154 std::copy(vect.begin(), vect.end(), bv1.inserter());
155 cout << "Bitset:";
156
157 PrintContainer(bv1.first(), bv1.end()); // 200;275;300
158 }
159 }
160 catch(std::exception& ex)
161 {
162 std::cerr << ex.what() << std::endl;
163 return 1;
164 }
165
166
167 return 0;
168}
169
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Constant iterator designed to enumerate "ON" bits.
Definition bm.h:600
Bitvector Bit-vector container with runtime compression of bits.
Definition bm.h:108
size_type count() const BMNOEXCEPT
population cout (count of ON bits)
Definition bm.h:2194
bm::id_t size_type
Definition bm.h:117
insert_iterator inserter()
Definition bm.h:1245
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition bm.h:1770
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition bm.h:1776
bm::bvector ::size_type bm_size_type
Definition sample12.cpp:53
void PrintContainer(T first, T last)
Definition sample8.cpp:55
void Print(bm::bvector<>::size_type n)
Definition sample8.cpp:49
int main(void)
Definition sample8.cpp:65