BitMagic-C++
sample16.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 sample16.cpp
20Example for finding first and last bits in bit-vector (dynamic range).
21
22Ranges of bit-vectors can be used to find probability of intersection.
23For instance, in some corner cases AND product can be predicted empty if vectors
24belong to different ranges.
25
26 @sa bm::aggregator
27*/
28
29/*! \file sample16.cpp
30 \brief Example: how to use bm::aggregator<> for logical operations
31
32 bm::aggregator<> uses cache blocking techniques and bandwidth optimizations
33 to do logical operations (OR, AND, AND-SUB) faster, than if we do it by
34 combining bit-vectors one by one, sequentially.
35*/
36
37#include <stdlib.h>
38#include <iostream>
39#include <vector>
40#include <memory>
41
42#include "bm.h"
43#include "bmaggregator.h"
44#include "bmundef.h" /* clear the pre-proc defines from BM */
45
46// Utility template function used to print container
47template<class T> void PrintContainer(T first, T last)
48{
49 if (first == last)
50 std::cout << "<EMPTY SET>";
51 else
52 for (; first != last; ++first)
53 std::cout << *first << ";";
54 std::cout << std::endl;
55}
56
57
58const unsigned max_vectors = 10;
59
60int main(void)
61{
62 try
63 {
64 // declare standalone aggregator for logical operations
66
67 std::cout << "AGRUMENT (GROUP 0) SETS:" << std::endl;
68 // make vector of bit-vectors, set some bits
69 std::vector<std::unique_ptr<bm::bvector<> > > vect;
70 for (unsigned i = 0; i < max_vectors; ++i)
71 {
72 std::unique_ptr<bm::bvector<>> bv(new bm::bvector<>());
73 bv->set(i);
74 bv->set(i+1);
75 bv->set(10000);
76 bv->set(20000);
77 PrintContainer(bv->first(), bv->end());
78 bv->optimize();
79 vect.push_back(std::move(bv));
80 }
81 std::cout << std::endl;
82
83 try
84 {
85 // in a loop we add all agruments to the aggregator
86 // (aggregator does not take ownership of pointers it receives)
87 //
88 for (unsigned i = 0; i < vect.size(); ++i)
89 {
90 agg.add(vect[i].get());
91 }
92
93 bm::bvector<> bv_res; // target vector for aggregation
94
95 agg.combine_or(bv_res); // perform logical OR on a vector group
96
97 std::cout << "OR:" << std::endl;
98 PrintContainer(bv_res.first(), bv_res.end()); // 0, 1, 2, 3, 4... 10000, 20000
99
100 // since we did not call bm::aggregator::reset() the vector group
101 // still remains attached
102
103 std::cout << "AND:" << std::endl;
104 agg.combine_and(bv_res); // AND on the same vector group
105 PrintContainer(bv_res.first(), bv_res.end()); // 10000, 20000
106
107 agg.reset(); // reset the aggregator
108
109 // fused logical AND MINUS example
110 // AND(vector-group-0) SUBstract (AND NOT) vector-group-1
111 for (unsigned i = 0; i < vect.size(); ++i)
112 {
113 const unsigned group0 = 0; // vector group 0
114 agg.add(vect[i].get(), group0);
115 }
116 // Note that we do not set vector group 1 yet, so operation just
117 // runs as a regular AND MINUS an empty-set
118
119 agg.combine_and_sub(bv_res); // AND-SUB vector group1 and group2
120
121 std::cout << "AND-SUB(empty):" << std::endl;
122 PrintContainer(bv_res.first(), bv_res.end()); // 10000, 20000
123
124 bm::bvector<> bv_not { 10, 10000 };
125 agg.add(&bv_not, 1); // add to vector-group-1
126
127 agg.combine_and_sub(bv_res); // AND-SUB vector group0 minus group1
128
129 std::cout << "AND-SUB:" << std::endl;
130 PrintContainer(bv_res.first(), bv_res.end()); // 20000
131
132 agg.reset();
133
134 }
135 catch(std::exception& ex)
136 {
137 std::cerr << ex.what() << std::endl;
138 agg.reset(); // reset
139 throw;
140 }
141
142 }
143 catch(std::exception& ex)
144 {
145 std::cerr << ex.what() << std::endl;
146 return 1;
147 }
148
149
150 return 0;
151}
152
153
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
Algorithms for fast aggregation of N bvectors.
pre-processor un-defines to avoid global space pollution (internal)
Algorithms for fast aggregation of a group of bit-vectors.
Definition: bmaggregator.h:122
bool combine_and_sub(bvector_type &bv_target)
Aggregate added group of vectors using fused logical AND-SUB Operation does NOT perform an explicit r...
void reset()
Reset aggregate groups, forget all attached vectors.
Definition: bmaggregator.h:932
void combine_or(bvector_type &bv_target)
Aggregate added group of vectors using logical OR Operation does NOT perform an explicit reset of arg...
Definition: bmaggregator.h:994
void combine_and(bvector_type &bv_target)
Aggregate added group of vectors using logical AND Operation does NOT perform an explicit reset of ar...
size_t add(const bvector_type *bv, unsigned agr_group=0)
Attach source bit-vector to a argument group (0 or 1).
Definition: bmaggregator.h:986
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
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 PrintContainer(T first, T last)
Definition: sample16.cpp:47
const unsigned max_vectors
Definition: sample16.cpp:58
int main(void)
Definition: sample16.cpp:60