-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_support.h
73 lines (51 loc) · 1.35 KB
/
select_support.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "rank_support.h"
class select_support
{
private:
rank_support *r; // Rank support on which this select support functions.
uint64_t select(uint64_t rank, bool bit);
public:
select_support() {}
select_support(rank_support *R) { build(R); }
void build(rank_support *R) { r = R; }
uint64_t select1(uint64_t rank);
uint64_t select0(uint64_t rank);
uint64_t overhead();
};
uint64_t select_support::select(uint64_t rank, bool bit)
{
if(!rank || rank > r -> bitvector_len())
return std::numeric_limits<uint64_t>::max();
uint64_t low = 0, high = r -> bitvector_len() - 1, mid, soln;
soln = std::numeric_limits<uint64_t>::max();
while(low <= high)
{
mid = (low + high) / 2;
uint64_t rankMid = (bit ? r -> rank1(mid) : r -> rank0(mid));
if(rankMid < rank)
low = mid + 1;
else if(rankMid > rank)
high = mid - 1;
else
{
soln = mid;
if(mid > 0)
high = mid - 1;
else
break;
}
}
return soln;
}
uint64_t select_support::select1(uint64_t rank)
{
return select(rank, 1);
}
uint64_t select_support::select0(uint64_t rank)
{
return select(rank, 0);
}
uint64_t select_support::overhead()
{
return 0;
}