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()
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(LIB_ERR_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)) {
207 update
[k
] = l
->header
;
210 q
= newNodeOfLevel(k
);
213 #if SKIPLIST_0TIMER_DEBUG
214 q
->flags
= SKIPLIST_NODE_FLAG_INSERTED
; /* debug */
217 ++(l
->stats
->forward
[k
]);
219 zlog_debug("%s: incremented stats @%p:%d, now %ld", __func__
, l
, k
,
220 l
->stats
->forward
[k
] - (struct skiplistnode
*)NULL
);
225 q
->forward
[k
] = p
->forward
[k
];
230 * If this is the last item in the list, update the "last" pointer
232 if (!q
->forward
[0]) {
243 int skiplist_delete(register struct skiplist
*l
, register void *key
,
244 register void *value
) /* used only if duplicates allowed */
247 struct skiplistnode
*update
[MaxNumberOfLevels
];
248 register struct skiplistnode
*p
, *q
;
252 /* to make debugging easier */
253 for (k
= 0; k
< MaxNumberOfLevels
; ++k
)
259 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
264 if (l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) {
265 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
266 && (q
->value
!= value
)) {
268 for (i
= 0; i
<= l
->level
; ++i
) {
269 if (update
[i
]->forward
[i
] == q
)
276 if (q
&& (*l
->cmp
)(q
->key
, key
) == 0) {
277 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)
278 || (q
->value
== value
)) {
281 * found node to delete
283 #if SKIPLIST_0TIMER_DEBUG
284 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
287 * If we are deleting the last element of the list,
288 * update the list's "last" pointer.
291 if (update
[0] == l
->header
)
297 for (k
= 0; k
<= m
&& (p
= update
[k
])->forward
[k
] == q
;
299 p
->forward
[k
] = q
->forward
[k
];
301 --(l
->stats
->forward
[k
- 1]);
303 zlog_debug("%s: decremented stats @%p:%d, now %ld",
305 l
->stats
->forward
[k
- 1]
306 - (struct skiplistnode
*)NULL
);
310 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
311 while (l
->header
->forward
[m
] == NULL
&& m
> 0)
325 * Obtain first value matching "key". Unless SKIPLIST_FLAG_ALLOW_DUPLICATES
326 * is set, this will also be the only value matching "key".
328 * Also set a cursor for use with skiplist_next_value.
330 int skiplist_first_value(register struct skiplist
*l
, /* in */
331 register void *key
, /* in */
332 void **valuePointer
, /* out */
333 void **cursor
) /* out */
336 register struct skiplistnode
*p
, *q
;
342 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
347 if (!q
|| (*l
->cmp
)(q
->key
, key
))
351 *valuePointer
= q
->value
;
359 int skiplist_search(register struct skiplist
*l
, register void *key
,
362 return skiplist_first_value(l
, key
, valuePointer
, NULL
);
367 * Caller supplies key and value of an existing item in the list.
368 * Function returns the value of the next list item that has the
369 * same key (useful when SKIPLIST_FLAG_ALLOW_DUPLICATES is set).
371 * Returns 0 on success. If the caller-supplied key and value
372 * do not correspond to a list element, or if they specify the
373 * last element with the given key, -1 is returned.
375 int skiplist_next_value(register struct skiplist
*l
, /* in */
376 register void *key
, /* in */
377 void **valuePointer
, /* in/out */
378 void **cursor
) /* in/out */
381 register struct skiplistnode
*p
, *q
;
385 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)) {
389 if (!cursor
|| !*cursor
) {
397 while (q
= p
->forward
[k
],
398 q
&& (*l
->cmp
)(q
->key
, key
) < 0)
403 * Find matching value
405 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
406 && (q
->value
!= *valuePointer
)) {
410 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0)
411 || (q
->value
!= *valuePointer
)) {
419 q
= (struct skiplistnode
*)*cursor
;
428 * If we reached end-of-list or if the key is no longer the same,
431 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0))
434 *valuePointer
= q
->value
;
441 int skiplist_first(register struct skiplist
*l
, void **keyPointer
,
444 register struct skiplistnode
*p
;
447 p
= l
->header
->forward
[0];
452 *keyPointer
= p
->key
;
455 *valuePointer
= p
->value
;
462 int skiplist_last(register struct skiplist
*l
, void **keyPointer
,
468 *keyPointer
= l
->last
->key
;
470 *valuePointer
= l
->last
->value
;
479 int skiplist_empty(register struct skiplist
*l
)
488 * Use this to walk the list. Caller sets *cursor to NULL to obtain
489 * first element. Return value of 0 indicates valid cursor/element
490 * returned, otherwise NULL cursor arg or EOL.
492 int skiplist_next(register struct skiplist
*l
, /* in */
493 void **keyPointer
, /* out */
494 void **valuePointer
, /* out */
495 void **cursor
) /* in/out */
497 struct skiplistnode
*p
;
505 p
= l
->header
->forward
[0];
516 *keyPointer
= p
->key
;
519 *valuePointer
= p
->value
;
526 int skiplist_delete_first(register struct skiplist
*l
)
529 register struct skiplistnode
*p
, *q
;
535 q
= l
->header
->forward
[0];
540 for (k
= l
->level
; k
>= 0; --k
) {
541 if (p
->forward
[k
] == q
) {
542 p
->forward
[k
] = q
->forward
[k
];
543 if ((k
== l
->level
) && (p
->forward
[k
] == NULL
)
551 #if SKIPLIST_0TIMER_DEBUG
552 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
555 * If we are deleting the last element of the list,
556 * update the list's "last" pointer.
562 --(l
->stats
->forward
[nodelevel
]);
564 zlog_debug("%s: decremented stats @%p:%d, now %ld", __func__
, l
,
566 l
->stats
->forward
[nodelevel
] - (struct skiplistnode
*)NULL
);
572 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
581 void skiplist_debug(struct vty
*vty
, struct skiplist
*l
)
586 l
= skiplist_last_created
;
587 vty_out(vty
, "Skiplist %p has max level %d\n", l
, l
->level
);
588 for (i
= l
->level
; i
>= 0; --i
)
589 vty_out(vty
, " @%d: %ld\n", i
,
590 (long)((l
->stats
->forward
[i
])
591 - (struct skiplistnode
*)NULL
));
594 static void *scramble(int i
)
598 result
= (unsigned)(i
& 0xff) << 24;
599 result
|= (unsigned)i
>> 8;
601 return (void *)result
;
604 #define sampleSize 65536
605 void skiplist_test(struct vty
*vty
)
609 void *keys
[sampleSize
];
612 zlog_debug("%s: entry", __func__
);
614 l
= skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
, NULL
, NULL
);
616 zlog_debug("%s: skiplist_new returned %p", __func__
, l
);
618 for (i
= 0; i
< 4; i
++) {
620 for (k
= 0; k
< sampleSize
; k
++) {
622 zlog_debug("%s: (%d:%d)", __func__
, i
, k
);
624 // keys[k] = (void *)random();
625 keys
[k
] = (void *)scramble(k
);
626 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
627 zlog_debug("error in insert #%d,#%d", i
, k
);
630 zlog_debug("%s: inserts done", __func__
);
632 for (k
= 0; k
< sampleSize
; k
++) {
635 zlog_debug("[%d:%d]", i
, k
);
636 if (skiplist_search(l
, keys
[k
], &v
))
637 zlog_debug("error in search #%d,#%d", i
, k
);
640 zlog_debug("search returned wrong value");
644 for (k
= 0; k
< sampleSize
; k
++) {
647 zlog_debug("<%d:%d>", i
, k
);
648 if (skiplist_delete(l
, keys
[k
], keys
[k
]))
649 zlog_debug("error in delete");
650 keys
[k
] = (void *)scramble(k
^ 0xf0f0f0f0);
651 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
652 zlog_debug("error in insert #%d,#%d", i
, k
);
655 for (k
= 0; k
< sampleSize
; k
++) {
658 zlog_debug("{%d:%d}", i
, k
);
659 if (skiplist_delete_first(l
))
660 zlog_debug("error in delete_first");