-
Notifications
You must be signed in to change notification settings - Fork 0
/
N48.cpp
96 lines (85 loc) · 2.59 KB
/
N48.cpp
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
#include <assert.h>
#include <algorithm>
#include "N.h"
namespace ART_OLC {
bool N48::isFull() const {
return count == 48;
}
bool N48::isUnderfull() const {
return count == 12;
}
void N48::insert(uint8_t key, N *n) {
unsigned pos = count;
if (children[pos]) {
for (pos = 0; children[pos] != nullptr; pos++);
}
children[pos] = n;
childIndex[key] = (uint8_t) pos;
count++;
}
template<class NODE>
void N48::copyTo(NODE *n) const {
for (unsigned i = 0; i < 256; i++) {
if (childIndex[i] != emptyMarker) {
n->insert(i, children[childIndex[i]]);
}
}
}
bool N48::change(uint8_t key, N *val) {
children[childIndex[key]] = val;
return true;
}
N *N48::getChild(const uint8_t k) const {
if (childIndex[k] == emptyMarker) {
return nullptr;
} else {
return children[childIndex[k]];
}
}
void N48::remove(uint8_t k) {
assert(childIndex[k] != emptyMarker);
children[childIndex[k]] = nullptr;
childIndex[k] = emptyMarker;
count--;
assert(getChild(k) == nullptr);
}
N *N48::getAnyChild() const {
N *anyChild = nullptr;
for (unsigned i = 0; i < 256; i++) {
if (childIndex[i] != emptyMarker) {
if (N::isLeaf(children[childIndex[i]])) {
return children[childIndex[i]];
} else {
anyChild = children[childIndex[i]];
};
}
}
return anyChild;
}
void N48::deleteChildren() {
for (unsigned i = 0; i < 256; i++) {
if (childIndex[i] != emptyMarker) {
N::deleteChildren(children[childIndex[i]]);
N::deleteNode(children[childIndex[i]]);
}
}
}
uint64_t N48::getChildren(uint8_t start, uint8_t end, std::tuple<uint8_t, N *> *&children,
uint32_t &childrenCount) const {
restart:
bool needRestart = false;
uint64_t v;
v = readLockOrRestart(needRestart);
if (needRestart) goto restart;
childrenCount = 0;
for (unsigned i = start; i <= end; i++) {
if (this->childIndex[i] != emptyMarker) {
children[childrenCount] = std::make_tuple(i, this->children[this->childIndex[i]]);
childrenCount++;
}
}
readUnlockOrRestart(v, needRestart);
if (needRestart) goto restart;
return v;
}
}