-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbasic_node.h
More file actions
349 lines (295 loc) · 10.5 KB
/
basic_node.h
File metadata and controls
349 lines (295 loc) · 10.5 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
#pragma once
#include "parlay/alloc.h"
#include "utils.h"
#include "basic_node_helpers.h"
#include "byte_encode.h"
#include "compression.h"
namespace cpam {
using node_size_t = unsigned int;
//using node_size_t = size_t;
// *******************************************
// BASIC NODE
// *******************************************
template<class balance_data, class _Entry, class EntryEncoder, size_t kBlockSize>
struct basic_node {
public:
using ET = _Entry;
using node = void;
using basic = basic_node<balance_data, _Entry, EntryEncoder, kBlockSize>;
static constexpr size_t kCompressionBlockSize = kBlockSize;
static constexpr size_t B = kCompressionBlockSize;
static constexpr size_t kBaseCaseSize = 8*B + 2;
static constexpr size_t kNodeLimit = 4*B;
static constexpr size_t kBlockSizeUpperBound = 2*B*sizeof(ET) + 3*sizeof(node_size_t);
struct regular_node : balance_data {
node_size_t r; // reference count, top-bit is always "1"
node_size_t s;
node* lc;
node* rc;
ET entry;
};
using allocator = parlay::type_allocator<regular_node>;
struct compressed_node {
node_size_t r; // reference count, top-bit is always "0"
node_size_t s; // number of entries used (size)
node_size_t size_in_bytes; // space allocated in bytes.
};
// Complex nodes have between B and 2B elements.
struct complex_bytes {
uint8_t arr[kBlockSizeUpperBound];};
using complex_allocator = parlay::type_allocator<complex_bytes>;
static bool is_complex(node* a) {
assert(is_regular(a));
return size(a) > 2*B; }
static bool is_simplex(node* a) {
return size(a) <= 2*B; }
static inline bool is_regular(node* a) {
return !a || ((regular_node*)a)->r & kTopBit; }
static inline bool is_compressed(node* a) { return !is_regular(a); }
// Useful if running in debug mode for catching bugs.
static regular_node* cast_to_regular(node* a) {
assert(is_regular(a));
return (regular_node*)a;
}
static compressed_node* cast_to_compressed(node* a) {
assert(is_compressed(a));
return (compressed_node*)a;
}
static regular_node* generic_node(node* a) {
return (regular_node*)a;
}
static node_size_t size(node* a) { return (a == NULL) ? 0 : generic_node(a)->s; }
static node_size_t ref_cnt(node* a) { return (a) ? generic_node(a)->r & kLowBitMask : 0; }
// Used by balance_utils
static void update(node* na) {
auto a = cast_to_regular(na);
a->s = size(a->lc) + size(a->rc) + 1; }
static regular_node* make_regular_node(const ET& e) {
regular_node* o = allocator::alloc();
o->r = 1;
o->r |= kTopBit;
parlay::assign_uninitialized(o->entry, e);
return o;
}
static regular_node* single(const ET& e) {
regular_node* r = make_regular_node(e);
r->lc = r->rc = NULL;
r->s = 1;
return r;
}
static node* empty() {return NULL;}
inline static ET& get_entry(node* a) { return cast_to_regular(a)->entry; }
inline static ET* get_entry_p(node* a) { return &(cast_to_regular(a)->entry); }
static void set_entry(node* a, ET e) { cast_to_regular(a)->entry = e; }
static constexpr node_size_t kTopBit = ((node_size_t)1) << (sizeof(node_size_t)*8-1);
static constexpr node_size_t kLowBitMask = kTopBit - ((node_size_t)1);
/* ========================== Compression ============================= */
template<typename F>
static void inplace_update(node* a, const F& f) {
assert(!is_regular(a));
auto c = cast_to_compressed(a);
uint8_t* data_start = (((uint8_t*)c) + 3*sizeof(node_size_t));
EntryEncoder::inplace_update(data_start, c->s, f);
}
template<typename F>
static void iterate_seq(node* a, const F& f) {
if (!a) return;
if (is_regular(a)) {
auto r = cast_to_regular(a);
iterate_seq(r->lc, f);
f(get_entry(r));
iterate_seq(r->rc, f);
} else {
auto c = cast_to_compressed(a);
uint8_t* data_start = (((uint8_t*)c) + 3*sizeof(node_size_t));
EntryEncoder::decode(data_start, c->s, f);
}
}
template<typename F>
static bool iterate_cond(node* a, const F& f) {
if (!a) return true;
if (is_regular(a)) {
auto r = cast_to_regular(a);
bool ret = iterate_cond(r->lc, f);
if (!ret) return ret;
ret = f(get_entry(r));
if (!ret) return ret;
ret = iterate_cond(r->rc, f);
return ret;
} else {
auto c = cast_to_compressed(a);
uint8_t* data_start = (((uint8_t*)c) + 3*sizeof(node_size_t));
return EntryEncoder::decode_cond(data_start, c->s, f);
}
}
template <class F, class Comp, class K>
static std::optional<ET> find_compressed(node* b, const F& f, const Comp& comp, const K& k) {
auto c = cast_to_compressed(b);
uint8_t* data_start = (((uint8_t*)c) + 3*sizeof(node_size_t));
return EntryEncoder::find(data_start, c->s, f, comp, k);
}
// Used by GC to copy a compressed node. TODO: update to work correctly with
// diff-encoding.
static node* make_compressed_node(node* b) {
return basic_node_helpers::make_compressed_node<basic>(b, B);
}
static bool check_compressed_node(node* a) {
auto c = cast_to_compressed(a);
assert(c);
assert(!is_regular(c));
assert(size(c) <= 2*B);
return (!is_regular(c)) && (size(c) <= 2*B);
}
static bool will_be_compressed(size_t sz) {
return (sz >= B) && (sz <= 2*B);
}
static bool will_be_compressed(node* l, node* r, node* join) {
assert(size(join) == 1);
size_t sz = size(l) + size(r) + size(join);
return will_be_compressed(sz);
}
static node* finalize(node* root) {
auto sz = size(root);
assert(sz > 0 || root == nullptr);
if (sz < B && sz > 0) {
auto ret = make_compressed_node(root);
decrement_recursive(root);
return ret;
}
return root;
}
// takes a pointer to an array of ETs, and a length of the number of ETs to
// construct, and returns a compressed node.
static compressed_node* make_single_compressed_node(ET* e, size_t s) {
assert(s <= 2*B);
size_t encoded_size = EntryEncoder::encoded_size(e, s);
size_t node_size = sizeof(compressed_node) + encoded_size;
compressed_node* c_node = (compressed_node*)utils::new_array_no_init<uint8_t>(node_size);
//compressed_node* c_node = (compressed_node*)complex_allocator::alloc();
c_node->r = 1;
c_node->s = s;
c_node->size_in_bytes = node_size;
uint8_t* encoded_data = (((uint8_t*)c_node) + sizeof(compressed_node));
EntryEncoder::encode(e, s, encoded_data);
check_compressed_node(c_node);
return c_node;
}
static node* make_compressed(node* l, node* r, regular_node* e) {
return basic_node_helpers::make_compressed<basic>(l, r, e, B);
}
static node* make_compressed(ET* stack, size_t tot) {
return basic_node_helpers::make_compressed<basic>(stack, tot, B);
}
static node* make_compressed(node* l, node* r) {
return make_compressed(l, r, nullptr);
}
// TODO: change return type to void.
static ET* compressed_node_elms(node* _c, ET* tmp_arr) {
assert(is_compressed(_c));
auto c = cast_to_compressed(_c);
uint8_t* data_start = (((uint8_t*)c) + 3*sizeof(node_size_t));
size_t i = 0;
auto f = [&] (const ET& et) {
parlay::assign_uninitialized(tmp_arr[i++], et);
};
EntryEncoder::decode(data_start, c->s, f);
return tmp_arr;
}
/* ================================ GC ================================== */
// Handles both regular and compressed nodes.
static void free_node(node* va) {
if (is_regular(va)) {
auto a = cast_to_regular(va);
(a->entry).~ET();
allocator::free(a);
} else {
auto c = cast_to_compressed(va);
uint8_t* data_start = (((uint8_t*)c) + 3*sizeof(node_size_t));
EntryEncoder::destroy(data_start, c->s);
auto array_size = c->size_in_bytes;
utils::free_array<uint8_t>((uint8_t*)va, array_size);
}
}
// precondition: a is non-null, and caller has a reference count on a.
static bool decrement_count(node* a) {
if ((utils::fetch_and_add(&generic_node(a)->r, -1) & kLowBitMask) == 1) {
free_node(a);
return true;
}
return false;
}
// precondition: a is non-null
static void increment_count(node* a, node_size_t i) {
utils::write_add(&(generic_node(a)->r), i);
}
// atomically decrement ref count and deletes node if zero
static bool decrement(node* t) {
if (t) {
return decrement_count(t);
}
return false;
}
// atomically decrement ref count and if zero:
// delete node and recursively decrement the two children
static void decrement_recursive(node* t) {
if (!t) return;
if (is_regular(t)) {
node* lsub = cast_to_regular(t)->lc;
node* rsub = cast_to_regular(t)->rc;
if (decrement(t)) {
utils::fork_no_result(size(lsub) >= kNodeLimit,
[&]() { decrement_recursive(lsub); },
[&]() { decrement_recursive(rsub); });
}
} else {
decrement(t);
}
}
/* ================================ Debug routines ================================ */
static void print_node_info(node* a, std::string node_name) {
// if (!a) { std::cout << "Empty node!" << std::endl; return; }
// if (is_regular(a)) {
// auto r = cast_to_regular(a);
// std::cout << "Node " << node_name << " is regular. Key = " << get_entry(a).first << " Size = " << size(a) << " left child = " << size(r->lc) << " right_child = " << size(r->rc) << std::endl;
// } else {
// auto c = cast_to_compressed(a);
// std::cout << "Node " << node_name << " is compressed. Size = " << c->s << std::endl;
// }
}
static void print_inorder_rec(node* a) {
if (!a) return;
if (is_regular(a)) {
auto r = cast_to_regular(a);
print_inorder_rec(r->lc);
std::cout << ". Size(" << size(a) << ") ";
print_inorder_rec(r->rc);
} else {
std::cout << "Compressed[" << size(a) << "] ";
}
}
static void print_inorder(node* a) {
if (a) {
print_inorder_rec(a);
} else {
std::cout << "Empty tree.";
}
std::cout << std::endl;
}
static void print_rec_impl(node* a) {
}
static void print_rec(node* a) {
if (a) {
print_rec_impl(a);
} else {
std::cout << "Empty tree.";
}
std::cout << std::endl;
}
// for two input sizes of n and m, should we do a parallel fork
// assumes work proportional to m log (n/m + 1)
static bool do_parallel(size_t n, size_t m) {
if (m > n) std::swap(n,m);
return (m > 8 && (m * parlay::log2_up(n/m + 1)) > kNodeLimit);
}
};
} // namespace cpam