summaryrefslogtreecommitdiff
path: root/tables/apr_skiplist.c
diff options
context:
space:
mode:
authorylavic <ylavic@13f79535-47bb-0310-9956-ffa450edef68>2015-04-09 14:36:27 +0000
committerylavic <ylavic@13f79535-47bb-0310-9956-ffa450edef68>2015-04-09 14:36:27 +0000
commitd75c980a6474c65fb3efe16c478aeecb18e067a0 (patch)
treec1165a2f6577462a130f24adaede8d7703af2595 /tables/apr_skiplist.c
parentb3b79f624111eca33cf54c3198469bfc58671fca (diff)
downloadlibapr-d75c980a6474c65fb3efe16c478aeecb18e067a0.tar.gz
skiplist: fix the minimum height (to one).
The height of a skiplist is always at least one, for the top node, even if our implementation may delay its creation on the first insert or delete it on the last remove (for optimization purpose). This also helps apr_skiplist_remove() to never return zero (not found) when it deletes the last node. git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@1672366 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'tables/apr_skiplist.c')
-rw-r--r--tables/apr_skiplist.c24
1 files changed, 17 insertions, 7 deletions
diff --git a/tables/apr_skiplist.c b/tables/apr_skiplist.c
index 6075bb60c..b5027ecf7 100644
--- a/tables/apr_skiplist.c
+++ b/tables/apr_skiplist.c
@@ -428,24 +428,34 @@ APR_DECLARE(void *) apr_skiplist_element(apr_skiplistnode *iter)
static int skiplisti_remove(apr_skiplist *sl, apr_skiplistnode *m,
apr_skiplist_freefunc myfree);
+static APR_INLINE int skiplist_height(const apr_skiplist *sl)
+{
+ /* Skiplists (even empty) always have a top node, although this
+ * implementation defers its creation until the first insert, or
+ * deletes it with the last remove. We want the real height here.
+ */
+ return sl->height ? sl->height : 1;
+}
+
static apr_skiplistnode *insert_compare(apr_skiplist *sl, void *data,
apr_skiplist_compare comp, int add,
apr_skiplist_freefunc myfree)
{
apr_skiplistnode *m, *p, *tmp, *ret = NULL;
- int ch, nh = 1;
+ int ch, top_nh, nh = 1;
+ ch = skiplist_height(sl);
if (sl->preheight) {
while (nh < sl->preheight && get_b_rand()) {
nh++;
}
}
else {
- while (nh <= sl->height && get_b_rand()) {
+ while (nh <= ch && get_b_rand()) {
nh++;
}
}
- ch = sl->height;
+ top_nh = nh;
/* Now we have in nh the height at which we wish to insert our new node,
* and in ch the current height: don't create skip paths to the inserted
@@ -477,8 +487,8 @@ static apr_skiplistnode *insert_compare(apr_skiplist *sl, void *data,
if (top != sl->top) {
m = sl->top;
skiplist_qclear(&sl->stack_q);
- ch = sl->height;
- nh = ch + 1;
+ ch = skiplist_height(sl);
+ nh = top_nh;
}
continue;
}
@@ -673,7 +683,7 @@ static int skiplisti_remove(apr_skiplist *sl, apr_skiplistnode *m,
sl->bottom = sl->bottomend = NULL;
sl->topend = NULL;
}
- return sl->height; /* return 1; ?? */
+ return skiplist_height(sl);
}
APR_DECLARE(int) apr_skiplist_remove_node(apr_skiplist *sl,
@@ -783,7 +793,7 @@ APR_DECLARE(size_t) apr_skiplist_size(const apr_skiplist *sl)
APR_DECLARE(int) apr_skiplist_height(const apr_skiplist *sl)
{
- return sl->height;
+ return skiplist_height(sl);
}
APR_DECLARE(int) apr_skiplist_preheight(const apr_skiplist *sl)