-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN4.cpp
109 lines (97 loc) · 2.97 KB
/
N4.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
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <assert.h>
#include <algorithm>
#include "N.h"
namespace ART_OLC {
void N4::deleteChildren() {
for (uint32_t i = 0; i < count; ++i) {
N::deleteChildren(children[i]);
N::deleteNode(children[i]);
}
}
bool N4::isFull() const {
return count == 4;
}
bool N4::isUnderfull() const {
return false;
}
void N4::insert(uint8_t key, N *n) {
unsigned pos;
for (pos = 0; (pos < count) && (keys[pos] < key); pos++);
memmove(keys + pos + 1, keys + pos, count - pos);
memmove(children + pos + 1, children + pos, (count - pos) * sizeof(N*));
keys[pos] = key;
children[pos] = n;
count++;
}
template<class NODE>
void N4::copyTo(NODE *n) const {
for (uint32_t i = 0; i < count; ++i) {
n->insert(keys[i], children[i]);
}
}
bool N4::change(uint8_t key, N *val) {
for (uint32_t i = 0; i < count; ++i) {
if (keys[i] == key) {
children[i] = val;
return true;
}
}
assert(false);
__builtin_unreachable();
}
N *N4::getChild(const uint8_t k) const {
for (uint32_t i = 0; i < count; ++i) {
if (keys[i] == k) {
return children[i];
}
}
return nullptr;
}
void N4::remove(uint8_t k) {
for (uint32_t i = 0; i < count; ++i) {
if (keys[i] == k) {
memmove(keys + i, keys + i + 1, count - i - 1);
memmove(children + i, children + i + 1, (count - i - 1) * sizeof(N *));
count--;
return;
}
}
}
N *N4::getAnyChild() const {
N *anyChild = nullptr;
for (uint32_t i = 0; i < count; ++i) {
if (N::isLeaf(children[i])) {
return children[i];
} else {
anyChild = children[i];
}
}
return anyChild;
}
std::tuple<N *, uint8_t> N4::getSecondChild(const uint8_t key) const {
for (uint32_t i = 0; i < count; ++i) {
if (keys[i] != key) {
return std::make_tuple(children[i], keys[i]);
}
}
return std::make_tuple(nullptr, 0);
}
uint64_t N4::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 (uint32_t i = 0; i < count; ++i) {
if (this->keys[i] >= start && this->keys[i] <= end) {
children[childrenCount] = std::make_tuple(this->keys[i], this->children[i]);
childrenCount++;
}
}
readUnlockOrRestart(v, needRestart);
if (needRestart) goto restart;
return v;
}
}