X-Git-Url: https://tinc-vpn.org/git/browse?p=tinc;a=blobdiff_plain;f=lib%2Frbl.c;h=1a40535051a5b3704ff9fe0d5882cf3272555c00;hp=d79c5030726e47faf37f07c750dbb11be0069133;hb=394ed3fb174bb629bfb4b441fe58842562f955de;hpb=cc7c078774db955cece9b263022e6c1ca955fc10 diff --git a/lib/rbl.c b/lib/rbl.c index d79c5030..1a405350 100644 --- a/lib/rbl.c +++ b/lib/rbl.c @@ -17,12 +17,16 @@ along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - $Id: rbl.c,v 1.1.2.6 2000/11/19 11:05:59 guus Exp $ + $Id: rbl.c,v 1.1.2.11 2000/11/22 19:14:08 guus Exp $ */ +#include "config.h" + +#include #include #include "rbl.h" +#include /* Allocate a new rbl node */ rbl_t *new_rbl() @@ -33,6 +37,8 @@ rbl_t *new_rbl() /* Free a rbl node */ void free_rbl(rbl_t *rbl) { + if(rbl->data && rbl->tree->delete) + rbl->tree->delete(rbl->data); free(rbl); } @@ -58,11 +64,11 @@ void free_rbltree(rbltree_t *tree) } /* Search closest match in the tree */ -rbl_t *rbl_search_closest(rbltree_t *tree, void *data) +rbl_t *rbl_search_closest_rbl(rbltree_t *tree, void *data) { rbl_t *rbl, *next; int result; - + next = rbl = tree->top; while(next) @@ -82,31 +88,53 @@ rbl_t *rbl_search_closest(rbltree_t *tree, void *data) return rbl; } +void *rbl_search_closest(rbltree_t *tree, void *data) +{ + rbl_t *rbl; + + rbl = rbl_search_closest_rbl(tree, data); + + if(rbl) + return rbl->data; + else + return NULL; +} + /* Search exact match or return NULL pointer */ -rbl_t *rbl_search(rbltree_t *tree, void *data) +rbl_t *rbl_search_rbl(rbltree_t *tree, void *data) { - rbl_t *rbl, *next; + rbl_t *rbl; int result; + + rbl = tree->top; - next = rbl = tree->top; - - while(next) + while(rbl) { - rbl = next; - result = tree->compare(data, rbl->data); if(result < 0) - next = rbl->left; + rbl = rbl->left; else if(result > 0) - next = rbl->right; + rbl = rbl->right; else return rbl; } - + return NULL; } +void *rbl_search(rbltree_t *tree, void *data) +{ + rbl_t *rbl; + + rbl = rbl_search_rbl(tree, data); + + if(rbl) + return rbl->data; + else + return NULL; +} + /* Red-black tree operations taken from Introduction to Algorithms, Cormen, Leiserson & Rivest, chapter 14. */ @@ -171,7 +199,7 @@ rbl_t *rbl_insert_rbl(rbltree_t *tree, rbl_t *rbl) if(tree->top) { - closest = rbl_search_closest(tree, rbl->data); + closest = rbl_search_closest_rbl(tree, rbl->data); result = tree->compare(rbl->data, closest->data); if(result < 0) { @@ -414,7 +442,7 @@ rbl_t *rbl_unlink_rbl(rbl_t *rbl) if(y->color == RBL_BLACK && x) rbl_delete_fixup(x); - + return rbl; } @@ -423,44 +451,34 @@ rbl_t *rbl_unlink(rbltree_t *tree, void *data) { rbl_t *rbl; - rbl = rbl_search(tree, data); + rbl = rbl_search_rbl(tree, data); if(rbl) - return rbl_unlink_rbl(rbl); - else - return NULL; + rbl_unlink_rbl(rbl); + + return rbl; } /* Unlink node and free it */ void rbl_delete_rbl(rbl_t *rbl) { - free_rbl(rbl_unlink_rbl(rbl)); + rbl_unlink_rbl(rbl); + free_rbl(rbl); } /* Search node in tree, unlink and free it */ void rbl_delete(rbltree_t *tree, void *data) { - free_rbl(rbl_unlink(tree, data)); -} - -rbl_unlink_rbltree_branch(rbl_t *rbl) -{ - if(rbl->left) - rbl_unlink_rbltree_branch(rbl->left); + rbl_t *rbl; - if(rbl->right) - rbl_unlink_rbltree_branch(rbl->right); + rbl = rbl_unlink(tree, data); - if(rbl->parent) - { - if(rbl == rbl->parent->left) - rbl->parent->left = NULL; - else - rbl->parent->right = NULL; + if(rbl) + free_rbl(rbl); } /* Optimized unlinking for a complete tree */ -rbl_unlink_rbltree(rbltree_t *tree) +void rbl_unlink_rbltree(rbltree_t *tree) { rbl_t *rbl, *next; @@ -481,14 +499,14 @@ rbl_unlink_rbltree(rbltree_t *tree) } /* Optimized deletion for a complete tree */ -rbl_delete_rbltree(rbltree_t *tree) +void rbl_delete_rbltree(rbltree_t *tree) { rbl_t *rbl, *next; for(rbl = tree->head; rbl; rbl = next) { next = rbl->next; - tree->delete(rbl->data) + free_rbl(rbl); } tree->top = NULL;