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
;
450 register struct skiplist
*l
,
454 register struct skiplistnode
*p
;
457 p
= l
->header
->forward
[0];
462 *keyPointer
= p
->key
;
465 *valuePointer
= p
->value
;
474 register struct skiplist
*l
,
481 *keyPointer
= l
->last
->key
;
483 *valuePointer
= l
->last
->value
;
494 register struct skiplist
*l
)
503 * Use this to walk the list. Caller sets *cursor to NULL to obtain
504 * first element. Return value of 0 indicates valid cursor/element
505 * returned, otherwise NULL cursor arg or EOL.
509 register struct skiplist
*l
, /* in */
510 void **keyPointer
, /* out */
511 void **valuePointer
, /* out */
512 void **cursor
) /* in/out */
514 struct skiplistnode
*p
;
522 p
= l
->header
->forward
[0];
533 *keyPointer
= p
->key
;
536 *valuePointer
= p
->value
;
544 skiplist_delete_first(
545 register struct skiplist
*l
)
548 register struct skiplistnode
*p
, *q
;
554 q
= l
->header
->forward
[0];
559 for (k
= l
->level
; k
>= 0; --k
) {
560 if (p
->forward
[k
] == q
) {
561 p
->forward
[k
] = q
->forward
[k
];
562 if ((k
== l
->level
) && (p
->forward
[k
] == NULL
) && (l
->level
> 0))
569 #if SKIPLIST_0TIMER_DEBUG
570 q
->flags
&= ~SKIPLIST_NODE_FLAG_INSERTED
;
573 * If we are deleting the last element of the list,
574 * update the list's "last" pointer.
580 --(l
->stats
->forward
[nodelevel
]);
582 zlog_debug("%s: decremented stats @%p:%d, now %ld", __func__
, l
, nodelevel
,
583 l
->stats
->forward
[nodelevel
] - (struct skiplistnode
*)NULL
);
589 XFREE(MTYPE_SKIP_LIST_NODE
, q
);
599 skiplist_debug(struct vty
*vty
, struct skiplist
*l
)
604 l
= skiplist_last_created
;
605 vty_out(vty
, "Skiplist %p has max level %d%s", l
, l
->level
, VTY_NEWLINE
);
606 for (i
= l
->level
; i
>= 0; --i
)
607 vty_out(vty
, " @%d: %ld%s",
608 i
, (long)((l
->stats
->forward
[i
]) - (struct skiplistnode
*)NULL
),
617 result
= (i
& 0xff) << 24;
620 return (void *)result
;
623 #define sampleSize 65536
625 skiplist_test(struct vty
*vty
) {
628 void *keys
[sampleSize
];
631 zlog_debug("%s: entry", __func__
);
633 l
= skiplist_new(SKIPLIST_FLAG_ALLOW_DUPLICATES
, NULL
, NULL
);
635 zlog_debug("%s: skiplist_new returned %p", __func__
, l
);
637 for (i
=0; i
< 4; i
++) {
639 for (k
=0; k
< sampleSize
; k
++) {
641 zlog_debug("%s: (%d:%d)", __func__
, i
, k
);
643 //keys[k] = (void *)random();
644 keys
[k
] = (void *)scramble(k
);
645 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
646 zlog_debug("error in insert #%d,#%d",i
,k
);
649 zlog_debug("%s: inserts done", __func__
);
651 for (k
=0; k
< sampleSize
; k
++) {
654 zlog_debug("[%d:%d]", i
, k
);
655 if (skiplist_search(l
, keys
[k
], &v
))
656 zlog_debug("error in search #%d,#%d",i
,k
);
659 zlog_debug("search returned wrong value");
664 for (k
=0; k
< sampleSize
; k
++) {
667 zlog_debug("<%d:%d>", i
, k
);
668 if (skiplist_delete(l
, keys
[k
], keys
[k
]))
669 zlog_debug("error in delete");
670 keys
[k
] = (void *)scramble(k
^ 0xf0f0f0f0);
671 if (skiplist_insert(l
, keys
[k
], keys
[k
]))
672 zlog_debug("error in insert #%d,#%d",i
,k
);
675 for (k
=0; k
< sampleSize
; k
++) {
678 zlog_debug("{%d:%d}", i
, k
);
679 if (skiplist_delete_first(l
))
680 zlog_debug("error in delete_first");