Skip to content

Commit

Permalink
Added key+value equivalents for the map getters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Clark Gaebel committed Nov 19, 2014
1 parent 585a07e commit 7ced000
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 11 additions & 2 deletions phf/src/map.rs
Expand Up @@ -102,6 +102,11 @@ impl<K, V> Map<K, V> {
}
}

/// Like `get`, but returns both the key and the value.
pub fn get_kv<Sized? T>(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv<K> {
self.get_entry(key, |k| key.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)
Expand All @@ -113,6 +118,12 @@ impl<K, V> Map<K, V> {
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_kv_equiv<Sized? T>(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv<K> {
self.get_entry(key, |k| key.equiv(k))
}

/// Returns an iterator over the key/value pairs in the map.
///
/// Entries are retuned in an arbitrary but fixed order.
Expand Down Expand Up @@ -203,5 +214,3 @@ impl<'a, K, V> DoubleEndedIterator<&'a V> for Values<'a, K, V> {
}

impl<'a, K, V> ExactSize<&'a V> for Values<'a, K, V> {}


12 changes: 11 additions & 1 deletion phf/src/ordered_map.rs
Expand Up @@ -115,6 +115,11 @@ impl<K, V> OrderedMap<K, V> {
}
}

/// Like `get`, but returns both the key and the value.
pub fn get_kv<Sized? T>(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv<K> {
self.get_entry(key, |k| key.equiv(k)).map(|(_, r)| r)
}

/// 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)
Expand All @@ -132,6 +137,12 @@ impl<K, V> OrderedMap<K, V> {
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_kv_equiv<Sized? T>(&self, key: &T) -> Option<&(K, V)> where T: PhfHash+Equiv<K> {
self.get_entry(key, |k| key.equiv(k)).map(|(_, r)| r)
}

/// Returns an iterator over the key/value pairs in the map.
///
/// Entries are returned in the same order in which they were defined.
Expand Down Expand Up @@ -252,4 +263,3 @@ impl<'a, K, V> RandomAccessIterator<&'a V> for Values<'a, K, V> {
}

impl<'a, K, V> ExactSize<&'a V> for Values<'a, K, V> {}

0 comments on commit 7ced000

Please sign in to comment.