BitMagicC++Library
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 */
30 
31 #include <iostream>
32 #include <algorithm>
33 #include <vector>
34 #include <list>
35 
36 using std::vector;
37 using 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 "bmalgo.h"
46 
47 using namespace std;
48 
49 void Print(unsigned n)
50 {
51  cout << n << endl;;
52 }
53 
54 // Utility template function used to print container
55 template<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 
65 int main(void)
66 {
67  try
68  {
69  bm::bvector<> bv;
70  bv[10] = true;
71  bv[100] = true;
72  bv[10000] = true;
73 
74  // initialize unsorted, fairly random array for an experiment
75  // it even allowes duplicates (see 12)
76  //
77  unsigned int arr[] = {2, 10000, 5, 12, 255, 12, 300};
78 
79 
80  cout << "Source set 1:";
81  PrintContainer(bv.first(), bv.end());
82 
83  cout << "Source set 2:";
84  PrintContainer(&arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
85 
86  // AND operation between bit-vector and a plain array
87  // expect one result: 10000
88  // please note, that array in this case comes unsorted
89  //
90  bm::combine_and(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
91  cout << "Result 1(AND): ";
92  PrintContainer(bv.first(), bv.end());
93 
94 
95  // re-initalize the bit-vector
96  bv.clear();
97  bv[10] = true;
98  bv[100] = true;
99  bv[10000] = true;
100 
101  // OR operation to merge bit-vector and array
102  // please note that it naturally works as sort-unique for the array
103  //
104  bm::combine_or(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
105  cout << "Result 2(OR): ";
106  PrintContainer(bv.first(), bv.end());
107 
108  // sort the array, using STL sort method
109  // combine operation on sorted arrays tend to be faster
110  //
111  std::sort(&arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
112 
113  // AND on sorted array is faster
114  //
115  bm::combine_and_sorted(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
116  cout << "Result 3(AND): ";
117  PrintContainer(bv.first(), bv.end());
118 
119  // SUB (AND NOT or MINUS) also works faster on sorted input
120  // the result should be an EMPTY set
121  bm::combine_sub(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
122  cout << "Result 4(MINUS): ";
123  PrintContainer(bv.first(), bv.end());
124 
125  }
126  catch(std::exception& ex)
127  {
128  std::cerr << ex.what() << std::endl;
129  return 1;
130  }
131 
132 
133  return 0;
134 }
135 
void combine_or(BV &bv, It first, It last)
OR Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1148
void combine_sub(BV &bv, It first, It last)
SUB Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1317
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition: bm.h:1749
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition: bm.h:1758
void Print(unsigned n)
Definition: sample7.cpp:49
void combine_and_sorted(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1402
void combine_and(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
Definition: bmalgo_impl.h:1434
void PrintContainer(T first, T last)
Definition: sample7.cpp:55
void clear(bool free_mem=false)
Clears every bit in the bitvector.
Definition: bm.h:1317
int main(void)
Definition: sample7.cpp:65