BitMagic-C++
sample7.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 sample7.cpp
20
21 Example how to use logical operations between arrays and bit-vectors
22
23 \sa bm::combine_and
24 \sa bm::combine_and_sorted
25 \sa bm::combine_sub
26 \sa bm::combine_or
27 \sa bm::combine_xor
28
29 \sa bvsetalgebra.cpp
30*/
31
32/*! \file sample7.cpp
33 \brief Example: set operations between bvector<> and arrays of integers.
34*/
35
36
37#include <iostream>
38#include <algorithm>
39#include <vector>
40#include <list>
41
42using std::vector;
43using std::list;
44
45// This example requires STL compatibility
46#ifdef BM_NO_STL
47# undef BM_NO_STL
48#endif
49#include "bm.h"
50#include "bmalgo.h"
51#include "bmundef.h" /* clear the pre-proc defines from BM */
52
53using namespace std;
54
55inline
56void Print(unsigned n)
57{
58 cout << n << endl;;
59}
60
61// Utility template function used to print container
62template<class T> void PrintContainer(T first, T last)
63{
64 if (first == last)
65 cout << "<EMPTY SET>";
66 else
67 for(;first != last; ++first)
68 cout << *first << ";";
69 cout << endl;
70}
71
72int main(void)
73{
74 try
75 {
77 bv[10] = true;
78 bv[100] = true;
79 bv[10000] = true;
80
81 // initialize unsorted, fairly random array for an experiment
82 // it even allowes duplicates (see 12)
83 //
84 bm::bvector<>::size_type arr[] = {2, 10000, 5, 12, 255, 12, 300};
85
86
87 cout << "Source set 1:";
88 PrintContainer(bv.first(), bv.end());
89
90 cout << "Source set 2:";
91 PrintContainer(&arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
92
93 // AND operation between bit-vector and a plain array
94 // expect one result: 10000
95 // please note, that array in this case comes unsorted
96 //
97 bm::combine_and(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
98 cout << "Result 1(AND): ";
99 PrintContainer(bv.first(), bv.end());
100
101
102 // re-initalize the bit-vector
103 bv.clear();
104 bv[10] = true;
105 bv[100] = true;
106 bv[10000] = true;
107
108 // OR operation to merge bit-vector and array
109 // please note that it naturally works as sort-unique for the array
110 //
111 bm::combine_or(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
112 cout << "Result 2(OR): ";
113 PrintContainer(bv.first(), bv.end());
114
115 // sort the array, using STL sort method
116 // combine operation on sorted arrays tend to be faster
117 //
118 std::sort(&arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
119
120 // AND on sorted array is faster
121 //
122 bm::combine_and_sorted(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
123 cout << "Result 3(AND): ";
124 PrintContainer(bv.first(), bv.end());
125
126 // SUB (AND NOT or MINUS) also works faster on sorted input
127 // the result should be an EMPTY set
128 bm::combine_sub(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
129 cout << "Result 4(MINUS): ";
130 PrintContainer(bv.first(), bv.end());
131
132 }
133 catch(std::exception& ex)
134 {
135 std::cerr << ex.what() << std::endl;
136 return 1;
137 }
138
139
140 return 0;
141}
142
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Algorithms for bvector<> (main include)
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
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
void clear(const size_type *ids, size_type ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition: bm.h:4114
void combine_and_sorted(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1333
void combine_and(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1365
void combine_sub(BV &bv, It first, It last)
SUB Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1248
void combine_or(BV &bv, It first, It last)
OR Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1080
void PrintContainer(T first, T last)
Definition: sample7.cpp:62
void Print(unsigned n)
Definition: sample7.cpp:56
int main(void)
Definition: sample7.cpp:72