-
Notifications
You must be signed in to change notification settings - Fork 23
/
skiplist.c
338 lines (280 loc) · 7.44 KB
/
skiplist.c
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
/*
* Copyright (c) 2012 - 2013, Jackson Lie <liexusong@qq.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include "skiplist.h"
#ifdef compEQ
#undef compEQ
#endif
#define compEQ(a, b) (a == b)
#ifdef zmalloc
#undef zmalloc
#endif
#ifdef zfree
#undef zfree
#endif
#define zmalloc(s) malloc(s)
#define zfree(p) free(p)
#define MAXLEVEL 32
static inline int
mx_skiplist_max_comp(int a, int b) {
return a > b;
}
static inline int
mx_skiplist_min_comp(int a, int b) {
return a < b;
}
/**
* Insert new node into skiplist
* @param list, SkipList object
* @param key, the index key
* @param rec, the value
*/
int mx_skiplist_insert(mx_skiplist_t *list, int key, void *rec)
{
int i, newLevel;
mx_skiplist_node_t *update[MAXLEVEL+1];
mx_skiplist_node_t *x;
x = list->root;
for (i = list->level; i >= 0; i--) {
while (x->forward[i] != list->root &&
list->cmp(x->forward[i]->key, key))
x = x->forward[i];
update[i] = x;
}
for (newLevel = 0;
rand() < (RAND_MAX / 2) && newLevel < MAXLEVEL;
newLevel++) /* void */ ;
if (newLevel > list->level) {
for (i = list->level + 1; i <= newLevel; i++)
update[i] = list->root; /* update root node's forwards */
list->level = newLevel;
}
if ((x = zmalloc(sizeof(mx_skiplist_node_t) + newLevel * sizeof(mx_skiplist_node_t *))) == 0)
return SKL_STATUS_MEM_EXHAUSTED; /* not enough memory */
x->key = key;
x->rec = rec;
for (i = 0; i <= newLevel; i++) {
x->forward[i] = update[i]->forward[i];
update[i]->forward[i] = x;
}
list->size++;
return SKL_STATUS_OK;
}
/**
* Get the min node of skiplist
*/
int mx_skiplist_find_top(mx_skiplist_t *list, void **rec)
{
if (list->root->forward[0] == list->root) /* empty */
return SKL_STATUS_KEY_NOT_FOUND;
*rec = list->root->forward[0]->rec;
return SKL_STATUS_OK;
}
/**
* Delete the min node
*/
void mx_skiplist_delete_top(mx_skiplist_t *list)
{
mx_skiplist_node_t *node;
int i, newLevel;
/* skiplist empty */
if (list->root->forward[0] == list->root) return;
node = list->root->forward[0]; /* first node */
for (i = 0; i <= list->level; i++) {
if (list->root->forward[i] == node) {
list->root->forward[i] = node->forward[i];
} else {
break;
}
}
while ((list->level > 0) &&
(list->root->forward[list->level] == list->root))
{
list->level--;
}
list->size--;
zfree(node);
}
/**
* Find the first record by key
*/
int mx_skiplist_find_key(mx_skiplist_t *list, int key, void **rec)
{
int i;
mx_skiplist_node_t *x = list->root;
for (i = list->level; i >= 0; i--) {
while (x->forward[i] != list->root
&& list->cmp(x->forward[i]->key, key))
x = x->forward[i];
}
x = x->forward[0];
if (x != list->root && compEQ(x->key, key)) {
*rec = x->rec;
return SKL_STATUS_OK;
}
return SKL_STATUS_KEY_NOT_FOUND;
}
/**
* Delete the first node by key
*/
int mx_skiplist_delete_key(mx_skiplist_t *list, int key, void **rec)
{
int i;
mx_skiplist_node_t *update[MAXLEVEL+1], *x;
x = list->root;
for (i = list->level; i >= 0; i--) {
while (x->forward[i] != list->root
&& list->cmp(x->forward[i]->key, key))
x = x->forward[i];
update[i] = x;
}
x = x->forward[0];
if (x == list->root || !compEQ(x->key, key))
return SKL_STATUS_KEY_NOT_FOUND;
for (i = 0; i <= list->level; i++) {
if (update[i]->forward[i] != x) break;
update[i]->forward[i] = x->forward[i];
}
if (rec) *rec = x->rec;
zfree(x);
while ((list->level > 0) &&
(list->root->forward[list->level] == list->root))
{
list->level--;
}
list->size--;
return SKL_STATUS_OK;
}
/**
* Find the first node of the key
*/
int mx_skiplist_find_node(mx_skiplist_t *list, int key, mx_skiplist_node_t **node)
{
int i;
mx_skiplist_node_t *x = list->root;
for (i = list->level; i >= 0; i--) {
while (x->forward[i] != list->root
&& list->cmp(x->forward[i]->key, key))
x = x->forward[i];
}
x = x->forward[0];
if (x != list->root && compEQ(x->key, key)) {
*node = x;
return SKL_STATUS_OK;
}
return SKL_STATUS_KEY_NOT_FOUND;
}
/**
* Get iterator by key
*/
int mx_skiplist_get_iterator(mx_skiplist_t *list,
mx_skiplist_iterator_t *iterator, int key, int limit)
{
mx_skiplist_node_t *node;
int retval;
if ((retval = mx_skiplist_find_node(list, key, &node))
== SKL_STATUS_OK) {
iterator->list = list;
iterator->begin = node;
iterator->current = node;
iterator->limit = limit;
}
return retval;
}
/**
* Get skiplist level
*/
inline int mx_skiplist_level(mx_skiplist_t *list)
{
if (NULL != list)
return list->level;
return -1;
}
/**
* Get skiplist elements count
*/
inline int mx_skiplist_size(mx_skiplist_t *list)
{
if (NULL != list)
return list->size;
return -1;
}
/**
* Return the skiplist is empty
*/
inline int mx_skiplist_empty(mx_skiplist_t *list)
{
if (list != NULL && list->size <= 0)
return 1;
return 0;
}
/**
* Create new skiplist
*/
mx_skiplist_t *mx_skiplist_create(int type)
{
mx_skiplist_t *list;
mx_skiplist_comp_handler_t cmp;
int i;
switch (type) {
case MX_SKIPLIST_MAX_TYPE:
cmp = mx_skiplist_max_comp;
break;
case MX_SKIPLIST_MIN_TYPE:
cmp = mx_skiplist_min_comp;
break;
default:
return NULL;
}
list = zmalloc(sizeof(*list));
if (!list) {
return NULL;
}
if ((list->root = zmalloc(sizeof(mx_skiplist_node_t) +
MAXLEVEL * sizeof(mx_skiplist_node_t *))) == NULL)
{
zfree(list);
return NULL;
}
for (i = 0; i <= MAXLEVEL; i++) {
list->root->forward[i] = list->root; /* point to root */
}
list->cmp = cmp;
list->level = 0;
list->size = 0;
return list;
}
/*
* skiplist destroy function
*/
void mx_skiplist_destroy(mx_skiplist_t *list,
mx_skiplist_destroy_handler_t destroy_callback)
{
void *value;
while (!mx_skiplist_empty(list)) {
mx_skiplist_find_top(list, (void **)&value);
mx_skiplist_delete_top(list);
if (destroy_callback) {
destroy_callback(value);
}
}
zfree(list);
}
/* End of file */