BitMagic-C++
sample17.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 sample17.cpp
20Example rank and select operations.
21
22*/
23
24/*! \file sample17.cpp
25 \brief Example: rank and select operations using rank-select index
26*/
27
28#include <stdlib.h>
29#include <iostream>
30#include <vector>
31#include <memory>
32
33#include "bm.h"
34#include "bmundef.h" /* clear the pre-proc defines from BM */
35
36int main(void)
37{
38 try
39 {
40 bm::bvector<> bv { 1, 20, 30, 31 }; // init a test bit-vector
41
42 // construct rank-select index
43 std::unique_ptr<bm::bvector<>::rs_index_type>
44 rs_idx(new bm::bvector<>::rs_index_type());
45 bv.build_rs_index(rs_idx.get());
46
47 // lets find a few ranks here
48 //
49 auto r1 = bv.rank(20, *rs_idx);
50 std::cout << r1 << std::endl; // 2
51
52 r1 = bv.rank(21, *rs_idx);
53 std::cout << r1 << std::endl; // still 2
54
55 r1 = bv.rank(30, *rs_idx);
56 std::cout << r1 << std::endl; // 3
57
58 // position value corrected rank
59 // one special case of rank function returns rank-1
60 // if position bit is set or just rank, otherwise
61 //
62 // this is an equivalent of
63 // bv.count_range(0, n) - bv.text(n)
64 // (just faster, because of the fused rank-test)
65 //
66
67 auto r1c = bv.rank_corrected(31, *rs_idx); // 3
68 std::cout << r1c << std::endl;
69 r1c = bv.rank_corrected(32, *rs_idx); // 4
70 std::cout << r1c << std::endl;
71 r1c = bv.rank_corrected(33, *rs_idx); // 4
72 std::cout << r1c << std::endl;
73
74
75 // now perform a search for a position for a rank
76 //
78 bool found = bv.select(2, pos, *rs_idx);
79 if (found)
80 std::cout << pos << std::endl; // 20
81 else
82 std::cout << "Rank not found." << std::endl;
83
84 found = bv.select(2, pos, *rs_idx);
85 if (found)
86 std::cout << pos << std::endl; // 30
87 else
88 std::cout << "Rank not found." << std::endl;
89
90 found = bv.select(5, pos, *rs_idx);
91 if (found)
92 std::cout << pos << std::endl;
93 else
94 std::cout << "Rank not found." << std::endl; // this!
95
96 }
97 catch(std::exception& ex)
98 {
99 std::cerr << ex.what() << std::endl;
100 return 1;
101 }
102
103
104 return 0;
105}
106
107
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
pre-processor un-defines to avoid global space pollution (internal)
Bitvector Bit-vector container with runtime compression of bits.
Definition: bm.h:115
bool select(size_type rank, size_type &pos, const rs_index_type &rs_idx) const BMNOEXCEPT
select bit-vector position for the specified rank(bitcount)
Definition: bm.h:5045
bvector_size_type size_type
Definition: bm.h:121
rs_index< allocator_type > rs_index_type
Definition: bm.h:816
int main(void)
Definition: sample17.cpp:36