BitMagic-C++
sample24.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 sample24.cpp
20@brief Example for finding bit-vector ranges with the specifed number of ON bits
21
22The use case here:
23 - search algorithm runs a query, the result set represented as a bit-vector
24
25 - second stage needs to calculate statistics (or run a sub-query) on each
26 element of a result set and we want to do it in-parallel so we need to
27 create batches (or jobs) with approximately equal complexity
28
29 - we assume that each found element in the bitset has approximately same
30 complexity, so to model the situation we need to find range boundaries with
31 the specifed population count
32
33 @sa bm::rank_range_split
34 @sa bm::bvector::get_enumerator
35 @sa bm::bvector::enumerator
36
37*/
38
39/*! \file sample24.cpp
40 \brief Example: demo for bm::rank_range_split
41*/
42
43#include <stdlib.h>
44#include <iostream>
45#include <vector>
46#include <utility>
47
48#include "bm.h"
49#include "bmalgo.h"
50#include "bmundef.h" /* clear the pre-proc defines from BM */
51
52using namespace std;
53
55typedef std::vector<std::pair<bv_size_type, bv_size_type> > bv_ranges_vector;
56
57int main(void)
58{
59 try
60 {
61 const unsigned ranges = 3; // split into 3 ranges
62 bm::bvector<> bv { 10, 20, 100, 200, 300, 655000, bm::id_max-1 };
63
64
65 bv_size_type cnt = bv.count();
66 bv_size_type split_rank = cnt / ranges; // target population count
67
68 bv_ranges_vector pair_vect;
69
70 // run the search for splits, traget bin size is split_rank
71
72 cout << "Target split rank:" << split_rank << endl;
73
74 bm::rank_range_split(bv, split_rank, pair_vect);
75
76 // go through each range and print all bit values
77 for (size_t k = 0; k < pair_vect.size(); ++k)
78 {
79 const auto& p = pair_vect[k];
80 cout << k << ": [" << p.first << ".." << p.second << "] ";
81
82 // find and print bits in the target range with bvector<>::enumerator
83 bm::bvector<>::enumerator en = bv.get_enumerator(p.first);
84 for (; en.valid(); ++en)
85 {
86 auto v = *en;
87 if (v > p.second)
88 break;
89 cout << v << ", ";
90 } // for en
91 cout << endl;
92 } // for k
93
94 }
95 catch(std::exception& ex)
96 {
97 std::cerr << ex.what() << std::endl;
98 return 1;
99 }
100
101
102 return 0;
103}
104
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)
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:603
bool valid() const BMNOEXCEPT
Checks if iterator is still valid.
Definition: bm.h:283
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bvector_size_type size_type
Definition: bm.h:121
void rank_range_split(const BV &bv, typename BV::size_type rank, PairVect &target_v)
Algorithm to identify bit-vector ranges (splits) for the rank.
Definition: bmalgo.h:394
const unsigned id_max
Definition: bmconst.h:109
bm::bvector ::size_type bv_size_type
Definition: sample24.cpp:54
std::vector< std::pair< bv_size_type, bv_size_type > > bv_ranges_vector
Definition: sample24.cpp:55
int main(void)
Definition: sample24.cpp:57