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) do {\
79 if ((sl)->header->forward[0] && !(sl)->last) assert(0); \
80 if (!(sl)->header->forward[0] && (sl)->last) assert(0); \
90 register int level
= 0;
94 if (randomsLeft
<= 0) {
95 randomBits
= random();
96 randomsLeft
= BitsInRandom
/2;
104 if (level
>= MaxLevel
)
113 default_cmp(void *key1
, void *key2
)
123 skiplist_count(struct skiplist
*l
)
131 int (*cmp
) (void *key1
, void *key2
),
132 void (*del
) (void *val
))
134 struct skiplist
*new;
136 new = XCALLOC (MTYPE_SKIP_LIST
, sizeof (struct skiplist
));
141 new->header
= newNodeOfLevel(MaxNumberOfLevels
);
142 new->stats
= newNodeOfLevel(MaxNumberOfLevels
);
148 new->cmp
= default_cmp
;
153 skiplist_last_created
= new; /* debug */
159 skiplist_free(struct skiplist
*l
)
161 register struct skiplistnode
*p
, *q
;
167 if (l
->del
&& p
!= l
->header
)
169 XFREE(MTYPE_SKIP_LIST_NODE
, p
);
173 XFREE(MTYPE_SKIP_LIST_NODE
, l
->stats
);
174 XFREE(MTYPE_SKIP_LIST
, l
);
180 register struct skiplist
*l
,
182 register void *value
)
185 struct skiplistnode
*update
[MaxNumberOfLevels
];
186 register struct skiplistnode
*p
, *q
;
192 zlog_err("%s: key is 0, value is %p", __func__
, value
);
198 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0) p
= q
;
202 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) &&
203 q
&& ((*l
->cmp
)(q
->key
, key
) == 0)) {
211 update
[k
] = l
->header
;
214 q
= newNodeOfLevel(k
);
217 #if SKIPLIST_0TIMER_DEBUG
218 q
->flags
= SKIPLIST_NODE_FLAG_INSERTED
; /* debug */
221 ++(l
->stats
->forward
[k
]);
223 zlog_debug("%s: incremented stats @%p:%d, now %ld", __func__
, l
, k
,
224 l
->stats
->forward
[k
] - (struct skiplistnode
*)NULL
);
229 q
->forward
[k
] = p
->forward
[k
];
234 * If this is the last item in the list, update the "last" pointer
236 if (!q
->forward
[0]) {
249 register struct skiplist
*l
,
251 register void *value
) /* used only if duplicates allowed */
254 struct skiplistnode
*update
[MaxNumberOfLevels
];
255 register struct skiplistnode
*p
, *q
;
259 /* to make debugging easier */
260 for (k
= 0; k
< MaxNumberOfLevels
; ++k
)
266 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0) p
= q
;
270 if (l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) {
271 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0) && (q
->value
!= value
)) {
273 for (i
= 0; i
<= l
->level
; ++i
) {
274 if (update
[i
]->forward
[i
] == q
)
281 if (q
&& (*l
->cmp
)(q
->key
, key
) == 0) {
282 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
) ||
283 (q
->value
== value
)) {
286 * found node to delete
288 #if SKIPLIST_0TIMER_DEBUG
289 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
292 * If we are deleting the last element of the list,
293 * update the list's "last" pointer.
296 if (update
[0] == l
->header
)
302 for(k
=0; k
<=m
&& (p
=update
[k
])->forward
[k
] == q
; k
++) {
303 p
->forward
[k
] = q
->forward
[k
];
305 --(l
->stats
->forward
[k
-1]);
307 zlog_debug("%s: decremented stats @%p:%d, now %ld",
309 l
->stats
->forward
[k
-1] - (struct skiplistnode
*)NULL
);
313 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
314 while( l
->header
->forward
[m
] == NULL
&& m
> 0 )
328 * Obtain first value matching "key". Unless SKIPLIST_FLAG_ALLOW_DUPLICATES
329 * is set, this will also be the only value matching "key".
331 * Also set a cursor for use with skiplist_next_value.
334 skiplist_first_value(
335 register struct skiplist
*l
, /* in */
336 register void *key
, /* in */
337 void **valuePointer
, /* out */
338 void **cursor
) /* out */
341 register struct skiplistnode
*p
, *q
;
347 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
352 if (!q
|| (*l
->cmp
)(q
->key
, key
))
356 *valuePointer
= q
->value
;
366 register struct skiplist
*l
,
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.
385 register struct skiplist
*l
, /* in */
386 register void *key
, /* in */
387 void **valuePointer
, /* in/out */
388 void **cursor
) /* in/out */
391 register struct skiplistnode
*p
, *q
;
395 if (!(l
->flags
& SKIPLIST_FLAG_ALLOW_DUPLICATES
)) {
399 if (!cursor
|| !*cursor
) {
407 while (q
= p
->forward
[k
], q
&& (*l
->cmp
)(q
->key
, key
) < 0)
412 * Find matching value
414 while (q
&& ((*l
->cmp
)(q
->key
, key
) == 0) && (q
->value
!= *valuePointer
)) {
418 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0) || (q
->value
!= *valuePointer
)) {
426 q
= (struct skiplistnode
*)*cursor
;
435 * If we reached end-of-list or if the key is no longer the same,
438 if (!q
|| ((*l
->cmp
)(q
->key
, key
) != 0))
441 *valuePointer
= q
->value
;
449 register struct skiplist
*l
,
453 register struct skiplistnode
*p
;
456 p
= l
->header
->forward
[0];
461 *keyPointer
= p
->key
;
464 *valuePointer
= p
->value
;
473 register struct skiplist
*l
,
480 *keyPointer
= l
->last
->key
;
482 *valuePointer
= l
->last
->value
;
493 register struct skiplist
*l
)
502 * Use this to walk the list. Caller sets *cursor to NULL to obtain
503 * first element. Return value of 0 indicates valid cursor/element
504 * returned, otherwise NULL cursor arg or EOL.
508 register struct skiplist
*l
, /* in */
509 void **keyPointer
, /* out */
510 void **valuePointer
, /* out */
511 void **cursor
) /* in/out */
513 struct skiplistnode
*p
;
521 p
= l
->header
->forward
[0];
532 *keyPointer
= p
->key
;
535 *valuePointer
= p
->value
;
543 skiplist_delete_first(
544 register struct skiplist
*l
)
547 register struct skiplistnode
*p
, *q
;
553 q
= l
->header
->forward
[0];
558 for (k
= l
->level
; k
>= 0; --k
) {
559 if (p
->forward
[k
] == q
) {
560 p
->forward
[k
] = q
->forward
[k
];
561 if ((k
== l
->level
) && (p
->forward
[k
] == NULL
) && (l
->level
> 0))
568 #if SKIPLIST_0TIMER_DEBUG
569 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
572 * If we are deleting the last element of the list,
573 * update the list's "last" pointer.
579 --(l
->stats
->forward
[nodelevel
]);
581 zlog_debug("%s: decremented stats @%p:%d, now %ld", __func__
, l
, nodelevel
,
582 l
->stats
->forward
[nodelevel
] - (struct skiplistnode
*)NULL
);
588 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
598 skiplist_debug(struct vty
*vty
, struct skiplist
*l
)
603 l
= skiplist_last_created
;
604 vty_out(vty
, "Skiplist %p has max level %d%s", l
, l
->level
, VTY_NEWLINE
);
605 for (i
= l
->level
; i
>= 0; --i
)
606 vty_out(vty
, " @%d: %ld%s",
607 i
, (long)((l
->stats
->forward
[i
]) - (struct skiplistnode
*)NULL
),
616 result
= (i
& 0xff) << 24;
619 return (void *)result
;
622 #define sampleSize 65536
624 skiplist_test(struct vty
*vty
) {
627 void *keys
[sampleSize
];
630 zlog_debug("%s: entry", __func__
);
632 l
= skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
, NULL
, NULL
);
634 zlog_debug("%s: skiplist_new returned %p", __func__
, l
);
636 for (i
=0; i
< 4; i
++) {
638 for (k
=0; k
< sampleSize
; k
++) {
640 zlog_debug("%s: (%d:%d)", __func__
, i
, k
);
642 //keys[k] = (void *)random();
643 keys
[k
] = (void *)scramble(k
);
644 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
645 zlog_debug("error in insert #%d,#%d",i
,k
);
648 zlog_debug("%s: inserts done", __func__
);
650 for (k
=0; k
< sampleSize
; k
++) {
653 zlog_debug("[%d:%d]", i
, k
);
654 if (skiplist_search(l
, keys
[k
], &v
))
655 zlog_debug("error in search #%d,#%d",i
,k
);
658 zlog_debug("search returned wrong value");
663 for (k
=0; k
< sampleSize
; k
++) {
666 zlog_debug("<%d:%d>", i
, k
);
667 if (skiplist_delete(l
, keys
[k
], keys
[k
]))
668 zlog_debug("error in delete");
669 keys
[k
] = (void *)scramble(k
^ 0xf0f0f0f0);
670 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
671 zlog_debug("error in insert #%d,#%d",i
,k
);
674 for (k
=0; k
< sampleSize
; k
++) {
677 zlog_debug("{%d:%d}", i
, k
);
678 if (skiplist_delete_first(l
))
679 zlog_debug("error in delete_first");