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
52using namespace std;
53
54inline
55void Print(unsigned n)
56{
57 cout << n << endl;;
58}
59
60// Utility template function used to print container
61template<class T> void PrintContainer(T first, T last)
62{
63 if (first == last)
64 cout << "<EMPTY SET>";
65 else
66 for(;first != last; ++first)
67 cout << *first << ";";
68 cout << endl;
69}
70
71int main(void)
72{
73 try
74 {
76 bv[10] = true;
77 bv[100] = true;
78 bv[10000] = true;
79
80 // initialize unsorted, fairly random array for an experiment
81 // it even allowes duplicates (see 12)
82 //
83 bm::bvector<>::size_type arr[] = {2, 10000, 5, 12, 255, 12, 300};
84
85
86 cout << "Source set 1:";
87 PrintContainer(bv.first(), bv.end());
88
89 cout << "Source set 2:";
90 PrintContainer(&arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
91
92 // AND operation between bit-vector and a plain array
93 // expect one result: 10000
94 // please note, that array in this case comes unsorted
95 //
96 bm::combine_and(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
97 cout << "Result 1(AND): ";
98 PrintContainer(bv.first(), bv.end());
99
100
101 // re-initalize the bit-vector
102 bv.clear();
103 bv[10] = true;
104 bv[100] = true;
105 bv[10000] = true;
106
107 // OR operation to merge bit-vector and array
108 // please note that it naturally works as sort-unique for the array
109 //
110 bm::combine_or(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
111 cout << "Result 2(OR): ";
112 PrintContainer(bv.first(), bv.end());
113
114 // sort the array, using STL sort method
115 // combine operation on sorted arrays tend to be faster
116 //
117 std::sort(&arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
118
119 // AND on sorted array is faster
120 //
121 bm::combine_and_sorted(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
122 cout << "Result 3(AND): ";
123 PrintContainer(bv.first(), bv.end());
124
125 // SUB (AND NOT or MINUS) also works faster on sorted input
126 // the result should be an EMPTY set
127 bm::combine_sub(bv, &arr[0], &arr[0] + (sizeof(arr)/sizeof(arr[0])));
128 cout << "Result 4(MINUS): ";
129 PrintContainer(bv.first(), bv.end());
130
131 }
132 catch(std::exception& ex)
133 {
134 std::cerr << ex.what() << std::endl;
135 return 1;
136 }
137
138
139 return 0;
140}
141
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Algorithms for bvector<> (main include)
enumerator first() const
Returns enumerator pointing on the first non-zero bit.
Definition bm.h:1770
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:3546
enumerator end() const
Returns enumerator pointing on the next bit after the last.
Definition bm.h:1776
void combine_and_sorted(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
void combine_and(BV &bv, It first, It last)
AND Combine bitvector and the iterable sequence.
void combine_sub(BV &bv, It first, It last)
SUB Combine bitvector and the iterable sequence.
void combine_or(BV &bv, It first, It last)
OR Combine bitvector and the iterable sequence.
void PrintContainer(T first, T last)
Definition sample7.cpp:61
void Print(unsigned n)
Definition sample7.cpp:55
int main(void)
Definition sample7.cpp:71