@@ -42,9 +42,7 @@ impl<K: Ord, V> SortedMap<K, V> {
42
42
43
43
#[ inline]
44
44
pub fn insert ( & mut self , key : K , mut value : V ) -> Option < V > {
45
- let index = self . data . binary_search_by ( |& ( ref x, _) | x. cmp ( & key) ) ;
46
-
47
- match index {
45
+ match self . lookup_index_for ( & key) {
48
46
Ok ( index) => {
49
47
let mut slot = unsafe {
50
48
self . data . get_unchecked_mut ( index)
@@ -61,9 +59,7 @@ impl<K: Ord, V> SortedMap<K, V> {
61
59
62
60
#[ inline]
63
61
pub fn remove ( & mut self , key : & K ) -> Option < V > {
64
- let index = self . data . binary_search_by ( |& ( ref x, _) | x. cmp ( key) ) ;
65
-
66
- match index {
62
+ match self . lookup_index_for ( key) {
67
63
Ok ( index) => {
68
64
Some ( self . data . remove ( index) . 1 )
69
65
}
@@ -75,9 +71,7 @@ impl<K: Ord, V> SortedMap<K, V> {
75
71
76
72
#[ inline]
77
73
pub fn get ( & self , key : & K ) -> Option < & V > {
78
- let index = self . data . binary_search_by ( |& ( ref x, _) | x. cmp ( key) ) ;
79
-
80
- match index {
74
+ match self . lookup_index_for ( key) {
81
75
Ok ( index) => {
82
76
unsafe {
83
77
Some ( & self . data . get_unchecked ( index) . 1 )
@@ -91,9 +85,7 @@ impl<K: Ord, V> SortedMap<K, V> {
91
85
92
86
#[ inline]
93
87
pub fn get_mut ( & mut self , key : & K ) -> Option < & mut V > {
94
- let index = self . data . binary_search_by ( |& ( ref x, _) | x. cmp ( key) ) ;
95
-
96
- match index {
88
+ match self . lookup_index_for ( key) {
97
89
Ok ( index) => {
98
90
unsafe {
99
91
Some ( & mut self . data . get_unchecked_mut ( index) . 1 )
@@ -168,12 +160,9 @@ impl<K: Ord, V> SortedMap<K, V> {
168
160
169
161
debug_assert ! ( elements. windows( 2 ) . all( |w| w[ 0 ] . 0 < w[ 1 ] . 0 ) ) ;
170
162
171
- let index = {
172
- let first_element = & elements[ 0 ] . 0 ;
173
- self . data . binary_search_by ( |& ( ref x, _) | x. cmp ( first_element) )
174
- } ;
163
+ let start_index = self . lookup_index_for ( & elements[ 0 ] . 0 ) ;
175
164
176
- let drain = match index {
165
+ let drain = match start_index {
177
166
Ok ( index) => {
178
167
let mut drain = elements. drain ( ..) ;
179
168
self . data [ index] = drain. next ( ) . unwrap ( ) ;
@@ -200,18 +189,24 @@ impl<K: Ord, V> SortedMap<K, V> {
200
189
}
201
190
}
202
191
192
+ /// Looks up the key in `self.data` via `slice::binary_search()`.
193
+ #[ inline( always) ]
194
+ fn lookup_index_for ( & self , key : & K ) -> Result < usize , usize > {
195
+ self . data . binary_search_by ( |& ( ref x, _) | x. cmp ( key) )
196
+ }
197
+
203
198
#[ inline]
204
199
fn range_slice_indices < R > ( & self , range : R ) -> ( usize , usize )
205
200
where R : RangeBounds < K >
206
201
{
207
202
let start = match range. start ( ) {
208
203
Bound :: Included ( ref k) => {
209
- match self . data . binary_search_by ( | & ( ref x , _ ) | x . cmp ( k ) ) {
204
+ match self . lookup_index_for ( k ) {
210
205
Ok ( index) | Err ( index) => index
211
206
}
212
207
}
213
208
Bound :: Excluded ( ref k) => {
214
- match self . data . binary_search_by ( | & ( ref x , _ ) | x . cmp ( k ) ) {
209
+ match self . lookup_index_for ( k ) {
215
210
Ok ( index) => index + 1 ,
216
211
Err ( index) => index,
217
212
}
@@ -221,13 +216,13 @@ impl<K: Ord, V> SortedMap<K, V> {
221
216
222
217
let end = match range. end ( ) {
223
218
Bound :: Included ( ref k) => {
224
- match self . data . binary_search_by ( | & ( ref x , _ ) | x . cmp ( k ) ) {
219
+ match self . lookup_index_for ( k ) {
225
220
Ok ( index) => index + 1 ,
226
221
Err ( index) => index,
227
222
}
228
223
}
229
224
Bound :: Excluded ( ref k) => {
230
- match self . data . binary_search_by ( | & ( ref x , _ ) | x . cmp ( k ) ) {
225
+ match self . lookup_index_for ( k ) {
231
226
Ok ( index) | Err ( index) => index,
232
227
}
233
228
}
0 commit comments