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.
|
|
|
|
|
2013-04-19 11:17:24 -05:00
|
|
|
/*! Composable external iterators
|
|
|
|
|
|
|
|
The `Iterator` trait defines an interface for objects which implement iteration as a state machine.
|
|
|
|
|
|
|
|
Algorithms like `zip` are provided as `Iterator` implementations which wrap other objects
|
|
|
|
implementing the `Iterator` trait.
|
|
|
|
|
|
|
|
*/
|
2013-04-09 09:54:32 -05:00
|
|
|
|
2013-06-21 05:12:01 -05:00
|
|
|
#[allow(default_methods)]; // solid enough for the use case here
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use cmp;
|
2013-06-24 00:21:32 -05:00
|
|
|
use iter::Times;
|
2013-05-17 10:18:09 -05:00
|
|
|
use num::{Zero, One};
|
2013-06-06 15:34:50 -05:00
|
|
|
use option::{Option, Some, None};
|
|
|
|
use ops::{Add, Mul};
|
2013-06-18 21:59:48 -05:00
|
|
|
use cmp::Ord;
|
2013-06-06 15:34:50 -05:00
|
|
|
use clone::Clone;
|
2013-04-09 09:54:32 -05:00
|
|
|
|
2013-06-21 06:57:22 -05:00
|
|
|
/// Conversion from an `Iterator`
|
|
|
|
pub trait FromIterator<A, T: Iterator<A>> {
|
|
|
|
/// Build a container with elements from an external iterator.
|
|
|
|
pub fn from_iterator(iterator: &mut T) -> Self;
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An interface for dealing with "external iterators". These types of iterators
|
|
|
|
/// can be resumed at any time as all state is stored internally as opposed to
|
|
|
|
/// being located on the call stack.
|
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-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
/// Return a lower bound and upper bound on the remaining length of the iterator.
|
|
|
|
///
|
|
|
|
/// The common use case for the estimate is pre-allocating space to store the results.
|
|
|
|
fn size_hint(&self) -> (Option<uint>, Option<uint>) { (None, None) }
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 11:17:24 -05:00
|
|
|
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
|
|
|
|
/// implementations of the `Iterator` trait.
|
|
|
|
///
|
|
|
|
/// In the future these will be default methods instead of a utility trait.
|
2013-04-15 09:30:16 -05:00
|
|
|
pub trait IteratorUtil<A> {
|
2013-06-11 18:34:03 -05:00
|
|
|
/// Chain this iterator with another, returning a new iterator which will
|
2013-05-28 16:35:52 -05:00
|
|
|
/// finish iterating over the current iterator, and then it will iterate
|
|
|
|
/// over the other specified iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [0];
|
|
|
|
/// let b = [1];
|
2013-06-10 16:50:12 -05:00
|
|
|
/// let mut it = a.iter().chain_(b.iter());
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert_eq!(it.next().get(), &0);
|
|
|
|
/// assert_eq!(it.next().get(), &1);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-06-10 16:50:12 -05:00
|
|
|
fn chain_<U: Iterator<A>>(self, other: U) -> ChainIterator<A, Self, U>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Creates an iterator which iterates over both this and the specified
|
|
|
|
/// iterators simultaneously, yielding the two elements as pairs. When
|
|
|
|
/// either iterator returns None, all further invocations of next() will
|
|
|
|
/// return None.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [0];
|
|
|
|
/// let b = [1];
|
|
|
|
/// let mut it = a.iter().zip(b.iter());
|
|
|
|
/// assert_eq!(it.next().get(), (&0, &1));
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-06-07 08:42:35 -05:00
|
|
|
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<A, Self, B, U>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-04-15 09:30:16 -05:00
|
|
|
// FIXME: #5898: should be called map
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Creates a new iterator which will apply the specified function to each
|
|
|
|
/// element returned by the first, yielding the mapped element instead. This
|
|
|
|
/// similar to the `vec::map` function.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2];
|
|
|
|
/// let mut it = a.iter().transform(|&x| 2 * x);
|
|
|
|
/// assert_eq!(it.next().get(), 2);
|
|
|
|
/// assert_eq!(it.next().get(), 4);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-04-15 09:30:16 -05:00
|
|
|
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Creates an iterator which applies the predicate to each element returned
|
|
|
|
/// by this iterator. Only elements which have the predicate evaluate to
|
|
|
|
/// `true` will be yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2];
|
|
|
|
/// let mut it = a.iter().filter(|&x| *x > 1);
|
|
|
|
/// assert_eq!(it.next().get(), &2);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-04-15 09:30:16 -05:00
|
|
|
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-06-11 18:34:03 -05:00
|
|
|
/// Creates an iterator which both filters and maps elements.
|
2013-05-28 16:35:52 -05:00
|
|
|
/// If the specified function returns None, the element is skipped.
|
|
|
|
/// Otherwise the option is unwrapped and the new value is yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2];
|
|
|
|
/// let mut it = a.iter().filter_map(|&x| if x > 1 {Some(2 * x)} else {None});
|
|
|
|
/// assert_eq!(it.next().get(), 4);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-05-17 09:00:48 -05:00
|
|
|
fn filter_map<'r, B>(self, f: &'r fn(A) -> Option<B>) -> FilterMapIterator<'r, A, B, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Creates an iterator which yields a pair of the value returned by this
|
|
|
|
/// iterator plus the current index of iteration.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [100, 200];
|
|
|
|
/// let mut it = a.iter().enumerate();
|
|
|
|
/// assert_eq!(it.next().get(), (0, &100));
|
|
|
|
/// assert_eq!(it.next().get(), (1, &200));
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-06-07 08:42:35 -05:00
|
|
|
fn enumerate(self) -> EnumerateIterator<A, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Creates an iterator which invokes the predicate on elements until it
|
2013-06-11 18:34:03 -05:00
|
|
|
/// returns false. Once the predicate returns false, all further elements are
|
2013-05-28 16:35:52 -05:00
|
|
|
/// yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 2, 1];
|
|
|
|
/// let mut it = a.iter().skip_while(|&a| *a < 3);
|
|
|
|
/// assert_eq!(it.next().get(), &3);
|
|
|
|
/// assert_eq!(it.next().get(), &2);
|
|
|
|
/// assert_eq!(it.next().get(), &1);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-04-19 06:28:51 -05:00
|
|
|
fn skip_while<'r>(self, predicate: &'r fn(&A) -> bool) -> SkipWhileIterator<'r, A, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Creates an iterator which yields elements so long as the predicate
|
|
|
|
/// returns true. After the predicate returns false for the first time, no
|
|
|
|
/// further elements will be yielded.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 2, 1];
|
|
|
|
/// let mut it = a.iter().take_while(|&a| *a < 3);
|
|
|
|
/// assert_eq!(it.next().get(), &1);
|
|
|
|
/// assert_eq!(it.next().get(), &2);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-04-19 06:28:51 -05:00
|
|
|
fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Creates an iterator which skips the first `n` elements of this iterator,
|
|
|
|
/// and then it yields all further items.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// let mut it = a.iter().skip(3);
|
|
|
|
/// assert_eq!(it.next().get(), &4);
|
|
|
|
/// assert_eq!(it.next().get(), &5);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-06-06 01:39:25 -05:00
|
|
|
fn skip(self, n: uint) -> SkipIterator<A, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-06-10 16:50:12 -05:00
|
|
|
// FIXME: #5898: should be called take
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Creates an iterator which yields the first `n` elements of this
|
|
|
|
/// iterator, and then it will always return None.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
2013-06-10 16:50:12 -05:00
|
|
|
/// let mut it = a.iter().take_(3);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// assert_eq!(it.next().get(), &1);
|
|
|
|
/// assert_eq!(it.next().get(), &2);
|
|
|
|
/// assert_eq!(it.next().get(), &3);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-06-10 16:50:12 -05:00
|
|
|
fn take_(self, n: uint) -> TakeIterator<A, Self>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Creates a new iterator which behaves in a similar fashion to foldl.
|
|
|
|
/// There is a state which is passed between each iteration and can be
|
|
|
|
/// mutated as necessary. The yielded values from the closure are yielded
|
|
|
|
/// from the ScanIterator instance when not None.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// let mut it = a.iter().scan(1, |fac, &x| {
|
|
|
|
/// *fac = *fac * x;
|
|
|
|
/// Some(*fac)
|
|
|
|
/// });
|
|
|
|
/// assert_eq!(it.next().get(), 1);
|
|
|
|
/// assert_eq!(it.next().get(), 2);
|
|
|
|
/// assert_eq!(it.next().get(), 6);
|
|
|
|
/// assert_eq!(it.next().get(), 24);
|
|
|
|
/// assert_eq!(it.next().get(), 120);
|
|
|
|
/// assert!(it.next().is_none());
|
|
|
|
/// ~~~
|
2013-04-19 22:32:27 -05:00
|
|
|
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
|
|
|
|
-> ScanIterator<'r, A, B, Self, St>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-06-23 19:34:38 -05:00
|
|
|
/// Creates an iterator that maps each element to an iterator,
|
|
|
|
/// and yields the elements of the produced iterators
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let xs = [2u, 3];
|
|
|
|
/// let ys = [0u, 1, 0, 1, 2];
|
|
|
|
/// let mut it = xs.iter().flat_map_(|&x| Counter::new(0u, 1).take_(x));
|
|
|
|
/// // Check that `it` has the same elements as `ys`
|
|
|
|
/// let mut i = 0;
|
|
|
|
/// for it.advance |x: uint| {
|
|
|
|
/// assert_eq!(x, ys[i]);
|
|
|
|
/// i += 1;
|
|
|
|
/// }
|
|
|
|
/// ~~~
|
|
|
|
// FIXME: #5898: should be called `flat_map`
|
|
|
|
fn flat_map_<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
|
|
|
|
-> FlatMapIterator<'r, A, B, Self, U>;
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An adaptation of an external iterator to the for-loop protocol of rust.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
2013-06-11 22:54:05 -05:00
|
|
|
/// use std::iterator::Counter;
|
|
|
|
///
|
2013-05-28 16:35:52 -05:00
|
|
|
/// for Counter::new(0, 10).advance |i| {
|
|
|
|
/// io::println(fmt!("%d", i));
|
|
|
|
/// }
|
|
|
|
/// ~~~
|
2013-05-02 17:33:18 -05:00
|
|
|
fn advance(&mut self, f: &fn(A) -> bool) -> bool;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-06-03 16:48:52 -05:00
|
|
|
/// Loops through the entire iterator, collecting all of the elements into
|
2013-06-24 00:21:32 -05:00
|
|
|
/// a container implementing `FromIterator`.
|
2013-06-03 16:48:52 -05:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// let b: ~[int] = a.iter().transform(|&x| x).collect();
|
|
|
|
/// assert!(a == b);
|
|
|
|
/// ~~~
|
2013-06-24 00:21:32 -05:00
|
|
|
fn collect<B: FromIterator<A, Self>>(&mut self) -> B;
|
2013-06-03 16:48:52 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Loops through `n` iterations, returning the `n`th element of the
|
|
|
|
/// iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// let mut it = a.iter();
|
|
|
|
/// assert!(it.nth(2).get() == &3);
|
|
|
|
/// assert!(it.nth(2) == None);
|
|
|
|
/// ~~~
|
2013-05-18 03:27:58 -05:00
|
|
|
fn nth(&mut self, n: uint) -> Option<A>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Loops through the entire iterator, returning the last element of the
|
|
|
|
/// iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// assert!(a.iter().last().get() == &5);
|
|
|
|
/// ~~~
|
2013-06-07 13:45:31 -05:00
|
|
|
// FIXME: #5898: should be called `last`
|
|
|
|
fn last_(&mut self) -> Option<A>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Performs a fold operation over the entire iterator, returning the
|
|
|
|
/// eventual state at the end of the iteration.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// assert!(a.iter().fold(0, |a, &b| a + b) == 15);
|
|
|
|
/// ~~~
|
2013-05-17 09:54:58 -05:00
|
|
|
fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
2013-06-17 18:43:22 -05:00
|
|
|
// FIXME: #5898: should be called len
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Counts the number of elements in this iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// let mut it = a.iter();
|
2013-06-17 18:43:22 -05:00
|
|
|
/// assert!(it.len_() == 5);
|
|
|
|
/// assert!(it.len_() == 0);
|
2013-05-28 16:35:52 -05:00
|
|
|
/// ~~~
|
2013-06-17 18:43:22 -05:00
|
|
|
fn len_(&mut self) -> uint;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Tests whether the predicate holds true for all elements in the iterator.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// assert!(a.iter().all(|&x| *x > 0));
|
|
|
|
/// assert!(!a.iter().all(|&x| *x > 2));
|
|
|
|
/// ~~~
|
2013-06-07 23:07:55 -05:00
|
|
|
fn all(&mut self, f: &fn(A) -> bool) -> bool;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Tests whether any element of an iterator satisfies the specified
|
|
|
|
/// predicate.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// let mut it = a.iter();
|
2013-06-10 16:50:12 -05:00
|
|
|
/// assert!(it.any_(|&x| *x == 3));
|
|
|
|
/// assert!(!it.any_(|&x| *x == 3));
|
2013-05-28 16:35:52 -05:00
|
|
|
/// ~~~
|
2013-06-10 16:50:12 -05:00
|
|
|
fn any_(&mut self, f: &fn(A) -> bool) -> bool;
|
2013-06-15 16:42:31 -05:00
|
|
|
|
|
|
|
/// Return the first element satisfying the specified predicate
|
2013-06-15 17:02:05 -05:00
|
|
|
fn find_(&mut self, predicate: &fn(&A) -> bool) -> Option<A>;
|
2013-06-15 16:56:26 -05:00
|
|
|
|
|
|
|
/// Return the index of the first element satisfying the specified predicate
|
2013-06-15 17:02:05 -05:00
|
|
|
fn position_(&mut self, predicate: &fn(A) -> bool) -> Option<uint>;
|
2013-06-17 18:43:22 -05:00
|
|
|
|
|
|
|
/// Count the number of elements satisfying the specified predicate
|
|
|
|
fn count(&mut self, predicate: &fn(A) -> bool) -> uint;
|
2013-06-26 16:51:04 -05:00
|
|
|
|
|
|
|
/// Return the element that gives the maximum value from the specfied function
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-06-29 01:43:51 -05:00
|
|
|
/// ~~~ {.rust}
|
2013-06-26 16:51:04 -05:00
|
|
|
/// let xs = [-3, 0, 1, 5, -10];
|
|
|
|
/// assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
|
2013-06-29 01:43:51 -05:00
|
|
|
/// ~~~
|
2013-06-26 16:51:04 -05:00
|
|
|
fn max_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A>;
|
|
|
|
|
|
|
|
/// Return the element that gives the minimum value from the specfied function
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2013-06-29 01:43:51 -05:00
|
|
|
/// ~~~ {.rust}
|
2013-06-26 16:51:04 -05:00
|
|
|
/// let xs = [-3, 0, 1, 5, -10];
|
|
|
|
/// assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
|
2013-06-29 01:43:51 -05:00
|
|
|
/// ~~~
|
2013-06-26 16:51:04 -05:00
|
|
|
fn min_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A>;
|
2013-04-15 09:30:16 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 11:17:24 -05:00
|
|
|
/// Iterator adaptors provided for every `Iterator` implementation. The adaptor objects are also
|
|
|
|
/// implementations of the `Iterator` trait.
|
|
|
|
///
|
|
|
|
/// In the future these will be default methods instead of a utility trait.
|
2013-04-15 09:30:16 -05:00
|
|
|
impl<A, T: Iterator<A>> IteratorUtil<A> for T {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-10 16:50:12 -05:00
|
|
|
fn chain_<U: Iterator<A>>(self, other: U) -> ChainIterator<A, T, U> {
|
2013-04-19 10:29:38 -05:00
|
|
|
ChainIterator{a: self, b: other, flag: false}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-07 08:42:35 -05:00
|
|
|
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<A, T, B, U> {
|
2013-04-15 09:30:16 -05:00
|
|
|
ZipIterator{a: self, b: other}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: #5898: should be called map
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-15 09:30:16 -05:00
|
|
|
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, T> {
|
|
|
|
MapIterator{iter: self, f: f}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-15 09:30:16 -05:00
|
|
|
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, T> {
|
|
|
|
FilterIterator{iter: self, predicate: predicate}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-17 09:00:48 -05:00
|
|
|
fn filter_map<'r, B>(self, f: &'r fn(A) -> Option<B>) -> FilterMapIterator<'r, A, B, T> {
|
|
|
|
FilterMapIterator { iter: self, f: f }
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-07 08:42:35 -05:00
|
|
|
fn enumerate(self) -> EnumerateIterator<A, T> {
|
2013-04-18 04:34:07 -05:00
|
|
|
EnumerateIterator{iter: self, count: 0}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-19 06:28:51 -05:00
|
|
|
fn skip_while<'r>(self, predicate: &'r fn(&A) -> bool) -> SkipWhileIterator<'r, A, T> {
|
|
|
|
SkipWhileIterator{iter: self, flag: false, predicate: predicate}
|
2013-04-18 07:15:40 -05:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-19 06:28:51 -05:00
|
|
|
fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, T> {
|
2013-04-18 07:15:40 -05:00
|
|
|
TakeWhileIterator{iter: self, flag: false, predicate: predicate}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-06 01:39:25 -05:00
|
|
|
fn skip(self, n: uint) -> SkipIterator<A, T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
SkipIterator{iter: self, n: n}
|
|
|
|
}
|
|
|
|
|
2013-06-10 16:50:12 -05:00
|
|
|
// FIXME: #5898: should be called take
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-10 16:50:12 -05:00
|
|
|
fn take_(self, n: uint) -> TakeIterator<A, T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
TakeIterator{iter: self, n: n}
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-19 22:32:27 -05:00
|
|
|
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
|
|
|
|
-> ScanIterator<'r, A, B, T, St> {
|
|
|
|
ScanIterator{iter: self, f: f, state: initial_state}
|
|
|
|
}
|
|
|
|
|
2013-06-23 19:34:38 -05:00
|
|
|
#[inline]
|
|
|
|
fn flat_map_<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
|
|
|
|
-> FlatMapIterator<'r, A, B, T, U> {
|
|
|
|
FlatMapIterator{iter: self, f: f, subiter: None }
|
|
|
|
}
|
|
|
|
|
2013-04-15 09:30:16 -05:00
|
|
|
/// A shim implementing the `for` loop iteration protocol for iterator objects
|
|
|
|
#[inline]
|
2013-05-02 17:33:18 -05:00
|
|
|
fn advance(&mut self, f: &fn(A) -> bool) -> bool {
|
|
|
|
loop {
|
|
|
|
match self.next() {
|
|
|
|
Some(x) => {
|
|
|
|
if !f(x) { return false; }
|
|
|
|
}
|
|
|
|
None => { return true; }
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-17 08:54:32 -05:00
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-24 00:21:32 -05:00
|
|
|
fn collect<B: FromIterator<A, T>>(&mut self) -> B {
|
|
|
|
FromIterator::from_iterator(self)
|
2013-06-03 16:48:52 -05:00
|
|
|
}
|
|
|
|
|
2013-05-18 03:27:58 -05:00
|
|
|
/// Return the `n`th item yielded by an iterator.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-18 03:27:58 -05:00
|
|
|
fn nth(&mut self, mut n: uint) -> Option<A> {
|
2013-05-17 09:45:25 -05:00
|
|
|
loop {
|
|
|
|
match self.next() {
|
2013-05-18 03:27:58 -05:00
|
|
|
Some(x) => if n == 0 { return Some(x) },
|
|
|
|
None => return None
|
2013-05-17 09:45:25 -05:00
|
|
|
}
|
2013-05-18 03:27:58 -05:00
|
|
|
n -= 1;
|
2013-05-17 09:45:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-18 03:27:58 -05:00
|
|
|
/// Return the last item yielded by an iterator.
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-07 13:45:31 -05:00
|
|
|
fn last_(&mut self) -> Option<A> {
|
2013-05-18 03:27:58 -05:00
|
|
|
let mut last = None;
|
|
|
|
for self.advance |x| { last = Some(x); }
|
|
|
|
last
|
2013-05-17 09:45:25 -05:00
|
|
|
}
|
2013-05-17 09:54:58 -05:00
|
|
|
|
|
|
|
/// Reduce an iterator to an accumulated value
|
|
|
|
#[inline]
|
|
|
|
fn fold<B>(&mut self, init: B, f: &fn(B, A) -> B) -> B {
|
|
|
|
let mut accum = init;
|
|
|
|
loop {
|
|
|
|
match self.next() {
|
|
|
|
Some(x) => { accum = f(accum, x); }
|
|
|
|
None => { break; }
|
|
|
|
}
|
|
|
|
}
|
2013-06-15 16:42:31 -05:00
|
|
|
accum
|
2013-05-17 09:54:58 -05:00
|
|
|
}
|
|
|
|
|
2013-05-18 03:44:44 -05:00
|
|
|
/// Count the number of items yielded by an iterator
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-17 18:43:22 -05:00
|
|
|
fn len_(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) }
|
2013-05-17 10:24:43 -05:00
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-07 23:07:55 -05:00
|
|
|
fn all(&mut self, f: &fn(A) -> bool) -> bool {
|
|
|
|
for self.advance |x| { if !f(x) { return false; } }
|
2013-06-15 16:42:31 -05:00
|
|
|
true
|
2013-05-17 10:24:43 -05:00
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-10 16:50:12 -05:00
|
|
|
fn any_(&mut self, f: &fn(A) -> bool) -> bool {
|
2013-06-07 23:07:55 -05:00
|
|
|
for self.advance |x| { if f(x) { return true; } }
|
2013-06-15 16:42:31 -05:00
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the first element satisfying the specified predicate
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-15 17:02:05 -05:00
|
|
|
fn find_(&mut self, predicate: &fn(&A) -> bool) -> Option<A> {
|
2013-06-15 16:42:31 -05:00
|
|
|
for self.advance |x| {
|
|
|
|
if predicate(&x) { return Some(x) }
|
|
|
|
}
|
|
|
|
None
|
2013-05-17 10:24:43 -05:00
|
|
|
}
|
2013-06-15 16:56:26 -05:00
|
|
|
|
|
|
|
/// Return the index of the first element satisfying the specified predicate
|
|
|
|
#[inline]
|
2013-06-15 17:02:05 -05:00
|
|
|
fn position_(&mut self, predicate: &fn(A) -> bool) -> Option<uint> {
|
2013-06-15 16:56:26 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for self.advance |x| {
|
|
|
|
if predicate(x) {
|
|
|
|
return Some(i);
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2013-06-17 18:43:22 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn count(&mut self, predicate: &fn(A) -> bool) -> uint {
|
|
|
|
let mut i = 0;
|
|
|
|
for self.advance |x| {
|
|
|
|
if predicate(x) { i += 1 }
|
|
|
|
}
|
|
|
|
i
|
|
|
|
}
|
2013-06-26 16:51:04 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn max_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A> {
|
|
|
|
self.fold(None, |max: Option<(A, B)>, x| {
|
|
|
|
let x_val = f(&x);
|
|
|
|
match max {
|
|
|
|
None => Some((x, x_val)),
|
|
|
|
Some((y, y_val)) => if x_val > y_val {
|
|
|
|
Some((x, x_val))
|
|
|
|
} else {
|
|
|
|
Some((y, y_val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).map_consume(|(x, _)| x)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn min_by<B: Ord>(&mut self, f: &fn(&A) -> B) -> Option<A> {
|
|
|
|
self.fold(None, |min: Option<(A, B)>, x| {
|
|
|
|
let x_val = f(&x);
|
|
|
|
match min {
|
|
|
|
None => Some((x, x_val)),
|
|
|
|
Some((y, y_val)) => if x_val < y_val {
|
|
|
|
Some((x, x_val))
|
|
|
|
} else {
|
|
|
|
Some((y, y_val))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).map_consume(|(x, _)| x)
|
|
|
|
}
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A trait for iterators over elements which can be added together
|
2013-05-17 10:18:09 -05:00
|
|
|
pub trait AdditiveIterator<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Iterates over the entire iterator, summing up all the elements
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// let mut it = a.iter().transform(|&x| x);
|
|
|
|
/// assert!(it.sum() == 15);
|
|
|
|
/// ~~~
|
2013-05-17 10:18:09 -05:00
|
|
|
fn sum(&mut self) -> A;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Add<A, A> + Zero, T: Iterator<A>> AdditiveIterator<A> for T {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-17 10:18:09 -05:00
|
|
|
fn sum(&mut self) -> A { self.fold(Zero::zero::<A>(), |s, x| s + x) }
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A trait for iterators over elements whose elements can be multiplied
|
|
|
|
/// together.
|
2013-05-17 10:18:09 -05:00
|
|
|
pub trait MultiplicativeIterator<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Iterates over the entire iterator, multiplying all the elements
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
2013-06-11 22:54:05 -05:00
|
|
|
/// use std::iterator::Counter;
|
2013-05-28 16:35:52 -05:00
|
|
|
///
|
|
|
|
/// fn factorial(n: uint) -> uint {
|
|
|
|
/// Counter::new(1u, 1).take_while(|&i| i <= n).product()
|
|
|
|
/// }
|
|
|
|
/// assert!(factorial(0) == 1);
|
|
|
|
/// assert!(factorial(1) == 1);
|
|
|
|
/// assert!(factorial(5) == 120);
|
|
|
|
/// ~~~
|
2013-05-17 10:18:09 -05:00
|
|
|
fn product(&mut self) -> A;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Mul<A, A> + One, T: Iterator<A>> MultiplicativeIterator<A> for T {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-17 10:18:09 -05:00
|
|
|
fn product(&mut self) -> A { self.fold(One::one::<A>(), |p, x| p * x) }
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// A trait for iterators over elements which can be compared to one another.
|
|
|
|
/// The type of each element must ascribe to the `Ord` trait.
|
2013-05-17 10:18:09 -05:00
|
|
|
pub trait OrdIterator<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Consumes the entire iterator to return the maximum element.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// assert!(a.iter().max().get() == &5);
|
|
|
|
/// ~~~
|
2013-05-17 10:18:09 -05:00
|
|
|
fn max(&mut self) -> Option<A>;
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// Consumes the entire iterator to return the minimum element.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ~~~ {.rust}
|
|
|
|
/// let a = [1, 2, 3, 4, 5];
|
|
|
|
/// assert!(a.iter().min().get() == &1);
|
|
|
|
/// ~~~
|
2013-05-17 10:18:09 -05:00
|
|
|
fn min(&mut self) -> Option<A>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Ord, T: Iterator<A>> OrdIterator<A> for T {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-17 10:18:09 -05:00
|
|
|
fn max(&mut self) -> Option<A> {
|
|
|
|
self.fold(None, |max, x| {
|
|
|
|
match max {
|
|
|
|
None => Some(x),
|
|
|
|
Some(y) => Some(cmp::max(x, y))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-17 10:18:09 -05:00
|
|
|
fn min(&mut self) -> Option<A> {
|
|
|
|
self.fold(None, |min, x| {
|
|
|
|
match min {
|
|
|
|
None => Some(x),
|
|
|
|
Some(y) => Some(cmp::min(x, y))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which strings two iterators together
|
2013-06-07 08:42:35 -05:00
|
|
|
// FIXME #6967: Dummy A parameter to get around type inference bug
|
|
|
|
pub struct ChainIterator<A, T, U> {
|
2013-04-19 10:29:38 -05:00
|
|
|
priv a: T,
|
2013-05-01 21:08:33 -05:00
|
|
|
priv b: U,
|
2013-04-19 10:29:38 -05:00
|
|
|
priv flag: bool
|
|
|
|
}
|
|
|
|
|
2013-06-07 08:42:35 -05:00
|
|
|
impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> {
|
2013-04-19 10:29:38 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
if self.flag {
|
|
|
|
self.b.next()
|
|
|
|
} else {
|
|
|
|
match self.a.next() {
|
|
|
|
Some(x) => return Some(x),
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
self.flag = true;
|
|
|
|
self.b.next()
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
|
|
|
|
let (a_lower, a_upper) = self.a.size_hint();
|
|
|
|
let (b_lower, b_upper) = self.b.size_hint();
|
|
|
|
|
|
|
|
let lower = match (a_lower, b_lower) {
|
|
|
|
(Some(x), Some(y)) => Some(x + y),
|
|
|
|
(Some(x), None) => Some(x),
|
|
|
|
(None, Some(y)) => Some(y),
|
|
|
|
(None, None) => None
|
|
|
|
};
|
|
|
|
|
|
|
|
let upper = match (a_upper, b_upper) {
|
|
|
|
(Some(x), Some(y)) => Some(x + y),
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
|
|
|
|
(lower, upper)
|
|
|
|
}
|
2013-04-19 10:29:38 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which iterates two other iterators simultaneously
|
2013-06-07 08:42:35 -05:00
|
|
|
// FIXME #6967: Dummy A & B parameters to get around type inference bug
|
|
|
|
pub struct ZipIterator<A, T, B, U> {
|
2013-04-09 09:54:32 -05:00
|
|
|
priv a: T,
|
|
|
|
priv b: U
|
|
|
|
}
|
|
|
|
|
2013-06-07 08:42:35 -05:00
|
|
|
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<A, T, B, U> {
|
2013-04-09 09:54:32 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<(A, B)> {
|
|
|
|
match (self.a.next(), self.b.next()) {
|
|
|
|
(Some(x), Some(y)) => Some((x, y)),
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which maps the values of `iter` with `f`
|
2013-04-19 08:18:22 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
|
|
|
|
self.iter.size_hint()
|
|
|
|
}
|
2013-04-19 08:18:22 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which filters the elements of `iter` with `predicate`
|
2013-04-09 09:54:32 -05:00
|
|
|
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
|
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
|
|
|
|
let (_, upper) = self.iter.size_hint();
|
|
|
|
(None, upper) // can't know a lower bound, due to the predicate
|
|
|
|
}
|
2013-04-09 09:54:32 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which uses `f` to both filter and map elements from `iter`
|
2013-05-17 09:00:48 -05:00
|
|
|
pub struct FilterMapIterator<'self, A, B, T> {
|
|
|
|
priv iter: T,
|
|
|
|
priv f: &'self fn(A) -> Option<B>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B, T> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<B> {
|
2013-05-18 03:48:22 -05:00
|
|
|
for self.iter.advance |x| {
|
|
|
|
match (self.f)(x) {
|
|
|
|
Some(y) => return Some(y),
|
|
|
|
None => ()
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
|
|
|
}
|
2013-05-18 03:48:22 -05:00
|
|
|
None
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
2013-06-21 05:12:01 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (Option<uint>, Option<uint>) {
|
|
|
|
let (_, upper) = self.iter.size_hint();
|
|
|
|
(None, upper) // can't know a lower bound, due to the predicate
|
|
|
|
}
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which yields the current count and the element during iteration
|
2013-06-07 08:42:35 -05:00
|
|
|
// FIXME #6967: Dummy A parameter to get around type inference bug
|
|
|
|
pub struct EnumerateIterator<A, T> {
|
2013-04-18 04:34:07 -05:00
|
|
|
priv iter: T,
|
|
|
|
priv count: uint
|
|
|
|
}
|
|
|
|
|
2013-06-07 08:42:35 -05:00
|
|
|
impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<A, T> {
|
2013-04-18 04:34:07 -05:00
|
|
|
#[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
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which rejects elements while `predicate` is true
|
2013-04-19 06:28:51 -05:00
|
|
|
pub struct SkipWhileIterator<'self, A, T> {
|
2013-04-18 07:15:40 -05:00
|
|
|
priv iter: T,
|
|
|
|
priv flag: bool,
|
|
|
|
priv predicate: &'self fn(&A) -> bool
|
|
|
|
}
|
|
|
|
|
2013-04-19 06:28:51 -05:00
|
|
|
impl<'self, A, T: Iterator<A>> Iterator<A> for SkipWhileIterator<'self, A, T> {
|
2013-04-18 07:15:40 -05:00
|
|
|
#[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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which only accepts elements while `predicate` is true
|
2013-04-18 07:15:40 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 05:06:33 -05:00
|
|
|
|
2013-06-06 01:39:25 -05:00
|
|
|
/// An iterator which skips over `n` elements of `iter`.
|
|
|
|
// FIXME #6967: Dummy A parameter to get around type inference bug
|
|
|
|
pub struct SkipIterator<A, T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
priv iter: T,
|
|
|
|
priv n: uint
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:39:25 -05:00
|
|
|
impl<A, T: Iterator<A>> Iterator<A> for SkipIterator<A, T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
let mut next = self.iter.next();
|
|
|
|
if self.n == 0 {
|
|
|
|
next
|
|
|
|
} else {
|
|
|
|
let n = self.n;
|
|
|
|
for n.times {
|
|
|
|
match next {
|
|
|
|
Some(_) => {
|
|
|
|
next = self.iter.next();
|
|
|
|
loop
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
self.n = 0;
|
|
|
|
return None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.n = 0;
|
|
|
|
next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which only iterates over the first `n` iterations of `iter`.
|
2013-06-06 01:39:25 -05:00
|
|
|
// FIXME #6967: Dummy A parameter to get around type inference bug
|
|
|
|
pub struct TakeIterator<A, T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
priv iter: T,
|
|
|
|
priv n: uint
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:39:25 -05:00
|
|
|
impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<A, T> {
|
2013-04-19 05:06:33 -05:00
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
let next = self.iter.next();
|
|
|
|
if self.n != 0 {
|
|
|
|
self.n -= 1;
|
|
|
|
next
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-19 06:30:22 -05:00
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator to maintain state while iterating another iterator
|
2013-04-24 18:54:13 -05:00
|
|
|
pub struct ScanIterator<'self, A, B, T, St> {
|
|
|
|
priv iter: T,
|
|
|
|
priv f: &'self fn(&mut St, A) -> Option<B>,
|
2013-05-28 16:35:52 -05:00
|
|
|
|
|
|
|
/// The current internal state to be passed to the closure next.
|
2013-04-24 18:54:13 -05:00
|
|
|
state: St
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for ScanIterator<'self, A, B, T, St> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<B> {
|
|
|
|
self.iter.next().chain(|a| (self.f)(&mut self.state, a))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 19:34:38 -05:00
|
|
|
/// An iterator that maps each element to an iterator,
|
|
|
|
/// and yields the elements of the produced iterators
|
|
|
|
///
|
|
|
|
// FIXME #6967: Dummy B parameter to get around type inference bug
|
|
|
|
pub struct FlatMapIterator<'self, A, B, T, U> {
|
|
|
|
priv iter: T,
|
|
|
|
priv f: &'self fn(A) -> U,
|
|
|
|
priv subiter: Option<U>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
|
|
|
|
FlatMapIterator<'self, A, B, T, U> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<B> {
|
|
|
|
loop {
|
|
|
|
for self.subiter.mut_iter().advance |inner| {
|
|
|
|
for inner.advance |x| {
|
|
|
|
return Some(x)
|
|
|
|
}
|
|
|
|
}
|
2013-06-21 19:08:35 -05:00
|
|
|
match self.iter.next().map_consume(|x| (self.f)(x)) {
|
2013-06-23 19:34:38 -05:00
|
|
|
None => return None,
|
|
|
|
next => self.subiter = next,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An iterator which just modifies the contained state throughout iteration.
|
2013-04-19 09:07:07 -05:00
|
|
|
pub struct UnfoldrIterator<'self, A, St> {
|
|
|
|
priv f: &'self fn(&mut St) -> Option<A>,
|
2013-05-28 16:35:52 -05:00
|
|
|
/// Internal state that will be yielded on the next iteration
|
2013-04-19 22:32:27 -05:00
|
|
|
state: St
|
2013-04-19 09:07:07 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
impl<'self, A, St> UnfoldrIterator<'self, A, St> {
|
|
|
|
/// Creates a new iterator with the specified closure as the "iterator
|
|
|
|
/// function" and an initial state to eventually pass to the iterator
|
2013-04-19 09:07:07 -05:00
|
|
|
#[inline]
|
2013-06-28 06:52:37 -05:00
|
|
|
pub fn new<'a>(initial_state: St, f: &'a fn(&mut St) -> Option<A>)
|
2013-06-15 22:01:08 -05:00
|
|
|
-> UnfoldrIterator<'a, A, St> {
|
2013-04-19 09:07:07 -05:00
|
|
|
UnfoldrIterator {
|
|
|
|
f: f,
|
|
|
|
state: initial_state
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self, A, St> Iterator<A> for UnfoldrIterator<'self, A, St> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
(self.f)(&mut self.state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
/// An infinite iterator starting at `start` and advancing by `step` with each
|
|
|
|
/// iteration
|
2013-04-24 18:54:13 -05:00
|
|
|
pub struct Counter<A> {
|
2013-05-28 16:35:52 -05:00
|
|
|
/// The current state the counter is at (next value to be yielded)
|
2013-04-24 18:54:13 -05:00
|
|
|
state: A,
|
2013-05-28 16:35:52 -05:00
|
|
|
/// The amount that this iterator is stepping by
|
2013-04-24 18:54:13 -05:00
|
|
|
step: A
|
2013-04-19 22:32:27 -05:00
|
|
|
}
|
|
|
|
|
2013-05-28 16:35:52 -05:00
|
|
|
impl<A> Counter<A> {
|
|
|
|
/// Creates a new counter with the specified start/step
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-05-28 16:35:52 -05:00
|
|
|
pub fn new(start: A, step: A) -> Counter<A> {
|
2013-04-24 18:54:13 -05:00
|
|
|
Counter{state: start, step: step}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-04-24 18:54:13 -05:00
|
|
|
fn next(&mut self) -> Option<A> {
|
|
|
|
let result = self.state.clone();
|
|
|
|
self.state = self.state.add(&self.step); // FIXME: #6050
|
|
|
|
Some(result)
|
2013-04-19 22:32:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-19 06:30:22 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use prelude::*;
|
|
|
|
|
2013-05-24 21:35:29 -05:00
|
|
|
use uint;
|
|
|
|
|
2013-04-24 18:54:13 -05:00
|
|
|
#[test]
|
2013-06-06 15:34:50 -05:00
|
|
|
fn test_counter_from_iter() {
|
2013-06-10 16:50:12 -05:00
|
|
|
let mut it = Counter::new(0, 5).take_(10);
|
2013-06-21 06:57:22 -05:00
|
|
|
let xs: ~[int] = FromIterator::from_iterator(&mut it);
|
2013-04-24 18:54:13 -05:00
|
|
|
assert_eq!(xs, ~[0, 5, 10, 15, 20, 25, 30, 35, 40, 45]);
|
|
|
|
}
|
|
|
|
|
2013-04-19 10:29:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_chain() {
|
|
|
|
let xs = [0u, 1, 2, 3, 4, 5];
|
2013-05-01 21:08:33 -05:00
|
|
|
let ys = [30u, 40, 50, 60];
|
2013-04-19 10:29:38 -05:00
|
|
|
let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60];
|
2013-06-10 16:50:12 -05:00
|
|
|
let mut it = xs.iter().chain_(ys.iter());
|
2013-04-19 10:29:38 -05:00
|
|
|
let mut i = 0;
|
2013-06-07 08:42:35 -05:00
|
|
|
for it.advance |&x| {
|
2013-04-19 10:29:38 -05:00
|
|
|
assert_eq!(x, expected[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
2013-05-01 21:08:33 -05:00
|
|
|
|
2013-06-10 16:50:12 -05:00
|
|
|
let ys = Counter::new(30u, 10).take_(4);
|
|
|
|
let mut it = xs.iter().transform(|&x| x).chain_(ys);
|
2013-05-01 21:08:33 -05:00
|
|
|
let mut i = 0;
|
2013-06-07 08:42:35 -05:00
|
|
|
for it.advance |x| {
|
2013-05-01 21:08:33 -05:00
|
|
|
assert_eq!(x, expected[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
2013-04-19 10:29:38 -05:00
|
|
|
}
|
|
|
|
|
2013-05-17 09:00:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_filter_map() {
|
2013-06-10 16:50:12 -05:00
|
|
|
let mut it = Counter::new(0u, 1u).take_(10)
|
2013-06-07 08:42:35 -05:00
|
|
|
.filter_map(|x| if x.is_even() { Some(x*x) } else { None });
|
2013-06-06 15:34:50 -05:00
|
|
|
assert_eq!(it.collect::<~[uint]>(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
|
2013-05-17 09:00:48 -05:00
|
|
|
}
|
|
|
|
|
2013-04-19 06:30:22 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_enumerate() {
|
|
|
|
let xs = [0u, 1, 2, 3, 4, 5];
|
|
|
|
let mut it = xs.iter().enumerate();
|
2013-06-07 08:42:35 -05:00
|
|
|
for it.advance |(i, &x)| {
|
2013-04-19 06:30:22 -05:00
|
|
|
assert_eq!(i, x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_take_while() {
|
|
|
|
let xs = [0u, 1, 2, 3, 5, 13, 15, 16, 17, 19];
|
|
|
|
let ys = [0u, 1, 2, 3, 5, 13];
|
|
|
|
let mut it = xs.iter().take_while(|&x| *x < 15u);
|
|
|
|
let mut i = 0;
|
2013-06-07 08:42:35 -05:00
|
|
|
for it.advance |&x| {
|
2013-04-19 06:30:22 -05:00
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_skip_while() {
|
|
|
|
let xs = [0u, 1, 2, 3, 5, 13, 15, 16, 17, 19];
|
|
|
|
let ys = [15, 16, 17, 19];
|
|
|
|
let mut it = xs.iter().skip_while(|&x| *x < 15u);
|
|
|
|
let mut i = 0;
|
2013-06-07 08:42:35 -05:00
|
|
|
for it.advance |&x| {
|
2013-04-19 06:30:22 -05:00
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_skip() {
|
|
|
|
let xs = [0u, 1, 2, 3, 5, 13, 15, 16, 17, 19, 20, 30];
|
|
|
|
let ys = [13, 15, 16, 17, 19, 20, 30];
|
|
|
|
let mut it = xs.iter().skip(5);
|
|
|
|
let mut i = 0;
|
2013-06-06 01:39:25 -05:00
|
|
|
for it.advance |&x| {
|
2013-04-19 06:30:22 -05:00
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_take() {
|
|
|
|
let xs = [0u, 1, 2, 3, 5, 13, 15, 16, 17, 19];
|
|
|
|
let ys = [0u, 1, 2, 3, 5];
|
2013-06-10 16:50:12 -05:00
|
|
|
let mut it = xs.iter().take_(5);
|
2013-04-19 06:30:22 -05:00
|
|
|
let mut i = 0;
|
2013-06-06 01:39:25 -05:00
|
|
|
for it.advance |&x| {
|
2013-04-19 06:30:22 -05:00
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
2013-04-19 09:07:07 -05:00
|
|
|
|
2013-04-19 22:32:27 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_scan() {
|
|
|
|
// test the type inference
|
|
|
|
fn add(old: &mut int, new: &uint) -> Option<float> {
|
|
|
|
*old += *new as int;
|
|
|
|
Some(*old as float)
|
|
|
|
}
|
|
|
|
let xs = [0u, 1, 2, 3, 4];
|
|
|
|
let ys = [0f, 1f, 3f, 6f, 10f];
|
|
|
|
|
|
|
|
let mut it = xs.iter().scan(0, add);
|
|
|
|
let mut i = 0;
|
|
|
|
for it.advance |x| {
|
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
2013-06-23 19:34:38 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_flat_map() {
|
|
|
|
let xs = [0u, 3, 6];
|
|
|
|
let ys = [0u, 1, 2, 3, 4, 5, 6, 7, 8];
|
|
|
|
let mut it = xs.iter().flat_map_(|&x| Counter::new(x, 1).take_(3));
|
|
|
|
let mut i = 0;
|
|
|
|
for it.advance |x: uint| {
|
|
|
|
assert_eq!(x, ys[i]);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, ys.len());
|
|
|
|
}
|
|
|
|
|
2013-04-19 09:07:07 -05:00
|
|
|
#[test]
|
|
|
|
fn test_unfoldr() {
|
|
|
|
fn count(st: &mut uint) -> Option<uint> {
|
|
|
|
if *st < 10 {
|
|
|
|
let ret = Some(*st);
|
|
|
|
*st += 1;
|
|
|
|
ret
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-28 06:52:37 -05:00
|
|
|
let mut it = UnfoldrIterator::new(0, count);
|
2013-04-19 09:07:07 -05:00
|
|
|
let mut i = 0;
|
|
|
|
for it.advance |counted| {
|
|
|
|
assert_eq!(counted, i);
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert_eq!(i, 10);
|
|
|
|
}
|
2013-05-17 09:45:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_nth() {
|
|
|
|
let v = &[0, 1, 2, 3, 4];
|
|
|
|
for uint::range(0, v.len()) |i| {
|
2013-05-18 03:27:58 -05:00
|
|
|
assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
|
2013-05-17 09:45:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_last() {
|
|
|
|
let v = &[0, 1, 2, 3, 4];
|
2013-06-07 13:45:31 -05:00
|
|
|
assert_eq!(v.iter().last_().unwrap(), &4);
|
|
|
|
assert_eq!(v.slice(0, 1).iter().last_().unwrap(), &0);
|
2013-05-17 09:45:25 -05:00
|
|
|
}
|
2013-05-17 09:54:58 -05:00
|
|
|
|
|
|
|
#[test]
|
2013-06-17 18:43:22 -05:00
|
|
|
fn test_iterator_len() {
|
2013-05-17 09:54:58 -05:00
|
|
|
let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2013-06-17 18:43:22 -05:00
|
|
|
assert_eq!(v.slice(0, 4).iter().len_(), 4);
|
|
|
|
assert_eq!(v.slice(0, 10).iter().len_(), 10);
|
|
|
|
assert_eq!(v.slice(0, 0).iter().len_(), 0);
|
2013-05-17 09:54:58 -05:00
|
|
|
}
|
2013-05-17 10:18:09 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_sum() {
|
|
|
|
let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
assert_eq!(v.slice(0, 4).iter().transform(|&x| x).sum(), 6);
|
|
|
|
assert_eq!(v.iter().transform(|&x| x).sum(), 55);
|
|
|
|
assert_eq!(v.slice(0, 0).iter().transform(|&x| x).sum(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_product() {
|
|
|
|
let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
assert_eq!(v.slice(0, 4).iter().transform(|&x| x).product(), 0);
|
|
|
|
assert_eq!(v.slice(1, 5).iter().transform(|&x| x).product(), 24);
|
|
|
|
assert_eq!(v.slice(0, 0).iter().transform(|&x| x).product(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_max() {
|
|
|
|
let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
assert_eq!(v.slice(0, 4).iter().transform(|&x| x).max(), Some(3));
|
|
|
|
assert_eq!(v.iter().transform(|&x| x).max(), Some(10));
|
|
|
|
assert_eq!(v.slice(0, 0).iter().transform(|&x| x).max(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator_min() {
|
|
|
|
let v = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
|
|
|
assert_eq!(v.slice(0, 4).iter().transform(|&x| x).min(), Some(0));
|
|
|
|
assert_eq!(v.iter().transform(|&x| x).min(), Some(0));
|
|
|
|
assert_eq!(v.slice(0, 0).iter().transform(|&x| x).min(), None);
|
|
|
|
}
|
|
|
|
|
2013-06-03 16:48:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_collect() {
|
2013-06-06 15:34:50 -05:00
|
|
|
let a = ~[1, 2, 3, 4, 5];
|
2013-06-03 16:48:52 -05:00
|
|
|
let b: ~[int] = a.iter().transform(|&x| x).collect();
|
|
|
|
assert_eq!(a, b);
|
|
|
|
}
|
|
|
|
|
2013-05-17 10:24:43 -05:00
|
|
|
#[test]
|
|
|
|
fn test_all() {
|
|
|
|
let v = ~&[1, 2, 3, 4, 5];
|
2013-06-07 23:07:55 -05:00
|
|
|
assert!(v.iter().all(|&x| x < 10));
|
2013-05-17 10:24:43 -05:00
|
|
|
assert!(!v.iter().all(|&x| x.is_even()));
|
2013-06-07 23:07:55 -05:00
|
|
|
assert!(!v.iter().all(|&x| x > 100));
|
2013-05-17 10:24:43 -05:00
|
|
|
assert!(v.slice(0, 0).iter().all(|_| fail!()));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_any() {
|
|
|
|
let v = ~&[1, 2, 3, 4, 5];
|
2013-06-10 16:50:12 -05:00
|
|
|
assert!(v.iter().any_(|&x| x < 10));
|
|
|
|
assert!(v.iter().any_(|&x| x.is_even()));
|
|
|
|
assert!(!v.iter().any_(|&x| x > 100));
|
|
|
|
assert!(!v.slice(0, 0).iter().any_(|_| fail!()));
|
2013-05-17 10:24:43 -05:00
|
|
|
}
|
2013-06-15 16:42:31 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find() {
|
|
|
|
let v = &[1, 3, 9, 27, 103, 14, 11];
|
2013-06-15 17:02:05 -05:00
|
|
|
assert_eq!(*v.iter().find_(|x| *x & 1 == 0).unwrap(), 14);
|
|
|
|
assert_eq!(*v.iter().find_(|x| *x % 3 == 0).unwrap(), 3);
|
|
|
|
assert!(v.iter().find_(|x| *x % 12 == 0).is_none());
|
2013-06-15 16:42:31 -05:00
|
|
|
}
|
2013-06-15 16:56:26 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_position() {
|
|
|
|
let v = &[1, 3, 9, 27, 103, 14, 11];
|
2013-06-15 17:02:05 -05:00
|
|
|
assert_eq!(v.iter().position_(|x| *x & 1 == 0).unwrap(), 5);
|
|
|
|
assert_eq!(v.iter().position_(|x| *x % 3 == 0).unwrap(), 1);
|
|
|
|
assert!(v.iter().position_(|x| *x % 12 == 0).is_none());
|
2013-06-15 16:56:26 -05:00
|
|
|
}
|
2013-06-17 18:43:22 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_count() {
|
|
|
|
let xs = &[1, 2, 2, 1, 5, 9, 0, 2];
|
|
|
|
assert_eq!(xs.iter().count(|x| *x == 2), 3);
|
|
|
|
assert_eq!(xs.iter().count(|x| *x == 5), 1);
|
|
|
|
assert_eq!(xs.iter().count(|x| *x == 95), 0);
|
|
|
|
}
|
2013-06-26 16:51:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_max_by() {
|
|
|
|
let xs = [-3, 0, 1, 5, -10];
|
|
|
|
assert_eq!(*xs.iter().max_by(|x| x.abs()).unwrap(), -10);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_min_by() {
|
|
|
|
let xs = [-3, 0, 1, 5, -10];
|
|
|
|
assert_eq!(*xs.iter().min_by(|x| x.abs()).unwrap(), 0);
|
|
|
|
}
|
2013-04-19 06:30:22 -05:00
|
|
|
}
|