|
| 1 | +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Composable iterator objects |
| 12 | +
|
| 13 | +use prelude::*; |
| 14 | + |
| 15 | +pub trait Iterator<T> { |
| 16 | + /// Advance the iterator and return the next value. Return `None` when the end is reached. |
| 17 | + fn next(&mut self) -> Option<T>; |
| 18 | +} |
| 19 | + |
| 20 | +/// A shim implementing the `for` loop iteration protocol for iterator objects |
| 21 | +#[inline] |
| 22 | +pub fn advance<T, U: Iterator<T>>(iter: &mut U, f: &fn(T) -> bool) { |
| 23 | + loop { |
| 24 | + match iter.next() { |
| 25 | + Some(x) => { |
| 26 | + if !f(x) { return } |
| 27 | + } |
| 28 | + None => return |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +pub struct ZipIterator<T, U> { |
| 34 | + priv a: T, |
| 35 | + priv b: U |
| 36 | +} |
| 37 | + |
| 38 | +pub impl<A, B, T: Iterator<A>, U: Iterator<B>> ZipIterator<T, U> { |
| 39 | + #[inline(always)] |
| 40 | + fn new(a: T, b: U) -> ZipIterator<T, U> { |
| 41 | + ZipIterator{a: a, b: b} |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> { |
| 46 | + #[inline] |
| 47 | + fn next(&mut self) -> Option<(A, B)> { |
| 48 | + match (self.a.next(), self.b.next()) { |
| 49 | + (Some(x), Some(y)) => Some((x, y)), |
| 50 | + _ => None |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +pub struct FilterIterator<'self, A, T> { |
| 56 | + priv iter: T, |
| 57 | + priv predicate: &'self fn(&A) -> bool |
| 58 | +} |
| 59 | + |
| 60 | +pub impl<'self, A, T: Iterator<A>> FilterIterator<'self, A, T> { |
| 61 | + #[inline(always)] |
| 62 | + fn new(iter: T, predicate: &'self fn(&A) -> bool) -> FilterIterator<'self, A, T> { |
| 63 | + FilterIterator{iter: iter, predicate: predicate} |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> { |
| 68 | + #[inline] |
| 69 | + fn next(&mut self) -> Option<A> { |
| 70 | + for advance(self) |x| { |
| 71 | + if (self.predicate)(&x) { |
| 72 | + return Some(x); |
| 73 | + } else { |
| 74 | + loop |
| 75 | + } |
| 76 | + } |
| 77 | + None |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +pub struct MapIterator<'self, A, B, T> { |
| 82 | + priv iter: T, |
| 83 | + priv f: &'self fn(A) -> B |
| 84 | +} |
| 85 | + |
| 86 | +pub impl<'self, A, B, T: Iterator<A>> MapIterator<'self, A, B, T> { |
| 87 | + #[inline(always)] |
| 88 | + fn new(iter: T, f: &'self fn(A) -> B) -> MapIterator<'self, A, B, T> { |
| 89 | + MapIterator{iter: iter, f: f} |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> { |
| 94 | + #[inline] |
| 95 | + fn next(&mut self) -> Option<B> { |
| 96 | + match self.iter.next() { |
| 97 | + Some(a) => Some((self.f)(a)), |
| 98 | + _ => None |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments