rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] rbtree: rust: add RBTree::is_empty
@ 2025-06-16 10:36 Alice Ryhl
  2025-06-17  7:30 ` Benno Lossin
  2025-06-29 19:31 ` Miguel Ojeda
  0 siblings, 2 replies; 3+ messages in thread
From: Alice Ryhl @ 2025-06-16 10:36 UTC (permalink / raw)
  To: Miguel Ojeda; +Cc: rust-for-linux, linux-kernel, Alice Ryhl

In Rust Binder I need to be able to determine whether a red/black tree
is empty. Thus, add a method for that operation to replace

	rbtree.iter().next().is_none()

This is terrible, so add a method for this purpose. We do not add a
RBTree::len method because computing the number of elements requires
iterating the entire tree, but checking whether it is empty can be done
cheaply.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/rbtree.rs | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/rust/kernel/rbtree.rs b/rust/kernel/rbtree.rs
index 8d978c8967475cc2631b50721798f7e8cd1551fd..9457134eb3afdf7a214a28fcbe2e6b8065cc9a86 100644
--- a/rust/kernel/rbtree.rs
+++ b/rust/kernel/rbtree.rs
@@ -191,6 +191,12 @@ pub fn new() -> Self {
         }
     }
 
+    /// Returns true if this tree is empty.
+    #[inline]
+    pub fn is_empty(&self) -> bool {
+        self.root.rb_node.is_null()
+    }
+
     /// Returns an iterator over the tree nodes, sorted by key.
     pub fn iter(&self) -> Iter<'_, K, V> {
         Iter {

---
base-commit: 19272b37aa4f83ca52bdf9c16d5d81bdd1354494
change-id: 20250616-rbtree-is-empty-28f51fccb99a

Best regards,
-- 
Alice Ryhl <aliceryhl@google.com>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] rbtree: rust: add RBTree::is_empty
  2025-06-16 10:36 [PATCH] rbtree: rust: add RBTree::is_empty Alice Ryhl
@ 2025-06-17  7:30 ` Benno Lossin
  2025-06-29 19:31 ` Miguel Ojeda
  1 sibling, 0 replies; 3+ messages in thread
From: Benno Lossin @ 2025-06-17  7:30 UTC (permalink / raw)
  To: Alice Ryhl, Miguel Ojeda; +Cc: rust-for-linux, linux-kernel

On Mon Jun 16, 2025 at 12:36 PM CEST, Alice Ryhl wrote:
> In Rust Binder I need to be able to determine whether a red/black tree
> is empty. Thus, add a method for that operation to replace
>
> 	rbtree.iter().next().is_none()
>
> This is terrible, so add a method for this purpose. We do not add a
> RBTree::len method because computing the number of elements requires
> iterating the entire tree, but checking whether it is empty can be done
> cheaply.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Reviewed-by: Benno Lossin <lossin@kernel.org>

---
Cheers,
Benno

> ---
>  rust/kernel/rbtree.rs | 6 ++++++
>  1 file changed, 6 insertions(+)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] rbtree: rust: add RBTree::is_empty
  2025-06-16 10:36 [PATCH] rbtree: rust: add RBTree::is_empty Alice Ryhl
  2025-06-17  7:30 ` Benno Lossin
@ 2025-06-29 19:31 ` Miguel Ojeda
  1 sibling, 0 replies; 3+ messages in thread
From: Miguel Ojeda @ 2025-06-29 19:31 UTC (permalink / raw)
  To: Alice Ryhl; +Cc: Miguel Ojeda, rust-for-linux, linux-kernel

On Mon, Jun 16, 2025 at 12:36 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> In Rust Binder I need to be able to determine whether a red/black tree
> is empty. Thus, add a method for that operation to replace
>
>         rbtree.iter().next().is_none()
>
> This is terrible, so add a method for this purpose. We do not add a
> RBTree::len method because computing the number of elements requires
> iterating the entire tree, but checking whether it is empty can be done
> cheaply.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>

Applied to `rust-next` -- thanks everyone!

    [ Adjusted title. - Miguel ]

Cheers,
Miguel

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-29 19:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-16 10:36 [PATCH] rbtree: rust: add RBTree::is_empty Alice Ryhl
2025-06-17  7:30 ` Benno Lossin
2025-06-29 19:31 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).