From: Alan T. DeKok Date: Wed, 6 Mar 2024 14:18:47 +0000 (-0500) Subject: add first / last API X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=df8ccf9cc344fc66010f61eb60f8606857e0edb0;p=thirdparty%2Ffreeradius-server.git add first / last API --- diff --git a/src/lib/util/rb.c b/src/lib/util/rb.c index a7377cff818..a94d2f630d9 100644 --- a/src/lib/util/rb.c +++ b/src/lib/util/rb.c @@ -777,6 +777,36 @@ uint32_t fr_rb_num_elements(fr_rb_tree_t *tree) return tree->num_elements; } +void *fr_rb_first(fr_rb_tree_t *tree) +{ + fr_rb_node_t *x = tree->root; + + if (x == NIL) return NULL; + + /* + * First node is the leftmost + */ + while (x->left != NIL) x = x->left; + + return x->data; +} + + +void *fr_rb_last(fr_rb_tree_t *tree) +{ + fr_rb_node_t *x = tree->root; + + if (x == NIL) return NULL; + + /* + * Last node is the rightmost + */ + while (x->right != NIL) x = x->right; + + return x->data; +} + + /** Initialise an in-order iterator * * @param[out] iter to initialise. diff --git a/src/lib/util/rb.h b/src/lib/util/rb.h index 28f874dffcf..0fe56ccec7a 100644 --- a/src/lib/util/rb.h +++ b/src/lib/util/rb.h @@ -295,6 +295,10 @@ bool fr_rb_delete_by_inline_node(fr_rb_tree_t *tree, fr_rb_node_t *node) CC_HI uint32_t fr_rb_num_elements(fr_rb_tree_t *tree) CC_HINT(nonnull); +void *fr_rb_first(fr_rb_tree_t *tree) CC_HINT(nonnull); + +void *fr_rb_last(fr_rb_tree_t *tree) CC_HINT(nonnull); + /** Check to see if an item is in a tree by examining its inline #fr_rb_node_t * * This works because we use NIL sentinels to represent the absence of a child