[nasm:master] rbtree: add rb_search_exact()

nasm-bot for H. Peter Anvin (Intel) hpa at zytor.com
Wed Jul 8 09:51:03 PDT 2020


Commit-ID:  68e3802b238b964900acac9422a70e295482243f
Gitweb:     http://repo.or.cz/w/nasm.git?a=commitdiff;h=68e3802b238b964900acac9422a70e295482243f
Author:     H. Peter Anvin (Intel) <hpa at zytor.com>
AuthorDate: Wed, 8 Jul 2020 09:48:43 -0700
Committer:  H. Peter Anvin (Intel) <hpa at zytor.com>
CommitDate: Wed, 8 Jul 2020 09:49:38 -0700

rbtree: add rb_search_exact()

Sometimes we want to search for an exact key only, and reject the case
when tree->key < key. Add rb_search_exact() for this purpose, rather
than forcing the caller to perform the comparison in open code.

Signed-off-by: H. Peter Anvin (Intel) <hpa at zytor.com>


---
 include/rbtree.h | 5 +++++
 nasmlib/rbtree.c | 8 ++++++++
 2 files changed, 13 insertions(+)

diff --git a/include/rbtree.h b/include/rbtree.h
index 332f6f85..39d45aff 100644
--- a/include/rbtree.h
+++ b/include/rbtree.h
@@ -72,6 +72,11 @@ struct rbtree *rb_insert(struct rbtree *, struct rbtree *);
  */
 struct rbtree *rb_search(const struct rbtree *, uint64_t);
 
+/*
+ * Find a node in the tree exactly matching the key value.
+ */
+struct rbtree *rb_search_exact(const struct rbtree *, uint64_t);
+
 /*
  * Return the immediately previous or next node in key order.
  * Returns NULL if this node is the end of the tree.
diff --git a/nasmlib/rbtree.c b/nasmlib/rbtree.c
index 510f34b1..773338bb 100644
--- a/nasmlib/rbtree.c
+++ b/nasmlib/rbtree.c
@@ -87,6 +87,14 @@ struct rbtree *rb_search(const struct rbtree *tree, uint64_t key)
     return (struct rbtree *)best;
 }
 
+struct rbtree *rb_search_exact(const struct rbtree *tree, uint64_t key)
+{
+    struct rbtree *rv;
+
+    rv = rb_search(tree, key);
+    return (rv && rv->key == key) ? rv : NULL;
+}
+
 /* Reds two left in a row? */
 static inline bool is_red_left_left(struct rbtree *h)
 {


More information about the Nasm-commits mailing list