Skip to content

Commit

Permalink
Update for collections traits removal
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Nov 1, 2014
1 parent b697d13 commit f585e4c
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 76 deletions.
1 change: 0 additions & 1 deletion phf/src/lib.rs
Expand Up @@ -9,7 +9,6 @@

#[phase(plugin, link)]
extern crate core;
extern crate collections;

pub use shared::PhfHash;
#[doc(inline)]
Expand Down
35 changes: 20 additions & 15 deletions phf/src/map.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -39,18 +38,6 @@ pub struct Map<K:'static, V:'static> {
pub entries: &'static [(K, V)],
}

impl<K, V> Collection for Map<K, V> {
fn len(&self) -> uint {
self.entries.len()
}
}

impl<'a, K, V> MapTrait<K, V> for Map<K, V> where K: PhfHash+Eq {
fn find(&self, key: &K) -> Option<&V> {
self.get_entry(key, |k| key == k).map(|e| &e.1)
}
}

impl<K, V> fmt::Show for Map<K, V> where K: fmt::Show, V: fmt::Show {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
Expand All @@ -73,6 +60,16 @@ impl<K, V> Index<K, V> for Map<K, V> where K: PhfHash+Eq {
}

impl<K, V> Map<K, V> 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.
///
Expand All @@ -83,6 +80,16 @@ impl<K, V> Map<K, V> where K: PhfHash+Eq {
}

impl<K, V> Map<K, V> {
/// 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<Sized? T>(&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];
Expand All @@ -105,9 +112,7 @@ impl<K, V> Map<K, V> {
pub fn find_key_equiv<Sized? T>(&self, key: &T) -> Option<&K> where T: PhfHash+Equiv<K> {
self.get_entry(key, |k| key.equiv(k)).map(|e| &e.0)
}
}

impl<K, V> Map<K, V> {
/// Returns an iterator over the key/value pairs in the map.
///
/// Entries are retuned in an arbitrary but fixed order.
Expand Down
33 changes: 20 additions & 13 deletions phf/src/ordered_map.rs
Expand Up @@ -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.
///
Expand Down Expand Up @@ -59,25 +58,23 @@ impl<K, V> fmt::Show for OrderedMap<K, V> where K: fmt::Show, V: fmt::Show {
}
}

impl<K, V> Collection for OrderedMap<K, V> {
fn len(&self) -> uint {
self.entries.len()
}
}

impl<K, V> MapTrait<K, V> for OrderedMap<K, V> where K: PhfHash+Eq {
fn find(&self, key: &K) -> Option<&V> {
self.find_entry(key, |k| k == key).map(|(_, e)| &e.1)
}
}

impl<K, V> Index<K, V> for OrderedMap<K, V> where K: PhfHash+Eq {
fn index(&self, k: &K) -> &V {
self.find(k).expect("invalid key")
}
}

impl<K, V> OrderedMap<K, V> 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.
///
Expand All @@ -94,6 +91,16 @@ impl<K, V> OrderedMap<K, V> where K: PhfHash+Eq {
}

impl<K, V> OrderedMap<K, V> {
/// 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<Sized? T>(&self, key: &T, check: |&K| -> bool) -> Option<(uint, &(K, V))>
where T: PhfHash {
let (g, f1, f2) = key.phf_hash(self.key);
Expand Down
56 changes: 33 additions & 23 deletions phf/src/ordered_set.rs
Expand Up @@ -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.
///
Expand Down Expand Up @@ -52,48 +50,60 @@ impl<T> fmt::Show for OrderedSet<T> where T: fmt::Show {
}
}

impl<T> Collection for OrderedSet<T> {
impl<T: PhfHash+Eq> OrderedSet<T> {
/// 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<T> SetTrait<T> for OrderedSet<T> 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<uint> {
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<T>) -> bool {
pub fn is_disjoint(&self, other: &OrderedSet<T>) -> bool {
!self.iter().any(|value| other.contains(value))
}

/// Returns true if `other` contains all values in `self`.
#[inline]
fn is_subset(&self, other: &OrderedSet<T>) -> bool {
pub fn is_subset(&self, other: &OrderedSet<T>) -> 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<T>) -> bool {
other.is_subset(self)
}
}

impl<T: PhfHash+Eq> OrderedSet<T> {
/// Returns a reference to the set's internal static instance of the given
/// key.
///
/// This can be useful for interning schemes.
impl<T> OrderedSet<T> {
/// 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<uint> {
self.map.find_index(key)
/// Returns true if the `Set` contains no elements.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}

impl<T> OrderedSet<T> {
/// Like `contains`, but can operate on any type that is equivalent to a
/// value
#[inline]
Expand Down
58 changes: 34 additions & 24 deletions phf/src/set.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -50,42 +48,54 @@ impl<T> fmt::Show for Set<T> where T: fmt::Show {
}
}

impl<T> Collection for Set<T> {
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]
fn len(&self) -> uint {
self.map.len()
pub fn find_key(&self, key: &T) -> Option<&T> {
self.map.find_key(key)
}
}

impl<T> SetTrait<T> for Set<T> 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<T>) -> bool {
pub fn is_disjoint(&self, other: &Set<T>) -> bool {
!self.iter().any(|value| other.contains(value))
}

/// Returns true if `other` contains all values in `self`.
#[inline]
fn is_subset(&self, other: &Set<T>) -> bool {
pub 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.
/// 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<T>) -> bool {
other.is_subset(self)
}
}

impl<T> Set<T> {
/// 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]
Expand All @@ -106,17 +116,17 @@ impl<T> Set<T> {
///
/// 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()
}
Expand All @@ -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> {}


0 comments on commit f585e4c

Please sign in to comment.