31 #include "../support/common.hpp"
32 #include "../util/Vector.hpp"
48 template <util::AllocType AT = util::AllocType::MALLOC>
class RiceBitVector {
58 Builder(
const size_t alloc_words) : data(alloc_words) {}
61 const uint64_t lower_bits = v & ((uint64_t(1) << log2golomb) - 1);
62 int used_bits = bit_count & 63;
64 data.
resize((((bit_count + log2golomb + 7) / 8) + 7 + 7) / 8);
66 uint64_t *append_ptr = &data + bit_count / 64;
67 uint64_t cur_word = *append_ptr;
69 cur_word |= lower_bits << used_bits;
70 if (used_bits + log2golomb > 64) {
71 *(append_ptr++) = cur_word;
72 cur_word = lower_bits >> (64 - used_bits);
73 used_bits += log2golomb - 64;
75 *append_ptr = cur_word;
76 bit_count += log2golomb;
81 for (
const auto &u : unary) {
85 data.
resize((((bit_count + bit_inc + 7) / 8) + 7 + 7) / 8);
87 for (
const auto &u : unary) {
89 uint64_t *append_ptr = &data + bit_count / 64;
90 *append_ptr |= uint64_t(1) << (bit_count & 63);
105 size_t curr_fixed_offset = 0;
106 uint64_t curr_window_unary = 0;
107 uint64_t *curr_ptr_unary;
108 int valid_lower_bits_unary = 0;
117 rbv.curr_fixed_offset = 0;
118 rbv.curr_window_unary = 0;
119 rbv.valid_lower_bits_unary = 0;
132 if (curr_window_unary == 0) {
133 result += valid_lower_bits_unary;
134 curr_window_unary = *(curr_ptr_unary++);
135 valid_lower_bits_unary = 64;
136 while (__builtin_expect(curr_window_unary == 0, 0)) {
138 curr_window_unary = *(curr_ptr_unary++);
142 const size_t pos =
rho(curr_window_unary);
144 curr_window_unary >>= pos;
145 curr_window_unary >>= 1;
146 valid_lower_bits_unary -= pos + 1;
149 result <<= log2golomb;
152 memcpy(&fixed, (uint8_t *)&data + curr_fixed_offset / 8, 8);
153 result |= (fixed >> curr_fixed_offset % 8) & ((uint64_t(1) << log2golomb) - 1);
154 curr_fixed_offset += log2golomb;
160 size_t missing = nodes, cnt;
161 while ((cnt =
nu(curr_window_unary)) < missing) {
162 curr_window_unary = *(curr_ptr_unary++);
164 valid_lower_bits_unary = 64;
166 cnt =
select64(curr_window_unary, missing - 1);
167 curr_window_unary >>= cnt;
168 curr_window_unary >>= 1;
169 valid_lower_bits_unary -= cnt + 1;
171 curr_fixed_offset += fixed_len;
174 void readReset(
const size_t bit_pos,
const size_t unary_offset) {
176 curr_fixed_offset = bit_pos;
177 size_t unary_pos = bit_pos + unary_offset;
178 curr_ptr_unary = &data + unary_pos / 64;
179 curr_window_unary = *(curr_ptr_unary++) >> (unary_pos & 63);
180 valid_lower_bits_unary = 64 - (unary_pos & 63);