/
skiplist.h
186 lines (179 loc) · 4.91 KB
/
skiplist.h
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
#pragma once
#include "/root/env/snippets/cpp/cpp_test_common.h"
#include <type_traits>
#ifdef DebugCount
static int64_t gSkipListCount = 0;
#endif
template <typename KeyT>
class SkipList {
static constexpr double _P = 0.5;
static constexpr int MAX_LEVEL = 32;
template <typename>
friend class SkipListDebuger;
public:
struct Node {
KeyT key;
struct Level {
Node* next;
} level[];
};
Node* newNode(const KeyT& key, int level) {
auto node = newNode(level);
node->key = key;
return node;
}
Node* newNode(int level) {
auto node = static_cast<Node*>(operator new(sizeof(Node) + level * sizeof(typename Node::Level)));
if constexpr (!std::is_pod_v<KeyT>) {
return new (node) Node;
}
return node;
}
SkipList() {
head_ = newNode(MAX_LEVEL);
for (auto i = 0;i < MAX_LEVEL;i++) {
head_->level[i].next = nullptr;
}
}
~SkipList() {
auto cur = head_->level[0].next;
while(cur) {
auto next = cur->level[0].next;
delete cur;
cur = next;
}
delete head_;
}
bool insert(const KeyT& key) {
Node* update[MAX_LEVEL];
auto cur = head_;
for (auto level = level_ - 1;level >= 0;level--) {
while(1) {
if (auto next = cur->level[level].next; next && next->key < key) {
cur = next;
}else {
if (next && next->key == key) {
return false;
}
break;
}
}
update[level] = cur;
}
auto level = getRandomLevel();
#ifdef DebugCount
switch (key) {
case 1: level = 2;break;
case 2: level = 1;break;
case 3: level = 1;break;
case 4: level = 3;break;
}
#endif
if (level > level_) {
for (auto i = level_; i < level; i++) {
update[i] = head_;
}
level_ = level;
}
cur = newNode(key, level);
for (auto i = 0;i < level; i++) {
cur->level[i].next = update[i]->level[i].next;
update[i]->level[i].next = cur;
}
return true;
}
bool erase(const KeyT& key) {
Node* update[MAX_LEVEL];
auto cur = head_;
for (auto level = level_ - 1;level >= 0;level--) {
while(1) {
if (auto next = cur->level[level].next; next && next->key < key) {
cur = next;
}else {
break;
}
}
update[level] = cur;
}
cur = cur->level[0].next;
if (cur && cur->key == key) {
for (auto level = 0;level < level_;level++) {
if (auto& next = update[level]->level[level].next; next == cur) {
next = cur->level[level].next;
}
}
while (level_ > 1 && head_->level[level_ - 1].next == nullptr) {
level_--;
}
delete cur;
return true;
}
return false;
}
Node* find(const KeyT& key) {
#ifdef DebugCount
int count = 0;
#endif
auto cur = head_;
for (int level = level_ - 1;level >= 0;level--) {
LDEBUG(LOGVT(level), LOGVT(key));
while(1) {
if (auto next = cur->level[level].next;
#ifdef DebugCount
++gSkipListCount && ++count && next && next->key <= key) {
#else
next && next->key <= key) {
#endif
LDEBUG("next element", LOGVT(next->key));
cur = next;
}else {
LDEBUG("next level", LOGVT(key));
break;
}
}
if (cur && cur->key == key) {
#ifdef DebugCount
LDEBUG("finish", LOGVT(key), LOGVT(count));
#endif
return cur;
}
}
return nullptr;
}
private:
int getRandomLevel() {
static constexpr int threshold = _P * RAND_MAX;
int level = 1;
while(rand() < threshold) {
level++;
}
return min(level, MAX_LEVEL);
}
Node* head_;
int level_ = 1;
};
template <typename KeyT>
class SkipListDebuger {
public:
SkipList<KeyT> t_;
using Node = typename SkipList<KeyT>::Node;
bool debugCheck(set<KeyT> &expectResult) {
set<KeyT> s;
Node* cur = t_.head_->level[0].next;
while(cur != nullptr) {
s.insert(cur->key);
cur = cur->level[0].next;
}
if (s == expectResult) {
return true;
}
return false;
}
void debugPrint() {
}
#ifdef DebugCount
int64_t getDebugCount() {
return gSkipListCount;
}
#endif
};