11
11
use core:: prelude:: * ;
12
12
13
13
use alloc:: boxed:: Box ;
14
+
15
+ use core:: borrow:: BorrowFrom ;
14
16
use core:: default:: Default ;
15
17
use core:: fmt;
16
18
use core:: fmt:: Show ;
@@ -188,16 +190,16 @@ impl<K: Ord, V> Default for TreeMap<K,V> {
188
190
fn default ( ) -> TreeMap < K , V > { TreeMap :: new ( ) }
189
191
}
190
192
191
- impl < K : Ord , V > Index < K , V > for TreeMap < K , V > {
193
+ impl < K : Ord , Sized ? Q , V > Index < Q , V > for TreeMap < K , V > where Q : BorrowFrom < K > + Ord {
192
194
#[ inline]
193
- fn index < ' a > ( & ' a self , i : & K ) -> & ' a V {
195
+ fn index < ' a > ( & ' a self , i : & Q ) -> & ' a V {
194
196
self . get ( i) . expect ( "no entry found for key" )
195
197
}
196
198
}
197
199
198
- impl < K : Ord , V > IndexMut < K , V > for TreeMap < K , V > {
200
+ impl < K : Ord , Sized ? Q , V > IndexMut < Q , V > for TreeMap < K , V > where Q : BorrowFrom < K > + Ord {
199
201
#[ inline]
200
- fn index_mut < ' a > ( & ' a mut self , i : & K ) -> & ' a mut V {
202
+ fn index_mut < ' a > ( & ' a mut self , i : & Q ) -> & ' a mut V {
201
203
self . get_mut ( i) . expect ( "no entry found for key" )
202
204
}
203
205
}
@@ -446,6 +448,9 @@ impl<K: Ord, V> TreeMap<K, V> {
446
448
447
449
/// Returns a reference to the value corresponding to the key.
448
450
///
451
+ /// The key may be any borrowed form of the map's key type, but the ordering
452
+ /// on the borrowed form *must* match the ordering on the key type.
453
+ ///
449
454
/// # Example
450
455
///
451
456
/// ```
@@ -458,12 +463,17 @@ impl<K: Ord, V> TreeMap<K, V> {
458
463
/// ```
459
464
#[ inline]
460
465
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
461
- pub fn get ( & self , key : & K ) -> Option < & V > {
462
- tree_find_with ( & self . root , |k2| key. cmp ( k2) )
466
+ pub fn get < Sized ? Q > ( & self , key : & Q ) -> Option < & V >
467
+ where Q : BorrowFrom < K > + Ord
468
+ {
469
+ tree_find_with ( & self . root , |k2| key. cmp ( BorrowFrom :: borrow_from ( k2) ) )
463
470
}
464
471
465
472
/// Returns true if the map contains a value for the specified key.
466
473
///
474
+ /// The key may be any borrowed form of the map's key type, but the ordering
475
+ /// on the borrowed form *must* match the ordering on the key type.
476
+ ///
467
477
/// # Example
468
478
///
469
479
/// ```
@@ -476,7 +486,9 @@ impl<K: Ord, V> TreeMap<K, V> {
476
486
/// ```
477
487
#[ inline]
478
488
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
479
- pub fn contains_key ( & self , key : & K ) -> bool {
489
+ pub fn contains_key < Sized ? Q > ( & self , key : & Q ) -> bool
490
+ where Q : BorrowFrom < K > + Ord
491
+ {
480
492
self . get ( key) . is_some ( )
481
493
}
482
494
@@ -488,6 +500,9 @@ impl<K: Ord, V> TreeMap<K, V> {
488
500
489
501
/// Returns a mutable reference to the value corresponding to the key.
490
502
///
503
+ /// The key may be any borrowed form of the map's key type, but the ordering
504
+ /// on the borrowed form *must* match the ordering on the key type.
505
+ ///
491
506
/// # Example
492
507
///
493
508
/// ```
@@ -503,8 +518,10 @@ impl<K: Ord, V> TreeMap<K, V> {
503
518
/// ```
504
519
#[ inline]
505
520
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
506
- pub fn get_mut ( & mut self , key : & K ) -> Option < & mut V > {
507
- tree_find_with_mut ( & mut self . root , |x| key. cmp ( x) )
521
+ pub fn get_mut < Sized ? Q > ( & mut self , key : & Q ) -> Option < & mut V >
522
+ where Q : BorrowFrom < K > + Ord
523
+ {
524
+ tree_find_with_mut ( & mut self . root , |x| key. cmp ( BorrowFrom :: borrow_from ( x) ) )
508
525
}
509
526
510
527
/// Deprecated: Renamed to `insert`.
@@ -545,6 +562,9 @@ impl<K: Ord, V> TreeMap<K, V> {
545
562
/// Removes a key from the map, returning the value at the key if the key
546
563
/// was previously in the map.
547
564
///
565
+ /// The key may be any borrowed form of the map's key type, but the ordering
566
+ /// on the borrowed form *must* match the ordering on the key type.
567
+ ///
548
568
/// # Example
549
569
///
550
570
/// ```
@@ -556,7 +576,9 @@ impl<K: Ord, V> TreeMap<K, V> {
556
576
/// assert_eq!(map.remove(&1), None);
557
577
/// ```
558
578
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
559
- pub fn remove ( & mut self , key : & K ) -> Option < V > {
579
+ pub fn remove < Sized ? Q > ( & mut self , key : & Q ) -> Option < V >
580
+ where Q : BorrowFrom < K > + Ord
581
+ {
560
582
let ret = remove ( & mut self . root , key) ;
561
583
if ret. is_some ( ) { self . length -= 1 }
562
584
ret
@@ -589,6 +611,7 @@ impl<K, V> TreeMap<K, V> {
589
611
/// assert_eq!((*ua.unwrap()).as_slice(), "Curl-Rust/0.1");
590
612
/// ```
591
613
#[ inline]
614
+ #[ experimental = "likely to be renamed, may be removed" ]
592
615
pub fn find_with ( & self , f: |& K | -> Ordering ) -> Option < & V > {
593
616
tree_find_with ( & self . root , f)
594
617
}
@@ -613,6 +636,7 @@ impl<K, V> TreeMap<K, V> {
613
636
/// assert_eq!(t.get(&"User-Agent"), Some(&new_ua));
614
637
/// ```
615
638
#[ inline]
639
+ #[ experimental = "likely to be renamed, may be removed" ]
616
640
pub fn find_with_mut < ' a > ( & ' a mut self , f: |& K | -> Ordering ) -> Option < & ' a mut V > {
617
641
tree_find_with_mut ( & mut self . root , f)
618
642
}
@@ -1168,10 +1192,11 @@ fn insert<K: Ord, V>(node: &mut Option<Box<TreeNode<K, V>>>,
1168
1192
}
1169
1193
}
1170
1194
1171
- fn remove < K : Ord , V > ( node : & mut Option < Box < TreeNode < K , V > > > ,
1172
- key : & K ) -> Option < V > {
1195
+ fn remove < K , Sized ? Q , V > ( node : & mut Option < Box < TreeNode < K , V > > > , key : & Q ) -> Option < V >
1196
+ where K : Ord , Q : BorrowFrom < K > + Ord
1197
+ {
1173
1198
fn heir_swap < K : Ord , V > ( node : & mut Box < TreeNode < K , V > > ,
1174
- child : & mut Option < Box < TreeNode < K , V > > > ) {
1199
+ child : & mut Option < Box < TreeNode < K , V > > > ) {
1175
1200
// *could* be done without recursion, but it won't borrow check
1176
1201
for x in child. iter_mut ( ) {
1177
1202
if x. right . is_some ( ) {
@@ -1188,7 +1213,7 @@ fn remove<K: Ord, V>(node: &mut Option<Box<TreeNode<K, V>>>,
1188
1213
return None ; // bottom of tree
1189
1214
}
1190
1215
Some ( ref mut save) => {
1191
- let ( ret, rebalance) = match key. cmp ( & save. key ) {
1216
+ let ( ret, rebalance) = match key. cmp ( BorrowFrom :: borrow_from ( & save. key ) ) {
1192
1217
Less => ( remove ( & mut save. left , key) , true ) ,
1193
1218
Greater => ( remove ( & mut save. right , key) , true ) ,
1194
1219
Equal => {
@@ -1918,4 +1943,3 @@ mod bench {
1918
1943
bench_iter ( b, 100000 ) ;
1919
1944
}
1920
1945
}
1921
-
0 commit comments