forked from xjdrew/lua-zset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiplist.c
319 lines (273 loc) · 8.75 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
/*
* author: xjdrew
* date: 2014-06-03 20:38
*/
// skiplist similar with the version in redis
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "skiplist.h"
skiplistNode *slCreateNode(int level, double score, slobj *obj) {
skiplistNode *n = malloc(sizeof(*n) + level * sizeof(struct skiplistLevel));
n->score = score;
n->obj = obj;
return n;
}
skiplist *slCreate(void) {
int j;
skiplist *sl;
sl = malloc(sizeof(*sl));
sl->level = 1;
sl->length = 0;
sl->header = slCreateNode(SKIPLIST_MAXLEVEL, 0, NULL);
for (j=0; j < SKIPLIST_MAXLEVEL; j++) {
sl->header->level[j].forward = NULL;
sl->header->level[j].span = 0;
}
sl->header->backward = NULL;
sl->tail = NULL;
return sl;
}
slobj* slCreateObj(const char* ptr, size_t length) {
slobj *obj = malloc(sizeof(*obj));
obj->ptr = malloc(length + 1);
if(ptr) {
memcpy(obj->ptr, ptr, length);
}
obj->ptr[length] = '\0';
obj->length = length;
return obj;
}
void slFreeObj(slobj *obj) {
free(obj->ptr);
free(obj);
}
void slFreeNode(skiplistNode *node) {
slFreeObj(node->obj);
free(node);
}
void slFree(skiplist *sl) {
skiplistNode *node = sl->header->level[0].forward, *next;
free(sl->header);
while(node) {
next = node->level[0].forward;
slFreeNode(node);
node = next;
}
free(sl);
}
int slRandomLevel(void) {
int level = 1;
while((random() & 0xffff) < (SKIPLIST_P * 0xffff))
level += 1;
return (level < SKIPLIST_MAXLEVEL) ? level : SKIPLIST_MAXLEVEL;
}
int compareslObj(slobj *a, slobj *b) {
int cmp = memcmp(a->ptr, b->ptr, a->length <= b->length ? a->length : b->length);
if(cmp == 0) return a->length - b->length;
return cmp;
}
int equalslObj(slobj *a, slobj *b) {
return compareslObj(a, b) == 0;
}
void slInsert(skiplist *sl, double score, slobj *obj) {
skiplistNode *update[SKIPLIST_MAXLEVEL], *x;
unsigned int rank[SKIPLIST_MAXLEVEL];
int i, level;
x = sl->header;
for (i = sl->level-1; i >= 0; i--) {
/* store rank that is crossed to reach the insert position */
rank[i] = i == (sl->level-1) ? 0 : rank[i+1];
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
(x->level[i].forward->score == score &&
compareslObj(x->level[i].forward->obj,obj) < 0))) {
rank[i] += x->level[i].span;
x = x->level[i].forward;
}
update[i] = x;
}
/* we assume the key is not already inside, since we allow duplicated
* scores, and the re-insertion of score and redis object should never
* happen since the caller of slInsert() should test in the hash table
* if the element is already inside or not. */
level = slRandomLevel();
if (level > sl->level) {
for (i = sl->level; i < level; i++) {
rank[i] = 0;
update[i] = sl->header;
update[i]->level[i].span = sl->length;
}
sl->level = level;
}
x = slCreateNode(level,score,obj);
for (i = 0; i < level; i++) {
x->level[i].forward = update[i]->level[i].forward;
update[i]->level[i].forward = x;
/* update span covered by update[i] as x is inserted here */
x->level[i].span = update[i]->level[i].span - (rank[0] - rank[i]);
update[i]->level[i].span = (rank[0] - rank[i]) + 1;
}
/* increment span for untouched levels */
for (i = level; i < sl->level; i++) {
update[i]->level[i].span++;
}
x->backward = (update[0] == sl->header) ? NULL : update[0];
if (x->level[0].forward)
x->level[0].forward->backward = x;
else
sl->tail = x;
sl->length++;
}
/* Internal function used by slDelete, slDeleteByScore */
void slDeleteNode(skiplist *sl, skiplistNode *x, skiplistNode **update) {
int i;
for (i = 0; i < sl->level; i++) {
if (update[i]->level[i].forward == x) {
update[i]->level[i].span += x->level[i].span - 1;
update[i]->level[i].forward = x->level[i].forward;
} else {
update[i]->level[i].span -= 1;
}
}
if (x->level[0].forward) {
x->level[0].forward->backward = x->backward;
} else {
sl->tail = x->backward;
}
while(sl->level > 1 && sl->header->level[sl->level-1].forward == NULL)
sl->level--;
sl->length--;
}
/* Delete an element with matching score/object from the skiplist. */
int slDelete(skiplist *sl, double score, slobj *obj) {
skiplistNode *update[SKIPLIST_MAXLEVEL], *x;
int i;
x = sl->header;
for (i = sl->level-1; i >= 0; i--) {
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
(x->level[i].forward->score == score &&
compareslObj(x->level[i].forward->obj,obj) < 0)))
x = x->level[i].forward;
update[i] = x;
}
/* We may have multiple elements with the same score, what we need
* is to find the element with both the right score and object. */
x = x->level[0].forward;
if (x && score == x->score && equalslObj(x->obj,obj)) {
slDeleteNode(sl, x, update);
slFreeNode(x);
return 1;
} else {
return 0; /* not found */
}
return 0; /* not found */
}
/* Find the rank for an element by both score and key.
* Returns 0 when the element cannot be found, rank otherwise.
* Note that the rank is 1-based due to the span of sl->header to the
* first element. */
unsigned long slGetRank(skiplist *sl, double score, slobj *o) {
skiplistNode *x;
unsigned long rank = 0;
int i;
x = sl->header;
for (i = sl->level-1; i >= 0; i--) {
while (x->level[i].forward &&
(x->level[i].forward->score < score ||
(x->level[i].forward->score == score &&
compareslObj(x->level[i].forward->obj,o) <= 0))) {
rank += x->level[i].span;
x = x->level[i].forward;
}
/* x might be equal to sl->header, so test if obj is non-NULL */
if (x->obj && equalslObj(x->obj, o)) {
return rank;
}
}
return 0;
}
/* Finds an element by its rank. The rank argument needs to be 1-based. */
skiplistNode* slGetNodeByRank(skiplist *sl, unsigned long rank) {
if(rank == 0 || rank > sl->length) {
return NULL;
}
skiplistNode *x;
unsigned long traversed = 0;
int i;
x = sl->header;
for (i = sl->level-1; i >= 0; i--) {
while (x->level[i].forward && (traversed + x->level[i].span) <= rank)
{
traversed += x->level[i].span;
x = x->level[i].forward;
}
if (traversed == rank) {
return x;
}
}
return NULL;
}
/* range [min, max], left & right both include */
/* Returns if there is a part of the zset is in range. */
int slIsInRange(skiplist *sl, double min, double max) {
skiplistNode *x;
/* Test for ranges that will always be empty. */
if(min > max) {
return 0;
}
x = sl->tail;
if (x == NULL || x->score < min)
return 0;
x = sl->header->level[0].forward;
if (x == NULL || x->score > max)
return 0;
return 1;
}
/* Find the first node that is contained in the specified range.
* Returns NULL when no element is contained in the range. */
skiplistNode *slFirstInRange(skiplist *sl, double min, double max) {
skiplistNode *x;
int i;
/* If everything is out of range, return early. */
if (!slIsInRange(sl,min, max)) return NULL;
x = sl->header;
for (i = sl->level-1; i >= 0; i--) {
/* Go forward while *OUT* of range. */
while (x->level[i].forward && x->level[i].forward->score < min)
x = x->level[i].forward;
}
/* This is an inner range, so the next node cannot be NULL. */
x = x->level[0].forward;
return x;
}
/* Find the last node that is contained in the specified range.
* Returns NULL when no element is contained in the range. */
skiplistNode *slLastInRange(skiplist *sl, double min, double max) {
skiplistNode *x;
int i;
/* If everything is out of range, return early. */
if (!slIsInRange(sl, min, max)) return NULL;
x = sl->header;
for (i = sl->level-1; i >= 0; i--) {
/* Go forward while *IN* range. */
while (x->level[i].forward &&
x->level[i].forward->score <= max)
x = x->level[i].forward;
}
/* This is an inner range, so this node cannot be NULL. */
return x;
}
void slDump(skiplist *sl) {
skiplistNode *x;
int i;
x = sl->header;
i = 0;
while(x->level[0].forward) {
x = x->level[0].forward;
i++;
printf("node %d: score:%f, member:%s\n", i, x->score, x->obj->ptr);
}
}