11#![ allow( warnings) ]
2- #![ cfg_attr ( not ( test ) , no_std) ]
2+ #![ no_std]
33
44//! A fast, index-based Red-Black Tree with no heap allocations.
55//!
66//! ## Features
7- //! - No heap allocation
8- //! - All nodes are stored in a `array`, avoiding pointers
9- //! - Flat storage using `MaybeUninit`
10- //! - Suitable for `no_std` environments
7+ //! - **Flat storage**: all nodes are stored in an `array`, avoiding pointer indirection.
8+ //! - **No allocations per node**: avoids `Box`, `Rc`, or `Arc`.
9+ //! - **No-std**: works in embedded or bare-metal environments without relying on the Rust standard library.
10+ //! - **Preallocated with `MaybeUninit`**: memory for all nodes is allocated upfront, minimizing runtime overhead and ensuring safe initialization.
11+ //! - **Fixed capacity**: tree size is bounded at compile-time, making resource usage predictable.
1112//!
1213use core:: mem:: MaybeUninit ;
1314use core:: cmp:: Ordering ;
@@ -30,6 +31,7 @@ struct Node<K, V> {
3031 right : usize ,
3132}
3233
34+ /// An iterator over the entries of a `RedBlackTree`.
3335pub struct RedBlackTreeIter < ' a , K : Ord + ' a , V : ' a , const N : usize > {
3436 tree : & ' a RedBlackTree < K , V , N > ,
3537 index : usize ,
@@ -38,6 +40,7 @@ pub struct RedBlackTreeIter<'a, K: Ord + 'a, V: 'a, const N: usize> {
3840impl < ' a , K : Ord + ' a , V : ' a , const N : usize > Iterator for RedBlackTreeIter < ' a , K , V , N > {
3941 type Item = ( & ' a K , & ' a V ) ;
4042
43+ /// Advances the iterator and returns the next key-value pair in ascending order.
4144 fn next ( & mut self ) -> Option < Self :: Item > {
4245 if self . index == SENTINEL {
4346 let next = self . tree . min_usize ( self . tree . root ) ;
@@ -60,6 +63,7 @@ impl<'a, K: Ord + 'a, V: 'a, const N: usize> Iterator for RedBlackTreeIter<'a, K
6063}
6164
6265impl < ' a , K : Ord + ' a , V : ' a , const N : usize > DoubleEndedIterator for RedBlackTreeIter < ' a , K , V , N > {
66+ /// Advances the iterator from the back and returns the previous key-value pair in descending order.
6367 fn next_back ( & mut self ) -> Option < Self :: Item > {
6468 if self . index == SENTINEL {
6569 let next = self . tree . max_usize ( self . tree . root ) ;
@@ -720,6 +724,34 @@ mod tests {
720724 tree
721725 }
722726
727+ #[ test]
728+ fn test_example ( ) {
729+ let mut tree = RedBlackTree :: < i32 , & str , 10 > :: new ( ) ;
730+
731+ tree. insert ( 10 , "A" ) ;
732+ tree. insert ( 20 , "B" ) ;
733+ tree. insert ( 5 , "C" ) ;
734+
735+ tree. update ( 10 , "Updated A" ) ;
736+
737+ assert_eq ! ( tree. search( & 10 ) , Some ( & "Updated A" ) ) ;
738+ assert_eq ! ( tree. search( & 20 ) , Some ( & "B" ) ) ;
739+ assert_eq ! ( tree. search( & 5 ) , Some ( & "C" ) ) ;
740+ assert_eq ! ( tree. contains_key( & 20 ) , true ) ;
741+ assert_eq ! ( tree. contains_key( & 100 ) , false ) ;
742+
743+ // Check iteration order (should be sorted by key)
744+ let mut iter = tree. iter ( ) ;
745+ assert_eq ! ( iter. next( ) , Some ( ( & 5 , & "C" ) ) ) ;
746+ assert_eq ! ( iter. next( ) , Some ( ( & 10 , & "Updated A" ) ) ) ;
747+ assert_eq ! ( iter. next( ) , Some ( ( & 20 , & "B" ) ) ) ;
748+ assert_eq ! ( iter. next( ) , None ) ;
749+
750+ tree. remove ( 20 ) ;
751+ assert_eq ! ( tree. search( & 20 ) , None ) ;
752+ assert_eq ! ( tree. contains_key( & 20 ) , false ) ;
753+ }
754+
723755 #[ test]
724756 fn test_utils ( ) {
725757 let mut tree = setup_small_tree ( ) ;
0 commit comments