Implement r_rbtree_cont_last ##util

This commit is contained in:
condret 2021-03-16 16:54:34 +01:00
parent 93b4f34b84
commit a3e3b5af7c
2 changed files with 16 additions and 0 deletions

View File

@ -114,6 +114,7 @@ R_API RContRBNode *r_rbtree_cont_find_node(RContRBTree *tree, void *data, RContR
R_API RContRBNode *r_rbtree_cont_node_next(RContRBNode *node);
R_API void *r_rbtree_cont_find(RContRBTree *tree, void *data, RContRBCmp cmp, void *user);
R_API void *r_rbtree_cont_first(RContRBTree *tree);
R_API void *r_rbtree_cont_last(RContRBTree *tree);
#define r_rbtree_cont_foreach(tree, it, dat) \
for ((it) = r_rbtree_first ((tree)->root ? &(tree)->root->node : NULL); r_rbtree_iter_has(&it) && (dat = r_rbtree_iter_get (&it, RContRBNode, node)->data); r_rbtree_iter_next (&(it)))

View File

@ -557,6 +557,21 @@ R_API void *r_rbtree_cont_first(RContRBTree *tree) {
return (container_of (first_rbnode, RContRBNode, node))->data;
}
R_API void *r_rbtree_cont_last(RContRBTree *tree) {
r_return_val_if_fail (tree, NULL);
if (!tree->root) {
// empty tree
return NULL;
}
RBIter iter = r_rbtree_last (&tree->root->node);
if (iter.len == 0) {
// also empty tree
return NULL;
}
RBNode *last_rbnode = iter.path[iter.len-1];
return (container_of (last_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);