From d2af00d4e32412d6f6b7597786976c1a0b642956 Mon Sep 17 00:00:00 2001 From: Simon Sapin Date: Sat, 11 Apr 2015 08:27:15 +0200 Subject: [PATCH] Add `index` methods to `OrderedMap` and `OrderedSet`. This replaces the `RandomAccessIterator` impls removed in e2152739cbdd471116d88bb4a9cea4cdfede1e42. See https://github.com/servo/string-cache/issues/80 --- phf/src/ordered_map.rs | 6 ++++++ phf/src/ordered_set.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/phf/src/ordered_map.rs b/phf/src/ordered_map.rs index afbecc8a..9600ff04 100644 --- a/phf/src/ordered_map.rs +++ b/phf/src/ordered_map.rs @@ -81,6 +81,12 @@ impl OrderedMap { 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(&self, key: &T) -> Option<(&K, &V)> where T: Eq + PhfHash, K: Borrow { diff --git a/phf/src/ordered_set.rs b/phf/src/ordered_set.rs index 44fcbba1..4b4f4d8e 100644 --- a/phf/src/ordered_set.rs +++ b/phf/src/ordered_set.rs @@ -54,6 +54,12 @@ impl OrderedSet { 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(&self, value: &U) -> bool where U: Eq + PhfHash, T: Borrow { self.map.contains_key(value)