From 3829ac2a52f12b08501cb25d82de32f39fbe801e Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 22 Mar 2014 16:30:45 -0400 Subject: use TotalEq for HashMap Closes #5283 --- src/libcollections/enum_set.rs | 2 +- src/libcollections/hashmap.rs | 57 +++++++++++++++++++---------------------- src/libcollections/lru_cache.rs | 10 +++++--- 3 files changed, 34 insertions(+), 35 deletions(-) (limited to 'src/libcollections') diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index 7fda99d8d2c..07f3181d218 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -15,7 +15,7 @@ use std::num::Bitwise; -#[deriving(Clone, Eq, Hash, Show)] +#[deriving(Clone, Eq, TotalEq, Hash, Show)] /// A specialized Set implementation to use enum types. pub struct EnumSet { // We must maintain the invariant that no bits are set diff --git a/src/libcollections/hashmap.rs b/src/libcollections/hashmap.rs index a1f815df72e..5ec5db45f27 100644 --- a/src/libcollections/hashmap.rs +++ b/src/libcollections/hashmap.rs @@ -12,7 +12,7 @@ use std::container::{Container, Mutable, Map, MutableMap, Set, MutableSet}; use std::clone::Clone; -use std::cmp::{Eq, Equiv, max}; +use std::cmp::{Eq, TotalEq, Equiv, max}; use std::default::Default; use std::fmt; use std::fmt::Show; @@ -140,6 +140,7 @@ mod table { } /// A hash that is not zero, since we use that to represent empty buckets. + #[deriving(Eq)] pub struct SafeHash { priv hash: u64, } @@ -149,10 +150,6 @@ mod table { pub fn inspect(&self) -> u64 { self.hash } } - impl Eq for SafeHash { - fn eq(&self, other: &SafeHash) -> bool { self.hash == other.hash } - } - /// We need to remove hashes of 0. That's reserved for empty buckets. /// This function wraps up `hash_keyed` to be the only way outside this /// module to generate a SafeHash. @@ -698,7 +695,7 @@ fn grow_at(capacity: uint, load_factor: Fraction) -> uint { fraction_mul(capacity, load_factor) } -impl, V, S, H: Hasher> HashMap { +impl, V, S, H: Hasher> HashMap { /// Get the number of elements which will force the capacity to shrink. /// When size == self.shrink_at(), we halve the capacity. fn shrink_at(&self) -> uint { @@ -799,12 +796,12 @@ impl, V, S, H: Hasher> HashMap { } } -impl, V, S, H: Hasher> Container for HashMap { +impl, V, S, H: Hasher> Container for HashMap { /// Return the number of elements in the map fn len(&self) -> uint { self.table.size() } } -impl, V, S, H: Hasher> Mutable for HashMap { +impl, V, S, H: Hasher> Mutable for HashMap { /// Clear the map, removing all key-value pairs. fn clear(&mut self) { self.minimum_capacity = self.table.size(); @@ -819,7 +816,7 @@ impl, V, S, H: Hasher> Mutable for HashMap { } -impl, V, S, H: Hasher> Map for HashMap { +impl, V, S, H: Hasher> Map for HashMap { fn find<'a>(&'a self, k: &K) -> Option<&'a V> { self.search(k).map(|idx| { let (_, v) = self.table.read(&idx); @@ -832,7 +829,7 @@ impl, V, S, H: Hasher> Map for HashMap { } } -impl, V, S, H: Hasher> MutableMap for HashMap { +impl, V, S, H: Hasher> MutableMap for HashMap { fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> { match self.search(k) { None => None, @@ -969,7 +966,7 @@ impl, V, S, H: Hasher> MutableMap for HashMap } } -impl HashMap { +impl HashMap { /// Create an empty HashMap. pub fn new() -> HashMap { HashMap::with_capacity(INITIAL_CAPACITY) @@ -984,7 +981,7 @@ impl HashMap { } } -impl, V, S, H: Hasher> HashMap { +impl, V, S, H: Hasher> HashMap { pub fn with_hasher(hasher: H) -> HashMap { HashMap::with_capacity_and_hasher(INITIAL_CAPACITY, hasher) } @@ -1296,7 +1293,7 @@ impl, V, S, H: Hasher> HashMap { } } -impl, V: Clone, S, H: Hasher> HashMap { +impl, V: Clone, S, H: Hasher> HashMap { /// Like `find`, but returns a copy of the value. pub fn find_copy(&self, k: &K) -> Option { self.find(k).map(|v| (*v).clone()) @@ -1308,7 +1305,7 @@ impl, V: Clone, S, H: Hasher> HashMap { } } -impl, V: Eq, S, H: Hasher> Eq for HashMap { +impl, V: Eq, S, H: Hasher> Eq for HashMap { fn eq(&self, other: &HashMap) -> bool { if self.len() != other.len() { return false; } @@ -1321,7 +1318,7 @@ impl, V: Eq, S, H: Hasher> Eq for HashMap { } } -impl + Show, V: Show, S, H: Hasher> Show for HashMap { +impl + Show, V: Show, S, H: Hasher> Show for HashMap { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(write!(f.buf, r"\{")); @@ -1334,7 +1331,7 @@ impl + Show, V: Show, S, H: Hasher> Show for HashMap } } -impl, V, S, H: Hasher + Default> Default for HashMap { +impl, V, S, H: Hasher + Default> Default for HashMap { fn default() -> HashMap { HashMap::with_capacity_and_hasher(INITIAL_CAPACITY, Default::default()) } @@ -1358,7 +1355,7 @@ pub type Keys<'a, K, V> = pub type Values<'a, K, V> = iter::Map<'static, (&'a K, &'a V), &'a V, Entries<'a, K, V>>; -impl, V, S, H: Hasher + Default> FromIterator<(K, V)> for HashMap { +impl, V, S, H: Hasher + Default> FromIterator<(K, V)> for HashMap { fn from_iterator>(iter: &mut T) -> HashMap { let (lower, _) = iter.size_hint(); let mut map = HashMap::with_capacity_and_hasher(lower, Default::default()); @@ -1367,7 +1364,7 @@ impl, V, S, H: Hasher + Default> FromIterator<(K, V)> for Has } } -impl, V, S, H: Hasher + Default> Extendable<(K, V)> for HashMap { +impl, V, S, H: Hasher + Default> Extendable<(K, V)> for HashMap { fn extend>(&mut self, iter: &mut T) { for (k, v) in *iter { self.insert(k, v); @@ -1391,7 +1388,7 @@ pub struct HashSet { priv map: HashMap } -impl, S, H: Hasher> Eq for HashSet { +impl, S, H: Hasher> Eq for HashSet { // FIXME #11998: Since the value is a (), and `find` returns a Some(&()), // we trigger #11998 when matching on it. I've fallen back to manual // iteration until this is fixed. @@ -1402,17 +1399,17 @@ impl, S, H: Hasher> Eq for HashSet { } } -impl, S, H: Hasher> Container for HashSet { +impl, S, H: Hasher> Container for HashSet { /// Return the number of elements in the set fn len(&self) -> uint { self.map.len() } } -impl, S, H: Hasher> Mutable for HashSet { +impl, S, H: Hasher> Mutable for HashSet { /// Clear the set, removing all values. fn clear(&mut self) { self.map.clear() } } -impl, S, H: Hasher> Set for HashSet { +impl, S, H: Hasher> Set for HashSet { /// Return true if the set contains a value fn contains(&self, value: &T) -> bool { self.map.search(value).is_some() } @@ -1433,7 +1430,7 @@ impl, S, H: Hasher> Set for HashSet { } } -impl, S, H: Hasher> MutableSet for HashSet { +impl, S, H: Hasher> MutableSet for HashSet { /// Add a value to the set. Return true if the value was not already /// present in the set. fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) } @@ -1443,7 +1440,7 @@ impl, S, H: Hasher> MutableSet for HashSet { fn remove(&mut self, value: &T) -> bool { self.map.remove(value) } } -impl HashSet { +impl HashSet { /// Create an empty HashSet pub fn new() -> HashSet { HashSet::with_capacity(INITIAL_CAPACITY) @@ -1456,7 +1453,7 @@ impl HashSet { } } -impl, S, H: Hasher> HashSet { +impl, S, H: Hasher> HashSet { pub fn with_hasher(hasher: H) -> HashSet { HashSet::with_capacity_and_hasher(INITIAL_CAPACITY, hasher) } @@ -1529,7 +1526,7 @@ impl, S, H: Hasher> HashSet { } -impl + fmt::Show, S, H: Hasher> fmt::Show for HashSet { +impl + fmt::Show, S, H: Hasher> fmt::Show for HashSet { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { try!(write!(f.buf, r"\{")); @@ -1542,7 +1539,7 @@ impl + fmt::Show, S, H: Hasher> fmt::Show for HashSet { } } -impl, S, H: Hasher + Default> FromIterator for HashSet { +impl, S, H: Hasher + Default> FromIterator for HashSet { fn from_iterator>(iter: &mut I) -> HashSet { let (lower, _) = iter.size_hint(); let mut set = HashSet::with_capacity_and_hasher(lower, Default::default()); @@ -1551,7 +1548,7 @@ impl, S, H: Hasher + Default> FromIterator for HashSet, S, H: Hasher + Default> Extendable for HashSet { +impl, S, H: Hasher + Default> Extendable for HashSet { fn extend>(&mut self, iter: &mut I) { for k in *iter { self.insert(k); @@ -1559,7 +1556,7 @@ impl, S, H: Hasher + Default> Extendable for HashSet } } -impl Default for HashSet { +impl Default for HashSet { fn default() -> HashSet { HashSet::new() } } @@ -1601,7 +1598,7 @@ mod test_map { local_data_key!(drop_vector: vec::Vec) - #[deriving(Hash, Eq)] + #[deriving(Hash, Eq, TotalEq)] struct Dropable { k: int } diff --git a/src/libcollections/lru_cache.rs b/src/libcollections/lru_cache.rs index 28ea36fa231..e328b41cc0f 100644 --- a/src/libcollections/lru_cache.rs +++ b/src/libcollections/lru_cache.rs @@ -74,6 +74,8 @@ impl Eq for KeyRef { } } +impl TotalEq for KeyRef {} + impl LruEntry { fn new() -> LruEntry { LruEntry { @@ -94,7 +96,7 @@ impl LruEntry { } } -impl LruCache { +impl LruCache { /// Create an LRU Cache that holds at most `capacity` items. pub fn new(capacity: uint) -> LruCache { let cache = LruCache { @@ -218,7 +220,7 @@ impl LruCache { } } -impl fmt::Show for LruCache { +impl fmt::Show for LruCache { /// Return a string that lists the key-value pairs from most-recently /// used to least-recently used. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -247,14 +249,14 @@ impl fmt::Show for LruCache { } } -impl Container for LruCache { +impl Container for LruCache { /// Return the number of key-value pairs in the cache. fn len(&self) -> uint { self.map.len() } } -impl Mutable for LruCache { +impl Mutable for LruCache { /// Clear the cache of all key-value pairs. fn clear(&mut self) { self.map.clear(); -- cgit 1.4.1-3-g733a5