Add rb_empty().

This commit is contained in:
Jason Evans 2014-08-19 01:28:49 -07:00
parent 3a81cbd2d4
commit 1628e8615e
2 changed files with 16 additions and 0 deletions

View File

@ -158,6 +158,8 @@ struct { \
#define rb_proto(a_attr, a_prefix, a_rbt_type, a_type) \ #define rb_proto(a_attr, a_prefix, a_rbt_type, a_type) \
a_attr void \ a_attr void \
a_prefix##new(a_rbt_type *rbtree); \ a_prefix##new(a_rbt_type *rbtree); \
a_attr bool \
a_prefix##empty(a_rbt_type *rbtree); \
a_attr a_type * \ a_attr a_type * \
a_prefix##first(a_rbt_type *rbtree); \ a_prefix##first(a_rbt_type *rbtree); \
a_attr a_type * \ a_attr a_type * \
@ -224,6 +226,13 @@ a_prefix##reverse_iter(a_rbt_type *rbtree, a_type *start, \
* Args: * Args:
* tree: Pointer to an uninitialized red-black tree object. * tree: Pointer to an uninitialized red-black tree object.
* *
* static bool
* ex_empty(ex_t *tree);
* Description: Determine whether tree is empty.
* Args:
* tree: Pointer to an initialized red-black tree object.
* Ret: True if tree is empty, false otherwise.
*
* static ex_node_t * * static ex_node_t *
* ex_first(ex_t *tree); * ex_first(ex_t *tree);
* static ex_node_t * * static ex_node_t *
@ -309,6 +318,10 @@ a_attr void \
a_prefix##new(a_rbt_type *rbtree) { \ a_prefix##new(a_rbt_type *rbtree) { \
rb_new(a_type, a_field, rbtree); \ rb_new(a_type, a_field, rbtree); \
} \ } \
a_attr bool \
a_prefix##empty(a_rbt_type *rbtree) { \
return (rbtree->rbt_root == &rbtree->rbt_nil); \
} \
a_attr a_type * \ a_attr a_type * \
a_prefix##first(a_rbt_type *rbtree) { \ a_prefix##first(a_rbt_type *rbtree) { \
a_type *ret; \ a_type *ret; \

View File

@ -49,6 +49,7 @@ TEST_BEGIN(test_rb_empty)
tree_new(&tree); tree_new(&tree);
assert_true(tree_empty(&tree), "Tree should be empty");
assert_ptr_null(tree_first(&tree), "Unexpected node"); assert_ptr_null(tree_first(&tree), "Unexpected node");
assert_ptr_null(tree_last(&tree), "Unexpected node"); assert_ptr_null(tree_last(&tree), "Unexpected node");
@ -265,6 +266,8 @@ TEST_BEGIN(test_rb_random)
assert_u_eq(tree_iterate_reverse(&tree), k+1, assert_u_eq(tree_iterate_reverse(&tree), k+1,
"Unexpected node iteration count"); "Unexpected node iteration count");
assert_false(tree_empty(&tree),
"Tree should not be empty");
assert_ptr_not_null(tree_first(&tree), assert_ptr_not_null(tree_first(&tree),
"Tree should not be empty"); "Tree should not be empty");
assert_ptr_not_null(tree_last(&tree), assert_ptr_not_null(tree_last(&tree),