You wanna see the dict? (skiplist)

  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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#define ALLOW_REINSERT
//#define DEBUG
// Calculates log2 of number.
double Log2( double n )
{
// log(n)/log(2) is log2.
return log( n ) / log( 2 );
}
typedef struct ValueState {
int value;
unsigned char error_state;
} valuestate;
typedef struct SkipList {
char *key;
int value;
int m;
int *span;
struct SkipList **next;
} skiplist;
typedef struct SkipRanks {
int m;
skiplist **p;
int *ranks;
} skipranks;
skiplist *init(int size);
valuestate lookup(skiplist *list, char *key);
void assign(skiplist *list, char *key, int value);
skiplist **skip(skiplist *list, char *key);
skipranks skip_with_ranks(skiplist *list, char *key);
int insert(skiplist *list, char *key, int value);
int delete(skiplist *list, char *key);
skiplist *succ(skiplist *x);
void dump_skip_list(skiplist *list);
void free_skip_list(skiplist *list);
skiplist *init(int size) {
int i;
skiplist *list = (skiplist *)malloc(sizeof(skiplist) * size);
list->m = (int)ceil(Log2((double)size)) + 1;
list->span = (int *)malloc(sizeof(int) * list->m);
list->next = (skiplist **)malloc(sizeof(skiplist *) * list->m);
list->key = 0;
for (i = 0; i < list->m; i++) {
list->next[i] = NULL;
list->span[i] = 0;
}
return list;
}
valuestate lookup(skiplist *list, char *key) {
skiplist **p = skip(list, key);
skiplist *x = succ(p[0]);
free(p);
if (x == NULL || strcmp(x->key, key)) {
valuestate vs = {0, 1};
return vs;
} else {
valuestate vs = {x->value, 0};
return vs;
}
}
valuestate rank(skiplist *list, char *key) {
skiplist **p = (skiplist **)malloc(sizeof(skiplist *) * list->m);
skiplist *x = list;
int i;
int rank = 0;
for (i = list->m - 1; i >= 0; i--) {
while (x->next[i] != NULL && strcmp(key, x->next[i]->key) > 0) {
rank += x->span[i];
x = x->next[i];
}
p[i] = x;
}
x = succ(p[0]);
free(p);
if (x == NULL || strcmp(x->key, key)) {
valuestate vs = {0, 1};
return vs;
} else {
valuestate vs = {rank, 0};
return vs;
}
}
int insert(skiplist *list, char *key, int value) {
#ifdef DEBUG
char fence_line[] = "--------------------------------------------------";
printf("%s\n%s\n", fence_line, fence_line);
printf("Inserting \"%s\" key.\n", key);
dump_skip_list(list);
#endif
int error_state = 0;
skipranks sr = skip_with_ranks(list, key);
if (sr.p[0]->next[0] != NULL && !strcmp(sr.p[0]->next[0]->key, key)) {
#ifdef ALLOW_REINSERT
sr.p[0]->next[0]->value = value;
#else
printf("Wow! Such a fail! Reinsert forbidden!\n");
error_state = 1;
#endif
return error_state;
}
skiplist *x = (skiplist *)malloc(sizeof(skiplist));
x->value = value;
x->key = (char *)malloc(sizeof(char) * strlen(key));
strcpy(x->key, key);
x->span = (int *)malloc(sizeof(int) * list->m);
x->next = (skiplist **)malloc(sizeof(skiplist *) * list->m);
x->m = list->m;
int i;
for (i = 0; i < list->m; i++) {
x->next[i] = NULL;
x->span[i] = 0;
}
int x_rank = sr.ranks[0] + 1;
#ifdef DEBUG
printf("ranks:\n");
for (i = 0; i < list->m; i++)
printf("%d\t", sr.ranks[i]);
printf("\n");
printf(" rank: %d", x_rank);
#endif
i = 0;
long r = rand() * 2;
while (i < list->m && r%2 == 0) {
int delta = x_rank - sr.ranks[i];
if (sr.p[i]->next[i] == NULL) {
x->span[i] = 0;
} else {
if (sr.p[i]->span[i]) {
x->span[i] = sr.p[i]->span[i] - delta + 1;
}
}
sr.p[i]->span[i] = delta;
x->next[i] = sr.p[i]->next[i];
sr.p[i]->next[i] = x;
i++;
r >>= 2;
}
while (i < list->m) {
x->next[i] = NULL;
i++;
}
#ifdef DEBUG
printf("%s\n", fence_line);
printf("After inserting \"%s\" key.\n", key);
dump_skip_list(list);
printf("%s\n\n", fence_line);
#endif
free(sr.p);
free(sr.ranks);
return error_state;
}
int delete(skiplist *list, char *key) {
#ifdef DEBUG
char fence_line[] = "--------------------------------------------------";
printf("%s\n%s\n", fence_line, fence_line);
printf("Deleting \"%s\" key.\n", key);
dump_skip_list(list);
#endif
int error_state = 0;
skiplist **p = skip(list, key);
skiplist *x = succ(p[0]);
if (x == NULL || strcmp(x->key, key)) {
error_state = 1;
} else {
int i = 0;
for (i = 0; i < x->m; i++) {
p[i]->next[i] = x->next[i];
if (x->span[i]) {
p[i]->span[i] += x->span[i] - 1;
} else {
p[i]->span[i] = 0;
}
}
free(x->key);
free(x->next);
free(x->span);
free(x);
}
#ifdef DEBUG
printf("%s\n", fence_line);
printf("After deleting \"%s\" key.\n", key);
dump_skip_list(list);
printf("%s\n\n", fence_line);
#endif
free(p);
return error_state;
}
skipranks skip_with_ranks(skiplist *list, char *key) {
skipranks sr;
sr.m = list->m;
sr.p = (skiplist **)malloc(sizeof(skiplist *) * list->m);
sr.ranks = (int *)malloc(sizeof(int) * list->m);
skiplist *x = list;
int i;
for (i = list->m - 1; i >= 0; i--) {
int skip_rank = 0;
while (x->next[i] != NULL && strcmp(key, x->next[i]->key) > 0) {
skip_rank = x->span[i];
x = x->next[i];
sr.ranks[i] += skip_rank;
}
if (i < list->m - 1) {
sr.ranks[i] += sr.ranks[i+1];
}
sr.p[i] = x;
}
return sr;
}
skiplist **skip(skiplist *list, char *key) {
skiplist **p = (skiplist **)malloc(sizeof(skiplist *) * list->m);
skiplist *x = list;
int i;
for (i = list->m - 1; i >= 0; i--) {
while (x->next[i] != NULL && strcmp(key, x->next[i]->key) > 0) {
x = x->next[i];
}
p[i] = x;
}
return p;
}
void dump_skip_list(skiplist *list) {
char hole[] = "-------";
skiplist *x = list;
int length = 0;
while (x != NULL) {
length++;
x = x->next[0];
}
skiplist **items = (skiplist **)malloc(sizeof(skiplist *) * length);
x = list;
int i = 0;
while (x != NULL) {
items[i] = x;
x = x->next[0];
i++;
}
int j;
for (j = list->m - 1; j >= 0; j--) {
skiplist *current = list;
printf("(%-7s, v=%d, sp=%d) -> ", "<<init>>", list->value, list->span[j]);
for (i = 1; i < length; i++) {
char *filler;
int val = 0;
int span = 0;
if (current->next[j] == items[i]) {
current = items[i];
filler = items[i]->key;
val = items[i]->value;
span = items[i]->span[j];
} else {
filler = hole;
val = 0;
span = items[i]->span[j];
}
printf("(%-7s, v=%d, sp=%d) -> ", filler, val, span);
}
printf("NULL\n");
}
free(items);
}
void free_skip_list(skiplist *list) {
skiplist *next;
while(list != NULL) {
next = list->next[0];
if (list->key)
free(list->key);
free(list->next);
free(list->span);
free(list);
list = 0;
list = next;
}
}
skiplist *succ(skiplist *x) {
return x->next[0];
}
int ITEMS = 8;
int main(int argc, char const *argv[])
{
char *names[] = { "cabbage", "apple", "banana", "honey", "dessert", "fromage", "egg", "guava" };
int values[] = {0, 1, 2, 3, 4, 5, 6, 7};
int idx = 2;
if (argc == 2) {
idx = atoi(argv[1]);
}
skiplist *dict = init(ITEMS);
int i = 0;
//printf("1\n");
for (i = 0; i < ITEMS; i++)
insert(dict, names[i], values[i]);
//int idx = 1;
for (i = 0; i < ITEMS; i++) {
valuestate vs = lookup(dict, names[i]);
if (!vs.error_state) {
printf("Key \"%-7s\" exists! Here is value: %d\n", names[i], vs.value);
valuestate rs = rank(dict, names[i]);
if (!rs.error_state) {
printf("It's rank is %-7d\n", rs.value);
}
} else {
printf("Sorry, I can't find key \"%-12s\" in dict :(\n", names[i]);
}
}
dump_skip_list(dict);
int del_idx = 6;
printf("Trying to delete \"%s\" key:\n", names[del_idx]);
int err_state = delete(dict, names[del_idx]);
if (!err_state) {
printf("\"%s\" deleted.\n", names[del_idx]);
} else {
printf("There is no key \"%s\" in dict\n", names[del_idx]);
}
printf("\n Dict after \"%s\" key deleted:", names[del_idx]);
dump_skip_list(dict);
printf("\nDelete all keys (and one of them is missed):\n");
for (i = ITEMS - 1; i >= 0; i--) {
printf("Trying to delete \"%s\" key:\n", names[i]);
int err_state = delete(dict, names[i]);
if (!err_state) {
printf("\"%s\" deleted.\n", names[i]);
} else {
printf("There is no key \"%s\" in dict\n", names[i]);
}
}
printf("\n Dict aster all entries deleted.\n");
dump_skip_list(dict);
free_skip_list(dict);
return 0;
}