Skip to content

Commit

Permalink
Implement more iterator traits for PhfMap iters
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Jul 13, 2014
1 parent b89cc89 commit 4b48972
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions phf/src/lib.rs
Expand Up @@ -185,36 +185,60 @@ impl<'a, K, V> Iterator<&'a (K, V)> for PhfMapEntries<'a, K, V> {
}
}

impl<'a, K, V> DoubleEndedIterator<&'a (K, V)> for PhfMapEntries<'a, K, V> {
fn next_back(&mut self) -> Option<&'a (K, V)> {
self.iter.next_back()
}
}

impl<'a, K, V> ExactSize<&'a (K, V)> for PhfMapEntries<'a, K, V> {}

/// An iterator over the keys in a `PhfMap`.
pub struct PhfMapKeys<'a, K, V> {
iter: PhfMapEntries<'a, K, V>,
}

impl<'a, K, V> Iterator<&'a K> for PhfMapKeys<'a, K, V> {
fn next(&mut self) -> Option<&'a K> {
self.iter.next().map(|&(ref key, _)| key)
self.iter.next().map(|&(ref k, _)| k)
}

fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}

impl<'a, K, V> DoubleEndedIterator<&'a K> for PhfMapKeys<'a, K, V> {
fn next_back(&mut self) -> Option<&'a K> {
self.iter.next_back().map(|&(ref k, _)| k)
}
}

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

/// An iterator over the values in a `PhfMap`.
pub struct PhfMapValues<'a, K, V> {
iter: PhfMapEntries<'a, K, V>,
}

impl<'a, K, V> Iterator<&'a V> for PhfMapValues<'a, K, V> {
fn next(&mut self) -> Option<&'a V> {
self.iter.next().map(|&(_, ref value)| value)
self.iter.next().map(|&(_, ref v)| v)
}

fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}

impl<'a, K, V> DoubleEndedIterator<&'a V> for PhfMapValues<'a, K, V> {
fn next_back(&mut self) -> Option<&'a V> {
self.iter.next_back().map(|&(_, ref v)| v)
}
}

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

/// An immutable set constructed at compile time.
///
/// `PhfSet`s may be created with the `phf_set` macro:
Expand Down Expand Up @@ -311,17 +335,23 @@ pub struct PhfSetValues<'a, T> {
}

impl<'a, T> Iterator<&'a T> for PhfSetValues<'a, T> {
#[inline]
fn next(&mut self) -> Option<&'a T> {
self.iter.next()
}

#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}

impl<'a, T> DoubleEndedIterator<&'a T> for PhfSetValues<'a, T> {
fn next_back(&mut self) -> Option<&'a T> {
self.iter.next_back()
}
}

impl<'a, T> ExactSize<&'a T> for PhfSetValues<'a, T> {}

/// An order-preserving immutable map constructed at compile time.
///
/// Unlike a `PhfMap`, the order of entries in a `PhfOrderedMap` is guaranteed
Expand Down

0 comments on commit 4b48972

Please sign in to comment.