-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkipList.cpp
152 lines (127 loc) · 3.34 KB
/
SkipList.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
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
#include "SkipList.h"
SkipList::Node::Node(uint64_t _key, string _value, Node *_right, Node *_down)
: right(_right), down(_down), key(_key), value(std::move(_value)) {}
SkipList::Node::Node() : right(nullptr), down(nullptr), key(0), value() {}
SkipList::SkipList()
: rd(), mt(rd()), head(new Node), length(0), valueSize(0) {}
void SkipList::put(uint64_t key, const string &value) {
// if key exists, update the value of all nodes
static auto update = [](Node *p, const string &value) {
while (p) {
p->value = value;
p = p->down;
}
};
// find the place to insert
stack<Node *> path;
Node *p = head;
while (p) {
while (p->right && p->right->key < key) p = p->right;
// if key exists
if (p->right && p->right->key == key) {
valueSize -= p->right->value.size();
valueSize += value.size();
update(p->right, value);
return;
}
path.push(p);
p = p->down;
}
// insert and grow up
bool grow = true;
Node *down = nullptr;
while (grow && !path.empty()) {
p = path.top();
path.pop();
p->right = new Node(key, value, p->right, down);
down = p->right;
grow = shouldGrowUp();
}
// height + 1
if (grow) {
p = head;
head = new Node;
head->right = new Node(key, value, nullptr, down);
head->down = p;
}
length++;
valueSize += value.size();
}
string SkipList::get(uint64_t key) const {
Node *p = head;
while (p) {
while (p->right && p->right->key < key) p = p->right;
if (p->right && p->right->key == key) return p->right->value;
p = p->down;
}
return "";
}
bool SkipList::remove(uint64_t key) {
// search the key
stack<Node *> path;
Node *p = head;
while (p) {
while (p->right && p->right->key < key) p = p->right;
path.push(p);
p = p->down;
}
// Does the key exist?
p = path.top()->right;
if (!p || p->key != key) return false;
length--;
assert(valueSize >= p->value.size());
valueSize -= p->value.size();
// remove the tower
while (!path.empty()) {
p = path.top();
path.pop();
Node *t = p->right;
if (!t || t->key != key) return true;
p->right = t->right;
delete t;
}
return true;
}
bool SkipList::shouldGrowUp() { return mt() & 1; }
void SkipList::reset() {
while (head) {
Node *t, *p = head;
head = head->down;
while (p) {
t = p->right;
delete p;
p = t;
}
}
head = new Node();
length = 0;
valueSize = 0;
}
SkipList::~SkipList() {
reset();
delete head;
}
SkipList::ConstIterator SkipList::constBegin() const {
Node *p = head;
while (p->down) p = p->down;
return SkipList::ConstIterator(p);
}
uint64_t SkipList::getMinKey() const {
Node *p = head;
while (p->down) p = p->down;
if (p->right) return p->right->key;
return 0;
}
uint64_t SkipList::getMaxKey() const {
Node *p = head;
while (p->down) p = p->down;
while (p->right) p = p->right;
return p->key;
}
unsigned long SkipList::getLength() const { return length; }
unsigned long SkipList::getValueSize() const { return valueSize; }
SkipList::ConstIterator::ConstIterator(SkipList::Node *_p) : p(_p) {}
const uint64_t &SkipList::ConstIterator::key() { return p->key; }
const string &SkipList::ConstIterator::value() { return p->value; }
bool SkipList::ConstIterator::hasNext() const { return p->right; }
void SkipList::ConstIterator::next() { p = p->right; }