diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-23 21:29:35 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2014-02-24 07:44:10 +1100 |
| commit | efaf4db24c92e119e26dc575ffd6bfd3b91fb87d (patch) | |
| tree | e735230061b0c480550fdaad749a998bc48df78d /src/libcollections | |
| parent | 5444da54fd32b705eec28112e309f63b704e3f8c (diff) | |
| download | rust-efaf4db24c92e119e26dc575ffd6bfd3b91fb87d.tar.gz rust-efaf4db24c92e119e26dc575ffd6bfd3b91fb87d.zip | |
Transition to new `Hash`, removing IterBytes and std::to_bytes.
Diffstat (limited to 'src/libcollections')
| -rw-r--r-- | src/libcollections/enum_set.rs | 2 | ||||
| -rw-r--r-- | src/libcollections/hashmap.rs | 25 | ||||
| -rw-r--r-- | src/libcollections/lru_cache.rs | 16 |
3 files changed, 20 insertions, 23 deletions
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<E> { // 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<K,V> { /// 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<K,V> { priv k0: u64, priv k1: u64, @@ -131,14 +128,14 @@ impl<K:Hash + Eq,V> HashMap<K, V> { #[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<Q:Hash + Equiv<K>>(&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<K:Hash + Eq,V> MutableMap<K, V> for HashMap<K, V> { 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<V> { - 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<K: Hash + Eq, V> HashMap<K, V> { 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<E> + Hash + IterBytes + Eq, + K: Encodable<E> + Hash + Eq, V: Encodable<E> > Encodable<E> for HashMap<K, V> { fn encode(&self, e: &mut E) { @@ -942,7 +939,7 @@ impl< impl< D: Decoder, - K: Decodable<D> + Hash + IterBytes + Eq, + K: Decodable<D> + Hash + Eq, V: Decodable<D> > Decodable<D> for HashMap<K, V> { fn decode(d: &mut D) -> HashMap<K, V> { @@ -960,7 +957,7 @@ impl< impl< S: Encoder, - T: Encodable<S> + Hash + IterBytes + Eq + T: Encodable<S> + Hash + Eq > Encodable<S> for HashSet<T> { fn encode(&self, s: &mut S) { s.emit_seq(self.len(), |s| { @@ -975,7 +972,7 @@ impl< impl< D: Decoder, - T: Decodable<D> + Hash + IterBytes + Eq + T: Decodable<D> + Hash + Eq > Decodable<D> for HashSet<T> { fn decode(d: &mut D) -> HashSet<T> { 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<K, V> { priv tail: *mut LruEntry<K, V>, } -impl<K: IterBytes> IterBytes for KeyRef<K> { - fn iter_bytes(&self, lsb0: bool, f: Cb) -> bool { - unsafe{ (*self.k).iter_bytes(lsb0, f) } +impl<K: Hash> Hash for KeyRef<K> { + fn hash(&self, s: &mut sip::SipState) { + unsafe {(*self.k).hash(s)} } } @@ -93,7 +93,7 @@ impl<K, V> LruEntry<K, V> { } } -impl<K: IterBytes + Eq, V> LruCache<K, V> { +impl<K: Hash + Eq, V> LruCache<K, V> { /// Create an LRU Cache that holds at most `capacity` items. pub fn new(capacity: uint) -> LruCache<K, V> { let cache = LruCache { @@ -217,7 +217,7 @@ impl<K: IterBytes + Eq, V> LruCache<K, V> { } } -impl<A: ToStr + IterBytes + Eq, B: ToStr> ToStr for LruCache<A, B> { +impl<A: ToStr + Hash + Eq, B: ToStr> ToStr for LruCache<A, B> { /// Return a string that lists the key-value pairs from most-recently /// used to least-recently used. #[inline] @@ -250,14 +250,14 @@ impl<A: ToStr + IterBytes + Eq, B: ToStr> ToStr for LruCache<A, B> { } } -impl<K: IterBytes + Eq, V> Container for LruCache<K, V> { +impl<K: Hash + Eq, V> Container for LruCache<K, V> { /// Return the number of key-value pairs in the cache. fn len(&self) -> uint { self.map.len() } } -impl<K: IterBytes + Eq, V> Mutable for LruCache<K, V> { +impl<K: Hash + Eq, V> Mutable for LruCache<K, V> { /// Clear the cache of all key-value pairs. fn clear(&mut self) { self.map.clear(); |
