Skip to content

Commit

Permalink
Merge pull request servo#7 from servo/rustup-20140804
Browse files Browse the repository at this point in the history
Upgrade Rust
  • Loading branch information
larsbergstrom committed Aug 6, 2014
2 parents 881706c + 6bcf414 commit 150331b
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/phf/target/
/phf_mac/target/
/doc
target/
Cargo.lock
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ HOST_RUSTFLAGS ?= -O

PHF_SRC := $(VPATH)/phf/src/lib.rs
PHF_MAC_SRC := $(VPATH)/phf_mac/src/lib.rs
PHF_TEST_SRC := $(VPATH)/phf/src/test.rs
PHF_TEST_SRC := $(VPATH)/phf/tests/test.rs

all: phf.dummy phf_host.dummy phf_mac.dummy

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ static KEYWORDS: PhfMap<&'static str, Keyword> = phf_map! {
};

pub fn parse_keyword(keyword: &str) -> Option<Keyword> {
KEYWORDS.find(keyword).map(|t| t.clone())
KEYWORDS.find_equiv(keyword).map(|t| t.clone())
}
```
5 changes: 0 additions & 5 deletions phf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,3 @@ test = false
[dev_dependencies.phf_mac]

path = "../phf_mac"

[[test]]

name = "test"
path = "src/test.rs"
130 changes: 87 additions & 43 deletions phf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<K, V> Collection for PhfMap<K, V> {
}

impl<'a, K: Hash+Eq, V> Map<K, V> for PhfMap<K, V> {
fn find<'a>(&'a self, key: &K) -> Option<&'a V> {
fn find(&self, key: &K) -> Option<&V> {
self.get_entry(key, |k| key == k).map(|e| {
let &(_, ref v) = e;
v
Expand All @@ -82,34 +82,42 @@ impl<K: fmt::Show, V: fmt::Show> fmt::Show for PhfMap<K, V> {
}
}

impl<K: Hash+Eq, V> PhfMap<K, V> {
fn get_entry<'a, T: Hash>(&'a self, key: &T, check: |&K| -> bool)
-> Option<&'a (K, V)> {
let (g, f1, f2) = shared::hash(key, self.k1, self.k2);
let (d1, d2) = self.disps[g % self.disps.len()];
let entry @ &(ref s, _) = &self.entries[shared::displace(f1, f2, d1, d2) %
self.entries.len()];
if check(s) {
Some(entry)
} else {
None
}
impl<K: Hash+Eq, V> Index<K, V> for PhfMap<K, V> {
fn index(&self, k: &K) -> &V {
self.find(k).expect("invalid key")
}
}

impl<K: Hash+Eq, V> PhfMap<K, V> {
/// Returns a reference to the map's internal static instance of the given
/// key.
///
/// This can be useful for interning schemes.
pub fn find_key<'a>(&'a self, key: &K) -> Option<&'a K> {
pub fn find_key(&self, key: &K) -> Option<&K> {
self.get_entry(key, |k| key == k).map(|e| {
let &(ref k, _) = e;
k
})
}
}

impl<K, V> PhfMap<K, V> {
fn get_entry<T: Hash>(&self, key: &T, check: |&K| -> bool)
-> Option<&(K, V)> {
let (g, f1, f2) = shared::hash(key, self.k1, self.k2);
let (d1, d2) = self.disps[g % self.disps.len()];
let entry = &self.entries[shared::displace(f1, f2, d1, d2) %
self.entries.len()];
let &(ref s, _) = entry;
if check(s) {
Some(entry)
} else {
None
}
}

/// Like `find`, but can operate on any type that is equivalent to a key.
pub fn find_equiv<'a, T: Hash+Equiv<K>>(&'a self, key: &T)
-> Option<&'a V> {
pub fn find_equiv<T: Hash+Equiv<K>>(&self, key: &T) -> Option<&V> {
self.get_entry(key, |k| key.equiv(k)).map(|e| {
let &(_, ref v) = e;
v
Expand All @@ -118,8 +126,7 @@ impl<K: Hash+Eq, V> PhfMap<K, V> {

/// Like `find_key`, but can operate on any type that is equivalent to a
/// key.
pub fn find_key_equiv<'a, T: Hash+Equiv<K>>(&'a self, key: &T)
-> Option<&'a K> {
pub fn find_key_equiv<T: Hash+Equiv<K>>(&self, key: &T) -> Option<&K> {
self.get_entry(key, |k| key.equiv(k)).map(|e| {
let &(ref k, _) = e;
k
Expand Down Expand Up @@ -294,11 +301,27 @@ impl<T: Hash+Eq> PhfSet<T> {
///
/// This can be useful for interning schemes.
#[inline]
pub fn find_key<'a>(&'a self, key: &T) -> Option<&'a T> {
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<U: Hash+Equiv<T>>(&self, key: &U) -> bool {
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: Hash+Equiv<T>>(&self, key: &U) -> Option<&T> {
self.map.find_key_equiv(key)
}
}

impl<T> PhfSet<T> {
/// Returns an iterator over the values in the set.
///
Expand Down Expand Up @@ -394,22 +417,42 @@ impl<K, V> Collection for PhfOrderedMap<K, V> {
}
}

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

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

impl<K: Hash+Eq, V> PhfOrderedMap<K, V> {
fn get_entry<'a, T: Hash>(&'a self, key: &T, check: |&K| -> bool)
-> Option<&'a (K, V)> {
/// Returns a reference to the map's internal static instance of the given
/// key.
///
/// 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
})
}
}

impl<K, V> PhfOrderedMap<K, V> {
fn find_entry<T: Hash>(&self, key: &T, check: |&K| -> bool)
-> Option<&(K, V)> {
let (g, f1, f2) = shared::hash(key, self.k1, self.k2);
let (d1, d2) = self.disps[g % self.disps.len()];
let idx = self.idxs[shared::displace(f1, f2, d1, d2) % self.idxs.len()];
let entry @ &(ref s, _) = &self.entries[idx];
let entry = &self.entries[idx];
let &(ref s, _) = entry;

if check(s) {
Some(entry)
Expand All @@ -418,31 +461,18 @@ impl<K: Hash+Eq, V> PhfOrderedMap<K, V> {
}
}

/// Returns a reference to the map's internal static instance of the given
/// key.
///
/// This can be useful for interning schemes.
pub fn find_key<'a>(&'a self, key: &K) -> Option<&'a K> {
self.get_entry(key, |k| key == k).map(|e| {
let &(ref k, _) = e;
k
})
}

/// Like `find`, but can operate on any type that is equivalent to a key.
pub fn find_equiv<'a, T: Hash+Equiv<K>>(&'a self, key: &T)
-> Option<&'a V> {
self.get_entry(key, |k| key.equiv(k)).map(|e| {
pub fn find_equiv<T: Hash+Equiv<K>>(&self, key: &T) -> Option<&V> {
self.find_entry(key, |k| key.equiv(k)).map(|e| {
let &(_, ref v) = e;
v
})
}

/// Like `find_key`, but can operate on any type that is equivalent to a
/// key.
pub fn find_key_equiv<'a, T: Hash+Equiv<K>>(&'a self, key: &T)
-> Option<&'a K> {
self.get_entry(key, |k| key.equiv(k)).map(|e| {
pub fn find_key_equiv<T: Hash+Equiv<K>>(&self, key: &T) -> Option<&K> {
self.find_entry(key, |k| key.equiv(k)).map(|e| {
let &(ref k, _) = e;
k
})
Expand Down Expand Up @@ -651,12 +681,26 @@ impl<T: Hash+Eq> PhfOrderedSet<T> {
///
/// This can be useful for interning schemes.
#[inline]
pub fn find_key<'a>(&'a self, key: &T) -> Option<&'a T> {
pub fn find_key(&self, key: &T) -> Option<&T> {
self.map.find_key(key)
}
}

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

/// Returns an iterator over the values in the set.
///
/// Values are returned in the same order in which they were defined.
Expand Down
60 changes: 60 additions & 0 deletions phf/src/test.rs → phf/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ mod map {
assert_eq!(Some(&0), map.find_equiv(&"a".to_string().as_slice()));
}

#[test]
fn test_index_ok() {
static map: PhfMap<&'static str, int> = phf_map!(
"a" => 0,
);
assert_eq!(0, map["a"]);
}

#[test]
#[should_fail]
fn test_index_fail() {
static map: PhfMap<&'static str, int> = phf_map!(
"a" => 0,
);
map["b"];
}

macro_rules! test_key_type(
($t:ty, $($k:expr => $v:expr),+) => ({
static map: PhfMap<$t, int> = phf_map! {
Expand Down Expand Up @@ -224,6 +241,15 @@ mod set {
assert!(set.contains(&"world"));
assert_eq!(2, set.len());
}

#[test]
fn test_non_static_str_contains() {
static SET: PhfSet<&'static str> = phf_set! {
"hello",
"world",
};
assert!(SET.contains_equiv(&"hello".to_string().as_slice()));
}
}

mod ordered_map {
Expand Down Expand Up @@ -283,6 +309,31 @@ mod ordered_map {
let vec = MAP.values().map(|&v| v).collect::<Vec<_>>();
assert_eq!(vec, vec!(10i, 11, 12));
}

#[test]
fn test_index_ok() {
static map: PhfOrderedMap<&'static str, int> = phf_ordered_map!(
"a" => 0,
);
assert_eq!(0, map["a"]);
}

#[test]
#[should_fail]
fn test_index_fail() {
static map: PhfOrderedMap<&'static str, int> = phf_ordered_map!(
"a" => 0,
);
map["b"];
}

#[test]
fn test_non_static_str_key() {
static map: PhfOrderedMap<&'static str, int> = phf_ordered_map!(
"a" => 0,
);
assert_eq!(Some(&0), map.find_equiv(&"a".to_string().as_slice()));
}
}

mod ordered_set {
Expand Down Expand Up @@ -322,4 +373,13 @@ mod ordered_set {
let vec = SET.iter().map(|&e| e).collect::<Vec<_>>();
assert_eq!(vec, vec!("hello", "there", "world"));
}

#[test]
fn test_non_static_str_contains() {
static SET: PhfOrderedSet<&'static str> = phf_ordered_set! {
"hello",
"world",
};
assert!(SET.contains_equiv(&"hello".to_string().as_slice()));
}
}
17 changes: 9 additions & 8 deletions phf_mac/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use syntax::ext::base::{DummyResult,
ExtCtxt,
MacResult,
MacExpr};
use syntax::fold::Folder;
use syntax::parse;
use syntax::parse::token::{InternedString, COMMA, EOF, FAT_ARROW};
use syntax::print::pprust;
Expand Down Expand Up @@ -165,8 +166,8 @@ fn parse_map(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option<Vec<Entry>> {

let mut bad = false;
while parser.token != EOF {
let key = cx.expand_expr(parser.parse_expr());
let key_contents = parse_key(cx, key).unwrap_or_else(|| {
let key = cx.expander().fold_expr(parser.parse_expr());
let key_contents = parse_key(cx, &*key).unwrap_or_else(|| {
bad = true;
KeyStr(InternedString::new(""))
});
Expand Down Expand Up @@ -212,8 +213,8 @@ fn parse_set(cx: &mut ExtCtxt, tts: &[TokenTree]) -> Option<Vec<Entry>> {

let mut bad = false;
while parser.token != EOF {
let key = cx.expand_expr(parser.parse_expr());
let key_contents = parse_key(cx, key).unwrap_or_else(|| {
let key = cx.expander().fold_expr(parser.parse_expr());
let key_contents = parse_key(cx, &*key).unwrap_or_else(|| {
bad = true;
KeyStr(InternedString::new(""))
});
Expand Down Expand Up @@ -366,11 +367,11 @@ fn try_generate_hash(entries: &[Entry], rng: &mut XorShiftRng)
'disps: for d2 in range(0, table_len) {
try_map.clear();
for &key in bucket.keys.iter() {
let idx = shared::displace(hashes.get(key).f1,
hashes.get(key).f2,
let idx = shared::displace(hashes[key].f1,
hashes[key].f2,
d1,
d2) % table_len;
if map.get(idx).is_some() || try_map.find(&idx).is_some() {
if map[idx].is_some() || try_map.find(&idx).is_some() {
continue 'disps;
}
try_map.insert(idx, key);
Expand Down Expand Up @@ -405,7 +406,7 @@ fn create_map(cx: &mut ExtCtxt, sp: Span, entries: Vec<Entry>, state: HashState)
let disps = create_slice_expr(disps, sp);

let entries = state.map.iter().map(|&idx| {
let &Entry { key, value, .. } = entries.get(idx);
let &Entry { key, value, .. } = &entries[idx];
quote_expr!(&*cx, ($key, $value))
}).collect();
let entries = create_slice_expr(entries, sp);
Expand Down

0 comments on commit 150331b

Please sign in to comment.