-
Notifications
You must be signed in to change notification settings - Fork 17
/
rs_bit_vector.cpp
73 lines (65 loc) · 2.52 KB
/
rs_bit_vector.cpp
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 "rs_bit_vector.hpp"
namespace succinct {
void rs_bit_vector::build_indices(bool with_select_hints, bool with_select0_hints)
{
{
using broadword::popcount;
std::vector<uint64_t> block_rank_pairs;
uint64_t next_rank = 0;
uint64_t cur_subrank = 0;
uint64_t subranks = 0;
block_rank_pairs.push_back(0);
for (uint64_t i = 0; i < m_bits.size(); ++i) {
uint64_t word_pop = popcount(m_bits[i]);
uint64_t shift = i % block_size;
if (shift) {
subranks <<= 9;
subranks |= cur_subrank;
}
next_rank += word_pop;
cur_subrank += word_pop;
if (shift == block_size - 1) {
block_rank_pairs.push_back(subranks);
block_rank_pairs.push_back(next_rank);
subranks = 0;
cur_subrank = 0;
}
}
uint64_t left = block_size - m_bits.size() % block_size;
for (uint64_t i = 0; i < left; ++i) {
subranks <<= 9;
subranks |= cur_subrank;
}
block_rank_pairs.push_back(subranks);
if (m_bits.size() % block_size) {
block_rank_pairs.push_back(next_rank);
block_rank_pairs.push_back(0);
}
m_block_rank_pairs.steal(block_rank_pairs);
}
if (with_select_hints) {
std::vector<uint64_t> select_hints;
uint64_t cur_ones_threshold = select_ones_per_hint;
for (uint64_t i = 0; i < num_blocks(); ++i) {
if (block_rank(i + 1) > cur_ones_threshold) {
select_hints.push_back(i);
cur_ones_threshold += select_ones_per_hint;
}
}
select_hints.push_back(num_blocks());
m_select_hints.steal(select_hints);
}
if (with_select0_hints) {
std::vector<uint64_t> select0_hints;
uint64_t cur_zeros_threshold = select_zeros_per_hint;
for (uint64_t i = 0; i < num_blocks(); ++i) {
if (block_rank0(i + 1) > cur_zeros_threshold) {
select0_hints.push_back(i);
cur_zeros_threshold += select_zeros_per_hint;
}
}
select0_hints.push_back(num_blocks());
m_select0_hints.steal(select0_hints);
}
}
}