8000 rust: rbtree: add RBTree::is_empty · Rust-for-Linux/linux@fbcd4b7 · GitHub
[go: up one dir, main page]

Skip to content

Commit fbcd4b7

Browse files
Darksonnojeda
authored andcommitted
rust: rbtree: add RBTree::is_empty
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> Link: https://lore.kernel.org/r/20250616-rbtree-is-empty-v1-1-61f7cfb012e3@google.com [ Adjusted title. - Miguel ] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 0303584 commit fbcd4b7

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

rust/kernel/rbtree.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ impl<K, V> RBTree<K, V> {
191191
}
192192
}
193193

194+
/// Returns true if this tree is empty.
195+
#[inline]
196+
pub fn is_empty(&self) -> bool {
197+
self.root.rb_node.is_null()
198+
}
199+
194200
/// Returns an iterator over the tree nodes, sorted by key.
195201
pub fn iter(&self) -> Iter<'_, K, V> {
196202
Iter {

0 commit comments

Comments
 (0)
0