Skip to content

Commit

Permalink
Add index methods to OrderedMap and OrderedSet.
Browse files Browse the repository at this point in the history
This replaces the `RandomAccessIterator` impls removed in e215273.

See servo/string-cache#80
  • Loading branch information
SimonSapin committed Apr 11, 2015
1 parent 9cb9de9 commit d2af00d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions phf/src/ordered_map.rs
Expand Up @@ -81,6 +81,12 @@ impl<K, V> OrderedMap<K, V> {
self.get_internal(key).map(|(i, _)| i)
}

/// Returns references to both the key and values at an index
/// within the list used to initialize the ordered map. See `.get_index(key)`.
pub fn index(&self, index: usize) -> Option<(&K, &V)> {
self.entries.get(index).map(|&(ref k, ref v)| (k, v))
}

/// Like `get`, but returns both the key and the value.
pub fn get_entry<T: ?Sized>(&self, key: &T) -> Option<(&K, &V)>
where T: Eq + PhfHash, K: Borrow<T> {
Expand Down
6 changes: 6 additions & 0 deletions phf/src/ordered_set.rs
Expand Up @@ -54,6 +54,12 @@ impl<T> OrderedSet<T> {
self.map.get_index(key)
}

/// Returns references to both the key and values at an index
/// within the list used to initialize the ordered map. See `.get_index(key)`.
pub fn index(&self, index: usize) -> Option<&T> {
self.map.index(index).map(|(k, &())| k)
}

/// Returns true if `value` is in the `Set`.
pub fn contains<U: ?Sized>(&self, value: &U) -> bool where U: Eq + PhfHash, T: Borrow<U> {
self.map.contains_key(value)
Expand Down

0 comments on commit d2af00d

Please sign in to comment.