From efaf4db24c92e119e26dc575ffd6bfd3b91fb87d Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Sun, 23 Feb 2014 21:29:35 +1100 Subject: Transition to new `Hash`, removing IterBytes and std::to_bytes. --- src/libcollections/enum_set.rs | 2 +- src/libcollections/hashmap.rs | 25 +++++++++++-------------- src/libcollections/lru_cache.rs | 16 ++++++++-------- 3 files changed, 20 insertions(+), 23 deletions(-) (limited to 'src/libcollections') diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index 94d378b9dc4..0de6eaf53dd 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -15,7 +15,7 @@ use std::num::Bitwise; -#[deriving(Clone, Eq, IterBytes, ToStr, Encodable, Decodable)] +#[deriving(Clone, Eq, Hash, ToStr, Encodable, Decodable)] /// 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 5cdd5bf3812..fe56dbdd2f1 100644 --- a/src/libcollections/hashmap.rs +++ b/src/libcollections/hashmap.rs @@ -54,7 +54,7 @@ use std::cmp::max; use std::fmt; -use std::hash_old::Hash; +use std::hash::{Hash, Hasher, sip}; use std::iter::{FilterMap, Chain, Repeat, Zip}; use std::iter; use std::mem::replace; @@ -79,10 +79,7 @@ struct Bucket { /// hash function for internal state. This means that the order of all hash maps /// is randomized by keying each hash map randomly on creation. /// -/// It is required that the keys implement the `Eq` and `Hash` traits, although -/// this can frequently be achieved by just implementing the `Eq` and -/// `IterBytes` traits as `Hash` is automatically implemented for types that -/// implement `IterBytes`. +/// It is required that the keys implement the `Eq` and `Hash` traits. pub struct HashMap { priv k0: u64, priv k1: u64, @@ -131,14 +128,14 @@ impl HashMap { #[inline] fn bucket_for_key(&self, k: &K) -> SearchResult { - let hash = k.hash_keyed(self.k0, self.k1) as uint; + let hash = sip::hash_with_keys(self.k0, self.k1, k) as uint; self.bucket_for_key_with_hash(hash, k) } #[inline] fn bucket_for_key_equiv>(&self, k: &Q) -> SearchResult { - let hash = k.hash_keyed(self.k0, self.k1) as uint; + let hash = sip::hash_with_keys(self.k0, self.k1, k) as uint; self.bucket_for_key_with_hash_equiv(hash, k) } @@ -339,14 +336,14 @@ impl MutableMap for HashMap { self.expand(); } - let hash = k.hash_keyed(self.k0, self.k1) as uint; + let hash = sip::hash_with_keys(self.k0, self.k1, &k) as uint; self.insert_internal(hash, k, v) } /// Removes a key from the map, returning the value at the key if the key /// was previously in the map. fn pop(&mut self, k: &K) -> Option { - let hash = k.hash_keyed(self.k0, self.k1) as uint; + let hash = sip::hash_with_keys(self.k0, self.k1, k) as uint; self.pop_internal(hash, k) } } @@ -446,7 +443,7 @@ impl HashMap { self.expand(); } - let hash = k.hash_keyed(self.k0, self.k1) as uint; + let hash = sip::hash_with_keys(self.k0, self.k1, &k) as uint; let idx = match self.bucket_for_key_with_hash(hash, &k) { TableFull => fail!("Internal logic error"), FoundEntry(idx) => { found(&k, self.mut_value_for_bucket(idx), a); idx } @@ -925,7 +922,7 @@ pub type SetAlgebraItems<'a, T> = impl< E: Encoder, - K: Encodable + Hash + IterBytes + Eq, + K: Encodable + Hash + Eq, V: Encodable > Encodable for HashMap { fn encode(&self, e: &mut E) { @@ -942,7 +939,7 @@ impl< impl< D: Decoder, - K: Decodable + Hash + IterBytes + Eq, + K: Decodable + Hash + Eq, V: Decodable > Decodable for HashMap { fn decode(d: &mut D) -> HashMap { @@ -960,7 +957,7 @@ impl< impl< S: Encoder, - T: Encodable + Hash + IterBytes + Eq + T: Encodable + Hash + Eq > Encodable for HashSet { fn encode(&self, s: &mut S) { s.emit_seq(self.len(), |s| { @@ -975,7 +972,7 @@ impl< impl< D: Decoder, - T: Decodable + Hash + IterBytes + Eq + T: Decodable + Hash + Eq > Decodable for HashSet { fn decode(d: &mut D) -> HashSet { d.read_seq(|d, len| { diff --git a/src/libcollections/lru_cache.rs b/src/libcollections/lru_cache.rs index d05d2f2d981..ec387df7215 100644 --- a/src/libcollections/lru_cache.rs +++ b/src/libcollections/lru_cache.rs @@ -38,7 +38,7 @@ //! ``` use std::container::Container; -use std::to_bytes::Cb; +use std::hash::{Hash, sip}; use std::ptr; use std::cast; @@ -61,9 +61,9 @@ pub struct LruCache { priv tail: *mut LruEntry, } -impl IterBytes for KeyRef { - fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool { - unsafe{ (*self.k).iter_bytes(lsb0, f) } +impl Hash for KeyRef { + fn hash(&self, s: &mut sip::SipState) { + unsafe {(*self.k).hash(s)} } } @@ -93,7 +93,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 { @@ -217,7 +217,7 @@ impl LruCache { } } -impl ToStr for LruCache { +impl ToStr for LruCache { /// Return a string that lists the key-value pairs from most-recently /// used to least-recently used. #[inline] @@ -250,14 +250,14 @@ impl ToStr 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