Skip to content

Commit 5eec666

Browse files
committed
libcollections: use BorrowFrom in TreeSet, Map
This commit generalizes methods like `get` and `remove` for `TreeMap` and `TreeSet` to use the new `std::borrow` infrastructure. [breaking-change]
1 parent 4ab2235 commit 5eec666

File tree

2 files changed

+56
-17
lines changed

2 files changed

+56
-17
lines changed

src/libcollections/tree/map.rs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use core::prelude::*;
1212

1313
use alloc::boxed::Box;
14+
15+
use core::borrow::BorrowFrom;
1416
use core::default::Default;
1517
use core::fmt;
1618
use core::fmt::Show;
@@ -188,16 +190,16 @@ impl<K: Ord, V> Default for TreeMap<K,V> {
188190
fn default() -> TreeMap<K, V> { TreeMap::new() }
189191
}
190192

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 {
192194
#[inline]
193-
fn index<'a>(&'a self, i: &K) -> &'a V {
195+
fn index<'a>(&'a self, i: &Q) -> &'a V {
194196
self.get(i).expect("no entry found for key")
195197
}
196198
}
197199

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 {
199201
#[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 {
201203
self.get_mut(i).expect("no entry found for key")
202204
}
203205
}
@@ -446,6 +448,9 @@ impl<K: Ord, V> TreeMap<K, V> {
446448

447449
/// Returns a reference to the value corresponding to the key.
448450
///
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+
///
449454
/// # Example
450455
///
451456
/// ```
@@ -458,12 +463,17 @@ impl<K: Ord, V> TreeMap<K, V> {
458463
/// ```
459464
#[inline]
460465
#[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)))
463470
}
464471

465472
/// Returns true if the map contains a value for the specified key.
466473
///
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+
///
467477
/// # Example
468478
///
469479
/// ```
@@ -476,7 +486,9 @@ impl<K: Ord, V> TreeMap<K, V> {
476486
/// ```
477487
#[inline]
478488
#[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+
{
480492
self.get(key).is_some()
481493
}
482494

@@ -488,6 +500,9 @@ impl<K: Ord, V> TreeMap<K, V> {
488500

489501
/// Returns a mutable reference to the value corresponding to the key.
490502
///
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+
///
491506
/// # Example
492507
///
493508
/// ```
@@ -503,8 +518,10 @@ impl<K: Ord, V> TreeMap<K, V> {
503518
/// ```
504519
#[inline]
505520
#[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)))
508525
}
509526

510527
/// Deprecated: Renamed to `insert`.
@@ -545,6 +562,9 @@ impl<K: Ord, V> TreeMap<K, V> {
545562
/// Removes a key from the map, returning the value at the key if the key
546563
/// was previously in the map.
547564
///
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+
///
548568
/// # Example
549569
///
550570
/// ```
@@ -556,7 +576,9 @@ impl<K: Ord, V> TreeMap<K, V> {
556576
/// assert_eq!(map.remove(&1), None);
557577
/// ```
558578
#[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+
{
560582
let ret = remove(&mut self.root, key);
561583
if ret.is_some() { self.length -= 1 }
562584
ret
@@ -589,6 +611,7 @@ impl<K, V> TreeMap<K, V> {
589611
/// assert_eq!((*ua.unwrap()).as_slice(), "Curl-Rust/0.1");
590612
/// ```
591613
#[inline]
614+
#[experimental = "likely to be renamed, may be removed"]
592615
pub fn find_with(&self, f:|&K| -> Ordering) -> Option<&V> {
593616
tree_find_with(&self.root, f)
594617
}
@@ -613,6 +636,7 @@ impl<K, V> TreeMap<K, V> {
613636
/// assert_eq!(t.get(&"User-Agent"), Some(&new_ua));
614637
/// ```
615638
#[inline]
639+
#[experimental = "likely to be renamed, may be removed"]
616640
pub fn find_with_mut<'a>(&'a mut self, f:|&K| -> Ordering) -> Option<&'a mut V> {
617641
tree_find_with_mut(&mut self.root, f)
618642
}
@@ -1168,10 +1192,11 @@ fn insert<K: Ord, V>(node: &mut Option<Box<TreeNode<K, V>>>,
11681192
}
11691193
}
11701194

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+
{
11731198
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>>>) {
11751200
// *could* be done without recursion, but it won't borrow check
11761201
for x in child.iter_mut() {
11771202
if x.right.is_some() {
@@ -1188,7 +1213,7 @@ fn remove<K: Ord, V>(node: &mut Option<Box<TreeNode<K, V>>>,
11881213
return None; // bottom of tree
11891214
}
11901215
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)) {
11921217
Less => (remove(&mut save.left, key), true),
11931218
Greater => (remove(&mut save.right, key), true),
11941219
Equal => {
@@ -1918,4 +1943,3 @@ mod bench {
19181943
bench_iter(b, 100000);
19191944
}
19201945
}
1921-

src/libcollections/tree/set.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use core::prelude::*;
1212

13+
use core::borrow::BorrowFrom;
1314
use core::default::Default;
1415
use core::fmt;
1516
use core::fmt::Show;
@@ -396,6 +397,10 @@ impl<T: Ord> TreeSet<T> {
396397

397398
/// Returns `true` if the set contains a value.
398399
///
400+
/// The value may be any borrowed form of the set's value type,
401+
/// but the ordering on the borrowed form *must* match the
402+
/// ordering on the value type.
403+
///
399404
/// # Example
400405
///
401406
/// ```
@@ -407,7 +412,9 @@ impl<T: Ord> TreeSet<T> {
407412
/// ```
408413
#[inline]
409414
#[unstable = "matches collection reform specification, waiting for dust to settle"]
410-
pub fn contains(&self, value: &T) -> bool {
415+
pub fn contains<Sized? Q>(&self, value: &Q) -> bool
416+
where Q: Ord + BorrowFrom<T>
417+
{
411418
self.map.contains_key(value)
412419
}
413420

@@ -519,6 +526,10 @@ impl<T: Ord> TreeSet<T> {
519526
/// Removes a value from the set. Returns `true` if the value was
520527
/// present in the set.
521528
///
529+
/// The value may be any borrowed form of the set's value type,
530+
/// but the ordering on the borrowed form *must* match the
531+
/// ordering on the value type.
532+
///
522533
/// # Example
523534
///
524535
/// ```
@@ -532,7 +543,11 @@ impl<T: Ord> TreeSet<T> {
532543
/// ```
533544
#[inline]
534545
#[unstable = "matches collection reform specification, waiting for dust to settle"]
535-
pub fn remove(&mut self, value: &T) -> bool { self.map.remove(value).is_some() }
546+
pub fn remove<Sized? Q>(&mut self, value: &Q) -> bool
547+
where Q: Ord + BorrowFrom<T>
548+
{
549+
self.map.remove(value).is_some()
550+
}
536551
}
537552

538553
/// A lazy forward iterator over a set.

0 commit comments

Comments
 (0)