From 27769fc777da38494c3f8e2297af9099fbec0bc0 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 17 Jun 2011 09:24:02 +0200 Subject: [PATCH] avl tree: Report size of the tree and make that available in _get_size(). --- src/utils_avltree.c | 11 +++++++++++ src/utils_avltree.h | 15 +++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/utils_avltree.c b/src/utils_avltree.c index 3e258e95..0436d8fe 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 @@ -479,6 +480,7 @@ c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *)) t->root = NULL; t->compare = compare; + t->size = 0; return (t); } @@ -553,6 +555,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 +577,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 */ @@ -710,3 +714,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); +} diff --git a/src/utils_avltree.h b/src/utils_avltree.h index 355ced24..10fb5cbe 100644 --- a/src/utils_avltree.h +++ b/src/utils_avltree.h @@ -148,4 +148,19 @@ int c_avl_iterator_next (c_avl_iterator_t *iter, void **key, void **value); int c_avl_iterator_prev (c_avl_iterator_t *iter, void **key, void **value); void c_avl_iterator_destroy (c_avl_iterator_t *iter); +/* + * NAME + * c_avl_size + * + * DESCRIPTION + * Return the size (number of nodes) of the specified tree. + * + * PARAMETERS + * `t' AVL-tree to get the size of. + * + * RETURN VALUE + * Number of nodes in the tree, 0 if the tree is empty or NULL. + */ +int c_avl_size (c_avl_tree_t *t); + #endif /* UTILS_AVLTREE_H */ -- 2.30.2