From c1033118bf6b1561994170e18ff9cec8e9abc4b7 Mon Sep 17 00:00:00 2001 From: condret Date: Wed, 17 Mar 2021 14:38:47 +0100 Subject: [PATCH] Optimize r_rbtree_cont_{first/last} #18485 ##util --- libr/util/rbtree.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/libr/util/rbtree.c b/libr/util/rbtree.c index a65af158b3..546074972b 100644 --- a/libr/util/rbtree.c +++ b/libr/util/rbtree.c @@ -575,13 +575,11 @@ R_API void *r_rbtree_cont_first(RContRBTree *tree) { // empty tree return NULL; } - RBIter iter = r_rbtree_first (&tree->root->node); - if (iter.len == 0) { - // also empty tree - return NULL; + RBNode *node = &tree->root->node; + while (node->child[0]) { + node = node->child[0]; } - RBNode *first_rbnode = iter.path[iter.len-1]; - return (container_of (first_rbnode, RContRBNode, node))->data; + return (container_of (node, RContRBNode, node))->data; } R_API void *r_rbtree_cont_last(RContRBTree *tree) { @@ -590,13 +588,11 @@ R_API void *r_rbtree_cont_last(RContRBTree *tree) { // empty tree return NULL; } - RBIter iter = r_rbtree_last (&tree->root->node); - if (iter.len == 0) { - // also empty tree - return NULL; + RBNode *node = &tree->root->node; + while (node->child[1]) { + node = node->child[1]; } - RBNode *last_rbnode = iter.path[iter.len-1]; - return (container_of (last_rbnode, RContRBNode, node))->data; + return (container_of (node, RContRBNode, node))->data; } R_API void r_rbtree_cont_free(RContRBTree *tree) {