1*433d6423SLionel Sambuc /* Abstract AVL Tree Generic C Package. 2*433d6423SLionel Sambuc ** Interface generation header file. 3*433d6423SLionel Sambuc ** 4*433d6423SLionel Sambuc ** This code is in the public domain. See cavl_tree.html for interface 5*433d6423SLionel Sambuc ** documentation. 6*433d6423SLionel Sambuc ** 7*433d6423SLionel Sambuc ** Version: 1.5 Author: Walt Karas 8*433d6423SLionel Sambuc */ 9*433d6423SLionel Sambuc 10*433d6423SLionel Sambuc /* This header contains the definition of CHAR_BIT (number of bits in a 11*433d6423SLionel Sambuc ** char). */ 12*433d6423SLionel Sambuc #include <limits.h> 13*433d6423SLionel Sambuc 14*433d6423SLionel Sambuc #undef L__ 15*433d6423SLionel Sambuc #undef L__EST_LONG_BIT 16*433d6423SLionel Sambuc #undef L__SIZE 17*433d6423SLionel Sambuc #undef L__SC 18*433d6423SLionel Sambuc #undef L__LONG_BIT 19*433d6423SLionel Sambuc #undef L__BIT_ARR_DEFN 20*433d6423SLionel Sambuc 21*433d6423SLionel Sambuc #ifndef AVL_SEARCH_TYPE_DEFINED_ 22*433d6423SLionel Sambuc #define AVL_SEARCH_TYPE_DEFINED_ 23*433d6423SLionel Sambuc 24*433d6423SLionel Sambuc typedef enum 25*433d6423SLionel Sambuc { 26*433d6423SLionel Sambuc AVL_EQUAL = 1, 27*433d6423SLionel Sambuc AVL_LESS = 2, 28*433d6423SLionel Sambuc AVL_GREATER = 4, 29*433d6423SLionel Sambuc AVL_LESS_EQUAL = AVL_EQUAL | AVL_LESS, 30*433d6423SLionel Sambuc AVL_GREATER_EQUAL = AVL_EQUAL | AVL_GREATER 31*433d6423SLionel Sambuc } 32*433d6423SLionel Sambuc avl_search_type; 33*433d6423SLionel Sambuc 34*433d6423SLionel Sambuc #endif 35*433d6423SLionel Sambuc 36*433d6423SLionel Sambuc #ifdef AVL_UNIQUE 37*433d6423SLionel Sambuc 38*433d6423SLionel Sambuc #define L__ AVL_UNIQUE 39*433d6423SLionel Sambuc 40*433d6423SLionel Sambuc #else 41*433d6423SLionel Sambuc 42*433d6423SLionel Sambuc #define L__(X) X 43*433d6423SLionel Sambuc 44*433d6423SLionel Sambuc #endif 45*433d6423SLionel Sambuc 46*433d6423SLionel Sambuc /* Determine storage class for function prototypes. */ 47*433d6423SLionel Sambuc #ifdef AVL_PRIVATE 48*433d6423SLionel Sambuc 49*433d6423SLionel Sambuc #define L__SC static 50*433d6423SLionel Sambuc 51*433d6423SLionel Sambuc #else 52*433d6423SLionel Sambuc 53*433d6423SLionel Sambuc #define L__SC extern 54*433d6423SLionel Sambuc 55*433d6423SLionel Sambuc #endif 56*433d6423SLionel Sambuc 57*433d6423SLionel Sambuc #ifdef AVL_SIZE 58*433d6423SLionel Sambuc 59*433d6423SLionel Sambuc #define L__SIZE AVL_SIZE 60*433d6423SLionel Sambuc 61*433d6423SLionel Sambuc #else 62*433d6423SLionel Sambuc 63*433d6423SLionel Sambuc #define L__SIZE unsigned long 64*433d6423SLionel Sambuc 65*433d6423SLionel Sambuc #endif 66*433d6423SLionel Sambuc 67*433d6423SLionel Sambuc typedef struct 68*433d6423SLionel Sambuc { 69*433d6423SLionel Sambuc #ifdef AVL_INSIDE_STRUCT 70*433d6423SLionel Sambuc 71*433d6423SLionel Sambuc AVL_INSIDE_STRUCT 72*433d6423SLionel Sambuc 73*433d6423SLionel Sambuc #endif 74*433d6423SLionel Sambuc 75*433d6423SLionel Sambuc AVL_HANDLE root; 76*433d6423SLionel Sambuc } 77*433d6423SLionel Sambuc L__(avl); 78*433d6423SLionel Sambuc 79*433d6423SLionel Sambuc /* Function prototypes. */ 80*433d6423SLionel Sambuc 81*433d6423SLionel Sambuc L__SC void L__(init)(L__(avl) *tree); 82*433d6423SLionel Sambuc 83*433d6423SLionel Sambuc L__SC int L__(is_empty)(L__(avl) *tree); 84*433d6423SLionel Sambuc 85*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(insert)(L__(avl) *tree, AVL_HANDLE h); 86*433d6423SLionel Sambuc 87*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(search)(L__(avl) *tree, AVL_KEY k, avl_search_type st); 88*433d6423SLionel Sambuc 89*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(search_least)(L__(avl) *tree); 90*433d6423SLionel Sambuc 91*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(search_greatest)(L__(avl) *tree); 92*433d6423SLionel Sambuc 93*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(search_root)(L__(avl) *tree); 94*433d6423SLionel Sambuc 95*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(remove)(L__(avl) *tree, AVL_KEY k); 96*433d6423SLionel Sambuc 97*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(subst)(L__(avl) *tree, AVL_HANDLE new_node); 98*433d6423SLionel Sambuc 99*433d6423SLionel Sambuc #ifdef AVL_BUILD_ITER_TYPE 100*433d6423SLionel Sambuc 101*433d6423SLionel Sambuc L__SC int L__(build)( 102*433d6423SLionel Sambuc L__(avl) *tree, AVL_BUILD_ITER_TYPE p, L__SIZE num_nodes); 103*433d6423SLionel Sambuc 104*433d6423SLionel Sambuc #endif 105*433d6423SLionel Sambuc 106*433d6423SLionel Sambuc /* ANSI C/ISO C++ require that a long have at least 32 bits. Set 107*433d6423SLionel Sambuc ** L__EST_LONG_BIT to be the greatest multiple of 8 in the range 108*433d6423SLionel Sambuc ** 32 - 64 (inclusive) that is less than or equal to the number of 109*433d6423SLionel Sambuc ** bits in a long. 110*433d6423SLionel Sambuc */ 111*433d6423SLionel Sambuc 112*433d6423SLionel Sambuc #if (((LONG_MAX >> 31) >> 7) == 0) 113*433d6423SLionel Sambuc 114*433d6423SLionel Sambuc #define L__EST_LONG_BIT 32 115*433d6423SLionel Sambuc 116*433d6423SLionel Sambuc #elif (((LONG_MAX >> 31) >> 15) == 0) 117*433d6423SLionel Sambuc 118*433d6423SLionel Sambuc #define L__EST_LONG_BIT 40 119*433d6423SLionel Sambuc 120*433d6423SLionel Sambuc #elif (((LONG_MAX >> 31) >> 23) == 0) 121*433d6423SLionel Sambuc 122*433d6423SLionel Sambuc #define L__EST_LONG_BIT 48 123*433d6423SLionel Sambuc 124*433d6423SLionel Sambuc #elif (((LONG_MAX >> 31) >> 31) == 0) 125*433d6423SLionel Sambuc 126*433d6423SLionel Sambuc #define L__EST_LONG_BIT 56 127*433d6423SLionel Sambuc 128*433d6423SLionel Sambuc #else 129*433d6423SLionel Sambuc 130*433d6423SLionel Sambuc #define L__EST_LONG_BIT 64 131*433d6423SLionel Sambuc 132*433d6423SLionel Sambuc #endif 133*433d6423SLionel Sambuc 134*433d6423SLionel Sambuc /* Number of bits in a long. */ 135*433d6423SLionel Sambuc #define L__LONG_BIT (sizeof(long) * CHAR_BIT) 136*433d6423SLionel Sambuc 137*433d6423SLionel Sambuc /* The macro L__BIT_ARR_DEFN defines a bit array whose index is a (0-based) 138*433d6423SLionel Sambuc ** node depth. The definition depends on whether the maximum depth is more 139*433d6423SLionel Sambuc ** or less than the number of bits in a single long. 140*433d6423SLionel Sambuc */ 141*433d6423SLionel Sambuc 142*433d6423SLionel Sambuc #if ((AVL_MAX_DEPTH) > L__EST_LONG_BIT) 143*433d6423SLionel Sambuc 144*433d6423SLionel Sambuc /* Maximum depth may be more than number of bits in a long. */ 145*433d6423SLionel Sambuc 146*433d6423SLionel Sambuc #define L__BIT_ARR_DEFN(NAME) \ 147*433d6423SLionel Sambuc unsigned long NAME[((AVL_MAX_DEPTH) + L__LONG_BIT - 1) / L__LONG_BIT]; 148*433d6423SLionel Sambuc 149*433d6423SLionel Sambuc #else 150*433d6423SLionel Sambuc 151*433d6423SLionel Sambuc /* Maximum depth is definitely less than number of bits in a long. */ 152*433d6423SLionel Sambuc 153*433d6423SLionel Sambuc #define L__BIT_ARR_DEFN(NAME) unsigned long NAME; 154*433d6423SLionel Sambuc 155*433d6423SLionel Sambuc #endif 156*433d6423SLionel Sambuc 157*433d6423SLionel Sambuc /* Iterator structure. */ 158*433d6423SLionel Sambuc typedef struct 159*433d6423SLionel Sambuc { 160*433d6423SLionel Sambuc /* Tree being iterated over. */ 161*433d6423SLionel Sambuc L__(avl) *tree_; 162*433d6423SLionel Sambuc 163*433d6423SLionel Sambuc /* Records a path into the tree. If bit n is true, indicates 164*433d6423SLionel Sambuc ** take greater branch from the nth node in the path, otherwise 165*433d6423SLionel Sambuc ** take the less branch. bit 0 gives branch from root, and 166*433d6423SLionel Sambuc ** so on. */ 167*433d6423SLionel Sambuc L__BIT_ARR_DEFN(branch) 168*433d6423SLionel Sambuc 169*433d6423SLionel Sambuc /* Zero-based depth of path into tree. */ 170*433d6423SLionel Sambuc int depth; 171*433d6423SLionel Sambuc 172*433d6423SLionel Sambuc /* Handles of nodes in path from root to current node (returned by *). */ 173*433d6423SLionel Sambuc AVL_HANDLE path_h[(AVL_MAX_DEPTH) - 1]; 174*433d6423SLionel Sambuc } 175*433d6423SLionel Sambuc L__(iter); 176*433d6423SLionel Sambuc 177*433d6423SLionel Sambuc /* Iterator function prototypes. */ 178*433d6423SLionel Sambuc 179*433d6423SLionel Sambuc L__SC void L__(start_iter)( 180*433d6423SLionel Sambuc L__(avl) *tree, L__(iter) *iter, AVL_KEY k, avl_search_type st); 181*433d6423SLionel Sambuc 182*433d6423SLionel Sambuc L__SC void L__(start_iter_least)(L__(avl) *tree, L__(iter) *iter); 183*433d6423SLionel Sambuc 184*433d6423SLionel Sambuc L__SC void L__(start_iter_greatest)(L__(avl) *tree, L__(iter) *iter); 185*433d6423SLionel Sambuc 186*433d6423SLionel Sambuc L__SC AVL_HANDLE L__(get_iter)(L__(iter) *iter); 187*433d6423SLionel Sambuc 188*433d6423SLionel Sambuc L__SC void L__(incr_iter)(L__(iter) *iter); 189*433d6423SLionel Sambuc 190*433d6423SLionel Sambuc L__SC void L__(decr_iter)(L__(iter) *iter); 191*433d6423SLionel Sambuc 192*433d6423SLionel Sambuc L__SC void L__(init_iter)(L__(iter) *iter); 193*433d6423SLionel Sambuc 194*433d6423SLionel Sambuc #define AVL_IMPL_INIT 1 195*433d6423SLionel Sambuc #define AVL_IMPL_IS_EMPTY (1 << 1) 196*433d6423SLionel Sambuc #define AVL_IMPL_INSERT (1 << 2) 197*433d6423SLionel Sambuc #define AVL_IMPL_SEARCH (1 << 3) 198*433d6423SLionel Sambuc #define AVL_IMPL_SEARCH_LEAST (1 << 4) 199*433d6423SLionel Sambuc #define AVL_IMPL_SEARCH_GREATEST (1 << 5) 200*433d6423SLionel Sambuc #define AVL_IMPL_REMOVE (1 << 6) 201*433d6423SLionel Sambuc #define AVL_IMPL_BUILD (1 << 7) 202*433d6423SLionel Sambuc #define AVL_IMPL_START_ITER (1 << 8) 203*433d6423SLionel Sambuc #define AVL_IMPL_START_ITER_LEAST (1 << 9) 204*433d6423SLionel Sambuc #define AVL_IMPL_START_ITER_GREATEST (1 << 10) 205*433d6423SLionel Sambuc #define AVL_IMPL_GET_ITER (1 << 11) 206*433d6423SLionel Sambuc #define AVL_IMPL_INCR_ITER (1 << 12) 207*433d6423SLionel Sambuc #define AVL_IMPL_DECR_ITER (1 << 13) 208*433d6423SLionel Sambuc #define AVL_IMPL_INIT_ITER (1 << 14) 209*433d6423SLionel Sambuc #define AVL_IMPL_SUBST (1 << 15) 210*433d6423SLionel Sambuc 211*433d6423SLionel Sambuc #define AVL_IMPL_ALL (~0) 212*433d6423SLionel Sambuc 213*433d6423SLionel Sambuc #undef L__ 214*433d6423SLionel Sambuc #undef L__EST_LONG_BIT 215*433d6423SLionel Sambuc #undef L__SIZE 216*433d6423SLionel Sambuc #undef L__SC 217*433d6423SLionel Sambuc #undef L__LONG_BIT 218*433d6423SLionel Sambuc #undef L__BIT_ARR_DEFN 219