Skip to content

Commit

Permalink
Misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Oct 26, 2014
1 parent fdb7270 commit 2fe6940
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 49 deletions.
56 changes: 15 additions & 41 deletions phf/src/lib.rs
Expand Up @@ -4,7 +4,7 @@
//! literals, or any of the fixed-size integral types.
#![doc(html_root_url="http://www.rust-ci.org/sfackler")]
#![warn(missing_doc)]
#![feature(macro_rules)]
#![feature(macro_rules, tuple_indexing)]

use std::fmt;
use std::iter;
Expand Down Expand Up @@ -58,10 +58,7 @@ impl<K, V> Collection for PhfMap<K, V> {

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

Expand Down Expand Up @@ -92,10 +89,7 @@ impl<K, V> PhfMap<K, V> where K: PhfHash+Eq {
///
/// This can be useful for interning schemes.
pub fn find_key(&self, key: &K) -> Option<&K> {
self.get_entry(key, |k| key == k).map(|e| {
let &(ref k, _) = e;
k
})
self.get_entry(key, |k| key == k).map(|e| &e.0)
}
}

Expand All @@ -105,8 +99,7 @@ impl<K, V> PhfMap<K, V> {
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))
as uint];
let &(ref s, _) = entry;
if check(s) {
if check(&entry.0) {
Some(entry)
} else {
None
Expand All @@ -115,19 +108,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> {
self.get_entry(key, |k| key.equiv(k)).map(|e| {
let &(_, ref v) = e;
v
})
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> {
self.get_entry(key, |k| key.equiv(k)).map(|e| {
let &(ref k, _) = e;
k
})
self.get_entry(key, |k| key.equiv(k)).map(|e| &e.0)
}
}

Expand All @@ -143,14 +130,14 @@ impl<K, V> PhfMap<K, V> {
///
/// Keys are returned in an arbitrary but fixed order.
pub fn keys<'a>(&'a self) -> PhfMapKeys<'a, K, V> {
PhfMapKeys { iter: self.entries().map(|&(ref k, _)| k) }
PhfMapKeys { iter: self.entries().map(|e| &e.0) }
}

/// Returns an iterator over the values in the map.
///
/// Values are returned in an arbitrary but fixed order.
pub fn values<'a>(&'a self) -> PhfMapValues<'a, K, V> {
PhfMapValues { iter: self.entries().map(|&(_, ref v)| v) }
PhfMapValues { iter: self.entries().map(|e | &e.1) }
}
}

Expand Down Expand Up @@ -414,10 +401,7 @@ impl<K, V> Collection for PhfOrderedMap<K, V> {

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

Expand All @@ -433,10 +417,7 @@ impl<K, V> PhfOrderedMap<K, V> where K: PhfHash+Eq {
///
/// This can be useful for interning schemes.
pub fn find_key(&self, key: &K) -> Option<&K> {
self.find_entry(key, |k| k == key).map(|(_, e)| {
let &(ref k, _) = e;
k
})
self.find_entry(key, |k| k == key).map(|(_, e)| &e.0)
}

/// Returns the index of the key within the list used to initialize
Expand All @@ -453,9 +434,8 @@ impl<K, V> PhfOrderedMap<K, V> {
let (d1, d2) = self.disps[(g % (self.disps.len() as u32)) as uint];
let idx = self.idxs[(shared::displace(f1, f2, d1, d2) % (self.idxs.len() as u32)) as uint];
let entry = &self.entries[idx];
let &(ref s, _) = entry;

if check(s) {
if check(&entry.0) {
Some((idx, entry))
} else {
None
Expand All @@ -464,19 +444,13 @@ 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> {
self.find_entry(key, |k| key.equiv(k)).map(|(_, e)| {
let &(_, ref v) = e;
v
})
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> {
self.find_entry(key, |k| key.equiv(k)).map(|(_, e)| {
let &(ref k, _) = e;
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
Expand All @@ -496,14 +470,14 @@ impl<K, V> PhfOrderedMap<K, V> {
///
/// Keys are returned in the same order in which they were defined.
pub fn keys<'a>(&'a self) -> PhfOrderedMapKeys<'a, K, V> {
PhfOrderedMapKeys { iter: self.entries().map(|&(ref k, _)| k) }
PhfOrderedMapKeys { iter: self.entries().map(|e| &e.0) }
}

/// Returns an iterator over the values in the map.
///
/// Values are returned in the same order in which they were defined.
pub fn values<'a>(&'a self) -> PhfOrderedMapValues<'a, K, V> {
PhfOrderedMapValues { iter: self.entries().map(|&(_, ref v)| v) }
PhfOrderedMapValues { iter: self.entries().map(|e| &e.1) }
}
}

Expand Down
16 changes: 8 additions & 8 deletions phf_mac/src/util.rs
Expand Up @@ -92,7 +92,7 @@ pub fn generate_hash(cx: &mut ExtCtxt, sp: Span, entries: &[Entry]) -> HashState
let start = time::precise_time_s();
let state;
loop {
match try_generate_hash(entries[], &mut rng) {
match try_generate_hash(entries, &mut rng) {
Some(s) => {
state = s;
break;
Expand Down Expand Up @@ -122,7 +122,7 @@ pub fn try_generate_hash(entries: &[Entry], rng: &mut XorShiftRng) -> Option<Has

let key = rng.gen();

let hashes: Vec<Hashes> = entries.iter().map(|entry| {
let hashes: Vec<_> = entries.iter().map(|entry| {
let (g, f1, f2) = entry.key_contents.phf_hash(key);
Hashes {
g: g,
Expand Down Expand Up @@ -177,9 +177,9 @@ pub fn try_generate_hash(entries: &[Entry], rng: &mut XorShiftRng) -> Option<Has
}

// We've picked a good set of disps
*disps.get_mut(bucket.idx) = (d1, d2);
disps[bucket.idx] = (d1, d2);
for &(idx, key) in values_to_add.iter() {
*map.get_mut(idx) = Some(key);
map[idx] = Some(key);
}
continue 'buckets;
}
Expand All @@ -197,7 +197,7 @@ pub fn try_generate_hash(entries: &[Entry], rng: &mut XorShiftRng) -> Option<Has
}

pub fn create_map(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state: HashState)
-> Box<MacResult+'static> {
-> Box<MacResult+'static> {
let disps = state.disps.iter().map(|&(d1, d2)| {
quote_expr!(&*cx, ($d1, $d2))
}).collect();
Expand All @@ -224,14 +224,14 @@ pub fn create_set(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state: HashSt
}

pub fn create_ordered_map(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state: HashState)
-> Box<MacResult+'static> {
-> Box<MacResult+'static> {
let disps = state.disps.iter().map(|&(d1, d2)| {
quote_expr!(&*cx, ($d1, $d2))
}).collect();
let disps = cx.expr_vec(sp, disps);

let idxs = state.map.iter().map(|&idx| quote_expr!(&*cx, $idx)).collect();
let idxs =cx.expr_vec(sp, idxs);
let idxs = cx.expr_vec(sp, idxs);

let entries = entries.iter().map(|&Entry { ref key, ref value, .. }| {
quote_expr!(&*cx, ($key, $value))
Expand All @@ -248,7 +248,7 @@ pub fn create_ordered_map(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state
}

pub fn create_ordered_set(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state: HashState)
-> Box<MacResult+'static> {
-> Box<MacResult+'static> {
let map = create_ordered_map(cx, sp, entries, state).make_expr().unwrap();
MacExpr::new(quote_expr!(cx, ::phf::PhfOrderedSet { map: $map }))
}

0 comments on commit 2fe6940

Please sign in to comment.