Skip to content

Commit d854d72

Browse files
committed
---
yaml --- r: 159695 b: refs/heads/auto c: ff88510 h: refs/heads/master i: 159693: c367810 159691: e0f37e9 159687: 7f5626d 159679: 50a5ce3 v: v3
1 parent 3fcf300 commit d854d72

File tree

4 files changed

+43
-16
lines changed

4 files changed

+43
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: 7213de1c49e448c7c6ad2d30dc3e6b3a13e090df
13+
refs/heads/auto: ff88510535611f8497047584b18b819b7fe5cb3a
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libcollections/btree/map.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::prelude::*;
2121

2222
use self::StackOp::*;
2323
use super::node::*;
24+
use core::borrow::BorrowFrom;
2425
use std::hash::{Writer, Hash};
2526
use core::default::Default;
2627
use core::{iter, fmt, mem};
@@ -184,6 +185,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
184185

185186
/// Returns a reference to the value corresponding to the key.
186187
///
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+
///
187191
/// # Example
188192
///
189193
/// ```
@@ -195,7 +199,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
195199
/// assert_eq!(map.get(&2), None);
196200
/// ```
197201
#[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 {
199203
let mut cur_node = &self.root;
200204
loop {
201205
match cur_node.search(key) {
@@ -213,6 +217,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
213217

214218
/// Returns true if the map contains a value for the specified key.
215219
///
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+
///
216223
/// # Example
217224
///
218225
/// ```
@@ -224,7 +231,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
224231
/// assert_eq!(map.contains_key(&2), false);
225232
/// ```
226233
#[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 {
228235
self.get(key).is_some()
229236
}
230237

@@ -236,6 +243,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
236243

237244
/// Returns a mutable reference to the value corresponding to the key.
238245
///
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+
///
239249
/// # Example
240250
///
241251
/// ```
@@ -251,7 +261,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
251261
/// ```
252262
// See `get` for implementation notes, this is basically a copy-paste with mut's added
253263
#[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 {
255265
// temp_node is a Borrowck hack for having a mutable value outlive a loop iteration
256266
let mut temp_node = &mut self.root;
257267
loop {
@@ -410,6 +420,9 @@ impl<K: Ord, V> BTreeMap<K, V> {
410420
/// Removes a key from the map, returning the value at the key if the key
411421
/// was previously in the map.
412422
///
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+
///
413426
/// # Example
414427
///
415428
/// ```
@@ -421,7 +434,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
421434
/// assert_eq!(map.remove(&1), None);
422435
/// ```
423436
#[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 {
425438
// See `swap` for a more thorough description of the stuff going on in here
426439
let mut stack = stack::PartialSearchStack::new(self);
427440
loop {
@@ -790,14 +803,18 @@ impl<K: Show, V: Show> Show for BTreeMap<K, V> {
790803
}
791804
}
792805

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 {
795810
self.get(key).expect("no entry found for key")
796811
}
797812
}
798813

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 {
801818
self.get_mut(key).expect("no entry found for key")
802819
}
803820
}

branches/auto/src/libcollections/btree/node.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use core::prelude::*;
1919

2020
use core::{slice, mem, ptr};
2121
use core::iter::Zip;
22+
use core::borrow::BorrowFrom;
2223

2324
use vec;
2425
use vec::Vec;
@@ -73,19 +74,19 @@ impl<K: Ord, V> Node<K, V> {
7374
/// Searches for the given key in the node. If it finds an exact match,
7475
/// `Found` will be yielded with the matching index. If it doesn't find an exact match,
7576
/// `GoDown` will be yielded with the index of the subtree the key must lie in.
76-
pub fn search(&self, key: &K) -> SearchResult {
77+
pub fn search<Sized? Q>(&self, key: &Q) -> SearchResult where Q: BorrowFrom<K> + Ord {
7778
// FIXME(Gankro): Tune when to search linear or binary based on B (and maybe K/V).
7879
// For the B configured as of this writing (B = 6), binary search was *significantly*
7980
// worse for uints.
8081
self.search_linear(key)
8182
}
8283

83-
fn search_linear(&self, key: &K) -> SearchResult {
84+
fn search_linear<Sized? Q>(&self, key: &Q) -> SearchResult where Q: BorrowFrom<K> + Ord {
8485
for (i, k) in self.keys.iter().enumerate() {
85-
match k.cmp(key) {
86-
Less => {},
86+
match key.cmp(BorrowFrom::borrow_from(k)) {
87+
Greater => {},
8788
Equal => return Found(i),
88-
Greater => return GoDown(i),
89+
Less => return GoDown(i),
8990
}
9091
}
9192
GoDown(self.len())

branches/auto/src/libcollections/btree/set.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use core::prelude::*;
1515

1616
use btree_map::{BTreeMap, Keys, MoveEntries};
1717
use std::hash::Hash;
18+
use core::borrow::BorrowFrom;
1819
use core::default::Default;
1920
use core::{iter, fmt};
2021
use core::iter::Peekable;
@@ -167,6 +168,10 @@ impl<T: Ord> BTreeSet<T> {
167168

168169
/// Returns `true` if the set contains a value.
169170
///
171+
/// The value may be any borrowed form of the set's value type,
172+
/// but the ordering on the borrowed form *must* match the
173+
/// ordering on the value type.
174+
///
170175
/// # Example
171176
///
172177
/// ```
@@ -177,7 +182,7 @@ impl<T: Ord> BTreeSet<T> {
177182
/// assert_eq!(set.contains(&4), false);
178183
/// ```
179184
#[unstable = "matches collection reform specification, waiting for dust to settle"]
180-
pub fn contains(&self, value: &T) -> bool {
185+
pub fn contains<Sized? Q>(&self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
181186
self.map.contains_key(value)
182187
}
183188

@@ -291,6 +296,10 @@ impl<T: Ord> BTreeSet<T> {
291296
/// Removes a value from the set. Returns `true` if the value was
292297
/// present in the set.
293298
///
299+
/// The value may be any borrowed form of the set's value type,
300+
/// but the ordering on the borrowed form *must* match the
301+
/// ordering on the value type.
302+
///
294303
/// # Example
295304
///
296305
/// ```
@@ -303,7 +312,7 @@ impl<T: Ord> BTreeSet<T> {
303312
/// assert_eq!(set.remove(&2), false);
304313
/// ```
305314
#[unstable = "matches collection reform specification, waiting for dust to settle"]
306-
pub fn remove(&mut self, value: &T) -> bool {
315+
pub fn remove<Sized? Q>(&mut self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
307316
self.map.remove(value).is_some()
308317
}
309318
}

0 commit comments

Comments
 (0)