From b2416db396bc0e35fd64fd23c367f26b5fe78f5a Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 1 Nov 2014 14:10:10 -0700 Subject: [PATCH] Convert PhfSet to new naming conventions --- phf/src/lib.rs | 131 +---------------------------------------- phf/src/set.rs | 139 ++++++++++++++++++++++++++++++++++++++++++++ phf/tests/test.rs | 12 ++-- phf_mac/src/util.rs | 2 +- 4 files changed, 148 insertions(+), 136 deletions(-) create mode 100644 phf/src/set.rs diff --git a/phf/src/lib.rs b/phf/src/lib.rs index 4607c7ec..23026880 100644 --- a/phf/src/lib.rs +++ b/phf/src/lib.rs @@ -20,144 +20,17 @@ use collections::Set as SetTrait; pub use shared::PhfHash; pub use map::Map; +pub use set::Set; #[path="../../shared/mod.rs"] mod shared; pub mod map; +pub mod set; mod std { pub use core::fmt; } -/// An immutable set constructed at compile time. -/// -/// `PhfSet`s may be created with the `phf_set` macro: -/// -/// ```rust -/// # #![feature(phase)] -/// extern crate phf; -/// #[phase(plugin)] -/// extern crate phf_mac; -/// -/// use phf::PhfSet; -/// -/// static MY_SET: PhfSet<&'static str> = phf_set! { -/// "hello", -/// "world", -/// }; -/// -/// # fn main() {} -/// ``` -/// -/// # Note -/// -/// The fields of this struct are public so that they may be initialized by the -/// `phf_set` macro. They are subject to change at any time and should never be -/// accessed directly. -pub struct PhfSet { - #[doc(hidden)] - pub map: Map -} - -impl fmt::Show for PhfSet where T: fmt::Show { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - try!(write!(fmt, "{{")); - let mut first = true; - for entry in self.iter() { - if !first { - try!(write!(fmt, ", ")); - } - try!(write!(fmt, "{}", entry)); - first = false; - } - write!(fmt, "}}") - } -} - -impl Collection for PhfSet { - #[inline] - fn len(&self) -> uint { - self.map.len() - } -} - -impl SetTrait for PhfSet where T: PhfHash+Eq { - #[inline] - fn contains(&self, value: &T) -> bool { - self.map.contains_key(value) - } - - #[inline] - fn is_disjoint(&self, other: &PhfSet) -> bool { - !self.iter().any(|value| other.contains(value)) - } - - #[inline] - fn is_subset(&self, other: &PhfSet) -> bool { - self.iter().all(|value| other.contains(value)) - } -} - -impl PhfSet 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] - pub fn find_key(&self, key: &T) -> Option<&T> { - self.map.find_key(key) - } -} - -impl PhfSet { - /// Like `contains`, but can operate on any type that is equivalent to a - /// value - #[inline] - pub fn contains_equiv(&self, key: &U) -> bool where U: PhfHash+Equiv { - self.map.find_equiv(key).is_some() - } - - /// Like `find_key`, but can operate on any type that is equivalent to a - /// value - #[inline] - pub fn find_key_equiv(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv { - self.map.find_key_equiv(key) - } -} - -impl PhfSet { - /// Returns an iterator over the values in the set. - /// - /// Values are returned in an arbitrary but fixed order. - #[inline] - pub fn iter<'a>(&'a self) -> PhfSetValues<'a, T> { - PhfSetValues { iter: self.map.keys() } - } -} - -/// An iterator over the values in a `PhfSet`. -pub struct PhfSetValues<'a, T:'static> { - iter: map::Keys<'a, T, ()>, -} - -impl<'a, T> Iterator<&'a T> for PhfSetValues<'a, T> { - fn next(&mut self) -> Option<&'a T> { - self.iter.next() - } - - fn size_hint(&self) -> (uint, Option) { - self.iter.size_hint() - } -} - -impl<'a, T> DoubleEndedIterator<&'a T> for PhfSetValues<'a, T> { - fn next_back(&mut self) -> Option<&'a T> { - self.iter.next_back() - } -} - -impl<'a, T> ExactSize<&'a T> for PhfSetValues<'a, T> {} - /// An order-preserving immutable map constructed at compile time. /// /// Unlike a `PhfMap`, iteration order is guaranteed to match the definition diff --git a/phf/src/set.rs b/phf/src/set.rs new file mode 100644 index 00000000..8bfc5805 --- /dev/null +++ b/phf/src/set.rs @@ -0,0 +1,139 @@ +//! An immutable set constructed at compile time. +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. +/// +/// `Set`s may be created with the `phf_set` macro: +/// +/// ```rust +/// # #![feature(phase)] +/// extern crate phf; +/// #[phase(plugin)] +/// extern crate phf_mac; +/// +/// use phf::Set; +/// +/// static MY_SET: Set<&'static str> = phf_set! { +/// "hello", +/// "world", +/// }; +/// +/// # fn main() {} +/// ``` +/// +/// # Note +/// +/// The fields of this struct are public so that they may be initialized by the +/// `phf_set` macro. They are subject to change at any time and should never be +/// accessed directly. +pub struct Set { + #[doc(hidden)] + pub map: Map +} + +impl fmt::Show for Set where T: fmt::Show { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + try!(write!(fmt, "{{")); + let mut first = true; + for entry in self.iter() { + if !first { + try!(write!(fmt, ", ")); + } + try!(write!(fmt, "{}", entry)); + first = false; + } + write!(fmt, "}}") + } +} + +impl Collection for Set { + #[inline] + fn len(&self) -> uint { + self.map.len() + } +} + +impl SetTrait for Set where T: PhfHash+Eq { + #[inline] + fn contains(&self, value: &T) -> bool { + self.map.contains_key(value) + } + + #[inline] + fn is_disjoint(&self, other: &Set) -> bool { + !self.iter().any(|value| other.contains(value)) + } + + #[inline] + 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. + #[inline] + pub fn find_key(&self, key: &T) -> Option<&T> { + self.map.find_key(key) + } +} + +impl Set { + /// Like `contains`, but can operate on any type that is equivalent to a + /// value + #[inline] + pub fn contains_equiv(&self, key: &U) -> bool where U: PhfHash+Equiv { + self.map.find_equiv(key).is_some() + } + + /// Like `find_key`, but can operate on any type that is equivalent to a + /// value + #[inline] + pub fn find_key_equiv(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv { + self.map.find_key_equiv(key) + } +} + +impl Set { + /// Returns an iterator over the values in the 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() } + } +} + +/// An iterator over the values in a `Set`. +pub struct Entries<'a, T:'static> { + iter: map::Keys<'a, T, ()>, +} + +impl<'a, T> Iterator<&'a T> for Entries<'a, T> { + fn next(&mut self) -> Option<&'a T> { + self.iter.next() + } + + fn size_hint(&self) -> (uint, Option) { + self.iter.size_hint() + } +} + +impl<'a, T> DoubleEndedIterator<&'a T> for Entries<'a, T> { + fn next_back(&mut self) -> Option<&'a T> { + self.iter.next_back() + } +} + +impl<'a, T> ExactSize<&'a T> for Entries<'a, T> {} + + diff --git a/phf/tests/test.rs b/phf/tests/test.rs index 924bec88..85ae2ede 100644 --- a/phf/tests/test.rs +++ b/phf/tests/test.rs @@ -206,21 +206,21 @@ mod map { mod set { use std::collections::HashSet; - use phf::PhfSet; + use phf; #[allow(dead_code)] - static TRAILING_COMMA: PhfSet<&'static str> = phf_set! { + static TRAILING_COMMA: phf::Set<&'static str> = phf_set! { "foo", }; #[allow(dead_code)] - static NO_TRAILING_COMMA: PhfSet<&'static str> = phf_set! { + static NO_TRAILING_COMMA: phf::Set<&'static str> = phf_set! { "foo" }; #[test] fn test_two() { - static SET: PhfSet<&'static str> = phf_set! { + static SET: phf::Set<&'static str> = phf_set! { "hello", "world", }; @@ -232,7 +232,7 @@ mod set { #[test] fn test_iter() { - static SET: PhfSet<&'static str> = phf_set! { + static SET: phf::Set<&'static str> = phf_set! { "hello", "world", }; @@ -244,7 +244,7 @@ mod set { #[test] fn test_non_static_str_contains() { - static SET: PhfSet<&'static str> = phf_set! { + static SET: phf::Set<&'static str> = phf_set! { "hello", "world", }; diff --git a/phf_mac/src/util.rs b/phf_mac/src/util.rs index c7b053f0..945159aa 100644 --- a/phf_mac/src/util.rs +++ b/phf_mac/src/util.rs @@ -220,7 +220,7 @@ pub fn create_map(cx: &mut ExtCtxt, sp: Span, entries: Vec, state: HashSt pub fn create_set(cx: &mut ExtCtxt, sp: Span, entries: Vec, state: HashState) -> Box { let map = create_map(cx, sp, entries, state).make_expr().unwrap(); - MacExpr::new(quote_expr!(cx, ::phf::PhfSet { map: $map })) + MacExpr::new(quote_expr!(cx, ::phf::Set { map: $map })) } pub fn create_ordered_map(cx: &mut ExtCtxt, sp: Span, entries: Vec, state: HashState)