mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: I Hsin Cheng <richard120310@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, I Hsin Cheng <richard120310@gmail.com>
Subject: [PATCH] rbtree: Introduce rb_remove()
Date: Thu, 28 Mar 2024 14:45:39 +0800	[thread overview]
Message-ID: <20240328064539.95795-1-richard120310@gmail.com> (raw)

Implement the function "rb_remove()", which can perform the removal of a
certain key from the tree. Once the node with the searched key is found,
we call "rb_erase()" to perform the removal of the node, otherwise the
key doesn't exists in the tree then we return NULL.

Signed-off-by: I Hsin Cheng <richard120310@gmail.com>
---
 include/linux/rbtree.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
index f7edca369..1958be66f 100644
--- a/include/linux/rbtree.h
+++ b/include/linux/rbtree.h
@@ -302,6 +302,34 @@ rb_find_first(const void *key, const struct rb_root *tree,
 	return match;
 }
 
+/**
+ * rb_remove() - remove @key in tree @tree
+ * @key: key to remove
+ * @tree: tree to modify
+ * @less: operator defining the (partial) node order
+ */
+static __always_inline struct rb_node *
+rb_remove(const void *key, const struct rb_root *tree,
+	  int (*cmp)(const void *key, const struct rb_node *))
+{
+	struct rb_node *node = tree->rb_node;
+
+	while (node) {
+		int c = cmp(key, node);
+
+		if (c < 0)
+			node = node->rb_left;
+		else if (c > 0)
+			node = node->rb_right;
+		else {
+			rb_erase(node, tree->rb_node);
+			return node;
+		}
+	}
+
+	return NULL;
+}
+
 /**
  * rb_next_match() - find the next @key in @tree
  * @key: key to match
-- 
2.34.1


             reply	other threads:[~2024-03-28  6:46 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-28  6:45 I Hsin Cheng [this message]
2024-03-28 19:36 ` Andrew Morton
     [not found]   ` <CAH5jb=aY1y19=XAKJG4368Ey6Sq3759euKsWLkLMATO9ac55sw@mail.gmail.com>
2024-03-30  8:38     ` 鄭以新

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240328064539.95795-1-richard120310@gmail.com \
    --to=richard120310@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®