-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskiplist.h
371 lines (320 loc) · 10.1 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#ifndef _SKIPLIST_H_
#define _SKIPLIST_H_
#include <functional>
#include <cstdlib>
#include <utility>
#include <cstring>
#include <ctime>
#include "mempool.h"
namespace fcontainer {
typedef unsigned lev_t, siz_t;
template <typename _Key, typename _Cmp = std::less<_Key>>
class skiplist {
// typedef _Key key_t;
// typedef key_t* key_p;
// typedef key_t& key_r;
protected:
struct node {
_Key key;
lev_t node_lev;
off_t pre[skiplist::MAX_LEV];
off_t nex[skiplist::MAX_LEV];
};
MemoryPool<node> mp;
_Cmp comp;
lev_t list_lev;
off_t head;
off_t tail;
siz_t cnt;
public:
const static float RAND_PROB;
const static lev_t MAX_LEV;
class iterator {
friend class skiplist;
MemoryPool<node>* mp;
off_t nod;
public:
iterator() = default;
iterator(MemoryPool<node> *_mp, off_t _nod = 0L);
operator bool() const;
_Key operator * () const;
bool operator == (const iterator &o) const;
bool operator != (const iterator &o) const;
iterator& operator ++ ();
iterator& operator -- ();
iterator operator ++ (int);
iterator operator -- (int);
~iterator() = default;
};
protected:
lev_t getRandLev();
_Key getNodeKey(off_t nod);
void getNodeKey(off_t nod, _Key &nkey);
void putNodeKey(off_t nod, const _Key &nkey);
lev_t getNodeLev(off_t nod);
void putNodeLev(off_t nod, lev_t lev);
off_t getNodePre(off_t nod, siz_t pos = 0);
off_t getNodeNex(off_t nod, siz_t pos = 0);
void putNodePre(off_t nod, siz_t pos, off_t val);
void putNodeNex(off_t nod, siz_t pos, off_t val);
public:
skiplist(const std::string &_name);
skiplist(const skiplist &o) = delete;
siz_t size();
void clear();
iterator begin();
iterator end();
iterator rbegin();
iterator rend();
iterator lower_bound(const _Key &nkey);
iterator find(const _Key &nkey);
iterator insert(const _Key &nkey);
void erase(const _Key &nkey);
void erase(const iterator it);
~skiplist() = default;
};
template <typename _Key, typename _Cmp>
const lev_t skiplist<_Key, _Cmp>::MAX_LEV = 16u;
template <typename _Key, typename _Cmp>
const float skiplist<_Key, _Cmp>::RAND_PROB = 0.25f;
template <typename _Key, typename _Cmp>
skiplist<_Key, _Cmp>::iterator::iterator(MemoryPool<node> *_mp, off_t _nod): mp(_mp), nod(_nod) {}
template <typename _Key, typename _Cmp>
skiplist<_Key, _Cmp>::iterator::operator bool() const {return nod; }
template <typename _Key, typename _Cmp>
_Key skiplist<_Key, _Cmp>::iterator::operator * () const {
_Key tmp;
mp->read(tmp, nod + offsetof(node, key));
return tmp;
}
template <typename _Key, typename _Cmp>
bool skiplist<_Key, _Cmp>::iterator::operator == (const iterator &o) const {
return mp == o.mp && nod == o.nod;
}
template <typename _Key, typename _Cmp>
bool skiplist<_Key, _Cmp>::iterator::operator != (const iterator &o) const {
return mp != o.mp || nod != o.nod;
}
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator&
skiplist<_Key, _Cmp>::iterator::operator ++ () {
off_t nex;
mp->read(nex, nod + offsetof(node, nex)); nod = nex;
return *this;
}
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::iterator::operator ++ (int) {
iterator tmp(*this);
off_t nex;
mp->read(nex, nod + offsetof(node, nex)); nod = nex;
return tmp;
}
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator&
skiplist<_Key, _Cmp>::iterator::operator -- () {
off_t nex;
mp->read(nex, nod + offsetof(node, pre)); nod = nex;
return *this;
}
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::iterator::operator -- (int) {
iterator tmp(*this);
off_t nex;
mp->read(nex, nod + offsetof(node, pre)); nod = nex;
return tmp;
}
template <typename _Key, typename _Cmp>
skiplist<_Key, _Cmp>::skiplist(const std::string &_name): mp(_name + ".bin") {
mp.getHead(head);
mp.getTail(tail);
if(!head) {
head = tail = mp.malloc();
mp.write(node(), head);
mp.putHead(head);
mp.putTail(tail);
}
list_lev = 1;
srand(time(0));
}
template <typename _Key, typename _Cmp>
lev_t skiplist<_Key, _Cmp>::getRandLev() {
lev_t lev = 1;
while(lev < MAX_LEV && rand() < RAND_PROB * RAND_MAX) lev++;
return lev;
}
template <typename _Key, typename _Cmp>
_Key skiplist<_Key, _Cmp>::getNodeKey(off_t nod) {
_Key tmp;
mp.read(tmp, nod + offsetof(node, key));
return tmp;
}
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::getNodeKey(off_t nod, _Key &nkey) {
mp.read(nkey, nod + offsetof(node, key));
}
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::putNodeKey(off_t nod, const _Key &nkey) {
mp.write(nkey, nod + offsetof(node, key));
}
template <typename _Key, typename _Cmp>
lev_t skiplist<_Key, _Cmp>::getNodeLev(off_t nod) {
lev_t tmp;
mp.read(tmp, nod + offsetof(node, node_lev));
return tmp;
}
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::putNodeLev(off_t nod, lev_t lev) {
mp.write(lev, nod + offsetof(node, node_lev));
}
template <typename _Key, typename _Cmp>
off_t skiplist<_Key, _Cmp>::getNodePre(off_t nod, siz_t pos) {
off_t tmp;
mp.read(tmp, nod + offsetof(node, pre) + pos * sizeof(off_t));
return tmp;
}
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::putNodePre(off_t nod, siz_t pos, off_t val) {
mp.write(val, nod + offsetof(node, pre) + pos * sizeof(off_t));
}
template <typename _Key, typename _Cmp>
off_t skiplist<_Key, _Cmp>::getNodeNex(off_t nod, siz_t pos) {
off_t tmp;
mp.read(tmp, nod + offsetof(node, nex) + pos * sizeof(off_t));
return tmp;
}
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::putNodeNex(off_t nod, siz_t pos, off_t val) {
mp.write(val, nod + offsetof(node, nex) + pos * sizeof(off_t));
}
template <typename _Key, typename _Cmp>
siz_t skiplist<_Key, _Cmp>::size() {return cnt; }
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::begin() {return iterator(&mp, getNodeNex(head)); }
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::end() {return iterator(&mp); }
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::rbegin() {return iterator(&mp, tail); }
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::rend() {return iterator(&mp); }
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::clear() {
mp.clear();
head = tail = mp.malloc();
mp.write(node(), head);
mp.putHead(head);
mp.putTail(tail);
list_lev = 1;
}
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::lower_bound(const _Key &nkey) {
off_t cur = head;
for(int i = list_lev - 1; i >= 0; --i) {
off_t nex;
while((nex = getNodeNex(cur, i)) &&
comp(getNodeKey(nex), nkey)) cur = nex;
}
return iterator(&mp, getNodeNex(cur));
}
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::find(const _Key &nkey) {
off_t cur = head;
for(int i = list_lev - 1; i >= 0; --i) {
off_t nex;
while((nex = getNodeNex(cur, i)) &&
comp(getNodeKey(nex), nkey)) cur = nex;
}
cur = getNodeNex(cur);
if(comp(nkey, getNodeKey(cur))) return iterator(&mp);
return iterator(&mp, cur);
}
template <typename _Key, typename _Cmp>
typename skiplist<_Key, _Cmp>::iterator
skiplist<_Key, _Cmp>::insert(const _Key &nkey) {
off_t cur = head;
off_t upd[MAX_LEV];
memset(upd, 0, sizeof(upd));
for(int i = list_lev - 1; i >= 0; --i) {
off_t nex;
while((nex = getNodeNex(cur, i)) &&
comp(getNodeKey(nex), nkey)) cur = nex;
upd[i] = cur;
}
cur = getNodeNex(cur);
if(cur && !comp(nkey, getNodeKey(cur))) return iterator(&mp);
lev_t new_lev = getRandLev();
off_t new_node = mp.malloc();
mp.write(node(), new_node);
putNodeKey(new_node, nkey);
putNodeLev(new_node, new_lev);
if(new_lev > list_lev) {
for(int i = list_lev; i < new_lev; ++i) upd[i] = head;
list_lev = new_lev;
}
for(int i = 0; i < new_lev; ++i) {
off_t nex = getNodeNex(upd[i], i);
putNodePre(new_node, i, upd[i]);
putNodeNex(new_node, i, nex);
if(nex) putNodePre(nex, i, new_node);
putNodeNex(upd[i], i, new_node);
}
if(tail == upd[0]) {
tail = new_node;
mp.putTail(tail);
}
cnt++;
return iterator(&mp, new_node);
}
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::erase(const _Key &nkey) {
off_t cur = head;
off_t upd[MAX_LEV];
memset(upd, 0, sizeof(upd));
for(int i = list_lev - 1; i >= 0; --i) {
off_t nex;
while((nex = getNodeNex(cur, i)) &&
comp(getNodeKey(nex), nkey)) cur = nex;
upd[i] = cur;
}
cur = getNodeNex(cur);
if(!cur || comp(nkey, getNodeKey(cur))) return ;
lev_t cur_lev = getNodeLev(cur);
for(int i = 0; i < cur_lev; ++i) {
off_t nex = getNodeNex(cur, i);
if(nex) putNodePre(nex, i, upd[i]);
putNodeNex(upd[i], i, nex);
}
if(cur == tail) {
tail = upd[0];
mp.putTail(tail);
}
mp.free(cur);
cnt--;
}
template <typename _Key, typename _Cmp>
void skiplist<_Key, _Cmp>::erase(const iterator it) {
off_t cur = it.nod;
if(!cur || cur == head) return ;
lev_t cur_lev = getNodeLev(cur);
for(int i = 0; i < cur_lev; ++i) {
off_t pre = getNodePre(cur, i);
off_t nex = getNodeNex(cur, i);
if(nex) putNodePre(nex, i, pre);
putNodeNex(pre, i, nex);
}
if(cur == tail) {
tail = getNodePre(tail);
mp.putTail(tail);
}
mp.free(cur);
cnt--;
}
}
#endif // _SKIPLIST_H_