From 2f3c6053c2d754974a94aa45a49b8cce10ae88ba Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Wed, 19 Nov 2014 20:17:29 -0800 Subject: [PATCH] Update to use BorrowFrom --- README.md | 2 +- phf/src/map.rs | 71 ++++++++++++--------------------- phf/src/ordered_map.rs | 89 ++++++++++++++++-------------------------- phf/src/ordered_set.rs | 73 ++++++++++++---------------------- phf/src/set.rs | 72 ++++++++++++---------------------- phf/tests/test.rs | 20 +++++----- 6 files changed, 120 insertions(+), 207 deletions(-) diff --git a/README.md b/README.md index ffc68cb1..a93367ca 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,6 @@ static KEYWORDS: phf::Map<&'static str, Keyword> = phf_map! { }; pub fn parse_keyword(keyword: &str) -> Option { - KEYWORDS.find_equiv(keyword).map(|t| t.clone()) + KEYWORDS.get(keyword).map(|t| t.clone()) } ``` diff --git a/phf/src/map.rs b/phf/src/map.rs index f24a7c40..7b9c8424 100644 --- a/phf/src/map.rs +++ b/phf/src/map.rs @@ -1,5 +1,6 @@ //! An immutable map constructed at compile time. use core::prelude::*; +use core::borrow::BorrowFrom; use core::iter; use core::slice; use core::fmt; @@ -53,77 +54,55 @@ impl fmt::Show for Map where K: fmt::Show, V: fmt::Show { } } -impl Index for Map where K: PhfHash+Eq { - fn index(&self, k: &K) -> &V { +impl Index for Map where T: Eq + PhfHash + BorrowFrom { + fn index(&self, k: &T) -> &V { self.get(k).expect("invalid key") } } -impl Map where K: PhfHash+Eq { - /// Returns a reference to the value that `key` maps to. - pub fn get(&self, key: &K) -> Option<&V> { - self.get_entry_(key, |k| key == k).map(|e| &e.1) +impl Map { + /// 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() } /// Determines if `key` is in the `Map`. - pub fn contains_key(&self, key: &K) -> bool { + pub fn contains_key(&self, key: &T) -> bool where T: Eq + PhfHash + BorrowFrom { self.get(key).is_some() } + /// Returns a reference to the value that `key` maps to. + pub fn get(&self, key: &T) -> Option<&V> where T: Eq + PhfHash + BorrowFrom { + self.get_entry(key).map(|e| e.1) + } + /// Returns a reference to the map's internal static instance of the given /// key. /// /// This can be useful for interning schemes. - pub fn get_key(&self, key: &K) -> Option<&K> { - self.get_entry_(key, |k| key == k).map(|e| &e.0) + pub fn get_key(&self, key: &T) -> Option<&K> where T: Eq + PhfHash + BorrowFrom { + self.get_entry(key).map(|e| e.0) } -} -impl Map { - /// 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_(&self, key: &T, check: |&K| -> bool) -> Option<&(K, V)> where T: PhfHash { + /// Like `get`, but returns both the key and the value. + pub fn get_entry(&self, key: &T) -> Option<(&K, &V)> + where T: Eq + PhfHash + BorrowFrom { 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)) as uint]; - if check(&entry.0) { - Some(entry) + if BorrowFrom::borrow_from(&entry.0) == key { + Some((&entry.0, &entry.1)) } else { None } } - /// Like `get`, but returns both the key and the value. - pub fn get_entry(&self, key: &T) -> Option<(&K, &V)> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|e| (&e.0, &e.1)) - } - - /// Like `get`, but can operate on any type that is equivalent to a key. - pub fn get_equiv(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|e| &e.1) - } - - /// Like `get_key`, but can operate on any type that is equivalent to a - /// key. - pub fn get_key_equiv(&self, key: &T) -> Option<&K> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|e| &e.0) - } - - /// Like `get_kv`, but can operate on any type that is equivalent to a - /// key. - pub fn get_entry_equiv(&self, key: &T) -> Option<(&K, &V)> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|e| (&e.0, &e.1)) - } - /// Returns an iterator over the key/value pairs in the map. /// /// Entries are retuned in an arbitrary but fixed order. diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index ad7426f3..b06e2010 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -1,8 +1,10 @@ //! An order-preserving immutable map constructed at compile time. use core::prelude::*; +use core::borrow::BorrowFrom; use core::fmt; use core::slice; use core::iter; + use PhfHash; use shared; @@ -58,91 +60,68 @@ impl fmt::Show for OrderedMap where K: fmt::Show, V: fmt::Show { } } -impl Index for OrderedMap where K: PhfHash+Eq { - fn index(&self, k: &K) -> &V { +impl Index for OrderedMap where T: Eq + PhfHash + BorrowFrom { + fn index(&self, k: &T) -> &V { self.get(k).expect("invalid key") } } -impl OrderedMap where K: PhfHash+Eq { - /// Returns a reference to the value that `key` maps to. - pub fn get(&self, key: &K) -> Option<&V> { - self.get_entry_(key, |k| key == k).map(|(_, e)| &e.1) +impl OrderedMap { + /// Returns the number of entries in the `Map`. + pub fn len(&self) -> uint { + self.entries.len() } - /// Determines if `key` is in the `Map`. - pub fn contains_key(&self, key: &K) -> bool { - self.get(key).is_some() + /// Returns true if the `Map` is empty. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + /// Returns a reference to the value that `key` maps to. + pub fn get(&self, key: &T) -> Option<&V> where T: Eq + PhfHash + BorrowFrom { + self.get_entry(key).map(|e| e.1) } /// Returns a reference to the map's internal static instance of the given /// key. /// /// This can be useful for interning schemes. - pub fn get_key(&self, key: &K) -> Option<&K> { - self.get_entry_(key, |k| k == key).map(|(_, e)| &e.0) + pub fn get_key(&self, key: &T) -> Option<&K> where T: Eq + PhfHash + BorrowFrom { + self.get_entry(key).map(|e| e.0) } - /// Returns the index of the key within the list used to initialize - /// the ordered map. - pub fn get_index(&self, key: &K) -> Option { - self.get_entry_(key, |k| k == key).map(|(i, _)| i) + /// Determines if `key` is in the `Map`. + pub fn contains_key(&self, key: &T) -> bool where T: Eq + PhfHash + BorrowFrom { + self.get(key).is_some() } -} -impl OrderedMap { - /// Returns true if the `Map` is empty. - pub fn is_empty(&self) -> bool { - self.len() == 0 + /// Returns the index of the key within the list used to initialize + /// the ordered map. + pub fn get_index(&self, key: &T) -> Option + where T: Eq + PhfHash + BorrowFrom { + self.get_internal(key).map(|(i, _)| i) } - /// Returns the number of entries in the `Map`. - pub fn len(&self) -> uint { - self.entries.len() + /// Like `get`, but returns both the key and the value. + pub fn get_entry(&self, key: &T) -> Option<(&K, &V)> + where T: Eq + PhfHash + BorrowFrom { + self.get_internal(key).map(|(_, e)| e) } - fn get_entry_(&self, key: &T, check: |&K| -> bool) -> Option<(uint, &(K, V))> - where T: PhfHash { + fn get_internal(&self, key: &T) -> Option<(uint, (&K, &V))> + where T: Eq + PhfHash + BorrowFrom { let (g, f1, f2) = key.phf_hash(self.key); 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]; - if check(&entry.0) { - Some((idx, entry)) + if BorrowFrom::borrow_from(&entry.0) == key { + Some((idx, (&entry.0, &entry.1))) } else { None } } - /// Like `get`, but returns both the key and the value. - pub fn get_entry(&self, key: &T) -> Option<(&K, &V)> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|(_, e)| (&e.0, &e.1)) - } - - /// Like `get`, but can operate on any type that is equivalent to a key. - pub fn get_equiv(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|(_, e)| &e.1) - } - - /// Like `get_key`, but can operate on any type that is equivalent to a - /// key. - pub fn get_key_equiv(&self, key: &T) -> Option<&K> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|(_, e)| &e.0) - } - - /// Like `get_index`, but can operate on any type that is equivalent to a - /// key. - pub fn get_index_equiv(&self, key: &T) -> Option where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|(i, _)| i) - } - - /// Like `get_kv`, but can operate on any type that is equivalent to a - /// key. - pub fn get_entry_equiv(&self, key: &T) -> Option<(&K, &V)> where T: PhfHash+Equiv { - self.get_entry_(key, |k| key.equiv(k)).map(|(_, e)| (&e.0, &e.1)) - } - /// Returns an iterator over the key/value pairs in the map. /// /// Entries are returned in the same order in which they were defined. diff --git a/phf/src/ordered_set.rs b/phf/src/ordered_set.rs index d271360a..98ee9e00 100644 --- a/phf/src/ordered_set.rs +++ b/phf/src/ordered_set.rs @@ -1,5 +1,6 @@ //! An order-preserving immutable set constructed at compile time. use core::prelude::*; +use core::borrow::BorrowFrom; use core::fmt; use ordered_map; use {PhfHash, OrderedMap}; @@ -50,28 +51,46 @@ impl fmt::Show for OrderedSet where T: fmt::Show { } } -impl OrderedSet { +impl OrderedSet { + /// Returns the number of elements in the `OrderedSet`. + pub fn len(&self) -> uint { + self.map.len() + } + + /// Returns true if the `OrderedSet` contains no elements. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Returns a reference to the set's internal static instance of the given /// key. /// /// This can be useful for interning schemes. - #[inline] - pub fn get_key(&self, key: &T) -> Option<&T> { + pub fn get_key(&self, key: &U) -> Option<&T> where U: Eq + PhfHash + BorrowFrom { self.map.get_key(key) } /// Returns the index of the key within the list used to initialize /// the ordered set. - pub fn get_index(&self, key: &T) -> Option { + pub fn get_index(&self, key: &U) -> Option + where U: Eq + PhfHash + BorrowFrom { self.map.get_index(key) } /// Returns true if `value` is in the `Set`. - #[inline] - pub fn contains(&self, value: &T) -> bool { + pub fn contains(&self, value: &U) -> bool where U: Eq + PhfHash + BorrowFrom { self.map.contains_key(value) } + /// Returns an iterator over the values in the set. + /// + /// Values are returned in the same order in which they were defined. + pub fn iter<'a>(&'a self) -> Entries<'a, T> { + Entries { iter: self.map.keys() } + } +} + +impl OrderedSet where T: Eq + PhfHash { /// Returns true if `other` shares no elements with `self`. #[inline] pub fn is_disjoint(&self, other: &OrderedSet) -> bool { @@ -91,48 +110,6 @@ impl OrderedSet { } } -impl OrderedSet { - /// 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] - pub fn contains_equiv(&self, key: &U) -> bool where U: PhfHash+Equiv { - self.map.get_equiv(key).is_some() - } - - /// Like `get_key`, but can operate on any type that is equivalent to a - /// value - #[inline] - pub fn get_key_equiv(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv { - self.map.get_key_equiv(key) - } - - /// Like `get_index`, but can operate on any type that is equivalent to a - /// key. - pub fn get_index_equiv(&self, key: &U) -> Option where U: PhfHash+Equiv { - self.map.get_index_equiv(key) - } - - /// Returns an iterator over the values in the set. - /// - /// Values are returned in the same order in which they were defined. - #[inline] - pub fn iter<'a>(&'a self) -> Entries<'a, T> { - Entries { iter: self.map.keys() } - } -} - /// An iterator over the values in a `OrderedSet`. pub struct Entries<'a, T:'a> { iter: ordered_map::Keys<'a, T, ()>, diff --git a/phf/src/set.rs b/phf/src/set.rs index 45b41da9..b4c5174f 100644 --- a/phf/src/set.rs +++ b/phf/src/set.rs @@ -1,9 +1,11 @@ //! An immutable set constructed at compile time. use core::prelude::*; -use Map; +use core::borrow::BorrowFrom; use core::fmt; + use shared::PhfHash; use map; +use Map; /// An immutable set constructed at compile time. /// @@ -48,79 +50,55 @@ impl fmt::Show for Set where T: fmt::Show { } } -impl Set where T: PhfHash+Eq { +impl Set { + /// Returns the number of elements in the `Set`. + pub fn len(&self) -> uint { + self.map.len() + } + + /// Returns true if the `Set` contains no elements. + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + /// Returns a reference to the set's internal static instance of the given /// key. /// /// This can be useful for interning schemes. - #[inline] - pub fn get_key(&self, key: &T) -> Option<&T> { + pub fn get_key(&self, key: &U) -> Option<&T> where U: Eq + PhfHash + BorrowFrom { self.map.get_key(key) } /// Returns true if `value` is in the `Set`. - #[inline] - pub fn contains(&self, value: &T) -> bool { + pub fn contains(&self, value: &U) -> bool where U: Eq + PhfHash + BorrowFrom { self.map.contains_key(value) } + /// Returns an iterator over the values in the set. + /// + /// Values are returned in an arbitrary but fixed order. + pub fn iter<'a>(&'a self) -> Items<'a, T> { + Items { iter: self.map.keys() } + } +} + +impl Set where T: Eq + PhfHash { /// Returns true if `other` shares no elements with `self`. - #[inline] pub fn is_disjoint(&self, other: &Set) -> bool { !self.iter().any(|value| other.contains(value)) } /// Returns true if `other` contains all values in `self`. - #[inline] pub fn is_subset(&self, other: &Set) -> bool { self.iter().all(|value| other.contains(value)) } /// Returns true if `self` contains all values in `other`. - #[inline] pub fn is_superset(&self, other: &Set) -> bool { other.is_subset(self) } } -impl Set { - /// 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] - pub fn contains_equiv(&self, key: &U) -> bool where U: PhfHash+Equiv { - self.map.get_equiv(key).is_some() - } - - /// Like `get_key`, but can operate on any type that is equivalent to a - /// value - #[inline] - pub fn get_key_equiv(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv { - self.map.get_key_equiv(key) - } -} - -impl Set { - /// 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) -> Items<'a, T> { - Items { iter: self.map.keys() } - } -} - /// An iterator over the values in a `Set`. pub struct Items<'a, T:'static> { iter: map::Keys<'a, T, ()>, diff --git a/phf/tests/test.rs b/phf/tests/test.rs index ec2edbfd..07236544 100644 --- a/phf/tests/test.rs +++ b/phf/tests/test.rs @@ -112,7 +112,7 @@ mod map { static MAP: phf::Map<&'static str, int> = phf_map!( "a" => 0, ); - assert_eq!(Some(&0), MAP.get_equiv("a".to_string()[])); + assert_eq!(Some(&0), MAP.get("a".to_string()[])); } #[test] @@ -248,7 +248,7 @@ mod set { "hello", "world", }; - assert!(SET.contains_equiv("hello".to_string()[])); + assert!(SET.contains("hello".to_string()[])); } } @@ -289,9 +289,9 @@ mod ordered_map { assert_eq!(None, MAP.get_index(&"xyz")); assert_eq!(&"baz", MAP.keys().idx(MAP.get_index(&"baz").unwrap()).unwrap()); - assert_eq!(Some(0), MAP.get_index_equiv("foo".to_string()[])); - assert_eq!(Some(2), MAP.get_index_equiv("baz".to_string()[])); - assert_eq!(None, MAP.get_index_equiv("xyz".to_string()[])); + assert_eq!(Some(0), MAP.get_index("foo".to_string()[])); + assert_eq!(Some(2), MAP.get_index("baz".to_string()[])); + assert_eq!(None, MAP.get_index("xyz".to_string()[])); } #[test] @@ -349,7 +349,7 @@ mod ordered_map { static MAP: phf::OrderedMap<&'static str, int> = phf_ordered_map!( "a" => 0, ); - assert_eq!(Some(&0), MAP.get_equiv("a".to_string()[])); + assert_eq!(Some(&0), MAP.get("a".to_string()[])); } } @@ -392,9 +392,9 @@ mod ordered_set { assert_eq!(None, SET.get_index(&"xyz")); assert_eq!(&"baz", SET.iter().idx(SET.get_index(&"baz").unwrap()).unwrap()); - assert_eq!(Some(0), SET.get_index_equiv("foo".to_string()[])); - assert_eq!(Some(2), SET.get_index_equiv("baz".to_string()[])); - assert_eq!(None, SET.get_index_equiv("xyz".to_string()[])); + assert_eq!(Some(0), SET.get_index("foo".to_string()[])); + assert_eq!(Some(2), SET.get_index("baz".to_string()[])); + assert_eq!(None, SET.get_index("xyz".to_string()[])); } #[test] @@ -414,6 +414,6 @@ mod ordered_set { "hello", "world", }; - assert!(SET.contains_equiv("hello".to_string()[])); + assert!(SET.contains("hello".to_string()[])); } }