Skip to content

Commit

Permalink
Convert PhfSet to new naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Nov 1, 2014
1 parent 7fc934a commit b2416db
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 136 deletions.
131 changes: 2 additions & 129 deletions phf/src/lib.rs
Expand Up @@ -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<T:'static> {
#[doc(hidden)]
pub map: Map<T, ()>
}

impl<T> fmt::Show for PhfSet<T> 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<T> Collection for PhfSet<T> {
#[inline]
fn len(&self) -> uint {
self.map.len()
}
}

impl<T> SetTrait<T> for PhfSet<T> where T: PhfHash+Eq {
#[inline]
fn contains(&self, value: &T) -> bool {
self.map.contains_key(value)
}

#[inline]
fn is_disjoint(&self, other: &PhfSet<T>) -> bool {
!self.iter().any(|value| other.contains(value))
}

#[inline]
fn is_subset(&self, other: &PhfSet<T>) -> bool {
self.iter().all(|value| other.contains(value))
}
}

impl<T> PhfSet<T> 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<T> PhfSet<T> {
/// Like `contains`, but can operate on any type that is equivalent to a
/// value
#[inline]
pub fn contains_equiv<Sized? U>(&self, key: &U) -> bool where U: PhfHash+Equiv<T> {
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<Sized? U>(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv<T> {
self.map.find_key_equiv(key)
}
}

impl<T> PhfSet<T> {
/// 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<uint>) {
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
Expand Down
139 changes: 139 additions & 0 deletions 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<T:'static> {
#[doc(hidden)]
pub map: Map<T, ()>
}

impl<T> fmt::Show for Set<T> 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<T> Collection for Set<T> {
#[inline]
fn len(&self) -> uint {
self.map.len()
}
}

impl<T> SetTrait<T> for Set<T> where T: PhfHash+Eq {
#[inline]
fn contains(&self, value: &T) -> bool {
self.map.contains_key(value)
}

#[inline]
fn is_disjoint(&self, other: &Set<T>) -> bool {
!self.iter().any(|value| other.contains(value))
}

#[inline]
fn is_subset(&self, other: &Set<T>) -> bool {
self.iter().all(|value| other.contains(value))
}
}

impl<T> Set<T> 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<T> Set<T> {
/// Like `contains`, but can operate on any type that is equivalent to a
/// value
#[inline]
pub fn contains_equiv<Sized? U>(&self, key: &U) -> bool where U: PhfHash+Equiv<T> {
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<Sized? U>(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv<T> {
self.map.find_key_equiv(key)
}
}

impl<T> Set<T> {
/// 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<uint>) {
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> {}


12 changes: 6 additions & 6 deletions phf/tests/test.rs
Expand Up @@ -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",
};
Expand All @@ -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",
};
Expand All @@ -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",
};
Expand Down
2 changes: 1 addition & 1 deletion phf_mac/src/util.rs
Expand Up @@ -220,7 +220,7 @@ pub fn create_map(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state: HashSt
pub fn create_set(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state: HashState)
-> Box<MacResult+'static> {
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<Entry>, state: HashState)
Expand Down

0 comments on commit b2416db

Please sign in to comment.