// 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 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! Composable iterator objects use prelude::*; pub trait Iterator { /// Advance the iterator and return the next value. Return `None` when the end is reached. fn next(&mut self) -> Option; } pub trait IteratorUtil { fn zip>(self, other: U) -> ZipIterator; // 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 advance(&mut self, f: &fn(A) -> bool); } impl> IteratorUtil for T { #[inline(always)] fn zip>(self, other: U) -> ZipIterator { 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} } /// 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 } } } } pub struct ZipIterator { priv a: T, priv b: U } impl, U: Iterator> Iterator<(A, B)> for ZipIterator { #[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> Iterator for FilterIterator<'self, A, T> { #[inline] fn next(&mut self) -> Option { for self.iter.advance |x| { 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> Iterator for MapIterator<'self, A, B, T> { #[inline] fn next(&mut self) -> Option { match self.iter.next() { Some(a) => Some((self.f)(a)), _ => None } } }