From f585e4c88f1cd327e0b409c60deb51cd3f3d6b15 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 1 Nov 2014 15:43:40 -0700 Subject: [PATCH] Update for collections traits removal --- phf/src/lib.rs | 1 - phf/src/map.rs | 35 ++++++++++++++----------- phf/src/ordered_map.rs | 33 ++++++++++++++---------- phf/src/ordered_set.rs | 56 +++++++++++++++++++++++----------------- phf/src/set.rs | 58 +++++++++++++++++++++++++----------------- 5 files changed, 107 insertions(+), 76 deletions(-) diff --git a/phf/src/lib.rs b/phf/src/lib.rs index 9f418651..fc97f637 100644 --- a/phf/src/lib.rs +++ b/phf/src/lib.rs @@ -9,7 +9,6 @@ #[phase(plugin, link)] extern crate core; -extern crate collections; pub use shared::PhfHash; #[doc(inline)] diff --git a/phf/src/map.rs b/phf/src/map.rs index a320a5b5..00447747 100644 --- a/phf/src/map.rs +++ b/phf/src/map.rs @@ -4,7 +4,6 @@ use core::iter; use core::slice; use core::fmt; use shared; -use collections::Map as MapTrait; use shared::PhfHash; /// An immutable map constructed at compile time. @@ -39,18 +38,6 @@ pub struct Map { pub entries: &'static [(K, V)], } -impl Collection for Map { - fn len(&self) -> uint { - self.entries.len() - } -} - -impl<'a, K, V> MapTrait for Map where K: PhfHash+Eq { - fn find(&self, key: &K) -> Option<&V> { - self.get_entry(key, |k| key == k).map(|e| &e.1) - } -} - impl fmt::Show for Map where K: fmt::Show, V: fmt::Show { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { try!(write!(fmt, "{{")); @@ -73,6 +60,16 @@ impl Index for Map where K: PhfHash+Eq { } impl Map where K: PhfHash+Eq { + /// Returns a reference to the value that `key` maps to. + pub fn find(&self, key: &K) -> Option<&V> { + self.get_entry(key, |k| key == k).map(|e| &e.1) + } + + /// Determines if `key` is in the `Map`. + pub fn contains_key(&self, key: &K) -> bool { + self.find(key).is_some() + } + /// Returns a reference to the map's internal static instance of the given /// key. /// @@ -83,6 +80,16 @@ impl Map where K: PhfHash+Eq { } impl Map { + /// Returns true if the `Map` is empty. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Returns the number of entries in the `Map`. + pub fn len(&self) -> uint { + self.entries.len() + } + fn get_entry(&self, key: &T, check: |&K| -> bool) -> Option<&(K, V)> where T: PhfHash { let (g, f1, f2) = key.phf_hash(self.key); let (d1, d2) = self.disps[(g % (self.disps.len() as u32)) as uint]; @@ -105,9 +112,7 @@ impl Map { pub fn find_key_equiv(&self, key: &T) -> Option<&K> where T: PhfHash+Equiv { self.get_entry(key, |k| key.equiv(k)).map(|e| &e.0) } -} -impl Map { /// Returns an iterator over the key/value pairs in the map. /// /// Entries are retuned in an arbitrary but fixed order. diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index e19fd6f8..acfec983 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -5,7 +5,6 @@ use core::slice; use core::iter; use PhfHash; use shared; -use collections::Map as MapTrait; /// An order-preserving immutable map constructed at compile time. /// @@ -59,18 +58,6 @@ impl fmt::Show for OrderedMap where K: fmt::Show, V: fmt::Show { } } -impl Collection for OrderedMap { - fn len(&self) -> uint { - self.entries.len() - } -} - -impl MapTrait for OrderedMap where K: PhfHash+Eq { - fn find(&self, key: &K) -> Option<&V> { - self.find_entry(key, |k| k == key).map(|(_, e)| &e.1) - } -} - impl Index for OrderedMap where K: PhfHash+Eq { fn index(&self, k: &K) -> &V { self.find(k).expect("invalid key") @@ -78,6 +65,16 @@ impl Index for OrderedMap where K: PhfHash+Eq { } impl OrderedMap where K: PhfHash+Eq { + /// Returns a reference to the value that `key` maps to. + pub fn find(&self, key: &K) -> Option<&V> { + self.find_entry(key, |k| key == k).map(|(_, e)| &e.1) + } + + /// Determines if `key` is in the `Map`. + pub fn contains_key(&self, key: &K) -> bool { + self.find(key).is_some() + } + /// Returns a reference to the map's internal static instance of the given /// key. /// @@ -94,6 +91,16 @@ impl OrderedMap where K: PhfHash+Eq { } impl OrderedMap { + /// Returns true if the `Map` is empty. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Returns the number of entries in the `Map`. + pub fn len(&self) -> uint { + self.entries.len() + } + fn find_entry(&self, key: &T, check: |&K| -> bool) -> Option<(uint, &(K, V))> where T: PhfHash { let (g, f1, f2) = key.phf_hash(self.key); diff --git a/phf/src/ordered_set.rs b/phf/src/ordered_set.rs index 6ce43c8d..658d888a 100644 --- a/phf/src/ordered_set.rs +++ b/phf/src/ordered_set.rs @@ -3,8 +3,6 @@ use core::prelude::*; use core::fmt; use ordered_map; use {PhfHash, OrderedMap}; -use collections::Map as MapTrait; -use collections::Set as SetTrait; /// An order-preserving immutable set constructed at compile time. /// @@ -52,48 +50,60 @@ impl fmt::Show for OrderedSet where T: fmt::Show { } } -impl Collection for OrderedSet { +impl OrderedSet { + /// Returns a reference to the set's internal static instance of the given + /// key. + /// + /// This can be useful for interning schemes. #[inline] - fn len(&self) -> uint { - self.map.len() + pub fn find_key(&self, key: &T) -> Option<&T> { + self.map.find_key(key) } -} -impl SetTrait for OrderedSet where T: PhfHash+Eq { + /// Returns the index of the key within the list used to initialize + /// the ordered set. + pub fn find_index(&self, key: &T) -> Option { + self.map.find_index(key) + } + + /// Returns true if `value` is in the `Set`. #[inline] - fn contains(&self, value: &T) -> bool { + pub fn contains(&self, value: &T) -> bool { self.map.contains_key(value) } + /// Returns true if `other` shares no elements with `self`. #[inline] - fn is_disjoint(&self, other: &OrderedSet) -> bool { + pub fn is_disjoint(&self, other: &OrderedSet) -> bool { !self.iter().any(|value| other.contains(value)) } + /// Returns true if `other` contains all values in `self`. #[inline] - fn is_subset(&self, other: &OrderedSet) -> bool { + pub fn is_subset(&self, other: &OrderedSet) -> bool { self.iter().all(|value| other.contains(value)) } + + /// Returns true if `self` contains all values in `other`. + #[inline] + pub fn is_superset(&self, other: &OrderedSet) -> bool { + other.is_subset(self) + } } -impl OrderedSet { - /// Returns a reference to the set's internal static instance of the given - /// key. - /// - /// This can be useful for interning schemes. +impl OrderedSet { + /// Returns the number of elements in the `Set`. #[inline] - pub fn find_key(&self, key: &T) -> Option<&T> { - self.map.find_key(key) + pub fn len(&self) -> uint { + self.map.len() } - /// Returns the index of the key within the list used to initialize - /// the ordered set. - pub fn find_index(&self, key: &T) -> Option { - self.map.find_index(key) + /// Returns true if the `Set` contains no elements. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 } -} -impl OrderedSet { /// Like `contains`, but can operate on any type that is equivalent to a /// value #[inline] diff --git a/phf/src/set.rs b/phf/src/set.rs index 304e3553..9ef8969e 100644 --- a/phf/src/set.rs +++ b/phf/src/set.rs @@ -3,8 +3,6 @@ use core::prelude::*; use Map; use core::fmt; use shared::PhfHash; -use collections::Set as SetTrait; -use collections::Map as MapTrait; use map; /// An immutable set constructed at compile time. @@ -50,42 +48,54 @@ impl fmt::Show for Set where T: fmt::Show { } } -impl Collection for Set { +impl Set where T: PhfHash+Eq { + /// Returns a reference to the set's internal static instance of the given + /// key. + /// + /// This can be useful for interning schemes. #[inline] - fn len(&self) -> uint { - self.map.len() + pub fn find_key(&self, key: &T) -> Option<&T> { + self.map.find_key(key) } -} -impl SetTrait for Set where T: PhfHash+Eq { + /// Returns true if `value` is in the `Set`. #[inline] - fn contains(&self, value: &T) -> bool { + pub fn contains(&self, value: &T) -> bool { self.map.contains_key(value) } + /// Returns true if `other` shares no elements with `self`. #[inline] - fn is_disjoint(&self, other: &Set) -> bool { + pub fn is_disjoint(&self, other: &Set) -> bool { !self.iter().any(|value| other.contains(value)) } + /// Returns true if `other` contains all values in `self`. #[inline] - fn is_subset(&self, other: &Set) -> bool { + pub fn is_subset(&self, other: &Set) -> bool { self.iter().all(|value| other.contains(value)) } -} -impl Set where T: PhfHash+Eq { - /// Returns a reference to the set's internal static instance of the given - /// key. - /// - /// This can be useful for interning schemes. + /// Returns true if `self` contains all values in `other`. #[inline] - pub fn find_key(&self, key: &T) -> Option<&T> { - self.map.find_key(key) + pub fn is_superset(&self, other: &Set) -> bool { + other.is_subset(self) } } impl Set { + /// Returns the number of elements in the `Set`. + #[inline] + pub fn len(&self) -> uint { + self.map.len() + } + + /// Returns true if the `Set` contains no elements. + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Like `contains`, but can operate on any type that is equivalent to a /// value #[inline] @@ -106,17 +116,17 @@ impl Set { /// /// Values are returned in an arbitrary but fixed order. #[inline] - pub fn iter<'a>(&'a self) -> Entries<'a, T> { - Entries { iter: self.map.keys() } + pub fn iter<'a>(&'a self) -> Items<'a, T> { + Items { iter: self.map.keys() } } } /// An iterator over the values in a `Set`. -pub struct Entries<'a, T:'static> { +pub struct Items<'a, T:'static> { iter: map::Keys<'a, T, ()>, } -impl<'a, T> Iterator<&'a T> for Entries<'a, T> { +impl<'a, T> Iterator<&'a T> for Items<'a, T> { fn next(&mut self) -> Option<&'a T> { self.iter.next() } @@ -126,12 +136,12 @@ impl<'a, T> Iterator<&'a T> for Entries<'a, T> { } } -impl<'a, T> DoubleEndedIterator<&'a T> for Entries<'a, T> { +impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() } } -impl<'a, T> ExactSize<&'a T> for Entries<'a, T> {} +impl<'a, T> ExactSize<&'a T> for Items<'a, T> {}