add r_rbtree_cont_first

This commit is contained in:
condret 2020-08-09 04:49:35 +02:00
parent a7fc00814e
commit fbdc4f16dd
2 changed files with 18 additions and 0 deletions

View File

@ -110,6 +110,7 @@ R_API RContRBTree *r_rbtree_cont_newf(RContRBFree f);
R_API bool r_rbtree_cont_insert(RContRBTree *tree, void *data, RContRBCmp cmp, void *user);
R_API bool r_rbtree_cont_delete(RContRBTree *tree, void *data, RContRBCmp cmp, void *user);
R_API void *r_rbtree_cont_find(RContRBTree *tree, void *data, RContRBCmp cmp, void *user);
R_API void *r_rbtree_cont_first(RContRBTree *tree);
#define r_rbtree_cont_foreach(tree, it, dat) \
for ((it) = r_rbtree_first (&tree->root->node); r_rbtree_iter_has(&it) && (dat = r_rbtree_iter_get (&it, RContRBNode, node)->data); r_rbtree_iter_next (&(it)))

View File

@ -488,6 +488,23 @@ R_API void *r_rbtree_cont_find(RContRBTree *tree, void *data, RContRBCmp cmp, vo
return NULL;
}
// not a direct pendant to r_rbtree_first, but similar
// returns first element in the tree, not an iter or a node
R_API void *r_rbtree_cont_first(RContRBTree *tree) {
r_return_val_if_fail (tree, NULL);
if (!tree->root) {
// empty tree
return NULL;
}
RBIter iter = r_rbtree_first (&tree->root->node);
if (iter.len == 0) {
// also empty tree
return NULL;
}
RBNode *first_rbnode = iter.path[iter.len-1];
return (container_of (first_rbnode, RContRBNode, node))->data;
}
R_API void r_rbtree_cont_free(RContRBTree *tree) {
if (tree && tree->root) {
r_rbtree_free (&tree->root->node, cont_node_free, tree->free);