diff options
| author | Dylan Ede <dylanede@googlemail.com> | 2015-01-06 16:36:30 +0000 |
|---|---|---|
| committer | Alex Crichton <alex@alexcrichton.com> | 2015-01-06 11:59:26 -0800 |
| commit | 25eada15740fbe12ee2cae7fc6fe8e2c228b699d (patch) | |
| tree | bc24dedaf97ab376c4ac4f73b4c40d068c5ef84b /src/libstd/collections | |
| parent | 6539cb417f4a7c2d9d1afce44c196578d2b67f38 (diff) | |
| download | rust-25eada15740fbe12ee2cae7fc6fe8e2c228b699d.tar.gz rust-25eada15740fbe12ee2cae7fc6fe8e2c228b699d.zip | |
[breaking change] Revert Entry behaviour to take keys by value.
Diffstat (limited to 'src/libstd/collections')
| -rw-r--r-- | src/libstd/collections/hash/map.rs | 45 | ||||
| -rw-r--r-- | src/libstd/collections/mod.rs | 4 |
2 files changed, 22 insertions, 27 deletions
diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index a3fc38c34e8..ae53bc61599 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -14,7 +14,7 @@ use self::Entry::*; use self::SearchResult::*; use self::VacantEntryState::*; -use borrow::{BorrowFrom, ToOwned}; +use borrow::BorrowFrom; use clone::Clone; use cmp::{max, Eq, PartialEq}; use default::Default; @@ -922,14 +922,12 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> { #[stable] /// Gets the given key's corresponding entry in the map for in-place manipulation. - /// Regardless of whether or not `to_owned()` has been called, the key must hash the same way. - pub fn entry<'a, Q: ?Sized>(&'a mut self, key: &'a Q) -> Entry<'a, Q, K, V> - where Q: Eq + Hash<S> + ToOwned<K> + pub fn entry<'a>(&'a mut self, key: K) -> Entry<'a, K, V> { // Gotta resize now. self.reserve(1); - let hash = self.make_hash(key); + let hash = self.make_hash(&key); search_entry_hashed(&mut self.table, hash, key) } @@ -1142,9 +1140,8 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> { } } -fn search_entry_hashed<'a, K, V, Q: ?Sized>(table: &'a mut RawTable<K,V>, hash: SafeHash, k: &'a Q) - -> Entry<'a, Q, K, V> - where Q: Eq + ToOwned<K> +fn search_entry_hashed<'a, K: Eq, V>(table: &'a mut RawTable<K,V>, hash: SafeHash, k: K) + -> Entry<'a, K, V> { // Worst case, we'll find one empty bucket among `size + 1` buckets. let size = table.size(); @@ -1167,7 +1164,7 @@ fn search_entry_hashed<'a, K, V, Q: ?Sized>(table: &'a mut RawTable<K,V>, hash: // hash matches? if bucket.hash() == hash { // key matches? - if *k == *BorrowFrom::borrow_from(bucket.read().0) { + if k == *bucket.read().0 { return Occupied(OccupiedEntry{ elem: bucket, }); @@ -1331,19 +1328,19 @@ pub struct OccupiedEntry<'a, K: 'a, V: 'a> { #[stable] /// A view into a single empty location in a HashMap -pub struct VacantEntry<'a, Q: ?Sized + 'a, K: 'a, V: 'a> { +pub struct VacantEntry<'a, K: 'a, V: 'a> { hash: SafeHash, - key: &'a Q, + key: K, elem: VacantEntryState<K, V, &'a mut RawTable<K, V>>, } #[stable] /// A view into a single location in a map, which may be vacant or occupied -pub enum Entry<'a, Q: ?Sized + 'a, K: 'a, V: 'a> { +pub enum Entry<'a, K: 'a, V: 'a> { /// An occupied Entry Occupied(OccupiedEntry<'a, K, V>), /// A vacant Entry - Vacant(VacantEntry<'a, Q, K, V>), + Vacant(VacantEntry<'a, K, V>), } /// Possible states of a VacantEntry @@ -1409,10 +1406,10 @@ impl<'a, K: 'a, V: 'a> Iterator for Drain<'a, K, V> { } } -impl<'a, Q: ?Sized, K, V> Entry<'a, Q, K, V> { +impl<'a, K, V> Entry<'a, K, V> { #[unstable = "matches collection reform v2 specification, waiting for dust to settle"] /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant - pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, Q, K, V>> { + pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> { match self { Occupied(entry) => Ok(entry.into_mut()), Vacant(entry) => Err(entry), @@ -1455,17 +1452,17 @@ impl<'a, K, V> OccupiedEntry<'a, K, V> { } } -impl<'a, Q: ?Sized + 'a + ToOwned<K>, K: 'a, V: 'a> VacantEntry<'a, Q, K, V> { +impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> { #[stable] /// Sets the value of the entry with the VacantEntry's key, /// and returns a mutable reference to it pub fn insert(self, value: V) -> &'a mut V { match self.elem { NeqElem(bucket, ib) => { - robin_hood(bucket, ib, self.hash, self.key.to_owned(), value) + robin_hood(bucket, ib, self.hash, self.key, value) } NoElem(bucket) => { - bucket.put(self.hash, self.key.to_owned(), value).into_mut_refs().1 + bucket.put(self.hash, self.key, value).into_mut_refs().1 } } } @@ -1497,8 +1494,6 @@ mod test_map { use super::HashMap; use super::Entry::{Occupied, Vacant}; use iter::{range_inclusive, range_step_inclusive, repeat}; - use borrow::ToOwned; - use hash; use cell::RefCell; use rand::{weak_rng, Rng}; @@ -2092,7 +2087,7 @@ mod test_map { let mut map: HashMap<int, int> = xs.iter().map(|&x| x).collect(); // Existing key (insert) - match map.entry(&1) { + match map.entry(1) { Vacant(_) => unreachable!(), Occupied(mut view) => { assert_eq!(view.get(), &10); @@ -2104,7 +2099,7 @@ mod test_map { // Existing key (update) - match map.entry(&2) { + match map.entry(2) { Vacant(_) => unreachable!(), Occupied(mut view) => { let v = view.get_mut(); @@ -2116,7 +2111,7 @@ mod test_map { assert_eq!(map.len(), 6); // Existing key (take) - match map.entry(&3) { + match map.entry(3) { Vacant(_) => unreachable!(), Occupied(view) => { assert_eq!(view.remove(), 30); @@ -2127,7 +2122,7 @@ mod test_map { // Inexistent key (insert) - match map.entry(&10) { + match map.entry(10) { Occupied(_) => unreachable!(), Vacant(view) => { assert_eq!(*view.insert(1000), 1000); @@ -2158,7 +2153,7 @@ mod test_map { for i in range(0u, 1000) { let x = rng.gen_range(-10, 10); - match m.entry(&x) { + match m.entry(x) { Vacant(_) => {}, Occupied(e) => { println!("{}: remove {}", i, x); diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index ef9d28bbbb2..9b2a4926bcb 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -255,7 +255,7 @@ //! let message = "she sells sea shells by the sea shore"; //! //! for c in message.chars() { -//! match count.entry(&c) { +//! match count.entry(c) { //! Vacant(entry) => { entry.insert(1u); }, //! Occupied(mut entry) => *entry.get_mut() += 1, //! } @@ -290,7 +290,7 @@ //! for id in orders.into_iter() { //! // If this is the first time we've seen this customer, initialize them //! // with no blood alcohol. Otherwise, just retrieve them. -//! let person = match blood_alcohol.entry(&id) { +//! let person = match blood_alcohol.entry(id) { //! Vacant(entry) => entry.insert(Person{id: id, blood_alcohol: 0.0}), //! Occupied(entry) => entry.into_mut(), //! }; |
