11use super :: map:: MIN_LEN ;
22use super :: node:: { marker, ForceResult :: * , Handle , LeftOrRight :: * , NodeRef , Root } ;
3+ use core:: alloc:: Allocator ;
34
45impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
56 /// Stocks up a possibly underfull node by merging with or stealing from a
67 /// sibling. If successful but at the cost of shrinking the parent node,
78 /// returns that shrunk parent node. Returns an `Err` if the node is
89 /// an empty root.
9- fn fix_node_through_parent (
10+ fn fix_node_through_parent < A : Allocator > (
1011 self ,
12+ alloc : & A ,
1113 ) -> Result < Option < NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > > , Self > {
1214 let len = self . len ( ) ;
1315 if len >= MIN_LEN {
@@ -16,7 +18,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
1618 match self . choose_parent_kv ( ) {
1719 Ok ( Left ( mut left_parent_kv) ) => {
1820 if left_parent_kv. can_merge ( ) {
19- let parent = left_parent_kv. merge_tracking_parent ( ) ;
21+ let parent = left_parent_kv. merge_tracking_parent ( alloc ) ;
2022 Ok ( Some ( parent) )
2123 } else {
2224 left_parent_kv. bulk_steal_left ( MIN_LEN - len) ;
@@ -25,7 +27,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
2527 }
2628 Ok ( Right ( mut right_parent_kv) ) => {
2729 if right_parent_kv. can_merge ( ) {
28- let parent = right_parent_kv. merge_tracking_parent ( ) ;
30+ let parent = right_parent_kv. merge_tracking_parent ( alloc ) ;
2931 Ok ( Some ( parent) )
3032 } else {
3133 right_parent_kv. bulk_steal_right ( MIN_LEN - len) ;
@@ -52,9 +54,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
5254 ///
5355 /// This method does not expect ancestors to already be underfull upon entry
5456 /// and panics if it encounters an empty ancestor.
55- pub fn fix_node_and_affected_ancestors ( mut self ) -> bool {
57+ pub fn fix_node_and_affected_ancestors < A : Allocator > ( mut self , alloc : & A ) -> bool {
5658 loop {
57- match self . fix_node_through_parent ( ) {
59+ match self . fix_node_through_parent ( alloc ) {
5860 Ok ( Some ( parent) ) => self = parent. forget_type ( ) ,
5961 Ok ( None ) => return true ,
6062 Err ( _) => return false ,
@@ -65,29 +67,29 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
6567
6668impl < K , V > Root < K , V > {
6769 /// Removes empty levels on the top, but keeps an empty leaf if the entire tree is empty.
68- pub fn fix_top ( & mut self ) {
70+ pub fn fix_top < A : Allocator > ( & mut self , alloc : & A ) {
6971 while self . height ( ) > 0 && self . len ( ) == 0 {
70- self . pop_internal_level ( ) ;
72+ self . pop_internal_level ( alloc ) ;
7173 }
7274 }
7375
7476 /// Stocks up or merge away any underfull nodes on the right border of the
7577 /// tree. The other nodes, those that are not the root nor a rightmost edge,
7678 /// must already have at least MIN_LEN elements.
77- pub fn fix_right_border ( & mut self ) {
78- self . fix_top ( ) ;
79+ pub fn fix_right_border < A : Allocator > ( & mut self , alloc : & A ) {
80+ self . fix_top ( alloc ) ;
7981 if self . len ( ) > 0 {
80- self . borrow_mut ( ) . last_kv ( ) . fix_right_border_of_right_edge ( ) ;
81- self . fix_top ( ) ;
82+ self . borrow_mut ( ) . last_kv ( ) . fix_right_border_of_right_edge ( alloc ) ;
83+ self . fix_top ( alloc ) ;
8284 }
8385 }
8486
8587 /// The symmetric clone of `fix_right_border`.
86- pub fn fix_left_border ( & mut self ) {
87- self . fix_top ( ) ;
88+ pub fn fix_left_border < A : Allocator > ( & mut self , alloc : & A ) {
89+ self . fix_top ( alloc ) ;
8890 if self . len ( ) > 0 {
89- self . borrow_mut ( ) . first_kv ( ) . fix_left_border_of_left_edge ( ) ;
90- self . fix_top ( ) ;
91+ self . borrow_mut ( ) . first_kv ( ) . fix_left_border_of_left_edge ( alloc ) ;
92+ self . fix_top ( alloc ) ;
9193 }
9294 }
9395
@@ -113,16 +115,16 @@ impl<K, V> Root<K, V> {
113115}
114116
115117impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > {
116- fn fix_left_border_of_left_edge ( mut self ) {
118+ fn fix_left_border_of_left_edge < A : Allocator > ( mut self , alloc : & A ) {
117119 while let Internal ( internal_kv) = self . force ( ) {
118- self = internal_kv. fix_left_child ( ) . first_kv ( ) ;
120+ self = internal_kv. fix_left_child ( alloc ) . first_kv ( ) ;
119121 debug_assert ! ( self . reborrow( ) . into_node( ) . len( ) > MIN_LEN ) ;
120122 }
121123 }
122124
123- fn fix_right_border_of_right_edge ( mut self ) {
125+ fn fix_right_border_of_right_edge < A : Allocator > ( mut self , alloc : & A ) {
124126 while let Internal ( internal_kv) = self . force ( ) {
125- self = internal_kv. fix_right_child ( ) . last_kv ( ) ;
127+ self = internal_kv. fix_right_child ( alloc ) . last_kv ( ) ;
126128 debug_assert ! ( self . reborrow( ) . into_node( ) . len( ) > MIN_LEN ) ;
127129 }
128130 }
@@ -133,12 +135,15 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
133135 /// provisions an extra element to allow merging its children in turn
134136 /// without becoming underfull.
135137 /// Returns the left child.
136- fn fix_left_child ( self ) -> NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
138+ fn fix_left_child < A : Allocator > (
139+ self ,
140+ alloc : & A ,
141+ ) -> NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
137142 let mut internal_kv = self . consider_for_balancing ( ) ;
138143 let left_len = internal_kv. left_child_len ( ) ;
139144 debug_assert ! ( internal_kv. right_child_len( ) >= MIN_LEN ) ;
140145 if internal_kv. can_merge ( ) {
141- internal_kv. merge_tracking_child ( )
146+ internal_kv. merge_tracking_child ( alloc )
142147 } else {
143148 // `MIN_LEN + 1` to avoid readjust if merge happens on the next level.
144149 let count = ( MIN_LEN + 1 ) . saturating_sub ( left_len) ;
@@ -153,12 +158,15 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
153158 /// provisions an extra element to allow merging its children in turn
154159 /// without becoming underfull.
155160 /// Returns wherever the right child ended up.
156- fn fix_right_child ( self ) -> NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
161+ fn fix_right_child < A : Allocator > (
162+ self ,
163+ alloc : & A ,
164+ ) -> NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
157165 let mut internal_kv = self . consider_for_balancing ( ) ;
158166 let right_len = internal_kv. right_child_len ( ) ;
159167 debug_assert ! ( internal_kv. left_child_len( ) >= MIN_LEN ) ;
160168 if internal_kv. can_merge ( ) {
161- internal_kv. merge_tracking_child ( )
169+ internal_kv. merge_tracking_child ( alloc )
162170 } else {
163171 // `MIN_LEN + 1` to avoid readjust if merge happens on the next level.
164172 let count = ( MIN_LEN + 1 ) . saturating_sub ( right_len) ;
0 commit comments