-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix.hpp
448 lines (330 loc) · 12 KB
/
radix.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#ifndef RADIX_HPP
#define RADIX_HPP
#include <array>
#include <memory>
#include <cassert>
#include <iostream>
// inner nodes
template<class T, std::size_t level, std::size_t B, std::size_t L>
struct node {
static_assert(level > 0, "specialization error");
static_assert(B > 0, "branching error");
static_assert(B * level + L <= 64, "level cannot be represented");
static constexpr std::size_t children_size = 1ul << B;
using ptr_type = std::shared_ptr<node>;
using child_type = node<T, level - 1, B, L>;
using child_ptr_type = std::shared_ptr<child_type>;
using children_type = std::array<child_ptr_type, children_size>;
children_type children;
node(const children_type& children={}): children(children) { }
static constexpr std::size_t shift = L + B * (level - 1);
static constexpr std::size_t capacity = (shift + B >= 64) ? 0 : (1ul << (shift + B));
static constexpr std::size_t mask = (children_size - 1ul) << shift;
T& ref(std::size_t index) {
assert(!capacity || index < capacity);
const std::size_t sub = (index & mask) >> shift;
auto& c = children[sub];
if(!c) {
c = std::make_shared<child_type>();
}
const std::size_t next = index & ~mask;
return c->ref(next);
}
friend ptr_type try_emplace(ptr_type self, std::size_t index, const T& value);
ptr_type set(std::size_t index, const T& value) const {
assert(!capacity || index < capacity);
const std::size_t sub = (index & mask) >> shift;
const std::size_t next = index & ~mask;
// TODO skip sub copy?
ptr_type res = std::make_shared<node>(children);
if(res->children[sub]) {
res->children[sub] = res->children[sub]->set(next, value);
} else {
// allocating new child node, emplace
res->children[sub] = try_emplace(std::make_shared<child_type>(), next, value);
}
return res;
}
friend ptr_type try_emplace(ptr_type self, std::size_t index, const T& value) {
assert(self);
assert(!capacity || index < capacity);
if(!self.unique()) {
return self->set(index, value);
}
// at this point we're the only ref to *self (but writes to a destructed
// ref may not have completed yet so still potential data race :/ FIXME)
const std::size_t sub = (index & mask) >> shift;
const std::size_t next = index & ~mask;
auto& c = self->children[sub];
if(!c) {
// allocate child + set ret
c = std::make_shared<child_type>();
c->ref(next) = value;
return self;
}
// keep trying to emplace on child
c = try_emplace(std::move(c), next, value);
return self;
}
template<class Func>
void iter(const Func& func) const {
for(const auto& it: children) {
if(!it) return;
it->iter(func);
}
}
};
// leaf nodes
template<class T, std::size_t B, std::size_t L>
struct node<T, 0, B, L> {
static_assert(L > 0, "branching error");
static constexpr std::size_t capacity = 1ul << L;
static constexpr std::size_t items_size = capacity;
using items_type = std::array<T, items_size>;
items_type items;
T& ref(std::size_t index) {
assert(index < items_size);
return items[index];
}
using ptr_type = std::shared_ptr<node>;
ptr_type set(std::size_t index, const T& value) const {
assert(index < items_size);
ptr_type res = std::make_shared<node>();
for(std::size_t i = 0; i < items_size; ++i) {
res->items[i] = (i == index) ? value : items[i];
}
return res;
}
friend ptr_type try_emplace(ptr_type self, std::size_t index, const T& value) {
assert(self);
assert(index < items_size);
if(!self.unique()) {
return self->set(index, value);
}
// at this point we're the only ref to *self (but writes to a destructed
// ref may not have completed yet so still potential data race FIXME)
self->items[index] = value;
return self;
}
template<class Func>
void iter(const Func& func) const {
for(const auto& it: items) {
func(it);
}
}
};
template<class T, std::size_t B=7, std::size_t L=B>
class map {
template<std::size_t level>
using node_type = node<T, level, B, L>;
using ptr_type = std::shared_ptr<void>;
ptr_type ptr;
std::size_t level;
template<std::size_t level>
static std::shared_ptr<node_type<level>> cast(ptr_type ptr) {
ptr_type local = std::move(ptr);
return std::static_pointer_cast<node_type<level>>(local);
}
template<class Ret, class Func, class ... Args>
static Ret visit(std::size_t level, ptr_type ptr,
const Func& func, Args&& ... args) {
// TODO function jump table?
switch(level) {
case 0: return func(cast<0>(std::move(ptr)), std::forward<Args>(args)...);
case 1: return func(cast<1>(std::move(ptr)), std::forward<Args>(args)...);
case 2: return func(cast<2>(std::move(ptr)), std::forward<Args>(args)...);
case 3: return func(cast<3>(std::move(ptr)), std::forward<Args>(args)...);
case 4: return func(cast<4>(std::move(ptr)), std::forward<Args>(args)...);
case 5: return func(cast<5>(std::move(ptr)), std::forward<Args>(args)...);
case 6: return func(cast<6>(std::move(ptr)), std::forward<Args>(args)...);
// case 7: return func(root<7>(), std::forward<Args>(args)...);
// case 8: return func(root<8>(), std::forward<Args>(args)...);
// case 9: return func(root<9>(), std::forward<Args>(args)...);
default:
throw std::logic_error("derp");
}
}
struct set_visitor {
template<std::size_t level>
map operator()(std::shared_ptr<node_type<level>> self,
std::size_t index,
const T& value) const {
if(index < node_type<level>::capacity) {
return {self->set(index, value)};
} else {
auto next = std::make_shared<node_type<level + 1>>();
next->children[0] = self;
return {try_emplace(std::move(next), index, value)};
}
}
};
struct emplace_visitor {
template<std::size_t level>
map operator()(std::shared_ptr<node_type<level>> self,
std::size_t index,
const T& value) const {
if(index < node_type<level>::capacity) {
return {try_emplace(std::move(self), index, value)};
} else {
auto next = std::make_shared<node_type<level + 1>>();
next->children[0] = self;
return {try_emplace(std::move(next), index, value)};
}
}
};
struct get_visitor {
template<std::size_t level>
const T& operator()(std::shared_ptr<node_type<level>> self,
std::size_t index) const {
assert(index < node_type<level>::capacity);
return self->ref(index);
}
};
template<std::size_t level>
map(std::shared_ptr<node_type<level>> ptr): ptr(ptr), level(level) { }
public:
map() { }
// TODO rvalue overload
map set(std::size_t index, const T& value) const & {
if(!ptr) {
auto ptr = std::make_shared<node_type<0>>();
return map(ptr).set(index, value);
}
return map::visit<map>(level, ptr, set_visitor(), index, value);
}
map set(std::size_t index, const T& value) && {
if(!ptr) {
auto ptr = std::make_shared<node_type<0>>();
return map(ptr).set(index, value);
}
return map::visit<map>(level, std::move(ptr), emplace_visitor(), index, value);
}
const T& get(std::size_t index) const {
assert(ptr);
return map::visit<const T&>(level, ptr, get_visitor(), index);
}
};
template<class T, std::size_t B=7, std::size_t L=B>
class vector {
static_assert((64 - L) % B == 0, "last level is not full");
template<std::size_t level>
using node_type = node<T, level, B, L>;
using ptr_type = std::shared_ptr<void>;
ptr_type ptr;
std::size_t level;
std::size_t count;
template<std::size_t level>
static std::shared_ptr<node_type<level>> cast(ptr_type ptr) {
ptr_type local = std::move(ptr);
// note: this takes a const ref
return std::static_pointer_cast<node_type<level>>(local);
}
template<class Ret, class Func, class ... Args>
static Ret visit(std::size_t level, ptr_type ptr,
const Func& func, Args&& ... args) {
// TODO function jump table?
switch(level) {
case 0: return func(cast<0>(std::move(ptr)), std::forward<Args>(args)...);
case 1: return func(cast<1>(std::move(ptr)), std::forward<Args>(args)...);
case 2: return func(cast<2>(std::move(ptr)), std::forward<Args>(args)...);
case 3: return func(cast<3>(std::move(ptr)), std::forward<Args>(args)...);
case 4: return func(cast<4>(std::move(ptr)), std::forward<Args>(args)...);
case 5: return func(cast<5>(std::move(ptr)), std::forward<Args>(args)...);
case 6: return func(cast<6>(std::move(ptr)), std::forward<Args>(args)...);
// case 7: return func(root<7>(), std::forward<Args>(args)...);
// case 8: return func(root<8>(), std::forward<Args>(args)...);
// case 9: return func(root<9>(), std::forward<Args>(args)...);
default:
throw std::logic_error("derp");
}
}
struct push_back_visitor {
template<std::size_t level>
vector operator()(std::shared_ptr<node_type<level>> self,
std::size_t size,
const T& value) const {
if(size == node_type<level>::capacity) {
auto root = std::make_shared<node_type<level + 1>>();
root->children[0] = std::move(self);
root->ref(size) = value;
return {root, level + 1, size + 1};
}
return {self->set(size, value), level, size + 1};
}
};
struct push_back_emplace_visitor {
template<std::size_t level>
vector operator()(std::shared_ptr<node_type<level>> self,
std::size_t size,
const T& value) const {
if(size == node_type<level>::capacity) {
// need to allocate a new level
auto root = std::make_shared<node_type<level + 1>>();
root->children[0] = std::move(self);
root->ref(size) = value;
return {root, level + 1, size + 1};
}
return {try_emplace(std::move(self), size, value), level, size + 1};
}
};
struct get_visitor {
template<std::size_t level>
const T& operator()(std::shared_ptr<node_type<level>> self,
std::size_t index) const {
return self->ref(index);
}
};
struct iter_visitor {
template<std::size_t level, class Func>
void operator()(std::shared_ptr<node_type<level>> self,
const Func& func) const {
return self->iter(func);
}
};
vector(ptr_type ptr, std::size_t level, std::size_t count):
ptr(std::move(ptr)),
level(level),
count(count) { }
public:
std::size_t size() const { return count; }
vector():
ptr(std::make_shared<node_type<0>>()),
level(0),
count(0) { };
vector push_back(const T& value) const & {
return visit<vector>(level, ptr,
push_back_visitor(), size(), value);
}
vector push_back(const T& value) && {
return visit<vector>(level, std::move(ptr),
push_back_emplace_visitor(), size(), value);
}
const T& operator[](std::size_t index) const & {
assert(index < size());
return visit<const T&>(level, ptr, get_visitor(), index);
}
template<class Func>
void iter(const Func& func) const {
return visit<void>(level, ptr, iter_visitor(), func);
}
// const T& get(std::size_t index) const & {
// assert(index < root->capacity());
// assert(index < size());
// return root->get(index);
// }
// vector set(std::size_t index, const T& value) const & {
// assert(index < root->capacity());
// assert(index < size());
// return {root->set(index, value), size()};
// }
// vector set(std::size_t index, const T& value) && {
// assert(index < root->capacity());
// assert(index < size());
// root->ref(index) = value;
// return {std::move(root), size()};
// }
// friend std::ostream& operator<<(std::ostream& out, const vector& self) {
// return out << *self.root;
// }
};
#endif