1 /* xf86drmSL.c -- Skip list support
2 * Created: Mon May 10 09:28:13 1999 by faith@precisioninsight.com
3 * Revised: Thu Jun 3 16:13:01 1999 by faith@precisioninsight.com
5 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
27 * $XFree86: xc/programs/Xserver/hw/xfree86/os-support/linux/drm/xf86drmSL.c,v 1.2 2000/02/23 04:47:24 martin Exp $
31 * This file contains a straightforward skip list implementation.n
37 * [Pugh90] William Pugh. Skip Lists: A Probabilistic Alternative to
38 * Balanced Trees. CACM 33(6), June 1990, pp. 668-676.
47 # include <sys/time.h>
52 # include "xf86_ansic.h"
61 #define SL_LIST_MAGIC 0xfacade00LU
62 #define SL_ENTRY_MAGIC 0x00fab1edLU
63 #define SL_FREED_MAGIC 0xdecea5edLU
64 #define SL_MAX_LEVEL 16
66 #define SL_RANDOM_SEED 0xc01055a1LU
69 #define SL_ALLOC malloc
71 #define SL_RANDOM_DECL static int state = 0;
72 #define SL_RANDOM_INIT(seed) if (!state) { srandom(seed); ++state; }
73 #define SL_RANDOM random()
75 #define SL_ALLOC drmMalloc
76 #define SL_FREE drmFree
77 #define SL_RANDOM_DECL static void *state = NULL
78 #define SL_RANDOM_INIT(seed) if (!state) state = drmRandomCreate(seed)
79 #define SL_RANDOM drmRandom(state)
83 typedef struct SLEntry {
84 unsigned long magic; /* SL_ENTRY_MAGIC */
88 struct SLEntry *forward[1]; /* variable sized array */
89 } SLEntry, *SLEntryPtr;
91 typedef struct SkipList {
92 unsigned long magic; /* SL_LIST_MAGIC */
96 SLEntryPtr p0; /* Position for iteration */
97 } SkipList, *SkipListPtr;
100 extern void *N(SLCreate)(void);
101 extern int N(SLDestroy)(void *l);
102 extern int N(SLLookup)(void *l, unsigned long key, void **value);
103 extern int N(SLInsert)(void *l, unsigned long key, void *value);
104 extern int N(SLDelete)(void *l, unsigned long key);
105 extern int N(SLNext)(void *l, unsigned long *key, void **value);
106 extern int N(SLFirst)(void *l, unsigned long *key, void **value);
107 extern void N(SLDump)(void *l);
108 extern int N(SLLookupNeighbors)(void *l, unsigned long key,
109 unsigned long *prev_key, void **prev_value,
110 unsigned long *next_key, void **next_value);
113 static SLEntryPtr SLCreateEntry(int max_level, unsigned long key, void *value)
117 if (max_level < 0 || max_level > SL_MAX_LEVEL) max_level = SL_MAX_LEVEL;
119 entry = SL_ALLOC(sizeof(*entry)
120 + (max_level + 1) * sizeof(entry->forward[0]));
121 if (!entry) return NULL;
122 entry->magic = SL_ENTRY_MAGIC;
124 entry->value = value;
125 entry->levels = max_level + 1;
130 static int SLRandomLevel(void)
135 SL_RANDOM_INIT(SL_RANDOM_SEED);
137 while ((SL_RANDOM & 0x01) && level < SL_MAX_LEVEL) ++level;
141 void *N(SLCreate)(void)
146 list = SL_ALLOC(sizeof(*list));
147 if (!list) return NULL;
148 list->magic = SL_LIST_MAGIC;
150 list->head = SLCreateEntry(SL_MAX_LEVEL, 0, NULL);
153 for (i = 0; i <= SL_MAX_LEVEL; i++) list->head->forward[i] = NULL;
158 int N(SLDestroy)(void *l)
160 SkipListPtr list = (SkipListPtr)l;
164 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
166 for (entry = list->head; entry; entry = next) {
167 if (entry->magic != SL_ENTRY_MAGIC) return -1; /* Bad magic */
168 next = entry->forward[0];
169 entry->magic = SL_FREED_MAGIC;
173 list->magic = SL_FREED_MAGIC;
178 static SLEntryPtr SLLocate(void *l, unsigned long key, SLEntryPtr *update)
180 SkipListPtr list = (SkipListPtr)l;
184 if (list->magic != SL_LIST_MAGIC) return NULL;
186 for (i = list->level, entry = list->head; i >= 0; i--) {
187 while (entry->forward[i] && entry->forward[i]->key < key)
188 entry = entry->forward[i];
192 return entry->forward[0];
195 int N(SLInsert)(void *l, unsigned long key, void *value)
197 SkipListPtr list = (SkipListPtr)l;
199 SLEntryPtr update[SL_MAX_LEVEL + 1];
203 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
205 entry = SLLocate(list, key, update);
207 if (entry && entry->key == key) return 1; /* Already in list */
210 level = SLRandomLevel();
211 if (level > list->level) {
212 level = ++list->level;
213 update[level] = list->head;
216 entry = SLCreateEntry(level, key, value);
218 /* Fix up forward pointers */
219 for (i = 0; i <= level; i++) {
220 entry->forward[i] = update[i]->forward[i];
221 update[i]->forward[i] = entry;
225 return 0; /* Added to table */
228 int N(SLDelete)(void *l, unsigned long key)
230 SkipListPtr list = (SkipListPtr)l;
231 SLEntryPtr update[SL_MAX_LEVEL + 1];
235 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
237 entry = SLLocate(list, key, update);
239 if (!entry || entry->key != key) return 1; /* Not found */
241 /* Fix up forward pointers */
242 for (i = 0; i <= list->level; i++) {
243 if (update[i]->forward[i] == entry)
244 update[i]->forward[i] = entry->forward[i];
247 entry->magic = SL_FREED_MAGIC;
250 while (list->level && !list->head->forward[list->level]) --list->level;
255 int N(SLLookup)(void *l, unsigned long key, void **value)
257 SkipListPtr list = (SkipListPtr)l;
258 SLEntryPtr update[SL_MAX_LEVEL + 1];
261 entry = SLLocate(list, key, update);
263 if (entry && entry->key == key) {
271 int N(SLLookupNeighbors)(void *l, unsigned long key,
272 unsigned long *prev_key, void **prev_value,
273 unsigned long *next_key, void **next_value)
275 SkipListPtr list = (SkipListPtr)l;
276 SLEntryPtr update[SL_MAX_LEVEL + 1];
280 entry = SLLocate(list, key, update);
282 *prev_key = *next_key = key;
283 *prev_value = *next_value = NULL;
286 *prev_key = update[0]->key;
287 *prev_value = update[0]->value;
289 if (update[0]->forward[0]) {
290 *next_key = update[0]->forward[0]->key;
291 *next_value = update[0]->forward[0]->value;
298 int N(SLNext)(void *l, unsigned long *key, void **value)
300 SkipListPtr list = (SkipListPtr)l;
303 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
308 list->p0 = entry->forward[0];
310 *value = entry->value;
317 int N(SLFirst)(void *l, unsigned long *key, void **value)
319 SkipListPtr list = (SkipListPtr)l;
321 if (list->magic != SL_LIST_MAGIC) return -1; /* Bad magic */
323 list->p0 = list->head->forward[0];
324 return N(SLNext)(list, key, value);
327 /* Dump internal data structures for debugging. */
328 void N(SLDump)(void *l)
330 SkipListPtr list = (SkipListPtr)l;
334 if (list->magic != SL_LIST_MAGIC) {
335 printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
336 list->magic, SL_LIST_MAGIC);
340 printf("Level = %d, count = %d\n", list->level, list->count);
341 for (entry = list->head; entry; entry = entry->forward[0]) {
342 if (entry->magic != SL_ENTRY_MAGIC) {
343 printf("Bad magic: 0x%08lx (expected 0x%08lx)\n",
344 list->magic, SL_ENTRY_MAGIC);
346 printf("\nEntry %p <0x%08lx, %p> has %2d levels\n",
347 entry, entry->key, entry->value, entry->levels);
348 for (i = 0; i < entry->levels; i++) {
349 if (entry->forward[i]) {
350 printf(" %2d: %p <0x%08lx, %p>\n",
353 entry->forward[i]->key,
354 entry->forward[i]->value);
356 printf(" %2d: %p\n", i, entry->forward[i]);
363 static void print(SkipListPtr list)
368 if (N(SLFirst)(list, &key, &value)) {
370 printf("key = %5lu, value = %p\n", key, value);
371 } while (N(SLNext)(list, &key, &value));
375 static double do_time(int size, int iter)
379 unsigned long keys[1000000];
380 unsigned long previous;
383 struct timeval start, stop;
387 SL_RANDOM_INIT(12345);
389 list = N(SLCreate)();
391 for (i = 0; i < size; i++) {
393 N(SLInsert)(list, keys[i], NULL);
397 if (N(SLFirst)(list, &key, &value)) {
399 if (key <= previous) {
400 printf( "%lu !< %lu\n", previous, key);
403 } while (N(SLNext)(list, &key, &value));
406 gettimeofday(&start, NULL);
407 for (j = 0; j < iter; j++) {
408 for (i = 0; i < size; i++) {
409 if (N(SLLookup)(list, keys[i], &value))
410 printf("Error %lu %d\n", keys[i], i);
413 gettimeofday(&stop, NULL);
415 usec = (double)(stop.tv_sec * 1000000 + stop.tv_usec
416 - start.tv_sec * 1000000 - start.tv_usec) / (size * iter);
418 printf("%0.2f microseconds for list length %d\n", usec, size);
425 static void print_neighbors(void *list, unsigned long key)
427 unsigned long prev_key = 0;
428 unsigned long next_key = 0;
433 retval = drmSLLookupNeighbors(list, key,
434 &prev_key, &prev_value,
435 &next_key, &next_value);
436 printf("Neighbors of %5lu: %d %5lu %5lu\n",
437 key, retval, prev_key, next_key);
443 double usec, usec2, usec3, usec4;
445 list = N(SLCreate)();
446 printf( "list at %p\n", list);
449 printf("\n==============================\n\n");
451 N(SLInsert)(list, 123, NULL);
452 N(SLInsert)(list, 213, NULL);
453 N(SLInsert)(list, 50, NULL);
455 printf("\n==============================\n\n");
457 print_neighbors(list, 0);
458 print_neighbors(list, 50);
459 print_neighbors(list, 51);
460 print_neighbors(list, 123);
461 print_neighbors(list, 200);
462 print_neighbors(list, 213);
463 print_neighbors(list, 256);
464 printf("\n==============================\n\n");
466 N(SLDelete)(list, 50);
468 printf("\n==============================\n\n");
472 printf("\n==============================\n\n");
474 usec = do_time(100, 10000);
475 usec2 = do_time(1000, 500);
476 printf("Table size increased by %0.2f, search time increased by %0.2f\n",
477 1000.0/100.0, usec2 / usec);
479 usec3 = do_time(10000, 50);
480 printf("Table size increased by %0.2f, search time increased by %0.2f\n",
481 10000.0/100.0, usec3 / usec);
483 usec4 = do_time(100000, 4);
484 printf("Table size increased by %0.2f, search time increased by %0.2f\n",
485 100000.0/100.0, usec4 / usec);