BitMagic-C++
sample25.cpp
Go to the documentation of this file.
1/*
2Copyright(c) 2002-2021 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 sample25.cpp
20
21This example illustrates verious traversal methods based on iterator/enumerator or algorithms.
22
23 \sa bm::bvector
24 \sa bm::bvector::get_enumerator
25 \sa bm::bvector::enumerator
26 \sa bm::visit_each_bit
27 \sa bm::visit_each_bit_range
28 \sa bm::for_each_bit
29 \sa bm::for_each_bit_range
30*/
31
32/*! \file sample25.cpp
33 \brief Example: demo for bit-vector traversal techniques
34*/
35
36#include <stdlib.h>
37#include <iostream>
38#include <vector>
39#include <utility>
40#include <cassert>
41
42#include "bm.h"
43#include "bmalgo.h"
44#include "bmundef.h" /* clear the pre-proc defines from BM */
45
46using namespace std;
47
48extern "C" {
49
50static
51int bit_visitor_callback(void* handle_ptr, bm::id_t bit_idx) noexcept
52{
53 assert(handle_ptr);
54 unsigned* cnt = (unsigned*)handle_ptr;
55 if (*cnt >= 5)
56 return -1; // negative code indicates we requested to stop iteration
57 *cnt += 1;
58 cout << bit_idx << ", ";
59 return 0;
60}
61
62} // extern C
63
64/**
65* Visitor calss for bm::for_each_bit() algorithm
66* It is NOT a classic functor (with function call operator() overload)
67* For efficiency it needs to support two methods: add_bits and add_range
68* which corresponds to different internal methods of representation of
69* sets in the bm::bvector<> (bit-stream and D-GAP/RLE representation)
70*
71* This method somewhat exposes internals and requires writing a custom
72* object, but it is also the fastest method, used inside BitMagic
73* library a lot.
74*/
76{
78
79 /**
80 * Decoded bits callback
81 * @param offset - index offset in the bit-vector (decoding address context)
82 * @param bits - array of set bit indexes within the address context
83 offset + bits[i] gives us global index in the bit_vector
84 @param size - number of decoded bits (size of bits array)
85 */
86 int add_bits(size_type offset,
87 const unsigned char* bits,
88 unsigned size)
89 {
90 for (unsigned i = 0; i < size; ++i)
91 {
92 if (cnt >= 5)
93 return -1; // negative code indicates we requested to stop iteration
94 cout << offset + bits[i] << ", ";
95 ++cnt;
96 }
97 return 0;
98 }
99
100
101 /**
102 * Decoded range callback
103 * @param offset - index offset in the bit-vector (decoding address context)
104 @param size - number of consequitive ON bits (size of bits array)
105 */
106 int add_range(size_type offset, size_type size)
107 {
108 for (size_type i = 0; i < size; ++i)
109 {
110 if (cnt >= 5)
111 return -1; // negative code indicates we requested to stop iteration
112 cout << offset + i << ", ";
113 ++cnt;
114 }
115 return 0;
116 }
117 unsigned cnt = 0; // counter variable to limit the traversal
118};
119
120
121
122int main(void)
123{
124 try
125 {
126 bm::bvector<> bv { 0, 1, 10, 20, 100, 200, 300, 655000, bm::id_max-1 };
127 bv.optimize(); // compress the bit-vector
128
129 // CASE 1:
130 // print first 5 set elements using bm::bvector::enumerator
131 {
132 auto en = bv.get_enumerator(0); // 0 is a starting position in bit-vector
133 for (unsigned cnt = 0; en.valid() && cnt < 5; ++en, ++cnt)
134 cout << *en << ", ";
135 }
136 cout << endl;
137
138 // CASE 2:
139 // Use C-style callback with bm::visit_each_bit() algorithm
140 // callback function receives the execution context as a void ptr, free for
141 // reinterpretation, and can signal to interrupt the cycle by negative return code
142 // return code is passed back from the visit algorithm
143 {
144 unsigned cnt = 0;
145 void* ctx_ptr = &cnt;
146 int res = bm::visit_each_bit(bv, ctx_ptr, bit_visitor_callback);
147 cout << "\nvisitor return code = " << res << endl; // -1
148 }
149
150 // CASE 3:
151 // use bm::for_each_bit() and a visitor class to traverse the bit-vector
152 // This method is the fastest, but it exposes some details on internals
153 // of the bit-vector
154 {
155 bit_visitor_functor func; // declare the visitor object
156 int res = bm::for_each_bit(bv, func);
157 cout << "\nvisitor return code = " << res << endl; // -1
158 }
159
160 // If we want to traverse a range in bit-vector
161 // BitMagic offers range traversal algorithms with [from..to]
162 // closed range semantics
163 //
164
165 bm::bvector<>::size_type from(15), to(550);
166 cout << "\nRange traversal: [" << from << ".." << to << "]" << endl;
167
168 // CASE 2a: bm::visit_each_bit_range
169 {
170 unsigned cnt = 0;
171 int res = bm::visit_each_bit_range(bv, from, to, (void*)&cnt, bit_visitor_callback);
172 // note that return code would be 0 in this case,
173 // because range [15,500] has less than 5 set elements
174 cout << "\nvisitor return code = " << res << endl; // -1
175 }
176
177
178 // CASE 3a: bm::for_each_bit_range
179 {
180 bit_visitor_functor func; // declare the visitor object
181 int res = bm::for_each_bit_range(bv, from, to, func);
182 cout << "\nvisitor return code = " << res << endl; // 0
183 }
184
185
186 }
187 catch(std::exception& ex)
188 {
189 std::cerr << ex.what() << std::endl;
190 return 1;
191 }
192
193
194 return 0;
195}
196
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
int visit_each_bit(const BV &bv, void *handle_ptr, bit_visitor_callback_type callback_ptr)
bvector visitor scanner to traverse each 1 bit using C callback
Definition: bmalgo.h:336
int visit_each_bit_range(const BV &bv, typename BV::size_type left, typename BV::size_type right, void *handle_ptr, bit_visitor_callback_type callback_ptr)
bvector visitor scanner to traverse each bits in range (C callback)
Definition: bmalgo.h:362
int for_each_bit_range(const BV &bv, typename BV::size_type left, typename BV::size_type right, Func &bit_functor)
bit-vector range visitor to traverse each 1 bit
Definition: bmalgo.h:266
int for_each_bit(const BV &bv, Func &bit_functor)
bit-vector visitor scanner to traverse each 1 bit using C++ visitor
Definition: bmalgo.h:202
const unsigned id_max
Definition: bmconst.h:109
unsigned int id_t
Definition: bmconst.h:38
int main(void)
Definition: sample25.cpp:122
static int bit_visitor_callback(void *handle_ptr, bm::id_t bit_idx) noexcept
Definition: sample25.cpp:51
Visitor calss for bm::for_each_bit() algorithm It is NOT a classic functor (with function call operat...
Definition: sample25.cpp:76
int add_bits(size_type offset, const unsigned char *bits, unsigned size)
Decoded bits callback.
Definition: sample25.cpp:86
int add_range(size_type offset, size_type size)
Decoded range callback.
Definition: sample25.cpp:106
bm::bvector<>::size_type size_type
Definition: sample25.cpp:77