re-organize the iterator module a bit

This commit is contained in:
Daniel Micay 2013-04-19 09:18:22 -04:00
parent ae1c9ebf3c
commit d7a2ae6c42

View File

@ -22,11 +22,11 @@ pub trait IteratorUtil<A> {
// FIXME: #5898: should be called map
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, Self>;
fn enumerate(self) -> EnumerateIterator<Self>;
fn skip_while<'r>(self, predicate: &'r fn(&A) -> bool) -> SkipWhileIterator<'r, A, Self>;
fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, Self>;
fn skip(self, n: uint) -> SkipIterator<Self>;
fn take(self, n: uint) -> TakeIterator<Self>;
fn enumerate(self) -> EnumerateIterator<Self>;
fn advance(&mut self, f: &fn(A) -> bool);
}
@ -101,6 +101,21 @@ impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U
}
}
pub struct MapIterator<'self, A, B, T> {
priv iter: T,
priv f: &'self fn(A) -> B
}
impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
#[inline]
fn next(&mut self) -> Option<B> {
match self.iter.next() {
Some(a) => Some((self.f)(a)),
_ => None
}
}
}
pub struct FilterIterator<'self, A, T> {
priv iter: T,
priv predicate: &'self fn(&A) -> bool
@ -120,21 +135,6 @@ impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
}
}
pub struct MapIterator<'self, A, B, T> {
priv iter: T,
priv f: &'self fn(A) -> B
}
impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
#[inline]
fn next(&mut self) -> Option<B> {
match self.iter.next() {
Some(a) => Some((self.f)(a)),
_ => None
}
}
}
pub struct EnumerateIterator<T> {
priv iter: T,
priv count: uint