BitMagic-C++
sample7.cpp
Go to the documentation of this file.
1 /*
2 Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16 For 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 
42 using std::vector;
43 using std::list;
44 
45 // This example requires STL compatibility
46 #ifdef BM_NO_STL
47 # undef BM_NO_STL
48 #endif
49 
50 #include "bm.h"
51 #include "bmalgo.h"
52 
53 using namespace std;
54 
55 inline
56 void Print(unsigned n)
57 {
58  cout << n << endl;;
59 }
60 
61 // Utility template function used to print container
62 template<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 
72 int main(void)
73 {
74  try
75  {
76  bm::bvector<> bv;
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  unsigned int 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.
void clear(const bm::id_t *ids, unsigned ids_size, bm::sort_order so=bm::BM_UNKNOWN)
clear list of bits in this bitset
Definition: bm.h:3332
void combine_or(BV &bv, It first, It last)
OR Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1134
void combine_sub(BV &bv, It first, It last)
SUB Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1303
Algorithms for bvector<> (main include)
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:2098
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition: bm.h:2104
void Print(unsigned n)
Definition: sample7.cpp:56
void combine_and_sorted(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1388
void combine_and(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1420
void PrintContainer(T first, T last)
Definition: sample7.cpp:62
int main(void)
Definition: sample7.cpp:72