-
Notifications
You must be signed in to change notification settings - Fork 25
/
compact_elias_fano.hpp
420 lines (354 loc) · 14.9 KB
/
compact_elias_fano.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#pragma once
#include <stdexcept>
#include <succinct/bit_vector.hpp>
#include <succinct/broadword.hpp>
#include "global_parameters.hpp"
#include "util.hpp"
namespace ds2i {
struct compact_elias_fano {
struct offsets {
offsets()
{}
offsets(uint64_t base_offset,
uint64_t universe,
uint64_t n,
global_parameters const& params)
: universe(universe)
, n(n)
, log_sampling0(params.ef_log_sampling0)
, log_sampling1(params.ef_log_sampling1)
, lower_bits(universe > n ? succinct::broadword::msb(universe / n) : 0)
, mask((uint64_t(1) << lower_bits) - 1)
// pad with a zero on both sides as sentinels
, higher_bits_length(n + (universe >> lower_bits) + 2)
, pointer_size(ceil_log2(higher_bits_length))
, pointers0((higher_bits_length - n) >> log_sampling0) // XXX
, pointers1(n >> log_sampling1)
, pointers0_offset(base_offset)
, pointers1_offset(pointers0_offset + pointers0 * pointer_size)
, higher_bits_offset(pointers1_offset + pointers1 * pointer_size)
, lower_bits_offset(higher_bits_offset + higher_bits_length)
, end(lower_bits_offset + n * lower_bits)
{
assert(n > 0);
}
uint64_t universe;
uint64_t n;
uint64_t log_sampling0;
uint64_t log_sampling1;
uint64_t lower_bits;
uint64_t mask;
uint64_t higher_bits_length;
uint64_t pointer_size;
uint64_t pointers0;
uint64_t pointers1;
uint64_t pointers0_offset;
uint64_t pointers1_offset;
uint64_t higher_bits_offset;
uint64_t lower_bits_offset;
uint64_t end;
};
static DS2I_FLATTEN_FUNC uint64_t
bitsize(global_parameters const& params, uint64_t universe, uint64_t n)
{
return offsets(0, universe, n, params).end;
}
template <typename Iterator>
static void write(succinct::bit_vector_builder& bvb,
Iterator begin,
uint64_t universe, uint64_t n,
global_parameters const& params)
{
using succinct::util::ceil_div;
uint64_t base_offset = bvb.size();
offsets of(base_offset, universe, n, params);
// initialize all the bits to 0
bvb.zero_extend(of.end - base_offset);
uint64_t sample1_mask = (uint64_t(1) << of.log_sampling1) - 1;
uint64_t offset;
// utility function to set 0 pointers
auto set_ptr0s = [&](uint64_t begin, uint64_t end,
uint64_t rank_end) {
uint64_t begin_zeros = begin - rank_end;
uint64_t end_zeros = end - rank_end;
for (uint64_t ptr0 = ceil_div(begin_zeros, uint64_t(1) << of.log_sampling0);
(ptr0 << of.log_sampling0) < end_zeros;
++ptr0) {
if (!ptr0) continue;
offset = of.pointers0_offset + (ptr0 - 1) * of.pointer_size;
assert(offset + of.pointer_size <= of.pointers1_offset);
bvb.set_bits(offset, (ptr0 << of.log_sampling0) + rank_end,
of.pointer_size);
}
};
uint64_t last = 0;
uint64_t last_high = 0;
Iterator it = begin;
for (size_t i = 0; i < n; ++i) {
uint64_t v = *it++;
if (i && v < last) {
throw std::runtime_error("Sequence is not sorted");
}
assert(v < universe);
uint64_t high = (v >> of.lower_bits) + i + 1;
uint64_t low = v & of.mask;
bvb.set(of.higher_bits_offset + high, 1);
offset = of.lower_bits_offset + i * of.lower_bits;
assert(offset + of.lower_bits <= of.end);
bvb.set_bits(offset, low, of.lower_bits);
if (i && (i & sample1_mask) == 0) {
uint64_t ptr1 = i >> of.log_sampling1;
assert(ptr1 > 0);
offset = of.pointers1_offset + (ptr1 - 1) * of.pointer_size;
assert(offset + of.pointer_size <= of.higher_bits_offset);
bvb.set_bits(offset, high, of.pointer_size);
}
// write pointers for the run of zeros in [last_high, high)
set_ptr0s(last_high + 1, high, i);
last_high = high;
last = v;
}
// pointers to zeros after the last 1
set_ptr0s(last_high + 1, of.higher_bits_length, n); // XXX
}
class enumerator {
public:
typedef std::pair<uint64_t, uint64_t> value_type; // (position, value)
enumerator()
{}
enumerator(succinct::bit_vector const& bv, uint64_t offset,
uint64_t universe, uint64_t n,
global_parameters const& params)
: m_bv(&bv)
, m_of(offset, universe, n, params)
, m_position(size())
, m_value(m_of.universe)
{}
value_type move(uint64_t position)
{
assert(position <= m_of.n);
if (position == m_position) {
return value();
}
uint64_t skip = position - m_position;
// optimize small forward skips
if (DS2I_LIKELY(position > m_position && skip <= linear_scan_threshold)) {
m_position = position;
if (DS2I_UNLIKELY(m_position == size())) {
m_value = m_of.universe;
} else {
succinct::bit_vector::unary_enumerator he = m_high_enumerator;
for (size_t i = 0; i < skip; ++i) {
he.next();
}
m_value = ((he.position() - m_of.higher_bits_offset - m_position - 1)
<< m_of.lower_bits) | read_low();
m_high_enumerator = he;
}
return value();
}
return slow_move(position);
}
value_type next_geq(uint64_t lower_bound)
{
if (lower_bound == m_value) {
return value();
}
uint64_t high_lower_bound = lower_bound >> m_of.lower_bits;
uint64_t cur_high = m_value >> m_of.lower_bits;
uint64_t high_diff = high_lower_bound - cur_high;
if (DS2I_LIKELY(lower_bound > m_value
&& high_diff <= linear_scan_threshold)) {
// optimize small skips
next_reader next_value(*this, m_position + 1);
uint64_t val;
do {
m_position += 1;
if (DS2I_LIKELY(m_position < size())) {
val = next_value();
} else {
val = m_of.universe;
break;
}
} while (val < lower_bound);
m_value = val;
return value();
} else {
return slow_next_geq(lower_bound);
}
}
uint64_t size() const
{
return m_of.n;
}
value_type next()
{
m_position += 1;
assert(m_position <= size());
if (DS2I_LIKELY(m_position < size())) {
m_value = read_next();
} else {
m_value = m_of.universe;
}
return value();
}
uint64_t prev_value() const
{
if (m_position == 0) {
return 0;
}
uint64_t prev_high = 0;
if (DS2I_LIKELY(m_position < size())) {
prev_high = m_bv->predecessor1(m_high_enumerator.position() - 1);
} else {
prev_high = m_bv->predecessor1(m_of.lower_bits_offset - 1);
}
prev_high -= m_of.higher_bits_offset;
uint64_t prev_pos = m_position - 1;
uint64_t prev_low =
m_bv->get_word56(m_of.lower_bits_offset +
prev_pos * m_of.lower_bits)
& m_of.mask;
return ((prev_high - prev_pos - 1) << m_of.lower_bits) | prev_low;
}
uint64_t position() const
{
return m_position;
}
private:
value_type DS2I_NOINLINE slow_move(uint64_t position)
{
if (DS2I_UNLIKELY(position == size())) {
m_position = position;
m_value = m_of.universe;
return value();
}
uint64_t skip = position - m_position;
uint64_t to_skip;
if (position > m_position
&& (skip >> m_of.log_sampling1) == 0) {
to_skip = skip - 1;
} else {
uint64_t ptr = position >> m_of.log_sampling1;
uint64_t high_pos = pointer1(ptr);
uint64_t high_rank = ptr << m_of.log_sampling1;
m_high_enumerator = succinct::bit_vector::unary_enumerator
(*m_bv, m_of.higher_bits_offset + high_pos);
to_skip = position - high_rank;
}
m_high_enumerator.skip(to_skip);
m_position = position;
m_value = read_next();
return value();
}
value_type DS2I_NOINLINE slow_next_geq(uint64_t lower_bound)
{
if (DS2I_UNLIKELY(lower_bound >= m_of.universe)) {
return move(size());
}
uint64_t high_lower_bound = lower_bound >> m_of.lower_bits;
uint64_t cur_high = m_value >> m_of.lower_bits;
uint64_t high_diff = high_lower_bound - cur_high;
// XXX bounds checking!
uint64_t to_skip;
if (lower_bound > m_value
&& (high_diff >> m_of.log_sampling0) == 0) {
// note: at the current position in the bitvector there
// should be a 1, but since we already consumed it, it
// is 0 in the enumerator, so we need to skip it
to_skip = high_diff;
} else {
uint64_t ptr = high_lower_bound >> m_of.log_sampling0;
uint64_t high_pos = pointer0(ptr);
uint64_t high_rank0 = ptr << m_of.log_sampling0;
m_high_enumerator = succinct::bit_vector::unary_enumerator
(*m_bv, m_of.higher_bits_offset + high_pos);
to_skip = high_lower_bound - high_rank0;
}
m_high_enumerator.skip0(to_skip);
m_position = m_high_enumerator.position() - m_of.higher_bits_offset
- high_lower_bound;
next_reader read_value(*this, m_position);
while (true) {
if (DS2I_UNLIKELY(m_position == size())) {
m_value = m_of.universe;
return value();
}
auto val = read_value();
if (val >= lower_bound) {
m_value = val;
return value();
}
m_position++;
}
}
static const uint64_t linear_scan_threshold = 8;
inline value_type value() const
{
return value_type(m_position, m_value);
}
inline uint64_t read_low()
{
return m_bv->get_word56(m_of.lower_bits_offset
+ m_position * m_of.lower_bits)
& m_of.mask;
}
inline uint64_t read_next()
{
assert(m_position < size());
uint64_t high = m_high_enumerator.next() - m_of.higher_bits_offset;
return ((high - m_position - 1) << m_of.lower_bits) | read_low();
}
struct next_reader {
next_reader(enumerator& e, uint64_t position)
: e(e)
, high_enumerator(e.m_high_enumerator)
, high_base(e.m_of.higher_bits_offset + position + 1)
, lower_bits(e.m_of.lower_bits)
, lower_base(e.m_of.lower_bits_offset + position * lower_bits)
, mask(e.m_of.mask)
, bv(*e.m_bv)
{}
~next_reader()
{
e.m_high_enumerator = high_enumerator;
}
uint64_t operator()()
{
uint64_t high = high_enumerator.next() - high_base;
uint64_t low = bv.get_word56(lower_base) & mask;
high_base += 1;
lower_base += lower_bits;
return (high << lower_bits) | low;
}
enumerator& e;
succinct::bit_vector::unary_enumerator high_enumerator;
uint64_t high_base, lower_bits, lower_base, mask;
succinct::bit_vector const& bv;
};
inline uint64_t pointer(uint64_t offset, uint64_t i) const
{
if (i == 0) {
return 0;
} else {
return
m_bv->get_word56(offset + (i - 1) * m_of.pointer_size)
& ((uint64_t(1) << m_of.pointer_size) - 1);
}
}
inline uint64_t pointer0(uint64_t i) const
{
return pointer(m_of.pointers0_offset, i);
}
inline uint64_t pointer1(uint64_t i) const
{
return pointer(m_of.pointers1_offset, i);
}
succinct::bit_vector const* m_bv;
offsets m_of;
uint64_t m_position;
uint64_t m_value;
succinct::bit_vector::unary_enumerator m_high_enumerator;
};
};
}