-
Notifications
You must be signed in to change notification settings - Fork 25
/
indexed_sequence.hpp
166 lines (140 loc) · 6.36 KB
/
indexed_sequence.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
#pragma once
#include <stdexcept>
#include "compact_elias_fano.hpp"
#include "compact_ranked_bitvector.hpp"
#include "all_ones_sequence.hpp"
#include "global_parameters.hpp"
namespace ds2i {
struct indexed_sequence {
enum index_type {
elias_fano = 0,
ranked_bitvector = 1,
all_ones = 2,
index_types = 3
};
static const uint64_t type_bits = 1; // all_ones is implicit
static DS2I_FLATTEN_FUNC uint64_t
bitsize(global_parameters const& params, uint64_t universe, uint64_t n)
{
uint64_t best_cost = all_ones_sequence::bitsize(params, universe, n);
uint64_t ef_cost = compact_elias_fano::bitsize(params, universe, n) + type_bits;
if (ef_cost < best_cost) {
best_cost = ef_cost;
}
uint64_t rb_cost = compact_ranked_bitvector::bitsize(params, universe, n) + type_bits;
if (rb_cost < best_cost) {
best_cost = rb_cost;
}
return best_cost;
}
template <typename Iterator>
static void write(succinct::bit_vector_builder& bvb,
Iterator begin,
uint64_t universe, uint64_t n,
global_parameters const& params)
{
uint64_t best_cost = all_ones_sequence::bitsize(params, universe, n);
int best_type = all_ones;
if (best_cost) {
uint64_t ef_cost = compact_elias_fano::bitsize(params, universe, n) + type_bits;
if (ef_cost < best_cost) {
best_cost = ef_cost;
best_type = elias_fano;
}
uint64_t rb_cost = compact_ranked_bitvector::bitsize(params, universe, n) + type_bits;
if (rb_cost < best_cost) {
best_cost = rb_cost;
best_type = ranked_bitvector;
}
bvb.append_bits(best_type, type_bits);
}
switch (best_type) {
case elias_fano:
compact_elias_fano::write(bvb, begin,
universe, n,
params);
break;
case ranked_bitvector:
compact_ranked_bitvector::write(bvb, begin,
universe, n,
params);
break;
case all_ones:
all_ones_sequence::write(bvb, begin,
universe, n,
params);
break;
default:
assert(false);
}
}
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)
{
if (all_ones_sequence::bitsize(params, universe, n) == 0) {
m_type = all_ones;
} else {
m_type = index_type(bv.get_word56(offset)
& ((uint64_t(1) << type_bits) - 1));
}
switch (m_type) {
case elias_fano:
m_ef_enumerator = compact_elias_fano::enumerator(bv, offset + type_bits,
universe, n,
params);
break;
case ranked_bitvector:
m_rb_enumerator = compact_ranked_bitvector::enumerator(bv, offset + type_bits,
universe, n,
params);
break;
case all_ones:
m_ao_enumerator = all_ones_sequence::enumerator(bv, offset + type_bits,
universe, n,
params);
break;
default:
throw std::invalid_argument("Unsupported type");
}
}
#define ENUMERATOR_METHOD(RETURN_TYPE, METHOD, FORMALS, ACTUALS) \
RETURN_TYPE DS2I_FLATTEN_FUNC METHOD FORMALS \
{ \
switch (__builtin_expect(m_type, elias_fano)) { \
case elias_fano: \
return m_ef_enumerator.METHOD ACTUALS; \
case ranked_bitvector: \
return m_rb_enumerator.METHOD ACTUALS; \
case all_ones: \
return m_ao_enumerator.METHOD ACTUALS; \
default: \
assert(false); \
__builtin_unreachable(); \
} \
} \
/**/
// semicolons are redundant but they are needed to get emacs to
// align the lines properly
ENUMERATOR_METHOD(value_type, move, (uint64_t position), (position));
ENUMERATOR_METHOD(value_type, next_geq, (uint64_t lower_bound), (lower_bound));
ENUMERATOR_METHOD(value_type, next, (), ());
ENUMERATOR_METHOD(uint64_t, size, () const, ());
ENUMERATOR_METHOD(uint64_t, prev_value, () const, ());
#undef ENUMERATOR_METHOD
#undef ENUMERATOR_VOID_METHOD
private:
index_type m_type;
union {
compact_elias_fano::enumerator m_ef_enumerator;
compact_ranked_bitvector::enumerator m_rb_enumerator;
all_ones_sequence::enumerator m_ao_enumerator;
};
};
};
}