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 implementation 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"
66 DEFINE_MTYPE_STATIC(LIB
, SKIP_LIST
, "Skip List");
67 DEFINE_MTYPE_STATIC(LIB
, SKIP_LIST_NODE
, "Skip Node");
68 DEFINE_MTYPE_STATIC(LIB
, SKIP_LIST_STATS
, "Skiplist Counters");
70 #define BitsInRandom 31
72 #define MaxNumberOfLevels 16
73 #define MaxLevel (MaxNumberOfLevels-1)
74 #define newNodeOfLevel(l) \
75 XCALLOC(MTYPE_SKIP_LIST_NODE, \
76 sizeof(struct skiplistnode) \
77 + (l) * sizeof(struct skiplistnode *))
79 /* XXX must match type of (struct skiplist).level_stats */
80 #define newStatsOfLevel(l) \
81 XCALLOC(MTYPE_SKIP_LIST_STATS, ((l) + 1) * sizeof(int))
83 static int randomsLeft
;
84 static int randomBits
;
87 #define CHECKLAST(sl) \
89 if ((sl)->header->forward[0] && !(sl)->last) \
91 if (!(sl)->header->forward[0] && (sl)->last) \
99 static int randomLevel(void)
101 register int level
= 0;
105 if (randomsLeft
<= 0) {
106 randomBits
= frr_weak_random();
107 randomsLeft
= BitsInRandom
/ 2;
115 if (level
>= MaxLevel
)
123 static int default_cmp(const void *key1
, const void *key2
)
132 unsigned int skiplist_count(struct skiplist
*l
)
137 struct skiplist
*skiplist_new(int flags
,
138 int (*cmp
)(const void *key1
, const void *key2
),
139 void (*del
)(void *val
))
141 struct skiplist
*new;
143 new = XCALLOC(MTYPE_SKIP_LIST
, sizeof(struct skiplist
));
148 new->header
= newNodeOfLevel(MaxNumberOfLevels
);
149 new->level_stats
= newStatsOfLevel(MaxNumberOfLevels
);
155 new->cmp
= default_cmp
;
163 void skiplist_free(struct skiplist
*l
)
165 register struct skiplistnode
*p
, *q
;
171 if (l
->del
&& p
!= l
->header
)
173 XFREE(MTYPE_SKIP_LIST_NODE
, p
);
177 XFREE(MTYPE_SKIP_LIST_STATS
, l
->level_stats
);
178 XFREE(MTYPE_SKIP_LIST
, l
);
182 int skiplist_insert(register struct skiplist
*l
, register void *key
,
183 register void *value
)
186 struct skiplistnode
*update
[MaxNumberOfLevels
];
187 register struct skiplistnode
*p
, *q
;
191 #ifdef SKIPLIST_DEBUG
194 flog_err(EC_LIB_DEVELOPMENT
, "%s: key is 0, value is %p",
202 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
207 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) && q
208 && ((*l
->cmp
)(q
->key
, key
) == 0)) {
217 update
[k
] = l
->header
;
220 q
= newNodeOfLevel(k
);
223 #ifdef SKIPLIST_0TIMER_DEBUG
224 q
->flags
= SKIPLIST_NODE_FLAG_INSERTED
; /* debug */
227 ++(l
->level_stats
[k
]);
228 #ifdef SKIPLIST_DEBUG
229 zlog_debug("%s: incremented level_stats @%p:%d, now %d", __func__
, l
, k
,
235 q
->forward
[k
] = p
->forward
[k
];
240 * If this is the last item in the list, update the "last" pointer
242 if (!q
->forward
[0]) {
253 int skiplist_delete(register struct skiplist
*l
, register void *key
,
254 register void *value
) /* used only if duplicates allowed */
257 struct skiplistnode
*update
[MaxNumberOfLevels
];
258 register struct skiplistnode
*p
, *q
;
262 /* to make debugging easier */
263 for (k
= 0; k
< MaxNumberOfLevels
; ++k
)
269 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
274 if (l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) {
275 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
276 && (q
->value
!= value
)) {
278 for (i
= 0; i
<= l
->level
; ++i
) {
279 if (update
[i
]->forward
[i
] == q
)
286 if (q
&& (*l
->cmp
)(q
->key
, key
) == 0) {
287 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)
288 || (q
->value
== value
)) {
291 * found node to delete
293 #ifdef SKIPLIST_0TIMER_DEBUG
294 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
297 * If we are deleting the last element of the list,
298 * update the list's "last" pointer.
301 if (update
[0] == l
->header
)
307 for (k
= 0; k
<= m
&& (p
= update
[k
])->forward
[k
] == q
;
309 p
->forward
[k
] = q
->forward
[k
];
311 --(l
->level_stats
[k
- 1]);
312 #ifdef SKIPLIST_DEBUG
313 zlog_debug("%s: decremented level_stats @%p:%d, now %d",
314 __func__
, l
, k
- 1, l
->level_stats
[k
- 1]);
318 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
319 while (l
->header
->forward
[m
] == NULL
&& m
> 0)
333 * Obtain first value matching "key". Unless SKIPLIST_FLAG_ALLOW_DUPLICATES
334 * is set, this will also be the only value matching "key".
336 * Also set a cursor for use with skiplist_next_value.
338 int skiplist_first_value(register struct skiplist
*l
, /* in */
339 register const void *key
, /* in */
340 void **valuePointer
, /* out */
341 void **cursor
) /* out */
344 register struct skiplistnode
*p
, *q
;
350 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
355 if (!q
|| (*l
->cmp
)(q
->key
, key
))
359 *valuePointer
= q
->value
;
367 int skiplist_search(register struct skiplist
*l
, register void *key
,
370 return skiplist_first_value(l
, key
, valuePointer
, NULL
);
375 * Caller supplies key and value of an existing item in the list.
376 * Function returns the value of the next list item that has the
377 * same key (useful when SKIPLIST_FLAG_ALLOW_DUPLICATES is set).
379 * Returns 0 on success. If the caller-supplied key and value
380 * do not correspond to a list element, or if they specify the
381 * last element with the given key, -1 is returned.
383 int skiplist_next_value(register struct skiplist
*l
, /* in */
384 register const void *key
, /* in */
385 void **valuePointer
, /* in/out */
386 void **cursor
) /* in/out */
389 register struct skiplistnode
*p
, *q
;
393 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)) {
397 if (!cursor
|| !*cursor
) {
405 while (q
= p
->forward
[k
],
406 q
&& (*l
->cmp
)(q
->key
, key
) < 0)
411 * Find matching value
413 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
414 && (q
->value
!= *valuePointer
)) {
418 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0)
419 || (q
->value
!= *valuePointer
)) {
427 q
= (struct skiplistnode
*)*cursor
;
436 * If we reached end-of-list or if the key is no longer the same,
439 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0))
442 *valuePointer
= q
->value
;
449 int skiplist_first(register struct skiplist
*l
, void **keyPointer
,
452 register struct skiplistnode
*p
;
455 p
= l
->header
->forward
[0];
460 *keyPointer
= p
->key
;
463 *valuePointer
= p
->value
;
470 int skiplist_last(register struct skiplist
*l
, void **keyPointer
,
476 *keyPointer
= l
->last
->key
;
478 *valuePointer
= l
->last
->value
;
487 int skiplist_empty(register struct skiplist
*l
)
496 * Use this to walk the list. Caller sets *cursor to NULL to obtain
497 * first element. Return value of 0 indicates valid cursor/element
498 * returned, otherwise NULL cursor arg or EOL.
500 int skiplist_next(register struct skiplist
*l
, /* in */
501 void **keyPointer
, /* out */
502 void **valuePointer
, /* out */
503 void **cursor
) /* in/out */
505 struct skiplistnode
*p
;
513 p
= l
->header
->forward
[0];
524 *keyPointer
= p
->key
;
527 *valuePointer
= p
->value
;
534 int skiplist_delete_first(register struct skiplist
*l
)
537 register struct skiplistnode
*p
, *q
;
543 q
= l
->header
->forward
[0];
548 for (k
= l
->level
; k
>= 0; --k
) {
549 if (p
->forward
[k
] == q
) {
550 p
->forward
[k
] = q
->forward
[k
];
551 if ((k
== l
->level
) && (p
->forward
[k
] == NULL
)
559 #ifdef SKIPLIST_0TIMER_DEBUG
560 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
563 * If we are deleting the last element of the list,
564 * update the list's "last" pointer.
570 --(l
->level_stats
[nodelevel
]);
571 #ifdef SKIPLIST_DEBUG
572 zlog_debug("%s: decremented level_stats @%p:%d, now %d", __func__
, l
,
573 nodelevel
, l
->level_stats
[nodelevel
]);
579 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
588 void skiplist_debug(struct vty
*vty
, struct skiplist
*l
)
595 vty_out(vty
, "Skiplist %p has max level %d\n", l
, l
->level
);
596 for (i
= l
->level
; i
>= 0; --i
)
597 vty_out(vty
, " @%d: %d\n", i
, l
->level_stats
[i
]);
600 static void *scramble(int i
)
604 result
= (unsigned)(i
& 0xff) << 24;
605 result
|= (unsigned)i
>> 8;
607 return (void *)result
;
610 #define sampleSize 65536
611 void skiplist_test(struct vty
*vty
)
615 void *keys
[sampleSize
];
618 zlog_debug("%s: entry", __func__
);
620 l
= skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
, NULL
, NULL
);
622 zlog_debug("%s: skiplist_new returned %p", __func__
, l
);
624 for (i
= 0; i
< 4; i
++) {
626 for (k
= 0; k
< sampleSize
; k
++) {
628 zlog_debug("%s: (%d:%d)", __func__
, i
, k
);
630 // keys[k] = (void *)random();
631 keys
[k
] = scramble(k
);
632 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
633 zlog_debug("error in insert #%d,#%d", i
, k
);
636 zlog_debug("%s: inserts done", __func__
);
638 for (k
= 0; k
< sampleSize
; k
++) {
641 zlog_debug("[%d:%d]", i
, k
);
642 if (skiplist_search(l
, keys
[k
], &v
))
643 zlog_debug("error in search #%d,#%d", i
, k
);
646 zlog_debug("search returned wrong value");
650 for (k
= 0; k
< sampleSize
; k
++) {
653 zlog_debug("<%d:%d>", i
, k
);
654 if (skiplist_delete(l
, keys
[k
], keys
[k
]))
655 zlog_debug("error in delete");
656 keys
[k
] = scramble(k
^ 0xf0f0f0f0);
657 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
658 zlog_debug("error in insert #%d,#%d", i
, k
);
661 for (k
= 0; k
< sampleSize
; k
++) {
664 zlog_debug("{%d:%d}", i
, k
);
665 if (skiplist_delete_first(l
))
666 zlog_debug("error in delete_first");