BitMagic-C++
sample24.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 sample24.cpp
20 @brief Example for finding bit-vector ranges with the specifed number of ON bits
21 
22 The 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 
51 using namespace std;
52 
54 typedef std::vector<std::pair<bv_size_type, bv_size_type> > bv_ranges_vector;
55 
56 int main(void)
57 {
58  try
59  {
60  const unsigned ranges = 3; // split into 3 ranges
61  bm::bvector<> bv { 10, 20, 100, 200, 300, 655000, bm::id_max-1 };
62 
63 
64  bv_size_type cnt = bv.count();
65  bv_size_type split_rank = cnt / ranges; // target population count
66 
67  bv_ranges_vector pair_vect;
68 
69  // run the search for splits, traget bin size is split_rank
70 
71  cout << "Target split rank:" << split_rank << endl;
72 
73  bm::rank_range_split(bv, split_rank, pair_vect);
74 
75  // go through each range and print all bit values
76  for (size_t k = 0; k < pair_vect.size(); ++k)
77  {
78  const auto& p = pair_vect[k];
79  cout << k << ": [" << p.first << ".." << p.second << "] ";
80 
81  // find and print bits in the target range with bvector<>::enumerator
82  bm::bvector<>::enumerator en = bv.get_enumerator(p.first);
83  for (; en.valid(); ++en)
84  {
85  auto v = *en;
86  if (v > p.second)
87  break;
88  cout << v << ", ";
89  } // for en
90  cout << endl;
91  } // for k
92 
93  }
94  catch(std::exception& ex)
95  {
96  std::cerr << ex.what() << std::endl;
97  return 1;
98  }
99 
100 
101  return 0;
102 }
103 
bv_ranges_vector
std::vector< std::pair< bv_size_type, bv_size_type > > bv_ranges_vector
Definition: sample24.cpp:54
bm::bvector::enumerator
Constant iterator designed to enumerate "ON" bits.
Definition: bm.h:599
bv_size_type
bm::bvector ::size_type bv_size_type
Definition: sample24.cpp:53
bmalgo.h
Algorithms for bvector<> (main include)
bm::bvector<>
bm::id_max
const unsigned id_max
Definition: bmconst.h:108
bm::bvector::iterator_base::valid
bool valid() const BMNOEXCEPT
Checks if iterator is still valid. Analog of != 0 comparison for pointers.
Definition: bm.h:280
main
int main(void)
Definition: sample24.cpp:56
bm::bvector::size_type
bm::id_t size_type
Definition: bm.h:117
bm.h
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
bm::rank_range_split
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:411