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
35int main(void)
36{
37 try
38 {
39 bm::bvector<> bv { 1, 20, 30, 31 }; // init a test bit-vector
40
41 // construct rank-select index
42 std::unique_ptr<bm::bvector<>::rs_index_type>
43 rs_idx(new bm::bvector<>::rs_index_type());
44 bv.build_rs_index(rs_idx.get());
45
46 // lets find a few ranks here
47 //
48 auto r1 = bv.rank(20, *rs_idx);
49 std::cout << r1 << std::endl; // 2
50
51 r1 = bv.rank(21, *rs_idx);
52 std::cout << r1 << std::endl; // still 2
53
54 r1 = bv.rank(30, *rs_idx);
55 std::cout << r1 << std::endl; // 3
56
57 // position value corrected rank
58 // one special case of rank function returns rank-1
59 // if position bit is set or just rank, otherwise
60 //
61 // this is an equivalent of
62 // bv.count_range(0, n) - bv.text(n)
63 // (just faster, because of the fused rank-test)
64 //
65
66 auto r1c = bv.rank_corrected(31, *rs_idx); // 3
67 std::cout << r1c << std::endl;
68 r1c = bv.rank_corrected(32, *rs_idx); // 4
69 std::cout << r1c << std::endl;
70 r1c = bv.rank_corrected(33, *rs_idx); // 4
71 std::cout << r1c << std::endl;
72
73
74 // now perform a search for a position for a rank
75 //
77 bool found = bv.select(2, pos, *rs_idx);
78 if (found)
79 std::cout << pos << std::endl; // 20
80 else
81 std::cout << "Rank not found." << std::endl;
82
83 found = bv.select(2, pos, *rs_idx);
84 if (found)
85 std::cout << pos << std::endl; // 30
86 else
87 std::cout << "Rank not found." << std::endl;
88
89 found = bv.select(5, pos, *rs_idx);
90 if (found)
91 std::cout << pos << std::endl;
92 else
93 std::cout << "Rank not found." << std::endl; // this!
94
95 }
96 catch(std::exception& ex)
97 {
98 std::cerr << ex.what() << std::endl;
99 return 1;
100 }
101
102
103 return 0;
104}
105
106
Compressed bit-vector bvector<> container, set algebraic methods, traversal iterators.
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:4225
int main(void)
Definition sample1.cpp:38