-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusion_tree.hpp
403 lines (327 loc) · 9.28 KB
/
fusion_tree.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
#ifndef FUSION_TREE_H
#define FUSION_TREE_H
#include <bitset>
#include <cmath>
#include <array>
#include <vector>
#include <cstdint>
/* KeyLength == log(u) where u is |U| - universe size
* See http://courses.csail.mit.edu/6.897/spring03/scribe_notes/L4/lecture4.pdf
* for conceptual notes.
* This implementation accepts KeyLength value up to 2^64.
*/
template< size_t KeyLength, class Value >
class fusion_tree_static
{
private:
template< size_t Size >
using bitset = std::bitset<Size>;
template< class T, size_t Size >
using array = std::array<T, Size>;
template< class T >
using vector = std::vector<T>;
public:
using Key = bitset<KeyLength>;
class iterator;
class const_iterator;
using reference = Value&;
using const_reference = const Value&;
fusion_tree_static() :
m_root(nullptr)
{}
~fusion_tree_static() {
if (m_root != nullptr) {
delete m_root;
}
}
const_reference at(const Key& key) const {
iterator it(m_root->findBucket(key));
return *it;
}
reference at(const Key& key) {
iterator it(m_root->findBucket(key));
return *it;
}
const_iterator cbegin() const {
return const_iterator(m_root ? findMinNode(m_root) : nullptr);
}
const_iterator begin() const {
return cbegin();
}
iterator begin() const {
return iterator(m_root ? findMinNode(m_root) : nullptr);
}
const_iterator cend() const {
return const_iterator(nullptr);
}
const_iterator end() const {
return cend();
}
iterator end() const {
return iterator(nullptr);
}
iterator insert(const Key& key, const Value& value) {
// TO DO:
}
const_iterator find(const Key& key) const {
if (empty() == true) {
return cend();
}
return const_iterator(m_root->find(key));
}
iterator find(const Key& key) {
if (empty() == true) {
return cend();
}
return iterator(m_root->find(key));
}
bool empty() const {
return m_root == nullptr;
}
private:
class Node;
class Node_Regular;
class Node_Leaf;
Node_Regular* m_root;
};
template< size_t KeyLength, class Value >
class fusion_tree_static<KeyLength, Value>::Node /*Abstract*/ {
public:
Node() = default;
virtual Node(const Node& other) = default;
virtual Node(Node&& other) = default;
virtual ~Node() = default;
virtual Node& operator=(const Node& other) = default;
virtual Node& operator=(Node&& other) = default;
virtual static bool isLeaf() const = 0;
};
template< size_t KeyLength, class Value >
class fusion_tree_static<KeyLength, Value>::Node_Regular : public Node {
private:
static constexpr size_t capacity = (size_t)std::pow(KeyLength, 1.0 / 5.0);
using Sketch = bitset<KeyLength>;
struct Children {
using BinHeap = bitset<capacity + (capacity - 1)>;
/* Represents a binary search tree as a binary heap.
* If Tree[x] == true then node x is exists.
*/
using Tree = BinHeap;
using Entry = std::pair<Key, Node*>;
using Values = vector<Entry>;
Tree tree;
Values values;
Children() :
values()
{
values.reserve(capacity);
}
~Children() {
for (Entry& e : values) {
if (e.second != nullptr) {
delete e.second;
}
}
}
const Entry& operator[](const size_t childIndex) const {
return values[childIndex];
}
Entry& operator[](const size_t childIndex) {
return values[childIndex];
}
size_t count() const {
return values.size();
}
};
public:
virtual ~Node_Regular() override = default;
virtual static bool isLeaf() const override {
return false;
};
void insert(const Key& key, const Value& value) {
Node* node = find(key);
if (node != nullptr) {
static_cast<Node_Leaf*>(node)->data() = value;
return node;
}
}
bool contains(const Key& key) const {
return find(key) != nullptr;
}
Node_Leaf* find(const Key& key) const {
if (m_children.count() == 0) {
return nullptr;
}
Children::Entry& entry = findBucket(key);
if (entry.second->isLeaf() == true) {
if (entry.first == key) {
return static_cast<Node_Leaf*>(entry.second);
} else {
return nullptr;
}
} else {
return entry.second->find(key);
}
}
private:
Sketch m_sketch;
Children m_children;
enum class Endianness : bool {
bigEndian = 0,
littleEndian = 1
};
static Endianness WORD_ORDER = Endianness::bigEndian;
static Endianness determineEndianness() {
volatile int num = 1; // disable compiler optimization
WORD_ORDER = static_cast<Endianness>(*(char *)&num == 1);
return WORD_ORDER;
}
/* Returns position of Most Significant Bit set in number.
*/
template< class Number >
inline static size_t findMSB(const Number& number) {
/*
The code loads a 64-bit (IEEE-754 floating-point) double with a 32-bit integer
(with no paddding bits) by storing the integer in the mantissa while the exponent is set to 2^52.
From this newly minted double, 2^52 (expressed as a double) is subtracted,
which sets the resulting exponent to the log base 2 of the input value, number.
All that is left is shifting the exponent bits into position (20 bits right) and
subtracting the bias, 0x3FF (which is 1023 decimal).
This technique only takes 5 operations, but many CPUs are slow at manipulating doubles,
and the endianess of the architecture must be accommodated.
*/
determineEndianness();
std::uint32_t u[2] = {0, 0};
double d;
u[WORD_ORDER == Endianness::littleEndian] = 0x43300000;
u[WORD_ORDER != Endianness::littleEndian] = static_cast<unsigned int>(number);
memcpy(&d, u, sizeof(double));
d -= 4503599627370496.0;
memcpy(u, &d, sizeof(double));
size_t result = (u[WORD_ORDER == Endianness::littleEndian] >> 20) - 0x3FF;
return result;
}
Children::Entry& findBucket(const Key& key) {
Key sketch;
computeKeySketch(key, sketch);
size_t blockLength = m_children.count() * m_children.count() * m_children.count() * m_children.count() + 1u;
unsigned long long replicatedSketch = 0;
for (size_t i = 0; i < m_children.count(); ++i) {
replicatedSketch += 1ull << (i * blockLength);
}
unsigned long long mask = 0;
for (size_t i = 0; i < m_children.count() - 1u; ++i) {
mask += 1ull << (i * blockLength + blockLength - 1u);
}
replicatedSketch *= static_cast<unsigned long long>(sketch);
replicatedSketch = static_cast<unsigned long long>(m_sketch) - replicatedSketch;
replicatedSketch &= mask;
size_t; childIndex = m_children.count() - 1;
if (replicatedSketch != 0) {
childIndex = findMSB(replicatedSketch);
}
return m_children[childIndex];
}
/* Computes a node sketch.
* Have to be called after every node modification.
* Complexity: O(capacity ^ 4)
*/
void computeNodeSketch() {
m_sketch.reset();
if (m_children.count() < 2u) {
return;
}
Key sketch;
for (size_t i = 0; i < m_children.count(); ++i) {
// Compute child's sketch
computeKeySketch(m_children[i].first, sketch);
m_sketch |= (sketch << i);
}
}
void computeKeySketch(const Key& key, Key& sketch) const {
Key branchingPoints;
const size_t branchingPointsCount = determineBranchingPoints(key, branchingPoints);
vector<size_t> branchingPointsPositions;
for (size_t i = 0; i < branchingPointsCount; ++i) {
if (branchingPoints[point] == true) {
branchingPointsPositions.push_back(i);
}
}
sketch = branchingPoints & key;
// Create magic number m
vector<bool> mParts((size_t)std::pow(branchingPointsCount, 3));
mParts[KeyLength - branchingPointsPositions[0]] = true;
size_t partsCount = 1;
for (size_t i = 1; i < branchingPointsCount; ++i) {
size_t j = 0;
size_t k = 0;
size_t l = 0;
while (j != branchingPointsCount) {
if (k == branchingPointsCount) { ++j; k = 0; l = 0; continue; }
if (l == partsCount) { ++k; l = 0; continue; }
size_t number = branchingPointsPositions[i] + mParts[l] - branchingPointsPositions[j];
if (mParts[number] == false) {
mParts[number] = true;
++partsCount;
}
++l;
}
for (auto it = mParts.begin(), iend = mParts.end(); it != iend; ++it) {
if (*it == false) {
*it = true;
break;
}
}
}
unsigned long long m = 0;
size_t minPower = (size_t)(-1u);
for (size_t i = 0, iend = mParts.size(), id = 0; i != iend; ++i) {
if (mParts[i] == true) {
const size_t m_ith = branchingPointsPositions[id] + (size_t)std::pow(mParts.size(), i);
if (m_ith < minPower) {
minPower = m_ith;
}
m += 1ull << (i + m_ith);
++id;
}
}
sketch = (static_cast<unsigned long long>(sketch) * m) >> minPower;
sketch.set(mParts.size());
}
size_t determineBranchingPoints(const Key& nodeKey, Key& branchingPoints) const {
size_t keyPos = 0;
size_t treePos = 0;
constexpr size_t nonLeafHeight = capacity - 1;
while (treePos < nonLeafHeight) {
const size_t sibling = treePos * 2 + (2 - nodeKey[keyPos]);
// There is a branching point if sibling node exists
branchingPoints.set(keyPos, m_children.tree[sibling]);
treePos *= 2;
treePos += 1 + nodeKey[keyPos];
++keyPos;
}
return count;
}
};
template< size_t KeyLength, class Value >
class fusion_tree_static<KeyLength, Value>::Node_Leaf : public Node {
public:
Node_Leaf(const Value& value) :
m_value(value)
{}
Node_Leaf(Value&& value) :
m_value(std::move(value))
{}
virtual ~Node_Leaf() override = default;
virtual static bool isLeaf() const override {
return true;
}
const Value& data() const {
return m_data;
}
Value& data() {
return m_data;
}
private:
Value m_data;
};
#endif // FUSION_TREE_H