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
64 DEFINE_MTYPE_STATIC(LIB
, SKIP_LIST
, "Skip List")
65 DEFINE_MTYPE_STATIC(LIB
, SKIP_LIST_NODE
, "Skip Node")
67 #define BitsInRandom 31
69 #define MaxNumberOfLevels 16
70 #define MaxLevel (MaxNumberOfLevels-1)
71 #define newNodeOfLevel(l) XCALLOC(MTYPE_SKIP_LIST_NODE, sizeof(struct skiplistnode)+(l)*sizeof(struct skiplistnode *))
73 static int randomsLeft
;
74 static int randomBits
;
75 static struct skiplist
*skiplist_last_created
; /* debugging hack */
78 #define CHECKLAST(sl) \
80 if ((sl)->header->forward[0] && !(sl)->last) \
82 if (!(sl)->header->forward[0] && (sl)->last) \
90 static int randomLevel()
92 register int level
= 0;
96 if (randomsLeft
<= 0) {
97 randomBits
= random();
98 randomsLeft
= BitsInRandom
/ 2;
106 if (level
>= MaxLevel
)
114 static int default_cmp(void *key1
, void *key2
)
123 unsigned int skiplist_count(struct skiplist
*l
)
128 struct skiplist
*skiplist_new(int flags
, int (*cmp
)(void *key1
, void *key2
),
129 void (*del
)(void *val
))
131 struct skiplist
*new;
133 new = XCALLOC(MTYPE_SKIP_LIST
, sizeof(struct skiplist
));
138 new->header
= newNodeOfLevel(MaxNumberOfLevels
);
139 new->stats
= newNodeOfLevel(MaxNumberOfLevels
);
145 new->cmp
= default_cmp
;
150 skiplist_last_created
= new; /* debug */
155 void skiplist_free(struct skiplist
*l
)
157 register struct skiplistnode
*p
, *q
;
163 if (l
->del
&& p
!= l
->header
)
165 XFREE(MTYPE_SKIP_LIST_NODE
, p
);
169 XFREE(MTYPE_SKIP_LIST_NODE
, l
->stats
);
170 XFREE(MTYPE_SKIP_LIST
, l
);
174 int skiplist_insert(register struct skiplist
*l
, register void *key
,
175 register void *value
)
178 struct skiplistnode
*update
[MaxNumberOfLevels
];
179 register struct skiplistnode
*p
, *q
;
185 zlog_err("%s: key is 0, value is %p", __func__
, value
);
191 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
196 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) && q
197 && ((*l
->cmp
)(q
->key
, key
) == 0)) {
205 update
[k
] = l
->header
;
208 q
= newNodeOfLevel(k
);
211 #if SKIPLIST_0TIMER_DEBUG
212 q
->flags
= SKIPLIST_NODE_FLAG_INSERTED
; /* debug */
215 ++(l
->stats
->forward
[k
]);
217 zlog_debug("%s: incremented stats @%p:%d, now %ld", __func__
, l
, k
,
218 l
->stats
->forward
[k
] - (struct skiplistnode
*)NULL
);
223 q
->forward
[k
] = p
->forward
[k
];
228 * If this is the last item in the list, update the "last" pointer
230 if (!q
->forward
[0]) {
241 int skiplist_delete(register struct skiplist
*l
, register void *key
,
242 register void *value
) /* used only if duplicates allowed */
245 struct skiplistnode
*update
[MaxNumberOfLevels
];
246 register struct skiplistnode
*p
, *q
;
250 /* to make debugging easier */
251 for (k
= 0; k
< MaxNumberOfLevels
; ++k
)
257 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
262 if (l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) {
263 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
264 && (q
->value
!= value
)) {
266 for (i
= 0; i
<= l
->level
; ++i
) {
267 if (update
[i
]->forward
[i
] == q
)
274 if (q
&& (*l
->cmp
)(q
->key
, key
) == 0) {
275 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)
276 || (q
->value
== value
)) {
279 * found node to delete
281 #if SKIPLIST_0TIMER_DEBUG
282 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
285 * If we are deleting the last element of the list,
286 * update the list's "last" pointer.
289 if (update
[0] == l
->header
)
295 for (k
= 0; k
<= m
&& (p
= update
[k
])->forward
[k
] == q
;
297 p
->forward
[k
] = q
->forward
[k
];
299 --(l
->stats
->forward
[k
- 1]);
301 zlog_debug("%s: decremented stats @%p:%d, now %ld",
303 l
->stats
->forward
[k
- 1]
304 - (struct skiplistnode
*)NULL
);
308 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
309 while (l
->header
->forward
[m
] == NULL
&& m
> 0)
323 * Obtain first value matching "key". Unless SKIPLIST_FLAG_ALLOW_DUPLICATES
324 * is set, this will also be the only value matching "key".
326 * Also set a cursor for use with skiplist_next_value.
328 int skiplist_first_value(register struct skiplist
*l
, /* in */
329 register void *key
, /* in */
330 void **valuePointer
, /* out */
331 void **cursor
) /* out */
334 register struct skiplistnode
*p
, *q
;
340 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
345 if (!q
|| (*l
->cmp
)(q
->key
, key
))
349 *valuePointer
= q
->value
;
357 int skiplist_search(register struct skiplist
*l
, register void *key
,
360 return skiplist_first_value(l
, key
, valuePointer
, NULL
);
365 * Caller supplies key and value of an existing item in the list.
366 * Function returns the value of the next list item that has the
367 * same key (useful when SKIPLIST_FLAG_ALLOW_DUPLICATES is set).
369 * Returns 0 on success. If the caller-supplied key and value
370 * do not correspond to a list element, or if they specify the
371 * last element with the given key, -1 is returned.
373 int skiplist_next_value(register struct skiplist
*l
, /* in */
374 register void *key
, /* in */
375 void **valuePointer
, /* in/out */
376 void **cursor
) /* in/out */
379 register struct skiplistnode
*p
, *q
;
383 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)) {
387 if (!cursor
|| !*cursor
) {
395 while (q
= p
->forward
[k
],
396 q
&& (*l
->cmp
)(q
->key
, key
) < 0)
401 * Find matching value
403 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0)
404 && (q
->value
!= *valuePointer
)) {
408 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0)
409 || (q
->value
!= *valuePointer
)) {
417 q
= (struct skiplistnode
*)*cursor
;
426 * If we reached end-of-list or if the key is no longer the same,
429 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0))
432 *valuePointer
= q
->value
;
439 int skiplist_first(register struct skiplist
*l
, void **keyPointer
,
442 register struct skiplistnode
*p
;
445 p
= l
->header
->forward
[0];
450 *keyPointer
= p
->key
;
453 *valuePointer
= p
->value
;
460 int skiplist_last(register struct skiplist
*l
, void **keyPointer
,
466 *keyPointer
= l
->last
->key
;
468 *valuePointer
= l
->last
->value
;
477 int skiplist_empty(register struct skiplist
*l
)
486 * Use this to walk the list. Caller sets *cursor to NULL to obtain
487 * first element. Return value of 0 indicates valid cursor/element
488 * returned, otherwise NULL cursor arg or EOL.
490 int skiplist_next(register struct skiplist
*l
, /* in */
491 void **keyPointer
, /* out */
492 void **valuePointer
, /* out */
493 void **cursor
) /* in/out */
495 struct skiplistnode
*p
;
503 p
= l
->header
->forward
[0];
514 *keyPointer
= p
->key
;
517 *valuePointer
= p
->value
;
524 int skiplist_delete_first(register struct skiplist
*l
)
527 register struct skiplistnode
*p
, *q
;
533 q
= l
->header
->forward
[0];
538 for (k
= l
->level
; k
>= 0; --k
) {
539 if (p
->forward
[k
] == q
) {
540 p
->forward
[k
] = q
->forward
[k
];
541 if ((k
== l
->level
) && (p
->forward
[k
] == NULL
)
549 #if SKIPLIST_0TIMER_DEBUG
550 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
553 * If we are deleting the last element of the list,
554 * update the list's "last" pointer.
560 --(l
->stats
->forward
[nodelevel
]);
562 zlog_debug("%s: decremented stats @%p:%d, now %ld", __func__
, l
,
564 l
->stats
->forward
[nodelevel
] - (struct skiplistnode
*)NULL
);
570 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
579 void skiplist_debug(struct vty
*vty
, struct skiplist
*l
)
584 l
= skiplist_last_created
;
585 vty_out(vty
, "Skiplist %p has max level %d\n", l
, l
->level
);
586 for (i
= l
->level
; i
>= 0; --i
)
587 vty_out(vty
, " @%d: %ld\n", i
,
588 (long)((l
->stats
->forward
[i
])
589 - (struct skiplistnode
*)NULL
));
592 static void *scramble(int i
)
596 result
= (i
& 0xff) << 24;
599 return (void *)result
;
602 #define sampleSize 65536
603 void skiplist_test(struct vty
*vty
)
607 void *keys
[sampleSize
];
610 zlog_debug("%s: entry", __func__
);
612 l
= skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
, NULL
, NULL
);
614 zlog_debug("%s: skiplist_new returned %p", __func__
, l
);
616 for (i
= 0; i
< 4; i
++) {
618 for (k
= 0; k
< sampleSize
; k
++) {
620 zlog_debug("%s: (%d:%d)", __func__
, i
, k
);
622 // keys[k] = (void *)random();
623 keys
[k
] = (void *)scramble(k
);
624 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
625 zlog_debug("error in insert #%d,#%d", i
, k
);
628 zlog_debug("%s: inserts done", __func__
);
630 for (k
= 0; k
< sampleSize
; k
++) {
633 zlog_debug("[%d:%d]", i
, k
);
634 if (skiplist_search(l
, keys
[k
], &v
))
635 zlog_debug("error in search #%d,#%d", i
, k
);
638 zlog_debug("search returned wrong value");
642 for (k
= 0; k
< sampleSize
; k
++) {
645 zlog_debug("<%d:%d>", i
, k
);
646 if (skiplist_delete(l
, keys
[k
], keys
[k
]))
647 zlog_debug("error in delete");
648 keys
[k
] = (void *)scramble(k
^ 0xf0f0f0f0);
649 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
650 zlog_debug("error in insert #%d,#%d", i
, k
);
653 for (k
= 0; k
< sampleSize
; k
++) {
656 zlog_debug("{%d:%d}", i
, k
);
657 if (skiplist_delete_first(l
))
658 zlog_debug("error in delete_first");