forked from HiWong/SkipListPro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkipList.h
260 lines (209 loc) · 5.75 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
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
//
// Created by Wang Allen on 2018/2/6.
//
#ifndef SKIPLISTPRO_SKIPLIST_H
#define SKIPLISTPRO_SKIPLIST_H
#include <cstddef>
#include <cassert>
#include <ctime>
#include "Node.h"
#include "random.h"
using namespace std;
#define DEBUG
template<typename K, typename V>
class SkipList {
public:
SkipList(K footerKey) : rnd(0x12345678) {
createList(footerKey);
}
~SkipList() {
freeList();
}
//注意:这里要声明成Node<K,V>而不是Node,否则编译器会把它当成普通的类
Node<K, V> *search(K key) const;
bool insert(K key, V value);
bool remove(K key, V &value);
int size() {
return nodeCount;
}
int getLevel() {
return level;
}
private:
//初始化表
void createList(K footerKey);
//释放表
void freeList();
//创建一个新的结点,节点的层数为level
void createNode(int level, Node<K, V> *&node);
void createNode(int level, Node<K, V> *&node, K key, V value);
//随机生成一个level
int getRandomLevel();
void dumpAllNodes();
void dumpNodeDetail(Node<K, V> *node, int nodeLevel);
private:
int level;
Node<K, V> *header;
Node<K, V> *footer;
size_t nodeCount;
static const int MAX_LEVEL = 16;
Random rnd;
};
template<typename K, typename V>
void SkipList<K, V>::createList(K footerKey) {
createNode(0, footer);
footer->key = footerKey;
this->level = 0;
//设置头结
createNode(MAX_LEVEL, header);
for (int i = 0; i < MAX_LEVEL; ++i) {
header->forward[i] = footer;
}
nodeCount = 0;
}
template<typename K, typename V>
void SkipList<K, V>::createNode(int level, Node<K, V> *&node) {
node = new Node<K, V>(NULL, NULL);
//需要初始化数组
//注意:这里是level+1而不是level,因为数组是从0-level
node->forward = new Node<K, V> *[level + 1];
node->nodeLevel = level;
assert(node != NULL);
};
template<typename K, typename V>
void SkipList<K, V>::createNode(int level, Node<K, V> *&node, K key, V value) {
node = new Node<K, V>(key, value);
//需要初始化数组
if (level > 0) {
node->forward = new Node<K, V> *[level + 1];
}
node->nodeLevel = level;
assert(node != NULL);
};
template<typename K, typename V>
void SkipList<K, V>::freeList() {
Node<K, V> *p = header;
Node<K, V> *q;
while (p != NULL) {
q = p->forward[0];
delete p;
p = q;
}
delete p;
}
template<typename K, typename V>
Node<K, V> *SkipList<K, V>::search(const K key) const {
Node<K, V> *node = header;
for (int i = level; i >= 0; --i) {
while ((node->forward[i])->key < key) {
node = *(node->forward + i);
}
}
node = node->forward[0];
if (node->key == key) {
return node;
} else {
return nullptr;
}
};
template<typename K, typename V>
bool SkipList<K, V>::insert(K key, V value) {
Node<K, V> *update[MAX_LEVEL];
Node<K, V> *node = header;
for (int i = level; i >= 0; --i) {
while ((node->forward[i])->key < key) {
node = node->forward[i];
}
update[i] = node;
}
//首个结点插入时,node->forward[0]其实就是footer
node = node->forward[0];
//如果key已存在,则直接返回false
if (node->key == key) {
return false;
}
int nodeLevel = getRandomLevel();
if (nodeLevel > level) {
nodeLevel = ++level;
update[nodeLevel] = header;
}
//创建新结点
Node<K, V> *newNode;
createNode(nodeLevel, newNode, key, value);
//调整forward指针
for (int i = nodeLevel; i >= 0; --i) {
node = update[i];
newNode->forward[i] = node->forward[i];
node->forward[i] = newNode;
}
++nodeCount;
#ifdef DEBUG
dumpAllNodes();
#endif
return true;
};
template<typename K, typename V>
void SkipList<K, V>::dumpAllNodes() {
Node<K, V> *tmp = header;
while (tmp->forward[0] != footer) {
tmp = tmp->forward[0];
dumpNodeDetail(tmp, tmp->nodeLevel);
cout << "----------------------------" << endl;
}
cout << endl;
}
template<typename K, typename V>
void SkipList<K, V>::dumpNodeDetail(Node<K, V> *node, int nodeLevel) {
if (node == nullptr) {
return;
}
cout << "node->key:" << node->key << ",node->value:" << node->value << endl;
//注意是i<=nodeLevel而不是i<nodeLevel
for (int i = 0; i <= nodeLevel; ++i) {
cout << "forward[" << i << "]:" << "key:" << node->forward[i]->key << ",value:" << node->forward[i]->value
<< endl;
}
}
template<typename K, typename V>
bool SkipList<K, V>::remove(K key, V &value) {
Node<K, V> *update[MAX_LEVEL];
Node<K, V> *node = header;
for (int i = level; i >= 0; --i) {
while ((node->forward[i])->key < key) {
node = node->forward[i];
}
update[i] = node;
}
node = node->forward[0];
//如果结点不存在就返回false
if (node->key != key) {
return false;
}
value = node->value;
for (int i = 0; i <= level; ++i) {
if (update[i]->forward[i] != node) {
break;
}
update[i]->forward[i] = node->forward[i];
}
//释放结点
delete node;
//更新level的值,因为有可能在移除一个结点之后,level值会发生变化,及时移除可避免造成空间浪费
while (level > 0 && header->forward[level] == footer) {
--level;
}
--nodeCount;
#ifdef DEBUG
dumpAllNodes();
#endif
return true;
};
template<typename K, typename V>
int SkipList<K, V>::getRandomLevel() {
int level = static_cast<int>(rnd.Uniform(MAX_LEVEL));
if (level == 0) {
level = 1;
}
return level;
}
#endif //SKIPLISTPRO_SKIPLIST_H