2 * Copyright 1990 William Pugh
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted.
7 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
8 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
9 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
10 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
11 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
12 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
13 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
14 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
15 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
16 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
17 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
20 * Permission to include in quagga provide on March 31, 2016
24 * Skip List impementation based on code from William Pugh.
25 * ftp://ftp.cs.umd.edu/pub/skipLists/
27 * Skip Lists are a probabilistic alternative to balanced trees, as
28 * described in the June 1990 issue of CACM and were invented by
29 * William Pugh in 1987.
31 * This file contains source code to implement a dictionary using
32 * skip lists and a test driver to test the routines.
34 * A couple of comments about this implementation:
35 * The routine randomLevel has been hard-coded to generate random
36 * levels using p=0.25. It can be easily changed.
38 * The insertion routine has been implemented so as to use the
39 * dirty hack described in the CACM paper: if a random level is
40 * generated that is more than the current maximum level, the
41 * current maximum level plus one is used instead.
43 * Levels start at zero and go up to MaxLevel (which is equal to
44 * (MaxNumberOfLevels-1).
46 * The run-time flag SKIPLIST_FLAG_ALLOW_DUPLICATES determines whether or
47 * not duplicates are allowed for a given list. If set, duplicates are
48 * allowed and act in a FIFO manner. If not set, an insertion of a value
49 * already in the list updates the previously existing binding.
51 * BitsInRandom is defined to be the number of bits returned by a call to
52 * random(). For most all machines with 32-bit integers, this is 31 bits
63 #include "lib_errors.h"
65 DEFINE_MTYPE_STATIC(LIB
, SKIP_LIST
, "Skip List")
66 DEFINE_MTYPE_STATIC(LIB
, SKIP_LIST_NODE
, "Skip Node")
68 #define BitsInRandom 31
70 #define MaxNumberOfLevels 16
71 #define MaxLevel (MaxNumberOfLevels-1)
72 #define newNodeOfLevel(l) XCALLOC(MTYPE_SKIP_LIST_NODE, sizeof(struct skiplistnode)+(l)*sizeof(struct skiplistnode *))
74 static int randomsLeft
;
75 static int randomBits
;
76 static struct skiplist
*skiplist_last_created
; /* debugging hack */
79 #define CHECKLAST(sl) \
81 if ((sl)->header->forward[0] && !(sl)->last) \
83 if (!(sl)->header->forward[0] && (sl)->last) \
91 static int randomLevel(void)
93 register int level
= 0;
97 if (randomsLeft
<= 0) {
98 randomBits
= random();
99 randomsLeft
= BitsInRandom
/ 2;
107 if (level
>= MaxLevel
)
115 static int default_cmp(void *key1
, void *key2
)
124 unsigned int skiplist_count(struct skiplist
*l
)
129 struct skiplist
*skiplist_new(int flags
, int (*cmp
)(void *key1
, void *key2
),
130 void (*del
)(void *val
))
132 struct skiplist
*new;
134 new = XCALLOC(MTYPE_SKIP_LIST
, sizeof(struct skiplist
));
139 new->header
= newNodeOfLevel(MaxNumberOfLevels
);
140 new->stats
= newNodeOfLevel(MaxNumberOfLevels
);
146 new->cmp
= default_cmp
;
151 skiplist_last_created
= new; /* debug */
156 void skiplist_free(struct skiplist
*l
)
158 register struct skiplistnode
*p
, *q
;
164 if (l
->del
&& p
!= l
->header
)
166 XFREE(MTYPE_SKIP_LIST_NODE
, p
);
170 XFREE(MTYPE_SKIP_LIST_NODE
, l
->stats
);
171 XFREE(MTYPE_SKIP_LIST
, l
);
175 int skiplist_insert(register struct skiplist
*l
, register void *key
,
176 register void *value
)
179 struct skiplistnode
*update
[MaxNumberOfLevels
];
180 register struct skiplistnode
*p
, *q
;
186 flog_err(EC_LIB_DEVELOPMENT
, "%s: key is 0, value is %p",
193 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
198 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) && q
199 && ((*l
->cmp
)(q
->key
, key
) == 0)) {
208 update
[k
] = l
->header
;
211 q
= newNodeOfLevel(k
);
214 #if SKIPLIST_0TIMER_DEBUG
215 q
->flags
= SKIPLIST_NODE_FLAG_INSERTED
; /* debug */
218 ++(l
->stats
->forward
[k
]);
220 zlog_debug("%s: incremented stats @%p:%d, now %ld", __func__
, l
, k
,
221 l
->stats
->forward
[k
] - (struct skiplistnode
*)NULL
);
226 q
->forward
[k
] = p
->forward
[k
];
231 * If this is the last item in the list, update the "last" pointer
233 if (!q
->forward
[0]) {
244 int skiplist_delete(register struct skiplist
*l
, register void *key
,
245 register void *value
) /* used only if duplicates allowed */
248 struct skiplistnode
*update
[MaxNumberOfLevels
];
249 register struct skiplistnode
*p
, *q
;
253 /* to make debugging easier */
254 for (k
= 0; k
< MaxNumberOfLevels
; ++k
)
260 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
265 if (l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) {
266 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
267 && (q
->value
!= value
)) {
269 for (i
= 0; i
<= l
->level
; ++i
) {
270 if (update
[i
]->forward
[i
] == q
)
277 if (q
&& (*l
->cmp
)(q
->key
, key
) == 0) {
278 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)
279 || (q
->value
== value
)) {
282 * found node to delete
284 #if SKIPLIST_0TIMER_DEBUG
285 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
288 * If we are deleting the last element of the list,
289 * update the list's "last" pointer.
292 if (update
[0] == l
->header
)
298 for (k
= 0; k
<= m
&& (p
= update
[k
])->forward
[k
] == q
;
300 p
->forward
[k
] = q
->forward
[k
];
302 --(l
->stats
->forward
[k
- 1]);
304 zlog_debug("%s: decremented stats @%p:%d, now %ld",
306 l
->stats
->forward
[k
- 1]
307 - (struct skiplistnode
*)NULL
);
311 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
312 while (l
->header
->forward
[m
] == NULL
&& m
> 0)
326 * Obtain first value matching "key". Unless SKIPLIST_FLAG_ALLOW_DUPLICATES
327 * is set, this will also be the only value matching "key".
329 * Also set a cursor for use with skiplist_next_value.
331 int skiplist_first_value(register struct skiplist
*l
, /* in */
332 register void *key
, /* in */
333 void **valuePointer
, /* out */
334 void **cursor
) /* out */
337 register struct skiplistnode
*p
, *q
;
343 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
348 if (!q
|| (*l
->cmp
)(q
->key
, key
))
352 *valuePointer
= q
->value
;
360 int skiplist_search(register struct skiplist
*l
, register void *key
,
363 return skiplist_first_value(l
, key
, valuePointer
, NULL
);
368 * Caller supplies key and value of an existing item in the list.
369 * Function returns the value of the next list item that has the
370 * same key (useful when SKIPLIST_FLAG_ALLOW_DUPLICATES is set).
372 * Returns 0 on success. If the caller-supplied key and value
373 * do not correspond to a list element, or if they specify the
374 * last element with the given key, -1 is returned.
376 int skiplist_next_value(register struct skiplist
*l
, /* in */
377 register void *key
, /* in */
378 void **valuePointer
, /* in/out */
379 void **cursor
) /* in/out */
382 register struct skiplistnode
*p
, *q
;
386 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)) {
390 if (!cursor
|| !*cursor
) {
398 while (q
= p
->forward
[k
],
399 q
&& (*l
->cmp
)(q
->key
, key
) < 0)
404 * Find matching value
406 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
407 && (q
->value
!= *valuePointer
)) {
411 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0)
412 || (q
->value
!= *valuePointer
)) {
420 q
= (struct skiplistnode
*)*cursor
;
429 * If we reached end-of-list or if the key is no longer the same,
432 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0))
435 *valuePointer
= q
->value
;
442 int skiplist_first(register struct skiplist
*l
, void **keyPointer
,
445 register struct skiplistnode
*p
;
448 p
= l
->header
->forward
[0];
453 *keyPointer
= p
->key
;
456 *valuePointer
= p
->value
;
463 int skiplist_last(register struct skiplist
*l
, void **keyPointer
,
469 *keyPointer
= l
->last
->key
;
471 *valuePointer
= l
->last
->value
;
480 int skiplist_empty(register struct skiplist
*l
)
489 * Use this to walk the list. Caller sets *cursor to NULL to obtain
490 * first element. Return value of 0 indicates valid cursor/element
491 * returned, otherwise NULL cursor arg or EOL.
493 int skiplist_next(register struct skiplist
*l
, /* in */
494 void **keyPointer
, /* out */
495 void **valuePointer
, /* out */
496 void **cursor
) /* in/out */
498 struct skiplistnode
*p
;
506 p
= l
->header
->forward
[0];
517 *keyPointer
= p
->key
;
520 *valuePointer
= p
->value
;
527 int skiplist_delete_first(register struct skiplist
*l
)
530 register struct skiplistnode
*p
, *q
;
536 q
= l
->header
->forward
[0];
541 for (k
= l
->level
; k
>= 0; --k
) {
542 if (p
->forward
[k
] == q
) {
543 p
->forward
[k
] = q
->forward
[k
];
544 if ((k
== l
->level
) && (p
->forward
[k
] == NULL
)
552 #if SKIPLIST_0TIMER_DEBUG
553 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
556 * If we are deleting the last element of the list,
557 * update the list's "last" pointer.
563 --(l
->stats
->forward
[nodelevel
]);
565 zlog_debug("%s: decremented stats @%p:%d, now %ld", __func__
, l
,
567 l
->stats
->forward
[nodelevel
] - (struct skiplistnode
*)NULL
);
573 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
582 void skiplist_debug(struct vty
*vty
, struct skiplist
*l
)
587 l
= skiplist_last_created
;
588 vty_out(vty
, "Skiplist %p has max level %d\n", l
, l
->level
);
589 for (i
= l
->level
; i
>= 0; --i
)
590 vty_out(vty
, " @%d: %ld\n", i
,
591 (long)((l
->stats
->forward
[i
])
592 - (struct skiplistnode
*)NULL
));
595 static void *scramble(int i
)
599 result
= (unsigned)(i
& 0xff) << 24;
600 result
|= (unsigned)i
>> 8;
602 return (void *)result
;
605 #define sampleSize 65536
606 void skiplist_test(struct vty
*vty
)
610 void *keys
[sampleSize
];
613 zlog_debug("%s: entry", __func__
);
615 l
= skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
, NULL
, NULL
);
617 zlog_debug("%s: skiplist_new returned %p", __func__
, l
);
619 for (i
= 0; i
< 4; i
++) {
621 for (k
= 0; k
< sampleSize
; k
++) {
623 zlog_debug("%s: (%d:%d)", __func__
, i
, k
);
625 // keys[k] = (void *)random();
626 keys
[k
] = (void *)scramble(k
);
627 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
628 zlog_debug("error in insert #%d,#%d", i
, k
);
631 zlog_debug("%s: inserts done", __func__
);
633 for (k
= 0; k
< sampleSize
; k
++) {
636 zlog_debug("[%d:%d]", i
, k
);
637 if (skiplist_search(l
, keys
[k
], &v
))
638 zlog_debug("error in search #%d,#%d", i
, k
);
641 zlog_debug("search returned wrong value");
645 for (k
= 0; k
< sampleSize
; k
++) {
648 zlog_debug("<%d:%d>", i
, k
);
649 if (skiplist_delete(l
, keys
[k
], keys
[k
]))
650 zlog_debug("error in delete");
651 keys
[k
] = (void *)scramble(k
^ 0xf0f0f0f0);
652 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
653 zlog_debug("error in insert #%d,#%d", i
, k
);
656 for (k
= 0; k
< sampleSize
; k
++) {
659 zlog_debug("{%d:%d}", i
, k
);
660 if (skiplist_delete_first(l
))
661 zlog_debug("error in delete_first");