23 #define HEIGHT(node) (node == NIL(avl_node) ? -1 : (node)->height)
24 #define BALANCE(node) (HEIGHT((node)->right) - HEIGHT((node)->left))
26 #define compute_height(node) { \
27 int x=HEIGHT(node->left), y=HEIGHT(node->right); \
28 (node)->height = MAX(x,y) + 1; \
31 #define COMPARE(key, nodekey, compare) \
32 ((compare == avl_numcmp) ? \
33 (int) key - (int) nodekey : \
34 (*compare)(key, nodekey))
67 register int (*compare) () = tree->compar, diff;
76 if (value_p !=
NIL (
char *))
77 *value_p = node->
value;
80 node = (diff < 0) ? node->
left : node->
right;
99 for (node = tree->root; node->
left != 0; node = node->
left)
102 if (key_p !=
NIL (
char *))
104 if (value_p !=
NIL (
char *))
105 *value_p = node->
value;
125 for (node = tree->root; node->
right != 0; node = node->
right)
128 if (key_p !=
NIL (
char *))
130 if (value_p !=
NIL (
char *))
131 *value_p = node->
value;
142 register int stack_n = 0;
143 register int (*compare) () = tree->compar;
147 node_p = &tree->root;
153 stack_nodep[stack_n++] = node_p;
157 node_p = (diff < 0) ? &node->
left : &node->
right;
176 register int stack_n = 0;
177 register int (*compare) () = tree->compar;
181 node_p = &tree->root;
186 stack_nodep[stack_n++] = node_p;
191 *slot_p = &node->
value;
194 node_p = (diff < 0) ? &node->
left : &node->
right;
200 *slot_p = &(*node_p)->value;
212 register avl_node **node_p, *node, *rightmost;
213 register int stack_n = 0;
215 int (*compare) () = tree->compar, diff;
218 node_p = &tree->root;
226 stack_nodep[stack_n++] = node_p;
227 node_p = (diff < 0) ? &node->
left : &node->
right;
235 *value_p = node->
value;
238 *node_p = node->
right;
247 stack_nodep[stack_n++] = node_p;
321 if (gen->count == gen->tree->num_entries)
327 node = gen->nodelist[gen->count++];
328 if (key_p !=
NIL (
char *))
330 if (value_p !=
NIL (
char *))
331 *value_p = node->
value;
341 FREE (gen->nodelist);
350 register int stack_n = 0;
356 stack_nodep[stack_n++] = node_p;
357 node_p = &node->
right;
360 *node_p = node->
left;
370 register
int stack_n;
377 while (--stack_n >= 0)
379 node_p = stack_nodep[stack_n];
387 else if ((hr - hl) > 1)
393 height =
MAX (hl, hr) + 1;
394 if (height == node->
height)
405 register avl_node *old_root = *node_p, *new_root, *new_right;
409 *node_p = new_root = old_root->
right;
411 new_root->
left = old_root;
415 new_right = old_root->
right;
416 *node_p = new_root = new_right->
left;
419 new_root->
right = new_right;
420 new_root->
left = old_root;
432 register avl_node *old_root = *node_p, *new_root, *new_left;
436 *node_p = new_root = old_root->
left;
438 new_root->
right = old_root;
442 new_left = old_root->
left;
443 *node_p = new_root = new_left->
right;
446 new_root->
left = new_left;
447 new_root->
right = old_root;
462 (*func) (node->key, node->value);
476 (*func) (node->key, node->value);
503 void (*value_free) ();
507 free_entry (node->left, key_free, value_free);
508 free_entry (node->right, key_free, value_free);
510 (*key_free) (node->key);
512 (*value_free) (node->value);
522 void (*value_free) ();
524 free_entry (tree->root, key_free, value_free);
556 return (
int) x - (int) y;
575 int l_height, r_height, comp_height, bal;
585 comp_height =
MAX (l_height, r_height) + 1;
586 bal = r_height - l_height;
588 if (comp_height != node->height)
590 (void) printf (
"Bad height for 0x%08x: computed=%d stored=%d\n",
591 node, comp_height, node->height);
595 if (bal > 1 || bal < -1)
597 (void) printf (
"Out of balance at node 0x%08x, balance = %d\n",
603 (*compar) (node->left->key, node->key) > 0)
605 (void) printf (
"Bad ordering between 0x%08x and 0x%08x",
611 (*compar) (node->key, node->right->key) > 0)
613 (void) printf (
"Bad ordering between 0x%08x and 0x%08x",
void avl_free_table(avl_tree *tree, void(*)() key_free, void(*)() value_free)
avl_find_or_add(avl_tree *tree, char *key, char ***slot_p)
static void avl_walk_forward(avl_node *node, void(*)() func)
void avl_free_gen(avl_generator *gen)
avl_delete(avl_tree *tree, char **key_p, char **value_p)
avl_insert(avl_tree *tree, char *key, char *value)
static void avl_record_gen_backward(avl_node *node, avl_generator *gen)
static avl_node * new_node()
int avl_count(avl_tree *tree)
void avl_foreach(avl_tree *tree, void(*)() func, int direction)
static int do_check_tree()
#define COMPARE(key, nodekey, compare)
avl_last(avl_tree *tree, char **key_p, char **value_p)
static void do_rebalance()
static void free_entry(avl_node *node, void(*)() key_free, void(*)() value_free)
#define ABC_NAMESPACE_IMPL_END
#define compute_height(node)
avl_gen(avl_generator *gen, char **key_p, char **value_p)
avl_tree * avl_init_table(int(*)() compar)
#define ABC_NAMESPACE_IMPL_START
avl_lookup(avl_tree *tree, char *key, char **value_p)
avl_first(avl_tree *tree, char **key_p, char **value_p)
static void avl_record_gen_forward(avl_node *node, avl_generator *gen)
avl_generator * avl_init_gen(avl_tree *tree, int dir)
int avl_check_tree(avl_tree *tree)
static void avl_walk_backward(avl_node *node, void(*)() func)
static avl_node * find_rightmost()
int avl_numcmp(char *x, char *y)