Skip to content

Commit

Permalink
Switch from find to get
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Nov 15, 2014
1 parent af2dd53 commit 88abf6c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 71 deletions.
16 changes: 8 additions & 8 deletions phf/src/map.rs
Expand Up @@ -55,26 +55,26 @@ impl<K, V> fmt::Show for Map<K, V> where K: fmt::Show, V: fmt::Show {

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

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> {
pub fn get(&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()
self.get(key).is_some()
}

/// 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> {
pub fn get_key(&self, key: &K) -> Option<&K> {
self.get_entry(key, |k| key == k).map(|e| &e.0)
}
}
Expand Down Expand Up @@ -102,14 +102,14 @@ impl<K, V> Map<K, V> {
}
}

/// Like `find`, but can operate on any type that is equivalent to a key.
pub fn find_equiv<Sized? T>(&self, key: &T) -> Option<&V> where T: PhfHash+Equiv<K> {
/// Like `get`, but can operate on any type that is equivalent to a key.
pub fn get_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
/// Like `get_key`, but can operate on any type that is equivalent to a
/// key.
pub fn find_key_equiv<Sized? T>(&self, key: &T) -> Option<&K> where T: PhfHash+Equiv<K> {
pub fn get_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
36 changes: 18 additions & 18 deletions phf/src/ordered_map.rs
Expand Up @@ -60,33 +60,33 @@ impl<K, V> fmt::Show for OrderedMap<K, V> where K: fmt::Show, V: fmt::Show {

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")
self.get(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)
pub fn get(&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()
self.get(key).is_some()
}

/// 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)| &e.0)
pub fn get_key(&self, key: &K) -> Option<&K> {
self.get_entry(key, |k| k == key).map(|(_, e)| &e.0)
}

/// Returns the index of the key within the list used to initialize
/// the ordered map.
pub fn find_index(&self, key: &K) -> Option<uint> {
self.find_entry(key, |k| k == key).map(|(i, _)| i)
pub fn get_index(&self, key: &K) -> Option<uint> {
self.get_entry(key, |k| k == key).map(|(i, _)| i)
}
}

Expand All @@ -101,7 +101,7 @@ impl<K, V> OrderedMap<K, V> {
self.entries.len()
}

fn find_entry<Sized? T>(&self, key: &T, check: |&K| -> bool) -> Option<(uint, &(K, V))>
fn get_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 @@ -115,21 +115,21 @@ impl<K, V> OrderedMap<K, V> {
}
}

/// Like `find`, but can operate on any type that is equivalent to a key.
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 `get`, but can operate on any type that is equivalent to a key.
pub fn get_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
/// Like `get_key`, but can operate on any type that is equivalent to a
/// key.
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)
pub fn get_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)
}

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

