rust/src/libcore/iterator.rs

202 lines
5.6 KiB
Rust
Raw Normal View History

2013-04-09 09:54:32 -05:00
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Composable iterator objects
use prelude::*;
2013-04-15 09:30:16 -05:00
pub trait Iterator<A> {
2013-04-09 09:54:32 -05:00
/// Advance the iterator and return the next value. Return `None` when the end is reached.
2013-04-15 09:30:16 -05:00
fn next(&mut self) -> Option<A>;
2013-04-09 09:54:32 -05:00
}
2013-04-15 09:30:16 -05:00
pub trait IteratorUtil<A> {
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>;
// 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>;
2013-04-18 07:15:40 -05:00
fn dropwhile<'r>(self, predicate: &'r fn(&A) -> bool) -> DropWhileIterator<'r, A, Self>;
fn takewhile<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, Self>;
fn enumerate(self) -> EnumerateIterator<Self>;
2013-04-15 09:30:16 -05:00
fn advance(&mut self, f: &fn(A) -> bool);
}
impl<A, T: Iterator<A>> IteratorUtil<A> for T {
#[inline(always)]
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<T, U> {
ZipIterator{a: self, b: other}
}
// FIXME: #5898: should be called map
#[inline(always)]
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, T> {
MapIterator{iter: self, f: f}
}
#[inline(always)]
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, T> {
FilterIterator{iter: self, predicate: predicate}
}
#[inline(always)]
fn enumerate(self) -> EnumerateIterator<T> {
EnumerateIterator{iter: self, count: 0}
}
2013-04-18 07:15:40 -05:00
#[inline(always)]
fn dropwhile<'r>(self, predicate: &'r fn(&A) -> bool) -> DropWhileIterator<'r, A, T> {
DropWhileIterator{iter: self, flag: false, predicate: predicate}
}
#[inline(always)]
fn takewhile<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, T> {
TakeWhileIterator{iter: self, flag: false, predicate: predicate}
}
2013-04-15 09:30:16 -05:00
/// A shim implementing the `for` loop iteration protocol for iterator objects
#[inline]
fn advance(&mut self, f: &fn(A) -> bool) {
loop {
match self.next() {
Some(x) => {
if !f(x) { return }
}
None => return
2013-04-09 09:54:32 -05:00
}
}
}
}
pub struct ZipIterator<T, U> {
priv a: T,
priv b: U
}
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> {
#[inline]
fn next(&mut self) -> Option<(A, B)> {
match (self.a.next(), self.b.next()) {
(Some(x), Some(y)) => Some((x, y)),
_ => None
}
}
}
pub struct FilterIterator<'self, A, T> {
priv iter: T,
priv predicate: &'self fn(&A) -> bool
}
impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
#[inline]
fn next(&mut self) -> Option<A> {
2013-04-15 09:30:16 -05:00
for self.iter.advance |x| {
2013-04-09 09:54:32 -05:00
if (self.predicate)(&x) {
return Some(x);
} else {
loop
}
}
None
}
}
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
}
impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<T> {
#[inline]
fn next(&mut self) -> Option<(uint, A)> {
match self.iter.next() {
Some(a) => {
let ret = Some((self.count, a));
self.count += 1;
ret
}
_ => None
}
}
}
2013-04-18 07:15:40 -05:00
pub struct DropWhileIterator<'self, A, T> {
priv iter: T,
priv flag: bool,
priv predicate: &'self fn(&A) -> bool
}
impl<'self, A, T: Iterator<A>> Iterator<A> for DropWhileIterator<'self, A, T> {
#[inline]
fn next(&mut self) -> Option<A> {
let mut next = self.iter.next();
if self.flag {
next
} else {
loop {
match next {
Some(x) => {
if (self.predicate)(&x) {
next = self.iter.next();
loop
} else {
self.flag = true;
return Some(x)
}
}
None => return None
}
}
}
}
}
pub struct TakeWhileIterator<'self, A, T> {
priv iter: T,
priv flag: bool,
priv predicate: &'self fn(&A) -> bool
}
impl<'self, A, T: Iterator<A>> Iterator<A> for TakeWhileIterator<'self, A, T> {
#[inline]
fn next(&mut self) -> Option<A> {
if self.flag {
None
} else {
match self.iter.next() {
Some(x) => {
if (self.predicate)(&x) {
Some(x)
} else {
self.flag = true;
None
}
}
None => None
}
}
}
}