1*549b59edSchristos /* $NetBSD: ldap_avl.h,v 1.2 2021/08/14 16:14:55 christos Exp $ */ 2e670fd5cSchristos 3e670fd5cSchristos /* ldap_avl.h - avl tree definitions */ 4e670fd5cSchristos /* $OpenLDAP$ */ 5e670fd5cSchristos /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 6e670fd5cSchristos * 7e670fd5cSchristos * Copyright 1998-2021 The OpenLDAP Foundation. 8e670fd5cSchristos * All rights reserved. 9e670fd5cSchristos * 10e670fd5cSchristos * Redistribution and use in source and binary forms, with or without 11e670fd5cSchristos * modification, are permitted only as authorized by the OpenLDAP 12e670fd5cSchristos * Public License. 13e670fd5cSchristos * 14e670fd5cSchristos * A copy of this license is available in file LICENSE in the 15e670fd5cSchristos * top-level directory of the distribution or, alternatively, at 16e670fd5cSchristos * <http://www.OpenLDAP.org/license.html>. 17e670fd5cSchristos */ 18e670fd5cSchristos /* Portions Copyright (c) 1993 Regents of the University of Michigan. 19e670fd5cSchristos * All rights reserved. 20e670fd5cSchristos * 21e670fd5cSchristos * Redistribution and use in source and binary forms are permitted 22e670fd5cSchristos * provided that this notice is preserved and that due credit is given 23e670fd5cSchristos * to the University of Michigan at Ann Arbor. The name of the University 24e670fd5cSchristos * may not be used to endorse or promote products derived from this 25e670fd5cSchristos * software without specific prior written permission. This software 26e670fd5cSchristos * is provided ``as is'' without express or implied warranty. 27e670fd5cSchristos */ 28e670fd5cSchristos 29e670fd5cSchristos 30e670fd5cSchristos #ifndef _AVL 31e670fd5cSchristos #define _AVL 32e670fd5cSchristos 33e670fd5cSchristos #include <ldap_cdefs.h> 34e670fd5cSchristos 35e670fd5cSchristos /* 36e670fd5cSchristos * this structure represents a generic avl tree node. 37e670fd5cSchristos */ 38e670fd5cSchristos 39e670fd5cSchristos LDAP_BEGIN_DECL 40e670fd5cSchristos 41e670fd5cSchristos typedef struct avlnode Avlnode; 42e670fd5cSchristos 43e670fd5cSchristos struct avlnode { 44e670fd5cSchristos void* avl_data; 45e670fd5cSchristos struct avlnode *avl_link[2]; 46e670fd5cSchristos char avl_bits[2]; 47e670fd5cSchristos signed char avl_bf; 48e670fd5cSchristos }; 49e670fd5cSchristos 50e670fd5cSchristos #define avl_left avl_link[0] 51e670fd5cSchristos #define avl_right avl_link[1] 52e670fd5cSchristos #define avl_lbit avl_bits[0] 53e670fd5cSchristos #define avl_rbit avl_bits[1] 54e670fd5cSchristos 55e670fd5cSchristos typedef struct tavlnode TAvlnode; 56e670fd5cSchristos 57e670fd5cSchristos struct tavlnode { 58e670fd5cSchristos void* avl_data; 59e670fd5cSchristos struct tavlnode *avl_link[2]; 60e670fd5cSchristos char avl_bits[2]; 61e670fd5cSchristos signed char avl_bf; 62e670fd5cSchristos }; 63e670fd5cSchristos 64e670fd5cSchristos #ifdef AVL_INTERNAL 65e670fd5cSchristos 66e670fd5cSchristos /* balance factor values */ 67e670fd5cSchristos #define LH (-1) 68e670fd5cSchristos #define EH 0 69e670fd5cSchristos #define RH 1 70e670fd5cSchristos 71e670fd5cSchristos #define avl_bf2str(bf) ((bf) == -1 ? "LH" : (bf) == 0 ? "EH" : (bf) == 1 ? "RH" : "(unknown)" ) 72e670fd5cSchristos 73e670fd5cSchristos /* thread bits */ 74e670fd5cSchristos #define AVL_CHILD 0 75e670fd5cSchristos #define AVL_THREAD 1 76e670fd5cSchristos 77e670fd5cSchristos /* avl routines */ 78e670fd5cSchristos #define ldap_avl_getone(x) ((x) == 0 ? 0 : (x)->avl_data) 79e670fd5cSchristos #define ldap_avl_onenode(x) ((x) == 0 || ((x)->avl_left == 0 && (x)->avl_right == 0)) 80e670fd5cSchristos 81e670fd5cSchristos #endif /* AVL_INTERNALS */ 82e670fd5cSchristos 83e670fd5cSchristos #define ldap_avl_child(x,dir) ((x)->avl_bits[dir]) == AVL_CHILD ? \ 84e670fd5cSchristos (x)->avl_link[dir] : NULL 85e670fd5cSchristos #define ldap_avl_lchild(x) ldap_avl_child(x,0) 86e670fd5cSchristos #define ldap_avl_rchild(x) ldap_avl_child(x,1) 87e670fd5cSchristos 88e670fd5cSchristos typedef int (*AVL_APPLY) LDAP_P((void *, void*)); 89e670fd5cSchristos typedef int (*AVL_CMP) LDAP_P((const void*, const void*)); 90e670fd5cSchristos typedef int (*AVL_DUP) LDAP_P((void*, void*)); 91e670fd5cSchristos typedef void (*AVL_FREE) LDAP_P((void*)); 92e670fd5cSchristos 93e670fd5cSchristos LDAP_AVL_F( int ) 94e670fd5cSchristos ldap_avl_free LDAP_P(( Avlnode *root, AVL_FREE dfree )); 95e670fd5cSchristos 96e670fd5cSchristos LDAP_AVL_F( int ) 97e670fd5cSchristos ldap_avl_insert LDAP_P((Avlnode **, void*, AVL_CMP, AVL_DUP)); 98e670fd5cSchristos 99e670fd5cSchristos LDAP_AVL_F( void* ) 100e670fd5cSchristos ldap_avl_delete LDAP_P((Avlnode **, void*, AVL_CMP)); 101e670fd5cSchristos 102e670fd5cSchristos LDAP_AVL_F( void* ) 103e670fd5cSchristos ldap_avl_find LDAP_P((Avlnode *, const void*, AVL_CMP)); 104e670fd5cSchristos 105e670fd5cSchristos LDAP_AVL_F( Avlnode* ) 106e670fd5cSchristos ldap_avl_find2 LDAP_P((Avlnode *, const void*, AVL_CMP)); 107e670fd5cSchristos 108e670fd5cSchristos LDAP_AVL_F( void* ) 109e670fd5cSchristos ldap_avl_find_lin LDAP_P((Avlnode *, const void*, AVL_CMP)); 110e670fd5cSchristos 111e670fd5cSchristos #ifdef AVL_NONREENTRANT 112e670fd5cSchristos LDAP_AVL_F( void* ) 113e670fd5cSchristos ldap_avl_getfirst LDAP_P((Avlnode *)); 114e670fd5cSchristos 115e670fd5cSchristos LDAP_AVL_F( void* ) 116e670fd5cSchristos ldap_avl_getnext LDAP_P((void)); 117e670fd5cSchristos #endif 118e670fd5cSchristos 119e670fd5cSchristos LDAP_AVL_F( int ) 120e670fd5cSchristos ldap_avl_dup_error LDAP_P((void*, void*)); 121e670fd5cSchristos 122e670fd5cSchristos LDAP_AVL_F( int ) 123e670fd5cSchristos ldap_avl_dup_ok LDAP_P((void*, void*)); 124e670fd5cSchristos 125e670fd5cSchristos LDAP_AVL_F( int ) 126e670fd5cSchristos ldap_avl_apply LDAP_P((Avlnode *, AVL_APPLY, void*, int, int)); 127e670fd5cSchristos 128e670fd5cSchristos LDAP_AVL_F( int ) 129e670fd5cSchristos ldap_avl_prefixapply LDAP_P((Avlnode *, void*, AVL_CMP, void*, AVL_CMP, void*, int)); 130e670fd5cSchristos 131e670fd5cSchristos LDAP_AVL_F( int ) 132e670fd5cSchristos ldap_tavl_free LDAP_P(( TAvlnode *root, AVL_FREE dfree )); 133e670fd5cSchristos 134e670fd5cSchristos LDAP_AVL_F( int ) 135e670fd5cSchristos ldap_tavl_insert LDAP_P((TAvlnode **, void*, AVL_CMP, AVL_DUP)); 136e670fd5cSchristos 137e670fd5cSchristos LDAP_AVL_F( void* ) 138e670fd5cSchristos ldap_tavl_delete LDAP_P((TAvlnode **, void*, AVL_CMP)); 139e670fd5cSchristos 140e670fd5cSchristos LDAP_AVL_F( void* ) 141e670fd5cSchristos ldap_tavl_find LDAP_P((TAvlnode *, const void*, AVL_CMP)); 142e670fd5cSchristos 143e670fd5cSchristos LDAP_AVL_F( TAvlnode* ) 144e670fd5cSchristos ldap_tavl_find2 LDAP_P((TAvlnode *, const void*, AVL_CMP)); 145e670fd5cSchristos 146e670fd5cSchristos LDAP_AVL_F( TAvlnode* ) 147e670fd5cSchristos ldap_tavl_find3 LDAP_P((TAvlnode *, const void*, AVL_CMP, int *ret)); 148e670fd5cSchristos 149e670fd5cSchristos #define TAVL_DIR_LEFT 0 150e670fd5cSchristos #define TAVL_DIR_RIGHT 1 151e670fd5cSchristos 152e670fd5cSchristos LDAP_AVL_F( TAvlnode* ) 153e670fd5cSchristos ldap_tavl_end LDAP_P((TAvlnode *, int direction)); 154e670fd5cSchristos 155e670fd5cSchristos LDAP_AVL_F( TAvlnode* ) 156e670fd5cSchristos ldap_tavl_next LDAP_P((TAvlnode *, int direction)); 157e670fd5cSchristos 158e670fd5cSchristos /* apply traversal types */ 159e670fd5cSchristos #define AVL_PREORDER 1 160e670fd5cSchristos #define AVL_INORDER 2 161e670fd5cSchristos #define AVL_POSTORDER 3 162e670fd5cSchristos /* what apply returns if it ran out of nodes */ 163e670fd5cSchristos #define AVL_NOMORE (-6) 164e670fd5cSchristos 165e670fd5cSchristos LDAP_END_DECL 166e670fd5cSchristos 167e670fd5cSchristos #endif /* _AVL */ 168