Skip to content

Commit

Permalink
Update for Equiv DST changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Nov 1, 2014
1 parent b44065b commit 719de47
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
24 changes: 12 additions & 12 deletions phf/src/lib.rs
Expand Up @@ -104,7 +104,7 @@ impl<K, V> PhfMap<K, V> where K: PhfHash+Eq {
}

impl<K, V> PhfMap<K, V> {
fn get_entry<T>(&self, key: &T, check: |&K| -> bool) -> Option<&(K, V)> where T: PhfHash {
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];
let entry = &self.entries[(shared::displace(f1, f2, d1, d2) % (self.entries.len() as u32))
Expand All @@ -117,13 +117,13 @@ impl<K, V> PhfMap<K, V> {
}

/// Like `find`, but can operate on any type that is equivalent to a key.
pub fn find_equiv<T>(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv<K> {
pub fn find_equiv<Sized? T>(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv<K> {
self.get_entry(key, |k| key.equiv(k)).map(|e| &e.1)
}

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

impl<K, V> PhfOrderedMap<K, V> {
fn find_entry<T>(&self, key: &T, check: |&K| -> bool) -> Option<(uint, &(K, V))>
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);
let (d1, d2) = self.disps[(g % (self.disps.len() as u32)) as uint];
Expand All @@ -453,19 +453,19 @@ impl<K, V> PhfOrderedMap<K, V> {
}

/// Like `find`, but can operate on any type that is equivalent to a key.
pub fn find_equiv<T>(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv<K> {
pub fn find_equiv<Sized? T>(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv<K> {
self.find_entry(key, |k| key.equiv(k)).map(|(_, e)| &e.1)
}

/// Like `find_key`, but can operate on any type that is equivalent to a
/// key.
pub fn find_key_equiv<T>(&self, key: &T) -> Option<&K> where T: PhfHash+Equiv<K> {
pub fn find_key_equiv<Sized? T>(&self, key: &T) -> Option<&K> where T: PhfHash+Equiv<K> {
self.find_entry(key, |k| key.equiv(k)).map(|(_, e)| &e.0)
}

/// Like `find_index`, but can operate on any type that is equivalent to a
/// key.
pub fn find_index_equiv<T>(&self, key: &T) -> Option<uint> where T: PhfHash+Equiv<K> {
pub fn find_index_equiv<Sized? T>(&self, key: &T) -> Option<uint> where T: PhfHash+Equiv<K> {
self.find_entry(key, |k| key.equiv(k)).map(|(i, _)| i)
}

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

/// Like `find_index`, but can operate on any type that is equivalent to a
/// key.
pub fn find_index_equiv<U>(&self, key: &U) -> Option<uint> where U: PhfHash+Equiv<T> {
pub fn find_index_equiv<Sized? U>(&self, key: &U) -> Option<uint> where U: PhfHash+Equiv<T> {
self.map.find_index_equiv(key)
}

Expand Down
20 changes: 10 additions & 10 deletions phf/tests/test.rs
Expand Up @@ -112,7 +112,7 @@ mod map {
static MAP: PhfMap<&'static str, int> = phf_map!(
"a" => 0,
);
assert_eq!(Some(&0), MAP.find_equiv(&"a".to_string()[]));
assert_eq!(Some(&0), MAP.find_equiv("a".to_string()[]));
}

#[test]
Expand Down Expand Up @@ -248,7 +248,7 @@ mod set {
"hello",
"world",
};
assert!(SET.contains_equiv(&"hello".to_string()[]));
assert!(SET.contains_equiv("hello".to_string()[]));
}
}

Expand Down Expand Up @@ -289,9 +289,9 @@ mod ordered_map {
assert_eq!(None, MAP.find_index(&"xyz"));
assert_eq!(&"baz", MAP.keys().idx(MAP.find_index(&"baz").unwrap()).unwrap());

assert_eq!(Some(0), MAP.find_index_equiv(&"foo".to_string()[]));
assert_eq!(Some(2), MAP.find_index_equiv(&"baz".to_string()[]));
assert_eq!(None, MAP.find_index_equiv(&"xyz".to_string()[]));
assert_eq!(Some(0), MAP.find_index_equiv("foo".to_string()[]));
assert_eq!(Some(2), MAP.find_index_equiv("baz".to_string()[]));
assert_eq!(None, MAP.find_index_equiv("xyz".to_string()[]));
}

#[test]
Expand Down Expand Up @@ -349,7 +349,7 @@ mod ordered_map {
static MAP: PhfOrderedMap<&'static str, int> = phf_ordered_map!(
"a" => 0,
);
assert_eq!(Some(&0), MAP.find_equiv(&"a".to_string()[]));
assert_eq!(Some(&0), MAP.find_equiv("a".to_string()[]));
}
}

Expand Down Expand Up @@ -392,9 +392,9 @@ mod ordered_set {
assert_eq!(None, SET.find_index(&"xyz"));
assert_eq!(&"baz", SET.iter().idx(SET.find_index(&"baz").unwrap()).unwrap());

assert_eq!(Some(0), SET.find_index_equiv(&"foo".to_string()[]));
assert_eq!(Some(2), SET.find_index_equiv(&"baz".to_string()[]));
assert_eq!(None, SET.find_index_equiv(&"xyz".to_string()[]));
assert_eq!(Some(0), SET.find_index_equiv("foo".to_string()[]));
assert_eq!(Some(2), SET.find_index_equiv("baz".to_string()[]));
assert_eq!(None, SET.find_index_equiv("xyz".to_string()[]));
}

#[test]
Expand All @@ -414,6 +414,6 @@ mod ordered_set {
"hello",
"world",
};
assert!(SET.contains_equiv(&"hello".to_string()[]));
assert!(SET.contains_equiv("hello".to_string()[]));
}
}
20 changes: 19 additions & 1 deletion shared/mod.rs
@@ -1,4 +1,7 @@
extern crate xxhash;
extern crate core;

use self::core::kinds::Sized;

#[inline]
pub fn displace(f1: u32, f2: u32, d1: u32, d2: u32) -> u32 {
Expand All @@ -16,7 +19,7 @@ fn split(hash: u64) -> (u32, u32, u32) {
}

/// A trait implemented by types which can be used in PHF data structures
pub trait PhfHash {
pub trait PhfHash for Sized? {
/// Hashes the value of `self`, factoring in a seed
fn phf_hash(&self, seed: u64) -> (u32, u32, u32);
}
Expand All @@ -35,6 +38,21 @@ impl<'a> PhfHash for &'a [u8] {
}
}

impl PhfHash for str {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(xxhash::hash_with_seed(seed, &self))
}
}

impl PhfHash for [u8] {
#[inline]
fn phf_hash(&self, seed: u64) -> (u32, u32, u32) {
split(xxhash::oneshot(self, seed))
}
}


macro_rules! sip_impl(
($t:ty) => (
impl PhfHash for $t {
Expand Down

0 comments on commit 719de47

Please sign in to comment.