@@ -40,7 +40,7 @@ impl<'a, K: Ord + 'a, V: 'a, const N: usize> Iterator for RedBlackTreeIter<'a, K
4040
4141 fn next ( & mut self ) -> Option < Self :: Item > {
4242 if self . index == SENTINEL {
43- let next = self . tree . min ( self . tree . root ) ;
43+ let next = self . tree . min_usize ( self . tree . root ) ;
4444 if next == SENTINEL {
4545 return None ;
4646 }
@@ -62,7 +62,7 @@ impl<'a, K: Ord + 'a, V: 'a, const N: usize> Iterator for RedBlackTreeIter<'a, K
6262impl < ' a , K : Ord + ' a , V : ' a , const N : usize > DoubleEndedIterator for RedBlackTreeIter < ' a , K , V , N > {
6363 fn next_back ( & mut self ) -> Option < Self :: Item > {
6464 if self . index == SENTINEL {
65- let next = self . tree . max ( self . tree . root ) ;
65+ let next = self . tree . max_usize ( self . tree . root ) ;
6666 if next == SENTINEL {
6767 return None ;
6868 }
@@ -109,13 +109,6 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
109109 }
110110 }
111111
112- pub fn iter ( & self ) -> RedBlackTreeIter < ' _ , K , V , N > {
113- RedBlackTreeIter {
114- tree : self ,
115- index : SENTINEL ,
116- }
117- }
118-
119112 /// Searches for a value associated with the given key.
120113 ///
121114 /// Returns a reference to the value if the key is found, or `None` otherwise.
@@ -132,8 +125,6 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
132125 /// Inserts a key-value pair into the tree.
133126 ///
134127 /// If the key already exists, its value is replaced.
135- ///
136- /// Panics if the tree is already full (`N` elements).
137128 #[ inline( always) ]
138129 pub fn insert ( & mut self , key : K , value : V ) -> Option < & V > {
139130 let mut x = self . root ;
@@ -209,8 +200,7 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
209200
210201 /// Updates the value for an existing key.
211202 ///
212- /// If the key does not exist, this function does nothing.
213- ///
203+ /// If the key is not found, it is inserted with the provided key and value.
214204 #[ inline( always) ]
215205 pub fn update ( & mut self , key : K , value : V ) -> Option < & V > {
216206 let z = self . get_index_by_key ( & key) ;
@@ -362,7 +352,8 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
362352 self . get_mut_node_by_index ( x) . parent = y;
363353 }
364354
365- fn min_item ( & self ) -> Option < ( & K , & V ) > {
355+ /// Returns a reference to the smallest (minimum) key-value pair in the tree, if any.
356+ pub fn min ( & self ) -> Option < ( & K , & V ) > {
366357 let mut x = self . root ;
367358 if x == SENTINEL {
368359 return None
@@ -374,7 +365,8 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
374365 Some ( ( & item. key , & item. value ) )
375366 }
376367
377- fn max_item ( & self ) -> Option < ( & K , & V ) > {
368+ /// Returns a reference to the largest (maximum) key-value pair in the tree, if any.
369+ pub fn max ( & self ) -> Option < ( & K , & V ) > {
378370 let mut x = self . root ;
379371 if x == SENTINEL {
380372 return None
@@ -386,7 +378,7 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
386378 Some ( ( & item. key , & item. value ) )
387379 }
388380
389- fn min ( & self , mut x : usize ) -> usize {
381+ fn min_usize ( & self , mut x : usize ) -> usize {
390382 if x == SENTINEL {
391383 return x
392384 }
@@ -396,7 +388,7 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
396388 x
397389 }
398390
399- fn max ( & self , mut x : usize ) -> usize {
391+ fn max_usize ( & self , mut x : usize ) -> usize {
400392 if x == SENTINEL {
401393 return x
402394 }
@@ -412,7 +404,7 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
412404 }
413405
414406 if self . get_node_by_index ( x) . right != SENTINEL {
415- return self . min ( self . get_node_by_index ( x) . right )
407+ return self . min_usize ( self . get_node_by_index ( x) . right )
416408 }
417409
418410 let mut y = self . get_node_by_index ( x) . parent ;
@@ -430,7 +422,7 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
430422 }
431423
432424 if self . get_node_by_index ( x) . left != SENTINEL {
433- return self . max ( self . get_node_by_index ( x) . left ) ;
425+ return self . max_usize ( self . get_node_by_index ( x) . left ) ;
434426 }
435427
436428 let mut y = self . get_node_by_index ( x) . parent ;
@@ -464,7 +456,7 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
464456 ///
465457 /// If the key is not found, nothing happens.
466458 #[ inline( always) ]
467- pub fn remove ( & mut self , key : K ) {
459+ pub fn remove ( & mut self , key : K ) {
468460 let mut z = self . get_index_by_key ( & key) ;
469461 if z == SENTINEL {
470462 return ;
@@ -489,7 +481,7 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
489481 y_original_color = z_color;
490482 self . transplant ( z, z_left) ;
491483 } else {
492- y = self . min ( z_right) ;
484+ y = self . min_usize ( z_right) ;
493485 let ( y_right, y_color) = {
494486 let y_node = self . get_node_by_index ( y) ;
495487 ( y_node. right , y_node. color )
@@ -642,6 +634,14 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
642634 return self . nodes [ x] . assume_init_mut ( )
643635 }
644636 }
637+
638+ /// Returns an iterator over the key-value pairs of the tree in ascending key order.
639+ pub fn iter ( & self ) -> RedBlackTreeIter < ' _ , K , V , N > {
640+ RedBlackTreeIter {
641+ tree : self ,
642+ index : SENTINEL ,
643+ }
644+ }
645645
646646 /// Checks if the red-black tree invariants are preserved.
647647 pub fn is_valid ( & self ) -> bool {
@@ -681,6 +681,31 @@ impl<K: Ord, V, const N: usize> RedBlackTree<K,V,N> {
681681
682682 Some ( left_black + if node. color == Color :: Black { 1 } else { 0 } )
683683 }
684+
685+
686+ pub fn contains_key ( & self , key : & K ) -> bool {
687+ let x = self . get_index_by_key ( key) ;
688+ if SENTINEL == x {
689+ return false
690+ }
691+ true
692+ }
693+
694+ pub fn get_mut ( & mut self , key : & K ) -> Option < & mut V > {
695+ let x = self . get_index_by_key ( key) ;
696+ if SENTINEL == x {
697+ return None
698+ }
699+ Some ( & mut self . get_mut_node_by_index ( x) . value )
700+ }
701+
702+ pub fn len ( & self ) -> usize {
703+ N - self . free_len
704+ }
705+
706+ pub fn isEmpty ( & self ) -> bool {
707+ self . len ( ) == N
708+ }
684709}
685710
686711#[ cfg( test) ]
@@ -695,22 +720,29 @@ mod tests {
695720 tree
696721 }
697722
723+ #[ test]
724+ fn test_utils ( ) {
725+ let mut tree = setup_small_tree ( ) ;
726+ assert_eq ! ( tree. contains_key( & 10 ) , true ) ;
727+ assert_eq ! ( tree. get_mut( & 10 ) , Some ( & mut "A" ) ) ;
728+ assert_eq ! ( tree. len( ) , 3 ) ;
729+ assert_eq ! ( tree. isEmpty( ) , false ) ;
730+ }
731+
698732 #[ test]
699733 fn test_min_and_max ( ) {
700734 let tree = setup_small_tree ( ) ;
701- assert_eq ! ( tree. min_item ( ) , Some ( ( & 5 , & "C" ) ) ) ;
702- assert_eq ! ( tree. max_item ( ) , Some ( ( & 20 , & "B" ) ) ) ;
735+ assert_eq ! ( tree. min ( ) , Some ( ( & 5 , & "C" ) ) ) ;
736+ assert_eq ! ( tree. max ( ) , Some ( ( & 20 , & "B" ) ) ) ;
703737 }
704738
705739 #[ test]
706740 fn test_iter ( ) {
707741 let tree = setup_small_tree ( ) ;
708-
709- for pair in tree. iter ( ) {
710- }
711-
712- for pair in tree. iter ( ) . rev ( ) {
713- }
742+
743+ let mut iter = tree. iter ( ) ;
744+ assert_eq ! ( iter. next( ) , Some ( ( & 5 , & "C" ) ) ) ;
745+ assert_eq ! ( iter. next( ) , Some ( ( & 10 , & "A" ) ) ) ;
714746 }
715747
716748 #[ test]
0 commit comments