-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathveb.h
More file actions
383 lines (323 loc) · 7.53 KB
/
veb.h
File metadata and controls
383 lines (323 loc) · 7.53 KB
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
#pragma once
#include "definitions.h"
#include <optional>
#include <vector>
#include <memory>
#include <array>
#include <utility>
#include <limits>
#include <type_traits>
namespace doo
{
namespace detail
{
constexpr inline u64 pow_2(uint in) { return 1ULL << in; }
constexpr inline bool is_null(u64 in) { return in == std::numeric_limits<u64>::max(); }
constexpr inline bool is_not_null(u64 in) { return in != std::numeric_limits<u64>::max(); }
template<uint log2_u, bool shrink_to_fit, typename Enable = void>
class veb
{
public:
static_assert(log2_u > 0 && log2_u <= 63, "log2_u must be within [0,63]");
bool member(u64 x) const
{
if (x == min || x == max) return true;
if (x < min || x > max) return false; // Not within range
u64 h = high(x);
u64 l = low(x);
if (cluster[h] == nullptr || cluster[h]->empty()) return false;
return cluster[h]->member(l); // Tail call recursion
}
bool empty() const
{
return is_null(min);
}
void renew_key(u64 x_old, u64 x_new)
{
del(x_old);
del(x_new);
}
void insert(u64 x)
{
if (empty())
{
init(x);
return;
}
if (x < min)
std::swap(x, min);
if (x > max)
max = x;
u64 h = high(x);
u64 l = low(x);
make_subcluster(h);
if (cluster[h]->empty())
{
make_summary();
summary->insert(h);
cluster[h]->init(l);
}
else cluster[h]->insert(l);
}
u64 succ(u64 x) const
{
if (is_not_null(min) && x < min) return min;
u64 h = high(x);
u64 l = low(x);
u64 c_max;
if (cluster[h] == nullptr)
c_max = nullop;
else c_max = cluster[h]->max;
if (is_not_null(c_max) && l < c_max)
{
u64 j = cluster[h]->succ(l);
return idx(h, j);
}
u64 h_succ;
if (summary == nullptr)
h_succ = nullop;
else h_succ = summary->succ(h);
if (is_null(h_succ)) return nullop;
u64 j = cluster[h_succ]->min;
return idx(h_succ, j);
}
u64 pred(u64 x) const
{
if (is_not_null(max) && x > max) return max;
u64 h = high(x);
u64 l = low(x);
u64 c_min;
if (cluster[h] == nullptr)
c_min = nullop;
else c_min = cluster[h]->min;
if (is_not_null(c_min) && l > c_min)
{
u64 j = cluster[h]->pred(l);
return idx(h, j);
}
u64 h_pred;
if (summary == nullptr)
h_pred = nullop;
else h_pred = summary->pred(h);
if (is_null(h_pred))
{
if (is_not_null(min) && x > min) return min;
return nullop;
}
u64 j = cluster[h_pred]->max;
return idx(h_pred, j);
}
void del(u64 x)
{
if (min == max)
{
min = nullop;
max = nullop;
if constexpr (shrink_to_fit) summary = nullptr; // dealloc
return;
}
if (x == min)
{
u64 i = summary->min;
x = idx(i, cluster[i]->min);
min = x;
}
u64 h = high(x);
u64 l = low(x);
cluster[h]->del(l);
if (cluster[h]->empty())
{
if constexpr (shrink_to_fit) cluster[h] = nullptr;
summary->del(h);
if (x == max)
{
auto s_max = summary->max;
if (is_null(s_max)) max = min;
else max = idx(s_max, cluster[s_max]->max);
}
}
else if (x == max) max = idx(h, cluster[h]->max);
}
void init(u64 x)
{
min = x;
max = x;
}
u64 min = nullop, max = nullop;
private:
constexpr static u64 nullop = std::numeric_limits<u64>::max();
//constexpr key u = pow_2(log2_u);
constexpr static u64 sqrt_u_floor = pow_2(log2_u / 2);
constexpr static u64 sqrt_u_ceil = pow_2((log2_u + 1) / 2);
// Eg: 0001 << (log2_u / 2)(=2) -> 0100 - 0001 = 0011
constexpr static u64 lower_mask = sqrt_u_floor /*Set lower half bit*/ - 1;
constexpr static int half_log2_u = log2_u / 2;
constexpr static int half_log2_u_ceil = (log2_u + 1) / 2;
std::unique_ptr<veb<half_log2_u_ceil,shrink_to_fit>> summary;
std::array<std::unique_ptr<veb<half_log2_u,shrink_to_fit>>, sqrt_u_ceil> cluster;
constexpr u64 high(u64 in) const { return in >> half_log2_u; } // eg: 1101. u=4, halfU = 2. 1101 >> 2 == 0011
constexpr u64 low(u64 in) const { return in & lower_mask; }
constexpr u64 idx(u64 i, u64 j) const { return (i<<half_log2_u) | j; }
void make_subcluster(u64 h)
{
if (cluster[h] == nullptr)
cluster[h] = std::make_unique<veb<half_log2_u,shrink_to_fit>>();
}
void make_summary()
{
if (summary == nullptr)
summary = std::make_unique<veb<half_log2_u_ceil,shrink_to_fit>>();
}
};
template<uint log2_u, bool shrink_to_fit>
class veb<log2_u, shrink_to_fit, typename std::enable_if<log2_u <= 6>::type>
{
public:
void insert(u64 x)
{
bitvector |= pow_2(x);
if (x < min || is_null(min)) min = x;
if (x > max || is_null(max)) max = x;
}
void del(u64 x)
{
if (x == min) min = succ(min);
if (x == max) max = pred(max);
bitvector &= ~pow_2(x);
}
u64 succ(u64 x) const
{
u64 shifted = bitvector >> (x + 1);
if (shifted == 0ULL)
return nullop;
return idx_lsb(shifted) + x + 1;
}
u64 pred(u64 x) const
{
// Cannot left shift due to arithmetic overflow which is undefined behavior.
if (x == 0) return nullop;
/*
E.g. 01010. pred(3) == 1 -> 00001 << 3 == 01000 -> 01000 - 1 = 00111
*/
u64 mask = (1ULL << x) - 1;
uint64_t masked = bitvector & mask;
if (masked == 0ULL)
return nullop;
return idx_msb(masked);
}
bool empty() const
{
return bitvector == 0;
}
bool member(u64 key) const
{
return !!(bitvector & pow_2(key));
}
void init(u64 x)
{
bitvector |= pow_2(x);
min = x;
max = x;
}
u64 min = nullop, max = nullop;
private:
constexpr static u64 nullop = std::numeric_limits<u64>::max();
uint64_t bitvector = 0ULL;
// Get the index of the least significant set bit.
#ifdef __GNUC__
unsigned int idx_lsb(u64 k) const
{
return __builtin_ctzll(k);
}
unsigned int idx_msb(u64 k) const
{
return 63-__builtin_clzll(k);
}
#elif _MSC_VER
unsigned int idx_lsb(u64 k) const
{
unsigned long idx;
_BitScanForward64(&idx, k);
return idx;
}
unsigned int idx_msb(u64 k) const
{
unsigned long idx;
_BitScanReverse64(&idx, k);
return idx;
}
#endif
};
}
template<uint log2_u, bool shrink_to_fit = true>
class veb
{
public:
std::optional<u64> get_min() const
{
if (detail::is_null(container.min)) return {};
return container.min;
}
std::optional<u64> get_max() const
{
if (detail::is_null(container.max)) return {};
return container.max;
}
void insert(u64 x)
{
container.insert(x);
}
void insert_if_not_exists(u64 x)
{
if (!container.member(x))
container.insert(x);
}
void del(u64 x)
{
container.del(x);
}
void del_if_exists(u64 x)
{
if (container.member(x))
container.del(x);
}
std::optional<u64> succ(u64 x) const
{
u64 s = container.succ(x);
if (detail::is_null(s)) return {};
return s;
}
std::optional<u64> pred(u64 x) const
{
u64 s = container.pred(x);
if (detail::is_null(s)) return {};
return s;
}
void renew_key(u64 x_old, u64 x_new)
{
container.renew_key(x_old, x_new);
}
std::optional<u64> remove_min()
{
u64 min = container.min;
if (detail::is_null(min)) return {};
container.del(min);
return min;
}
bool empty() const
{
return container.empty();
}
bool member(u64 x) const
{
return container.member(x);
}
private:
detail::veb<log2_u, shrink_to_fit> container;
};
// TODO
template<uint log2_u>
class veb_prio
{
};
}