-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathrs_bit_vector.hpp
192 lines (160 loc) · 6.36 KB
/
rs_bit_vector.hpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#pragma once
#include <vector>
#include <algorithm>
#include "bit_vector.hpp"
#include "broadword.hpp"
namespace succinct {
class rs_bit_vector : public bit_vector {
public:
rs_bit_vector()
: bit_vector()
{}
template <class Range>
rs_bit_vector(Range const& from,
bool with_select_hints = false,
bool with_select0_hints = false)
: bit_vector(from)
{
build_indices(with_select_hints, with_select0_hints);
}
template <typename Visitor>
void map(Visitor& visit) {
bit_vector::map(visit);
visit
(m_block_rank_pairs, "m_block_rank_pairs")
(m_select_hints, "m_select_hints")
(m_select0_hints, "m_select0_hints")
;
}
void swap(rs_bit_vector& other) {
bit_vector::swap(other);
m_block_rank_pairs.swap(other.m_block_rank_pairs);
m_select_hints.swap(other.m_select_hints);
m_select0_hints.swap(other.m_select0_hints);
}
inline uint64_t num_ones() const {
return *(m_block_rank_pairs.end() - 2);
}
inline uint64_t num_zeros() const {
return size() - num_ones();
}
inline uint64_t rank(uint64_t pos) const {
assert(pos <= size());
if (pos == size()) {
return num_ones();
}
uint64_t sub_block = pos / 64;
uint64_t r = sub_block_rank(sub_block);
uint64_t sub_left = pos % 64;
if (sub_left) {
r += broadword::popcount(m_bits[sub_block] << (64 - sub_left));
}
return r;
}
inline uint64_t rank0(uint64_t pos) const {
return pos - rank(pos);
}
inline uint64_t select(uint64_t n) const {
using broadword::popcount;
using broadword::select_in_word;
assert(n < num_ones());
uint64_t a = 0;
uint64_t b = num_blocks();
if (m_select_hints.size()) {
uint64_t chunk = n / select_ones_per_hint;
if (chunk != 0) {
a = m_select_hints[chunk - 1];
}
b = m_select_hints[chunk] + 1;
}
uint64_t block = 0;
while (b - a > 1) {
uint64_t mid = a + (b - a) / 2;
uint64_t x = block_rank(mid);
if (x <= n) {
a = mid;
} else {
b = mid;
}
}
block = a;
assert(block < num_blocks());
uint64_t block_offset = block * block_size;
uint64_t cur_rank = block_rank(block);
assert(cur_rank <= n);
uint64_t rank_in_block_parallel = (n - cur_rank) * broadword::ones_step_9;
uint64_t sub_ranks = sub_block_ranks(block);
uint64_t sub_block_offset = broadword::uleq_step_9(sub_ranks, rank_in_block_parallel) * broadword::ones_step_9 >> 54 & 0x7;
cur_rank += sub_ranks >> (7 - sub_block_offset) * 9 & 0x1FF;
assert(cur_rank <= n);
uint64_t word_offset = block_offset + sub_block_offset;
return word_offset * 64 + select_in_word(m_bits[word_offset], n - cur_rank);
}
// TODO(ot): share code between select and select0
inline uint64_t select0(uint64_t n) const {
using broadword::popcount;
using broadword::select_in_word;
assert(n < num_zeros());
uint64_t a = 0;
uint64_t b = num_blocks();
if (m_select0_hints.size()) {
uint64_t chunk = n / select_zeros_per_hint;
if (chunk != 0) {
a = m_select0_hints[chunk - 1];
}
b = m_select0_hints[chunk] + 1;
}
uint64_t block = 0;
while (b - a > 1) {
uint64_t mid = a + (b - a) / 2;
uint64_t x = block_rank0(mid);
if (x <= n) {
a = mid;
} else {
b = mid;
}
}
block = a;
assert(block < num_blocks());
uint64_t block_offset = block * block_size;
uint64_t cur_rank0 = block_rank0(block);
assert(cur_rank0 <= n);
uint64_t rank_in_block_parallel = (n - cur_rank0) * broadword::ones_step_9;
uint64_t sub_ranks = 64 * broadword::inv_count_step_9 - sub_block_ranks(block);
uint64_t sub_block_offset = broadword::uleq_step_9(sub_ranks, rank_in_block_parallel) * broadword::ones_step_9 >> 54 & 0x7;
cur_rank0 += sub_ranks >> (7 - sub_block_offset) * 9 & 0x1FF;
assert(cur_rank0 <= n);
uint64_t word_offset = block_offset + sub_block_offset;
return word_offset * 64 + select_in_word(~m_bits[word_offset], n - cur_rank0);
}
protected:
inline uint64_t num_blocks() const {
return m_block_rank_pairs.size() / 2 - 1;
}
inline uint64_t block_rank(uint64_t block) const {
return m_block_rank_pairs[block * 2];
}
inline uint64_t sub_block_rank(uint64_t sub_block) const {
uint64_t r = 0;
uint64_t block = sub_block / block_size;
r += block_rank(block);
uint64_t left = sub_block % block_size;
r += sub_block_ranks(block) >> ((7 - left) * 9) & 0x1FF;
return r;
}
inline uint64_t sub_block_ranks(uint64_t block) const {
return m_block_rank_pairs[block * 2 + 1];
}
inline uint64_t block_rank0(uint64_t block) const {
return block * block_size * 64 - m_block_rank_pairs[block * 2];
}
void build_indices(bool with_select_hints, bool with_select0_hints);
static const uint64_t block_size = 8; // in 64bit words
static const uint64_t select_ones_per_hint = 64 * block_size * 2; // must be > block_size * 64
static const uint64_t select_zeros_per_hint = select_ones_per_hint;
typedef mapper::mappable_vector<uint64_t> uint64_vec;
uint64_vec m_block_rank_pairs;
uint64_vec m_select_hints;
uint64_vec m_select0_hints;
};
}