-
Notifications
You must be signed in to change notification settings - Fork 30
/
skip_list.c
212 lines (179 loc) · 5.45 KB
/
skip_list.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
/**
* @file skip_list.c
* @author hutusi ([email protected])
* @brief Refer to skip_list.h
* @date 2020-03-29
*
* @copyright Copyright (c) 2020, hutusi.com
*
*/
/**
* The implementaion of skip list reference to:
* https://www.geeksforgeeks.org/skip-list/
*
*/
#include "skip_list.h"
#include "def.h"
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define SKIP_LIST_NIL NULL
static SkipListNode *
skip_list_node_new(int level, SkipListKey key, SkipListValue value)
{
SkipListNode *node = (SkipListNode *)malloc(sizeof(SkipListNode));
node->key = key;
node->value = value;
node->next_array =
(SkipListNode **)malloc((level + 1) * sizeof(SkipListNode *));
for (int i = 0; i <= level; i++) {
node->next_array[i] = NULL;
}
return node;
}
void skip_list_free_node(SkipList *list, SkipListNode *node)
{
if (list->free_key_func && node->value) {
list->free_key_func(node->key);
}
if (list->free_value_func && node->value) {
list->free_value_func(node->value);
}
free(node->next_array);
free(node);
}
SkipList *skip_list_new(SkipListCompareFunc compare_func,
SkipListFreeKeyFunc free_key_func,
SkipListFreeValueFunc free_value_func)
{
SkipList *list = (SkipList *)malloc(sizeof(SkipList));
list->compare_func = compare_func;
list->free_key_func = free_key_func;
list->free_value_func = free_value_func;
list->level = 0;
list->head =
skip_list_node_new(SKIP_LIST_MAX_LEVEL, SKIP_LIST_NIL, SKIP_LIST_NIL);
return list;
}
void skip_list_free(SkipList *list)
{
SkipListNode *node = list->head;
while (node != NULL) {
SkipListNode *next = node->next_array[0];
skip_list_free_node(list, node);
node = next;
}
free(list);
}
static int skip_list_random_level(SkipList *list)
{
int level = 0;
while ((rand() & RAND_MAX) < (RAND_MAX / SKIP_LIST_RANDOM_FACTOR)) {
level++;
if (level >= SKIP_LIST_MAX_LEVEL)
break;
}
return level;
}
SkipListNode *
skip_list_insert(SkipList *list, SkipListKey key, SkipListValue value)
{
SkipListNode **updates = (SkipListNode **)malloc((SKIP_LIST_MAX_LEVEL + 1) *
sizeof(SkipListNode *));
for (int i = SKIP_LIST_MAX_LEVEL; i >= 0; i--) {
if (i == SKIP_LIST_MAX_LEVEL) {
updates[i] = list->head;
} else {
updates[i] = updates[i + 1];
}
SkipListNode *next = updates[i]->next_array[i];
while (next != NULL && list->compare_func(key, next->key) > 0) {
updates[i] = next;
next = next->next_array[i];
}
}
int level = skip_list_random_level(list);
SkipListNode *node = skip_list_node_new(level, key, value);
// update next_array on each level
for (int i = 0; i <= level; i++) {
node->next_array[i] = updates[i]->next_array[i];
updates[i]->next_array[i] = node;
}
if (level > list->level)
list->level = level;
free(updates);
return node;
}
SkipListNode *skip_list_remove_node(SkipList *list, SkipListKey key)
{
SkipListNode *prev = list->head;
for (int i = list->level; i >= 0; i--) {
SkipListNode *next = prev->next_array[i];
while (next != NULL) {
int cmp = list->compare_func(key, next->key);
if (cmp > 0) {
prev = next;
next = next->next_array[i];
} else if (cmp < 0) {
break;
} else {
SkipListNode **updates = (SkipListNode **)malloc(
(SKIP_LIST_MAX_LEVEL + 1) * sizeof(SkipListNode *));
SkipListNode *node = next;
updates[i] = prev;
for (int j = i - 1; j >= 0; j--) {
while (prev->next_array[j] != node)
prev = prev->next_array[j];
updates[j] = prev;
}
// update next_array on each level
for (int j = 0; j <= i; j++) {
updates[j]->next_array[j] = node->next_array[j];
}
for (int j = list->level; j >= 0; j--) {
if (list->head->next_array[j] == NULL) {
list->level--;
} else {
break;
}
}
free(updates);
return node;
}
}
}
return NULL;
}
SkipListValue skip_list_find(SkipList *list, SkipListKey key)
{
SkipListNode *prev = list->head;
for (int i = list->level; i >= 0; i--) {
SkipListNode *next = prev->next_array[i];
while (next != NULL) {
int cmp = list->compare_func(key, next->key);
if (cmp > 0) {
prev = next;
next = next->next_array[i];
} else if (cmp < 0) {
break;
} else {
return next->value;
}
}
}
return SKIP_LIST_NIL;
}
void skip_list_print(SkipList *list)
{
printf("\n Print Skip List: \n");
for (int i = 0; i <= list->level; i++) {
printf("Level %d: ", i);
SkipListNode *cursor = list->head->next_array[i];
while (cursor != NULL) {
printf("%4d", *(int *)cursor->key);
cursor = cursor->next_array[i];
}
printf("\n");
}
}