@@ -21,6 +21,7 @@ use core::prelude::*;
21
21
22
22
use self :: StackOp :: * ;
23
23
use super :: node:: * ;
24
+ use core:: borrow:: BorrowFrom ;
24
25
use std:: hash:: { Writer , Hash } ;
25
26
use core:: default:: Default ;
26
27
use core:: { iter, fmt, mem} ;
@@ -56,7 +57,7 @@ use ring_buf::RingBuf;
56
57
/// and possibly other factors. Using linear search, searching for a random element is expected
57
58
/// to take O(B log<sub>B</sub>n) comparisons, which is generally worse than a BST. In practice,
58
59
/// however, performance is excellent. `BTreeMap` is able to readily outperform `TreeMap` under
59
- /// many workloads, and is competetive where it doesn't. BTreeMap also generally *scales* better
60
+ /// many workloads, and is competitive where it doesn't. BTreeMap also generally *scales* better
60
61
/// than TreeMap, making it more appropriate for large datasets.
61
62
///
62
63
/// However, `TreeMap` may still be more appropriate to use in many contexts. If elements are very
@@ -184,6 +185,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
184
185
185
186
/// Returns a reference to the value corresponding to the key.
186
187
///
188
+ /// The key may be any borrowed form of the map's key type, but the ordering
189
+ /// on the borrowed form *must* match the ordering on the key type.
190
+ ///
187
191
/// # Example
188
192
///
189
193
/// ```
@@ -195,7 +199,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
195
199
/// assert_eq!(map.get(&2), None);
196
200
/// ```
197
201
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
198
- pub fn get ( & self , key : & K ) -> Option < & V > {
202
+ pub fn get < Sized ? Q > ( & self , key : & Q ) -> Option < & V > where Q : BorrowFrom < K > + Ord {
199
203
let mut cur_node = & self . root ;
200
204
loop {
201
205
match cur_node. search ( key) {
@@ -213,6 +217,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
213
217
214
218
/// Returns true if the map contains a value for the specified key.
215
219
///
220
+ /// The key may be any borrowed form of the map's key type, but the ordering
221
+ /// on the borrowed form *must* match the ordering on the key type.
222
+ ///
216
223
/// # Example
217
224
///
218
225
/// ```
@@ -224,7 +231,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
224
231
/// assert_eq!(map.contains_key(&2), false);
225
232
/// ```
226
233
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
227
- pub fn contains_key ( & self , key : & K ) -> bool {
234
+ pub fn contains_key < Sized ? Q > ( & self , key : & Q ) -> bool where Q : BorrowFrom < K > + Ord {
228
235
self . get ( key) . is_some ( )
229
236
}
230
237
@@ -236,6 +243,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
236
243
237
244
/// Returns a mutable reference to the value corresponding to the key.
238
245
///
246
+ /// The key may be any borrowed form of the map's key type, but the ordering
247
+ /// on the borrowed form *must* match the ordering on the key type.
248
+ ///
239
249
/// # Example
240
250
///
241
251
/// ```
@@ -251,7 +261,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
251
261
/// ```
252
262
// See `get` for implementation notes, this is basically a copy-paste with mut's added
253
263
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
254
- pub fn get_mut ( & mut self , key : & K ) -> Option < & mut V > {
264
+ pub fn get_mut < Sized ? Q > ( & mut self , key : & Q ) -> Option < & mut V > where Q : BorrowFrom < K > + Ord {
255
265
// temp_node is a Borrowck hack for having a mutable value outlive a loop iteration
256
266
let mut temp_node = & mut self . root ;
257
267
loop {
@@ -410,6 +420,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
410
420
/// Removes a key from the map, returning the value at the key if the key
411
421
/// was previously in the map.
412
422
///
423
+ /// The key may be any borrowed form of the map's key type, but the ordering
424
+ /// on the borrowed form *must* match the ordering on the key type.
425
+ ///
413
426
/// # Example
414
427
///
415
428
/// ```
@@ -421,7 +434,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
421
434
/// assert_eq!(map.remove(&1), None);
422
435
/// ```
423
436
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
424
- pub fn remove ( & mut self , key : & K ) -> Option < V > {
437
+ pub fn remove < Sized ? Q > ( & mut self , key : & Q ) -> Option < V > where Q : BorrowFrom < K > + Ord {
425
438
// See `swap` for a more thorough description of the stuff going on in here
426
439
let mut stack = stack:: PartialSearchStack :: new ( self ) ;
427
440
loop {
@@ -790,14 +803,18 @@ impl<K: Show, V: Show> Show for BTreeMap<K, V> {
790
803
}
791
804
}
792
805
793
- impl < K : Ord , V > Index < K , V > for BTreeMap < K , V > {
794
- fn index ( & self , key : & K ) -> & V {
806
+ impl < K : Ord , Sized ? Q , V > Index < Q , V > for BTreeMap < K , V >
807
+ where Q : BorrowFrom < K > + Ord
808
+ {
809
+ fn index ( & self , key : & Q ) -> & V {
795
810
self . get ( key) . expect ( "no entry found for key" )
796
811
}
797
812
}
798
813
799
- impl < K : Ord , V > IndexMut < K , V > for BTreeMap < K , V > {
800
- fn index_mut ( & mut self , key : & K ) -> & mut V {
814
+ impl < K : Ord , Sized ? Q , V > IndexMut < Q , V > for BTreeMap < K , V >
815
+ where Q : BorrowFrom < K > + Ord
816
+ {
817
+ fn index_mut ( & mut self , key : & Q ) -> & mut V {
801
818
self . get_mut ( key) . expect ( "no entry found for key" )
802
819
}
803
820
}
0 commit comments