ITS#4549 add tavl_find3() to return closest match

This commit is contained in:
Howard Chu 2006-05-19 15:24:16 +00:00
parent f0adb769ad
commit 2d97c1cae1
2 changed files with 23 additions and 0 deletions

View File

@ -134,6 +134,9 @@ tavl_find LDAP_P((Avlnode *, const void*, AVL_CMP));
LDAP_AVL_F( Avlnode* )
tavl_find2 LDAP_P((Avlnode *, const void*, AVL_CMP));
LDAP_AVL_F( Avlnode* )
tavl_find3 LDAP_P((Avlnode *, const void*, AVL_CMP, int *ret));
#define TAVL_DIR_LEFT 0
#define TAVL_DIR_RIGHT 1

View File

@ -443,6 +443,26 @@ tavl_free( Avlnode *root, AVL_FREE dfree )
* < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
*/
/*
* tavl_find2 - returns Avlnode instead of data pointer.
* tavl_find3 - as above, but returns Avlnode even if no match is found.
* also return the last comparison result in ret.
*/
Avlnode *
tavl_find3( Avlnode *root, const void *data, AVL_CMP fcmp, int *ret )
{
int cmp, dir;
Avlnode *prev;
while ( root != 0 && (cmp = (*fcmp)( data, root->avl_data )) != 0 ) {
prev = root;
dir = cmp > 0;
root = avl_child( root, dir );
}
*ret = cmp;
return root ? root : prev;
}
Avlnode *
tavl_find2( Avlnode *root, const void *data, AVL_CMP fcmp )
{