X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_avltree.c;h=a9b3862c0ecf4ba5533d0676c3fcdc3d9363a141;hb=1122c8fd30065b5990a5b798b7bfd8891736def9;hp=83821fe99c37671ad36ece3ab3d14ddcba84f1d1;hpb=8cf1acdb96d027b1d48c9dfb74fea313e63ceedf;p=collectd.git diff --git a/src/utils_avltree.c b/src/utils_avltree.c index 83821fe9..a9b3862c 100644 --- a/src/utils_avltree.c +++ b/src/utils_avltree.c @@ -51,6 +51,7 @@ struct c_avl_tree_s { c_avl_node_t *root; int (*compare) (const void *, const void *); + int size; }; struct c_avl_iterator_s @@ -141,6 +142,9 @@ static c_avl_node_t *rotate_right (c_avl_tree_t *t, c_avl_node_t *x) c_avl_node_t *y; c_avl_node_t *b; + assert (x != NULL); + assert (x->left != NULL); + p = x->parent; y = x->left; b = y->right; @@ -165,7 +169,7 @@ static c_avl_node_t *rotate_right (c_avl_tree_t *t, c_avl_node_t *x) y->height = calc_height (y); return (y); -} /* void rotate_left */ +} /* void rotate_right */ /* * (x) (y) @@ -182,6 +186,9 @@ static c_avl_node_t *rotate_left (c_avl_tree_t *t, c_avl_node_t *x) c_avl_node_t *y; c_avl_node_t *b; + assert (x != NULL); + assert (x->right != NULL); + p = x->parent; y = x->right; b = y->left; @@ -234,7 +241,7 @@ static void rebalance (c_avl_tree_t *t, c_avl_node_t *n) { assert (n->right != NULL); b_bottom = BALANCE (n->right); - assert ((b_bottom >= -1) || (b_bottom <= 1)); + assert ((b_bottom >= -1) && (b_bottom <= 1)); if (b_bottom == 1) n = rotate_right_left (t, n); else @@ -244,7 +251,7 @@ static void rebalance (c_avl_tree_t *t, c_avl_node_t *n) { assert (n->left != NULL); b_bottom = BALANCE (n->left); - assert ((b_bottom >= -1) || (b_bottom <= 1)); + assert ((b_bottom >= -1) && (b_bottom <= 1)); if (b_bottom == -1) n = rotate_left_right (t, n); else @@ -264,7 +271,7 @@ static void rebalance (c_avl_tree_t *t, c_avl_node_t *n) } /* while (n != NULL) */ } /* void rebalance */ -static c_avl_node_t *c_avl_node_next (c_avl_tree_t *t, c_avl_node_t *n) +static c_avl_node_t *c_avl_node_next (c_avl_node_t *n) { c_avl_node_t *r; /* return node */ @@ -309,7 +316,7 @@ static c_avl_node_t *c_avl_node_next (c_avl_tree_t *t, c_avl_node_t *n) return (r); } /* c_avl_node_t *c_avl_node_next */ -static c_avl_node_t *c_avl_node_prev (c_avl_tree_t *t, c_avl_node_t *n) +static c_avl_node_t *c_avl_node_prev (c_avl_node_t *n) { c_avl_node_t *r; /* return node */ @@ -364,13 +371,13 @@ static int _remove (c_avl_tree_t *t, c_avl_node_t *n) if (BALANCE (n) > 0) /* left subtree is higher */ { assert (n->left != NULL); - r = c_avl_node_prev (t, n); + r = c_avl_node_prev (n); } else /* right subtree is higher */ { assert (n->right != NULL); - r = c_avl_node_next (t, n); + r = c_avl_node_next (n); } assert ((r->left == NULL) || (r->right == NULL)); @@ -479,12 +486,15 @@ c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *)) t->root = NULL; t->compare = compare; + t->size = 0; return (t); } void c_avl_destroy (c_avl_tree_t *t) { + if (t == NULL) + return; free_node (t->root); free (t); } @@ -508,6 +518,7 @@ int c_avl_insert (c_avl_tree_t *t, void *key, void *value) { new->parent = NULL; t->root = new; + t->size = 1; return (0); } @@ -518,7 +529,7 @@ int c_avl_insert (c_avl_tree_t *t, void *key, void *value) if (cmp == 0) { free_node (new); - return (-1); + return (1); } else if (cmp < 0) { @@ -553,6 +564,7 @@ int c_avl_insert (c_avl_tree_t *t, void *key, void *value) } /* while (42) */ verify_tree (t->root); + ++t->size; return (0); } /* int c_avl_insert */ @@ -574,6 +586,7 @@ int c_avl_remove (c_avl_tree_t *t, const void *key, void **rkey, void **rvalue) status = _remove (t, n); verify_tree (t->root); + --t->size; return (status); } /* void *c_avl_remove */ @@ -581,6 +594,8 @@ int c_avl_get (c_avl_tree_t *t, const void *key, void **value) { c_avl_node_t *n; + assert (t != NULL); + n = search (t, key); if (n == NULL) return (-1); @@ -604,10 +619,18 @@ int c_avl_pick (c_avl_tree_t *t, void **key, void **value) n = t->root; while ((n->left != NULL) || (n->right != NULL)) { - int height_left = (n->left == NULL) ? 0 : n->left->height; - int height_right = (n->right == NULL) ? 0 : n->right->height; + if (n->left == NULL) + { + n = n->right; + continue; + } + else if (n->right == NULL) + { + n = n->left; + continue; + } - if (height_left > height_right) + if (n->left->height > n->right->height) n = n->left; else n = n->right; @@ -625,6 +648,7 @@ int c_avl_pick (c_avl_tree_t *t, void **key, void **value) *value = n->value; free_node (n); + --t->size; rebalance (t, p); return (0); @@ -662,7 +686,7 @@ int c_avl_iterator_next (c_avl_iterator_t *iter, void **key, void **value) } else { - n = c_avl_node_next (iter->tree, iter->node); + n = c_avl_node_next (iter->node); } if (n == NULL) @@ -691,7 +715,7 @@ int c_avl_iterator_prev (c_avl_iterator_t *iter, void **key, void **value) } else { - n = c_avl_node_prev (iter->tree, iter->node); + n = c_avl_node_prev (iter->node); } if (n == NULL) @@ -708,3 +732,10 @@ void c_avl_iterator_destroy (c_avl_iterator_t *iter) { free (iter); } + +int c_avl_size (c_avl_tree_t *t) +{ + if (t == NULL) + return (0); + return (t->size); +}