1
1
use super :: map:: MIN_LEN ;
2
2
use super :: node:: { marker, ForceResult :: * , Handle , LeftOrRight :: * , NodeRef , Root } ;
3
+ use core:: alloc:: Allocator ;
3
4
4
5
impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
5
6
/// Stocks up a possibly underfull node by merging with or stealing from a
6
7
/// sibling. If successful but at the cost of shrinking the parent node,
7
8
/// returns that shrunk parent node. Returns an `Err` if the node is
8
9
/// an empty root.
9
- fn fix_node_through_parent (
10
+ fn fix_node_through_parent < A : Allocator > (
10
11
self ,
12
+ alloc : & A ,
11
13
) -> Result < Option < NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > > , Self > {
12
14
let len = self . len ( ) ;
13
15
if len >= MIN_LEN {
@@ -16,7 +18,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
16
18
match self . choose_parent_kv ( ) {
17
19
Ok ( Left ( mut left_parent_kv) ) => {
18
20
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 ) ;
20
22
Ok ( Some ( parent) )
21
23
} else {
22
24
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> {
25
27
}
26
28
Ok ( Right ( mut right_parent_kv) ) => {
27
29
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 ) ;
29
31
Ok ( Some ( parent) )
30
32
} else {
31
33
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> {
52
54
///
53
55
/// This method does not expect ancestors to already be underfull upon entry
54
56
/// 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 {
56
58
loop {
57
- match self . fix_node_through_parent ( ) {
59
+ match self . fix_node_through_parent ( alloc ) {
58
60
Ok ( Some ( parent) ) => self = parent. forget_type ( ) ,
59
61
Ok ( None ) => return true ,
60
62
Err ( _) => return false ,
@@ -65,29 +67,29 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
65
67
66
68
impl < K , V > Root < K , V > {
67
69
/// 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 ) {
69
71
while self . height ( ) > 0 && self . len ( ) == 0 {
70
- self . pop_internal_level ( ) ;
72
+ self . pop_internal_level ( alloc ) ;
71
73
}
72
74
}
73
75
74
76
/// Stocks up or merge away any underfull nodes on the right border of the
75
77
/// tree. The other nodes, those that are not the root nor a rightmost edge,
76
78
/// 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 ) ;
79
81
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 ) ;
82
84
}
83
85
}
84
86
85
87
/// 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 ) ;
88
90
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 ) ;
91
93
}
92
94
}
93
95
@@ -113,16 +115,16 @@ impl<K, V> Root<K, V> {
113
115
}
114
116
115
117
impl < ' 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 ) {
117
119
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 ( ) ;
119
121
debug_assert ! ( self . reborrow( ) . into_node( ) . len( ) > MIN_LEN ) ;
120
122
}
121
123
}
122
124
123
- fn fix_right_border_of_right_edge ( mut self ) {
125
+ fn fix_right_border_of_right_edge < A : Allocator > ( mut self , alloc : & A ) {
124
126
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 ( ) ;
126
128
debug_assert ! ( self . reborrow( ) . into_node( ) . len( ) > MIN_LEN ) ;
127
129
}
128
130
}
@@ -133,12 +135,15 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
133
135
/// provisions an extra element to allow merging its children in turn
134
136
/// without becoming underfull.
135
137
/// 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 > {
137
142
let mut internal_kv = self . consider_for_balancing ( ) ;
138
143
let left_len = internal_kv. left_child_len ( ) ;
139
144
debug_assert ! ( internal_kv. right_child_len( ) >= MIN_LEN ) ;
140
145
if internal_kv. can_merge ( ) {
141
- internal_kv. merge_tracking_child ( )
146
+ internal_kv. merge_tracking_child ( alloc )
142
147
} else {
143
148
// `MIN_LEN + 1` to avoid readjust if merge happens on the next level.
144
149
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>,
153
158
/// provisions an extra element to allow merging its children in turn
154
159
/// without becoming underfull.
155
160
/// 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 > {
157
165
let mut internal_kv = self . consider_for_balancing ( ) ;
158
166
let right_len = internal_kv. right_child_len ( ) ;
159
167
debug_assert ! ( internal_kv. left_child_len( ) >= MIN_LEN ) ;
160
168
if internal_kv. can_merge ( ) {
161
- internal_kv. merge_tracking_child ( )
169
+ internal_kv. merge_tracking_child ( alloc )
162
170
} else {
163
171
// `MIN_LEN + 1` to avoid readjust if merge happens on the next level.
164
172
let count = ( MIN_LEN + 1 ) . saturating_sub ( right_len) ;
0 commit comments