/// Returns an iterator over the key/value pairs in the map.
Expand Down
22 changes: 11 additions & 11 deletions phf/src/ordered_set.rs
Expand Up @@ -56,14 +56,14 @@ impl<T: PhfHash+Eq> OrderedSet<T> {
///
/// This can be useful for interning schemes.
#[inline]
pub fn find_key(&self, key: &T) -> Option<&T> {
self.map.find_key(key)
pub fn get_key(&self, key: &T) -> Option<&T> {
self.map.get_key(key)
}

/// 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)
pub fn get_index(&self, key: &T) -> Option<uint> {
self.map.get_index(key)
}

/// Returns true if `value` is in the `Set`.
Expand Down Expand Up @@ -108,20 +108,20 @@ impl<T> OrderedSet<T> {
/// value
#[inline]
pub fn contains_equiv<Sized? U>(&self, key: &U) -> bool where U: PhfHash+Equiv<T> {
self.map.find_equiv(key).is_some()
self.map.get_equiv(key).is_some()
}

/// Like `find_key`, but can operate on any type that is equivalent to a
/// Like `get_key`, but can operate on any type that is equivalent to a
/// value
#[inline]
pub fn find_key_equiv<Sized? U>(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv<T> {
self.map.find_key_equiv(key)
pub fn get_key_equiv<Sized? U>(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv<T> {
self.map.get_key_equiv(key)
}

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

/// Returns an iterator over the values in the set.
Expand Down
12 changes: 6 additions & 6 deletions phf/src/set.rs
Expand Up @@ -54,8 +54,8 @@ impl<T> Set<T> where T: PhfHash+Eq {
///
/// This can be useful for interning schemes.
#[inline]
pub fn find_key(&self, key: &T) -> Option<&T> {
self.map.find_key(key)
pub fn get_key(&self, key: &T) -> Option<&T> {
self.map.get_key(key)
}

/// Returns true if `value` is in the `Set`.
Expand Down Expand Up @@ -100,14 +100,14 @@ impl<T> Set<T> {
/// value
#[inline]
pub fn contains_equiv<Sized? U>(&self, key: &U) -> bool where U: PhfHash+Equiv<T> {
self.map.find_equiv(key).is_some()
self.map.get_equiv(key).is_some()
}

/// Like `find_key`, but can operate on any type that is equivalent to a
/// Like `get_key`, but can operate on any type that is equivalent to a
/// value
#[inline]
pub fn find_key_equiv<Sized? U>(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv<T> {
self.map.find_key_equiv(key)
pub fn get_key_equiv<Sized? U>(&self, key: &U) -> Option<&T> where U: PhfHash+Equiv<T> {
self.map.get_key_equiv(key)
}
}

Expand Down
56 changes: 28 additions & 28 deletions phf/tests/test.rs
Expand Up @@ -24,9 +24,9 @@ mod map {
"foo" => 10,
"bar" => 11,
);
assert!(Some(&10) == MAP.find(&("foo")));
assert!(Some(&11) == MAP.find(&("bar")));
assert_eq!(None, MAP.find(&("asdf")));
assert!(Some(&10) == MAP.get(&("foo")));
assert!(Some(&11) == MAP.get(&("bar")));
assert_eq!(None, MAP.get(&("asdf")));
assert_eq!(2, MAP.len());
}

Expand Down Expand Up @@ -96,23 +96,23 @@ mod map {
"y" => 24,
"z" => 25,
);
assert!(MAP.find(&("a")) == Some(&0));
assert!(MAP.get(&("a")) == Some(&0));
}

#[test]
fn test_macro_key() {
static MAP: phf::Map<&'static str, int> = phf_map!(
concat!("foo", "bar") => 1
);
assert!(Some(&1) == MAP.find(&("foobar")));
assert!(Some(&1) == MAP.get(&("foobar")));
}

#[test]
fn test_non_static_str_key() {
static MAP: phf::Map<&'static str, int> = phf_map!(
"a" => 0,
);
assert_eq!(Some(&0), MAP.find_equiv("a".to_string()[]));
assert_eq!(Some(&0), MAP.get_equiv("a".to_string()[]));
}

#[test]
Expand All @@ -138,7 +138,7 @@ mod map {
$($k => $v),+
};
$(
assert_eq!(Some(&$v), MAP.find(&$k));
assert_eq!(Some(&$v), MAP.get(&$k));
)+
})
)
Expand Down Expand Up @@ -271,27 +271,27 @@ mod ordered_map {
"foo" => 10,
"bar" => 11,
);
assert!(Some(&10) == MAP.find(&"foo"));
assert!(Some(&11) == MAP.find(&"bar"));
assert_eq!(None, MAP.find(&"asdf"));
assert!(Some(&10) == MAP.get(&"foo"));
assert!(Some(&11) == MAP.get(&"bar"));
assert_eq!(None, MAP.get(&"asdf"));
assert_eq!(2, MAP.len());
}

#[test]
fn test_find_index() {
fn test_get_index() {
static MAP: phf::OrderedMap<&'static str, int> = phf_ordered_map!(
"foo" => 5,
"bar" => 5,
"baz" => 5,
);
assert_eq!(Some(0), MAP.find_index(&"foo"));
assert_eq!(Some(2), MAP.find_index(&"baz"));
assert_eq!(None, MAP.find_index(&"xyz"));
assert_eq!(&"baz", MAP.keys().idx(MAP.find_index(&"baz").unwrap()).unwrap());
assert_eq!(Some(0), MAP.get_index(&"foo"));
assert_eq!(Some(2), MAP.get_index(&"baz"));
assert_eq!(None, MAP.get_index(&"xyz"));
assert_eq!(&"baz", MAP.keys().idx(MAP.get_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.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()[]));
}

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

Expand Down Expand Up @@ -381,20 +381,20 @@ mod ordered_set {
}

#[test]
fn test_find_index() {
fn test_get_index() {
static SET: phf::OrderedSet<&'static str> = phf_ordered_set! {
"foo",
"bar",
"baz",
};
assert_eq!(Some(0), SET.find_index(&"foo"));
assert_eq!(Some(2), SET.find_index(&"baz"));
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.get_index(&"foo"));
assert_eq!(Some(2), SET.get_index(&"baz"));
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()[]));
}

#[test]
Expand Down

0 comments on commit 88abf6c

Please sign in to comment.