X-Git-Url: http://git.silcnet.org/gitweb/?a=blobdiff_plain;f=lib%2Fsilcutil%2Fsilcavltree.c;h=837803e42498191460113e32104ef0fce7a39324;hb=abc4156464a66d9961da10b9882df8c32cdc6164;hp=cef09195d6dd6aa4414f764695ec5a4afb0cd481;hpb=a6428c51f8544ca92870eb573f8a7bc7d1e16d19;p=runtime.git diff --git a/lib/silcutil/silcavltree.c b/lib/silcutil/silcavltree.c index cef09195..837803e4 100644 --- a/lib/silcutil/silcavltree.c +++ b/lib/silcutil/silcavltree.c @@ -45,6 +45,17 @@ #include +/* Define to 1 if you want hash table debug enabled */ +#ifndef SILC_AVL_TREE_DEBUG +#define SILC_AVL_TREE_DEBUG 0 +#endif /* !SILC_AVL_TREE_DEBUG */ + +#if SILC_ALV_TREE_DEBUG == 1 +#define SILC_TREE_DEBUG(fmt) SILC_LOG_DEBUG(fmt) +#else +#define SILC_TREE_DEBUG(fmt) +#endif + /************************ Static utility functions **************************/ /* Rotate left @@ -142,27 +153,29 @@ static int silc_avl_tree_rot_right(SilcTree *tree, SilcTreeHeader *h) /* Find entry */ void *silc_avl_tree_find(SilcTree *tree, void *entry, - SilcTreeCompare compare, void *context) + SilcCompare compare, void *context) { SilcTreeHeader *h; - SilcTreeCompare cmp = compare ? compare : tree->compare; + SilcCompare cmp = compare ? compare : tree->compare; void *cmp_context = compare ? context : tree->context; int ret; - SILC_LOG_DEBUG(("AVL tree %p, find %p", tree, entry)); + SILC_TREE_DEBUG(("AVL tree %p, find %p", tree, entry)); h = tree->root; while (h) { ret = cmp(entry, SILC_TREE_GET_ENTRY(tree, h), cmp_context); - if (!ret) { - SILC_LOG_DEBUG(("Found %p", SILC_TREE_GET_ENTRY(tree, h))); + if (ret == SILC_COMPARE_EQUAL_TO) { + SILC_TREE_DEBUG(("Found %p", SILC_TREE_GET_ENTRY(tree, h))); return SILC_TREE_GET_ENTRY(tree, h); } + if (ret == SILC_COMPARE_STOP) + return NULL; h = ret > 0 ? h->right : h->left; } - SILC_LOG_DEBUG(("Not found")); - silc_set_errno(SILC_ERR_NOT_FOUND); + SILC_TREE_DEBUG(("Not found")); + silc_set_errno_nofail(SILC_ERR_NOT_FOUND); return NULL; } @@ -173,7 +186,7 @@ SilcBool silc_avl_tree_add(SilcTree *tree, void *entry) SilcTreeHeader *h, *parent = NULL, *q = NULL; int ret = 0; - SILC_LOG_DEBUG(("AVL tree %p, adding %p", tree, entry)); + SILC_TREE_DEBUG(("AVL tree %p, adding %p", tree, entry)); /* If tree is empty, add to root */ if (!tree->root) { @@ -182,7 +195,7 @@ SilcBool silc_avl_tree_add(SilcTree *tree, void *entry) h->t = 0; tree->root = h; - SILC_LOG_DEBUG(("Entry %p added as root", entry)); + SILC_TREE_DEBUG(("Entry %p added as root", entry)); SILC_ASSERT(!tree->count); tree->count = 1; @@ -194,18 +207,18 @@ SilcBool silc_avl_tree_add(SilcTree *tree, void *entry) while (h) { /* Same entry context must not be in tree */ if (entry == SILC_TREE_GET_ENTRY(tree, h)) { - silc_set_errno(SILC_ERR_ALREADY_EXISTS); + silc_set_errno_nofail(SILC_ERR_ALREADY_EXISTS); return FALSE; } ret = tree->compare(entry, SILC_TREE_GET_ENTRY(tree, h), tree->context); - if (!tree->duplicates && ret == 0) { - silc_set_errno(SILC_ERR_ALREADY_EXISTS); + if (!tree->duplicates && ret == SILC_COMPARE_EQUAL_TO) { + silc_set_errno_nofail(SILC_ERR_ALREADY_EXISTS); return FALSE; } /* Duplicate entry, add to list */ - if (ret == 0) { + if (ret == SILC_COMPARE_EQUAL_TO) { q = SILC_TREE_GET_HEADER(tree, entry); q->duplicate = TRUE; q->parent = h; @@ -213,7 +226,7 @@ SilcBool silc_avl_tree_add(SilcTree *tree, void *entry) h->dup->parent = q; q->dup = h->dup; h->dup = q; - SILC_LOG_DEBUG(("Entry %p is duplicate to %p", entry, + SILC_TREE_DEBUG(("Entry %p is duplicate to %p", entry, SILC_TREE_GET_ENTRY(tree, h))); tree->count++; return TRUE; @@ -232,8 +245,8 @@ SilcBool silc_avl_tree_add(SilcTree *tree, void *entry) else parent->right = h; - SILC_LOG_DEBUG(("Entry %p added, parent %p", entry, - SILC_TREE_GET_ENTRY(tree, parent))); + SILC_TREE_DEBUG(("Entry %p added, parent %p", entry, + SILC_TREE_GET_ENTRY(tree, parent))); /* Update the new entry */ h->parent = parent; @@ -274,10 +287,10 @@ SilcBool silc_avl_tree_del(SilcTree *tree, void *entry) SilcTreeHeader *h, *q, *out, *parent = NULL, tmp; int left; - SILC_LOG_DEBUG(("AVL tree %p, delete %p", tree, entry)); + SILC_TREE_DEBUG(("AVL tree %p, delete %p", tree, entry)); if (!tree->root || !entry) { - silc_set_errno(SILC_ERR_INVALID_ARGUMENT); + silc_set_errno_nofail(SILC_ERR_INVALID_ARGUMENT); return FALSE; } @@ -316,7 +329,7 @@ SilcBool silc_avl_tree_del(SilcTree *tree, void *entry) h->right->parent = h->dup; /* Cleanup the deleted entry */ - SILC_LOG_DEBUG(("Deleted %p", SILC_TREE_GET_ENTRY(tree, h))); + SILC_TREE_DEBUG(("Deleted %p", SILC_TREE_GET_ENTRY(tree, h))); h->parent = h->left = h->right = h->dup = NULL; h->t = 0; @@ -358,7 +371,7 @@ SilcBool silc_avl_tree_del(SilcTree *tree, void *entry) out->parent = parent; /* Cleanup the deleted entry */ - SILC_LOG_DEBUG(("Deleted %p", SILC_TREE_GET_ENTRY(tree, h))); + SILC_TREE_DEBUG(("Deleted %p", SILC_TREE_GET_ENTRY(tree, h))); h->parent = h->left = h->right = h->dup = NULL; h->t = 0;