pub use self::MinMaxResult::*;
use clone::Clone;
use cmp;
use cmp::Ord;
use default::Default;
use mem;
use num::{ToPrimitive, Int};
use ops::{Add, Deref, FnMut};
use option::Option;
use option::Option::{Some, None};
use uint;
#[deprecated = "renamed to Extend"] pub use self::Extend as Extendable;
#[lang="iterator"]
#[unstable = "just split up for object safety"]
pub trait Iterator<A> {
fn next(&mut self) -> Option<A>;
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { (0, None) }
}
#[unstable = "may be replaced by a more general conversion trait"]
pub trait FromIterator<A> {
fn from_iter<T: Iterator<A>>(iterator: T) -> Self;
}
#[unstable = "just renamed as part of collections reform"]
pub trait Extend<A> {
fn extend<T: Iterator<A>>(&mut self, iterator: T);
}
#[unstable = "new convention for extension traits"]
pub trait IteratorExt<A>: Iterator<A> {
#[inline]
#[stable]
fn chain<U: Iterator<A>>(self, other: U) -> Chain<Self, U> {
Chain{a: self, b: other, flag: false}
}
#[inline]
#[stable]
fn zip<B, U: Iterator<B>>(self, other: U) -> Zip<Self, U> {
Zip{a: self, b: other}
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn map<B, F: FnMut(A) -> B>(self, f: F) -> Map<A, B, Self, F> {
Map{iter: self, f: f}
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn filter<P>(self, predicate: P) -> Filter<A, Self, P> where P: FnMut(&A) -> bool {
Filter{iter: self, predicate: predicate}
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn filter_map<B, F>(self, f: F) -> FilterMap<A, B, Self, F> where F: FnMut(A) -> Option<B> {
FilterMap { iter: self, f: f }
}
#[inline]
#[stable]
fn enumerate(self) -> Enumerate<Self> {
Enumerate{iter: self, count: 0}
}
#[inline]
#[stable]
fn peekable(self) -> Peekable<A, Self> {
Peekable{iter: self, peeked: None}
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn skip_while<P>(self, predicate: P) -> SkipWhile<A, Self, P> where P: FnMut(&A) -> bool {
SkipWhile{iter: self, flag: false, predicate: predicate}
}
#[inline]
#[unstable = "waiting for unboxed closures, may want to require peek"]
fn take_while<P>(self, predicate: P) -> TakeWhile<A, Self, P> where P: FnMut(&A) -> bool {
TakeWhile{iter: self, flag: false, predicate: predicate}
}
#[inline]
#[stable]
fn skip(self, n: uint) -> Skip<Self> {
Skip{iter: self, n: n}
}
#[inline]
#[stable]
fn take(self, n: uint) -> Take<Self> {
Take{iter: self, n: n}
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn scan<St, B, F>(self, initial_state: St, f: F) -> Scan<A, B, Self, St, F> where
F: FnMut(&mut St, A) -> Option<B>,
{
Scan{iter: self, f: f, state: initial_state}
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn flat_map<B, U, F>(self, f: F) -> FlatMap<A, B, Self, U, F> where
U: Iterator<B>,
F: FnMut(A) -> U,
{
FlatMap{iter: self, f: f, frontiter: None, backiter: None }
}
#[inline]
#[stable]
fn fuse(self) -> Fuse<Self> {
Fuse{iter: self, done: false}
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn inspect<F>(self, f: F) -> Inspect<A, Self, F> where F: FnMut(&A) {
Inspect{iter: self, f: f}
}
#[stable]
fn by_ref<'r>(&'r mut self) -> ByRef<'r, Self> {
ByRef{iter: self}
}
#[inline]
#[unstable = "waiting for general conversion traits, just changed to take self by value"]
fn collect<B: FromIterator<A>>(self) -> B {
FromIterator::from_iter(self)
}
#[unstable = "recently added as part of collections reform"]
fn partition<B, F>(mut self, mut f: F) -> (B, B) where
B: Default + Extend<A>, F: FnMut(&A) -> bool
{
let mut left: B = Default::default();
let mut right: B = Default::default();
for x in self {
if f(&x) {
left.extend(Some(x).into_iter())
} else {
right.extend(Some(x).into_iter())
}
}
(left, right)
}
#[inline]
#[stable]
fn nth(&mut self, mut n: uint) -> Option<A> {
for x in *self {
if n == 0 { return Some(x) }
n -= 1;
}
None
}
#[inline]
#[unstable = "just changed to take self by value"]
fn last(mut self) -> Option<A> {
let mut last = None;
for x in self { last = Some(x); }
last
}
#[inline]
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
fn fold<B, F>(mut self, init: B, mut f: F) -> B where F: FnMut(B, A) -> B {
let mut accum = init;
for x in self {
accum = f(accum, x);
}
accum
}
#[inline]
#[unstable = "just changed to take self by value"]
fn count(self) -> uint {
self.fold(0, |cnt, _x| cnt + 1)
}
#[inline]
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
fn all<F>(mut self, mut f: F) -> bool where F: FnMut(A) -> bool {
for x in self { if !f(x) { return false; } }
true
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn any<F>(&mut self, mut f: F) -> bool where F: FnMut(A) -> bool {
for x in *self { if f(x) { return true; } }
false
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn find<P>(&mut self, mut predicate: P) -> Option<A> where P: FnMut(&A) -> bool {
for x in *self {
if predicate(&x) { return Some(x) }
}
None
}
#[inline]
#[unstable = "waiting for unboxed closures"]
fn position<P>(&mut self, mut predicate: P) -> Option<uint> where P: FnMut(A) -> bool {
let mut i = 0;
for x in *self {
if predicate(x) {
return Some(i);
}
i += 1;
}
None
}
#[inline]
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
fn max_by<B: Ord, F>(self, mut f: F) -> Option<A> where F: FnMut(&A) -> B {
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(|(x, _)| x)
}
#[inline]
#[unstable = "waiting for unboxed closures, just changed to take self by value"]
fn min_by<B: Ord, F>(self, mut f: F) -> Option<A> where F: FnMut(&A) -> B {
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(|(x, _)| x)
}
}
#[unstable = "trait is unstable"]
impl<A, I> IteratorExt<A> for I where I: Iterator<A> {}
#[unstable = "newly added trait, likely to be merged with IteratorExt"]
pub trait IteratorPairExt<A, B>: Iterator<(A, B)> {
fn unzip<FromA, FromB>(mut self) -> (FromA, FromB) where
FromA: Default + Extend<A>, FromB: Default + Extend<B>
{
struct SizeHint<A>(uint, Option<uint>);
impl<A> Iterator<A> for SizeHint<A> {
fn next(&mut self) -> Option<A> { None }
fn size_hint(&self) -> (uint, Option<uint>) {
(self.0, self.1)
}
}
let (lo, hi) = self.size_hint();
let mut ts: FromA = Default::default();
let mut us: FromB = Default::default();
ts.extend(SizeHint(lo, hi));
us.extend(SizeHint(lo, hi));
for (t, u) in self {
ts.extend(Some(t).into_iter());
us.extend(Some(u).into_iter());
}
(ts, us)
}
}
impl<A, B, I> IteratorPairExt<A, B> for I where I: Iterator<(A, B)> {}
#[unstable = "recently split into two traits"]
pub trait DoubleEndedIterator<A>: Iterator<A> {
fn next_back(&mut self) -> Option<A>;
}
#[unstable = "new extension trait convention"]
pub trait DoubleEndedIteratorExt<A>: DoubleEndedIterator<A> {
#[inline]
#[stable]
fn rev(self) -> Rev<Self> {
Rev{iter: self}
}
}
#[unstable = "trait is unstable"]
impl<A, I> DoubleEndedIteratorExt<A> for I where I: DoubleEndedIterator<A> {}
#[experimental = "not widely used"]
pub trait MutableDoubleEndedIterator {
fn reverse_(&mut self);
}
#[experimental = "trait is experimental"]
impl<'a, A:'a, T: DoubleEndedIterator<&'a mut A>> MutableDoubleEndedIterator for T {
fn reverse_(&mut self) {
loop {
match (self.next(), self.next_back()) {
(Some(x), Some(y)) => mem::swap(x, y),
_ => break
}
}
}
}
#[experimental = "not widely used, may be better decomposed into Index and ExactSizeIterator"]
pub trait RandomAccessIterator<A>: Iterator<A> {
fn indexable(&self) -> uint;
fn idx(&mut self, index: uint) -> Option<A>;
}
#[unstable = "could move DoubleEndedIterator bound onto rposition with method-level where clauses"]
pub trait ExactSizeIterator<A> : DoubleEndedIterator<A> {
#[inline]
fn rposition<P>(&mut self, mut predicate: P) -> Option<uint> where P: FnMut(A) -> bool {
let len = self.len();
for i in range(0, len).rev() {
if predicate(self.next_back().expect("rposition: incorrect ExactSizeIterator")) {
return Some(i);
}
}
None
}
#[inline]
fn len(&self) -> uint {
let (lower, upper) = self.size_hint();
assert_eq!(upper, Some(lower));
lower
}
}
#[unstable = "trait is unstable"]
impl<A, T: ExactSizeIterator<A>> ExactSizeIterator<(uint, A)> for Enumerate<T> {}
#[unstable = "trait is unstable"]
impl<A, I, F> ExactSizeIterator<A> for Inspect<A, I, F> where
I: ExactSizeIterator<A>,
F: FnMut(&A),
{}
#[unstable = "trait is unstable"]
impl<A, T: ExactSizeIterator<A>> ExactSizeIterator<A> for Rev<T> {}
#[unstable = "trait is unstable"]
impl<A, B, I, F> ExactSizeIterator<B> for Map<A, B, I, F> where
I: ExactSizeIterator<A>,
F: FnMut(A) -> B,
{}
#[unstable = "trait is unstable"]
impl<A, B, T, U> ExactSizeIterator<(A, B)> for Zip<T, U>
where T: ExactSizeIterator<A>, U: ExactSizeIterator<B> {}
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Rev<T> {
iter: T
}
#[unstable = "trait is unstable"]
impl<A, T: DoubleEndedIterator<A>> Iterator<A> for Rev<T> {
#[inline]
fn next(&mut self) -> Option<A> { self.iter.next_back() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}
#[unstable = "trait is unstable"]
impl<A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Rev<T> {
#[inline]
fn next_back(&mut self) -> Option<A> { self.iter.next() }
}
#[experimental = "trait is experimental"]
impl<A, T: DoubleEndedIterator<A> + RandomAccessIterator<A>> RandomAccessIterator<A>
for Rev<T> {
#[inline]
fn indexable(&self) -> uint { self.iter.indexable() }
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
let amt = self.indexable();
self.iter.idx(amt - index - 1)
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct ByRef<'a, T:'a> {
iter: &'a mut T
}
#[unstable = "trait is unstable"]
impl<'a, A, T: Iterator<A>+'a> Iterator<A> for ByRef<'a, T> {
#[inline]
fn next(&mut self) -> Option<A> { self.iter.next() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}
#[unstable = "trait is unstable"]
impl<'a, A, T: DoubleEndedIterator<A>+'a> DoubleEndedIterator<A> for ByRef<'a, T> {
#[inline]
fn next_back(&mut self) -> Option<A> { self.iter.next_back() }
}
#[experimental = "needs to be re-evaluated as part of numerics reform"]
pub trait AdditiveIterator<A> {
fn sum(self) -> A;
}
macro_rules! impl_additive {
($A:ty, $init:expr) => {
#[experimental = "trait is experimental"]
impl<T: Iterator<$A>> AdditiveIterator<$A> for T {
#[inline]
fn sum(self) -> $A {
self.fold($init, |acc, x| acc + x)
}
}
};
}
impl_additive! { i8, 0 }
impl_additive! { i16, 0 }
impl_additive! { i32, 0 }
impl_additive! { i64, 0 }
impl_additive! { int, 0 }
impl_additive! { u8, 0 }
impl_additive! { u16, 0 }
impl_additive! { u32, 0 }
impl_additive! { u64, 0 }
impl_additive! { uint, 0 }
impl_additive! { f32, 0.0 }
impl_additive! { f64, 0.0 }
#[experimental = "needs to be re-evaluated as part of numerics reform"]
pub trait MultiplicativeIterator<A> {
fn product(self) -> A;
}
macro_rules! impl_multiplicative {
($A:ty, $init:expr) => {
#[experimental = "trait is experimental"]
impl<T: Iterator<$A>> MultiplicativeIterator<$A> for T {
#[inline]
fn product(self) -> $A {
self.fold($init, |acc, x| acc * x)
}
}
};
}
impl_multiplicative! { i8, 1 }
impl_multiplicative! { i16, 1 }
impl_multiplicative! { i32, 1 }
impl_multiplicative! { i64, 1 }
impl_multiplicative! { int, 1 }
impl_multiplicative! { u8, 1 }
impl_multiplicative! { u16, 1 }
impl_multiplicative! { u32, 1 }
impl_multiplicative! { u64, 1 }
impl_multiplicative! { uint, 1 }
impl_multiplicative! { f32, 1.0 }
impl_multiplicative! { f64, 1.0 }
#[unstable = "recently renamed for new extension trait conventions"]
pub trait IteratorOrdExt<A> {
fn max(self) -> Option<A>;
fn min(self) -> Option<A>;
fn min_max(self) -> MinMaxResult<A>;
}
#[unstable = "trait is unstable"]
impl<A: Ord, T: Iterator<A>> IteratorOrdExt<A> for T {
#[inline]
fn max(self) -> Option<A> {
self.fold(None, |max, x| {
match max {
None => Some(x),
Some(y) => Some(cmp::max(x, y))
}
})
}
#[inline]
fn min(self) -> Option<A> {
self.fold(None, |min, x| {
match min {
None => Some(x),
Some(y) => Some(cmp::min(x, y))
}
})
}
fn min_max(mut self) -> MinMaxResult<A> {
let (mut min, mut max) = match self.next() {
None => return NoElements,
Some(x) => {
match self.next() {
None => return OneElement(x),
Some(y) => if x < y {(x, y)} else {(y,x)}
}
}
};
loop {
let first = match self.next() {
None => break,
Some(x) => x
};
let second = match self.next() {
None => {
if first < min {
min = first;
} else if first > max {
max = first;
}
break;
}
Some(x) => x
};
if first < second {
if first < min {min = first;}
if max < second {max = second;}
} else {
if second < min {min = second;}
if max < first {max = first;}
}
}
MinMax(min, max)
}
}
#[deriving(Clone, PartialEq, Show)]
#[unstable = "waiting on namespaced enum conventions"]
pub enum MinMaxResult<T> {
NoElements,
OneElement(T),
MinMax(T, T)
}
#[stable]
impl<T: Clone> MinMaxResult<T> {
pub fn into_option(self) -> Option<(T,T)> {
match self {
NoElements => None,
OneElement(x) => Some((x.clone(), x)),
MinMax(x, y) => Some((x, y))
}
}
}
#[unstable = "recently renamed for extension trait conventions"]
pub trait IteratorCloneExt<A> {
fn cloned(self) -> Cloned<Self>;
}
#[unstable = "trait is unstable"]
impl<A: Clone, D: Deref<A>, I: Iterator<D>> IteratorCloneExt<A> for I {
fn cloned(self) -> Cloned<I> {
Cloned { it: self }
}
}
pub struct Cloned<I> {
it: I,
}
impl<A: Clone, D: Deref<A>, I: Iterator<D>> Iterator<A> for Cloned<I> {
fn next(&mut self) -> Option<A> {
self.it.next().cloned()
}
fn size_hint(&self) -> (uint, Option<uint>) {
self.it.size_hint()
}
}
impl<A: Clone, D: Deref<A>, I: DoubleEndedIterator<D>>
DoubleEndedIterator<A> for Cloned<I> {
fn next_back(&mut self) -> Option<A> {
self.it.next_back().cloned()
}
}
#[unstable = "trait is unstable"]
impl<A: Clone, D: Deref<A>, I: ExactSizeIterator<D>> ExactSizeIterator<A> for Cloned<I> {}
#[unstable = "recently renamed for extension trait conventions"]
pub trait CloneIteratorExt {
#[stable]
fn cycle(self) -> Cycle<Self>;
}
impl<A, I> CloneIteratorExt for I where I: Iterator<A> + Clone {
#[inline]
fn cycle(self) -> Cycle<I> {
Cycle{orig: self.clone(), iter: self}
}
}
#[deriving(Clone, Copy)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Cycle<T> {
orig: T,
iter: T,
}
impl<A, T: Clone + Iterator<A>> Iterator<A> for Cycle<T> {
#[inline]
fn next(&mut self) -> Option<A> {
match self.iter.next() {
None => { self.iter = self.orig.clone(); self.iter.next() }
y => y
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
match self.orig.size_hint() {
sz @ (0, Some(0)) => sz,
(0, _) => (0, None),
_ => (uint::MAX, None)
}
}
}
#[experimental = "trait is experimental"]
impl<A, T: Clone + RandomAccessIterator<A>> RandomAccessIterator<A> for Cycle<T> {
#[inline]
fn indexable(&self) -> uint {
if self.orig.indexable() > 0 {
uint::MAX
} else {
0
}
}
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
let liter = self.iter.indexable();
let lorig = self.orig.indexable();
if lorig == 0 {
None
} else if index < liter {
self.iter.idx(index)
} else {
self.orig.idx((index - liter) % lorig)
}
}
}
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Chain<T, U> {
a: T,
b: U,
flag: bool,
}
#[unstable = "trait is unstable"]
impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for Chain<T, U> {
#[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()
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (a_lower, a_upper) = self.a.size_hint();
let (b_lower, b_upper) = self.b.size_hint();
let lower = a_lower.saturating_add(b_lower);
let upper = match (a_upper, b_upper) {
(Some(x), Some(y)) => x.checked_add(y),
_ => None
};
(lower, upper)
}
}
#[unstable = "trait is unstable"]
impl<A, T: DoubleEndedIterator<A>, U: DoubleEndedIterator<A>> DoubleEndedIterator<A>
for Chain<T, U> {
#[inline]
fn next_back(&mut self) -> Option<A> {
match self.b.next_back() {
Some(x) => Some(x),
None => self.a.next_back()
}
}
}
#[experimental = "trait is experimental"]
impl<A, T: RandomAccessIterator<A>, U: RandomAccessIterator<A>> RandomAccessIterator<A>
for Chain<T, U> {
#[inline]
fn indexable(&self) -> uint {
let (a, b) = (self.a.indexable(), self.b.indexable());
a.saturating_add(b)
}
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
let len = self.a.indexable();
if index < len {
self.a.idx(index)
} else {
self.b.idx(index - len)
}
}
}
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Zip<T, U> {
a: T,
b: U
}
#[unstable = "trait is unstable"]
impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for Zip<T, U> {
#[inline]
fn next(&mut self) -> Option<(A, B)> {
match self.a.next() {
None => None,
Some(x) => match self.b.next() {
None => None,
Some(y) => Some((x, y))
}
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (a_lower, a_upper) = self.a.size_hint();
let (b_lower, b_upper) = self.b.size_hint();
let lower = cmp::min(a_lower, b_lower);
let upper = match (a_upper, b_upper) {
(Some(x), Some(y)) => Some(cmp::min(x,y)),
(Some(x), None) => Some(x),
(None, Some(y)) => Some(y),
(None, None) => None
};
(lower, upper)
}
}
#[unstable = "trait is unstable"]
impl<A, B, T: ExactSizeIterator<A>, U: ExactSizeIterator<B>> DoubleEndedIterator<(A, B)>
for Zip<T, U> {
#[inline]
fn next_back(&mut self) -> Option<(A, B)> {
let a_sz = self.a.len();
let b_sz = self.b.len();
if a_sz != b_sz {
if a_sz > b_sz {
for _ in range(0, a_sz - b_sz) { self.a.next_back(); }
} else {
for _ in range(0, b_sz - a_sz) { self.b.next_back(); }
}
}
match (self.a.next_back(), self.b.next_back()) {
(Some(x), Some(y)) => Some((x, y)),
(None, None) => None,
_ => unreachable!(),
}
}
}
#[experimental = "trait is experimental"]
impl<A, B, T: RandomAccessIterator<A>, U: RandomAccessIterator<B>>
RandomAccessIterator<(A, B)> for Zip<T, U> {
#[inline]
fn indexable(&self) -> uint {
cmp::min(self.a.indexable(), self.b.indexable())
}
#[inline]
fn idx(&mut self, index: uint) -> Option<(A, B)> {
match self.a.idx(index) {
None => None,
Some(x) => match self.b.idx(index) {
None => None,
Some(y) => Some((x, y))
}
}
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Map<A, B, I: Iterator<A>, F: FnMut(A) -> B> {
iter: I,
f: F,
}
#[stable]
impl<A, B, I, F> Clone for Map<A, B, I, F> where
I: Clone + Iterator<A>,
F: Clone + FnMut(A) -> B,
{
fn clone(&self) -> Map<A, B, I, F> {
Map {
iter: self.iter.clone(),
f: self.f.clone(),
}
}
}
impl<A, B, I, F> Map<A, B, I, F> where I: Iterator<A>, F: FnMut(A) -> B {
#[inline]
fn do_map(&mut self, elt: Option<A>) -> Option<B> {
match elt {
Some(a) => Some((self.f)(a)),
_ => None
}
}
}
#[unstable = "trait is unstable"]
impl<A, B, I, F> Iterator<B> for Map<A, B, I, F> where I: Iterator<A>, F: FnMut(A) -> B {
#[inline]
fn next(&mut self) -> Option<B> {
let next = self.iter.next();
self.do_map(next)
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}
#[unstable = "trait is unstable"]
impl<A, B, I, F> DoubleEndedIterator<B> for Map<A, B, I, F> where
I: DoubleEndedIterator<A>,
F: FnMut(A) -> B,
{
#[inline]
fn next_back(&mut self) -> Option<B> {
let next = self.iter.next_back();
self.do_map(next)
}
}
#[experimental = "trait is experimental"]
impl<A, B, I, F> RandomAccessIterator<B> for Map<A, B, I, F> where
I: RandomAccessIterator<A>,
F: FnMut(A) -> B,
{
#[inline]
fn indexable(&self) -> uint {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<B> {
let elt = self.iter.idx(index);
self.do_map(elt)
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Filter<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
iter: I,
predicate: P,
}
#[stable]
impl<A, I, P> Clone for Filter<A, I, P> where
I: Clone + Iterator<A>,
P: Clone + FnMut(&A) -> bool,
{
fn clone(&self) -> Filter<A, I, P> {
Filter {
iter: self.iter.clone(),
predicate: self.predicate.clone(),
}
}
}
#[unstable = "trait is unstable"]
impl<A, I, P> Iterator<A> for Filter<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
#[inline]
fn next(&mut self) -> Option<A> {
for x in self.iter {
if (self.predicate)(&x) {
return Some(x);
} else {
continue
}
}
None
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (_, upper) = self.iter.size_hint();
(0, upper) }
}
#[unstable = "trait is unstable"]
impl<A, I, P> DoubleEndedIterator<A> for Filter<A, I, P> where
I: DoubleEndedIterator<A>,
P: FnMut(&A) -> bool,
{
#[inline]
fn next_back(&mut self) -> Option<A> {
for x in self.iter.by_ref().rev() {
if (self.predicate)(&x) {
return Some(x);
}
}
None
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct FilterMap<A, B, I, F> where I: Iterator<A>, F: FnMut(A) -> Option<B> {
iter: I,
f: F,
}
#[stable]
impl<A, B, I, F> Clone for FilterMap<A, B, I, F> where
I: Clone + Iterator<A>,
F: Clone + FnMut(A) -> Option<B>,
{
fn clone(&self) -> FilterMap<A, B, I, F> {
FilterMap {
iter: self.iter.clone(),
f: self.f.clone(),
}
}
}
#[unstable = "trait is unstable"]
impl<A, B, I, F> Iterator<B> for FilterMap<A, B, I, F> where
I: Iterator<A>,
F: FnMut(A) -> Option<B>,
{
#[inline]
fn next(&mut self) -> Option<B> {
for x in self.iter {
match (self.f)(x) {
Some(y) => return Some(y),
None => ()
}
}
None
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (_, upper) = self.iter.size_hint();
(0, upper) }
}
#[unstable = "trait is unstable"]
impl<A, B, I, F> DoubleEndedIterator<B> for FilterMap<A, B, I, F> where
I: DoubleEndedIterator<A>,
F: FnMut(A) -> Option<B>,
{
#[inline]
fn next_back(&mut self) -> Option<B> {
for x in self.iter.by_ref().rev() {
match (self.f)(x) {
Some(y) => return Some(y),
None => ()
}
}
None
}
}
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Enumerate<T> {
iter: T,
count: uint
}
#[unstable = "trait is unstable"]
impl<A, T: Iterator<A>> Iterator<(uint, A)> for Enumerate<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
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}
#[unstable = "trait is unstable"]
impl<A, T: ExactSizeIterator<A>> DoubleEndedIterator<(uint, A)> for Enumerate<T> {
#[inline]
fn next_back(&mut self) -> Option<(uint, A)> {
match self.iter.next_back() {
Some(a) => {
let len = self.iter.len();
Some((self.count + len, a))
}
_ => None
}
}
}
#[experimental = "trait is experimental"]
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<(uint, A)> for Enumerate<T> {
#[inline]
fn indexable(&self) -> uint {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<(uint, A)> {
match self.iter.idx(index) {
Some(a) => Some((self.count + index, a)),
_ => None,
}
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
#[deriving(Copy)]
pub struct Peekable<A, T> {
iter: T,
peeked: Option<A>,
}
impl<A, T: Iterator<A>> Iterator<A> for Peekable<A, T> {
#[inline]
fn next(&mut self) -> Option<A> {
if self.peeked.is_some() { self.peeked.take() }
else { self.iter.next() }
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (lo, hi) = self.iter.size_hint();
if self.peeked.is_some() {
let lo = lo.saturating_add(1);
let hi = match hi {
Some(x) => x.checked_add(1),
None => None
};
(lo, hi)
} else {
(lo, hi)
}
}
}
#[stable]
impl<'a, A, T: Iterator<A>> Peekable<A, T> {
#[inline]
pub fn peek(&'a mut self) -> Option<&'a A> {
if self.peeked.is_none() {
self.peeked = self.iter.next();
}
match self.peeked {
Some(ref value) => Some(value),
None => None,
}
}
#[inline]
pub fn is_empty(&mut self) -> bool {
self.peek().is_none()
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct SkipWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
iter: I,
flag: bool,
predicate: P,
}
#[stable]
impl<A, I, P> Clone for SkipWhile<A, I, P> where
I: Clone + Iterator<A>,
P: Clone + FnMut(&A) -> bool,
{
fn clone(&self) -> SkipWhile<A, I, P> {
SkipWhile {
iter: self.iter.clone(),
flag: self.flag,
predicate: self.predicate.clone(),
}
}
}
#[unstable = "trait is unstable"]
impl<A, I, P> Iterator<A> for SkipWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
#[inline]
fn next(&mut self) -> Option<A> {
for x in self.iter {
if self.flag || !(self.predicate)(&x) {
self.flag = true;
return Some(x);
}
}
None
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (_, upper) = self.iter.size_hint();
(0, upper) }
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct TakeWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
iter: I,
flag: bool,
predicate: P,
}
#[stable]
impl<A, I, P> Clone for TakeWhile<A, I, P> where
I: Clone + Iterator<A>,
P: Clone + FnMut(&A) -> bool,
{
fn clone(&self) -> TakeWhile<A, I, P> {
TakeWhile {
iter: self.iter.clone(),
flag: self.flag,
predicate: self.predicate.clone(),
}
}
}
#[unstable = "trait is unstable"]
impl<A, I, P> Iterator<A> for TakeWhile<A, I, P> where I: Iterator<A>, P: FnMut(&A) -> bool {
#[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
}
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (_, upper) = self.iter.size_hint();
(0, upper) }
}
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Skip<T> {
iter: T,
n: uint
}
#[unstable = "trait is unstable"]
impl<A, T: Iterator<A>> Iterator<A> for Skip<T> {
#[inline]
fn next(&mut self) -> Option<A> {
let mut next = self.iter.next();
if self.n == 0 {
next
} else {
let mut n = self.n;
while n > 0 {
n -= 1;
match next {
Some(_) => {
next = self.iter.next();
continue
}
None => {
self.n = 0;
return None
}
}
}
self.n = 0;
next
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (lower, upper) = self.iter.size_hint();
let lower = lower.saturating_sub(self.n);
let upper = match upper {
Some(x) => Some(x.saturating_sub(self.n)),
None => None
};
(lower, upper)
}
}
#[experimental = "trait is experimental"]
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Skip<T> {
#[inline]
fn indexable(&self) -> uint {
self.iter.indexable().saturating_sub(self.n)
}
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
if index >= self.indexable() {
None
} else {
self.iter.idx(index + self.n)
}
}
}
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Take<T> {
iter: T,
n: uint
}
#[unstable = "trait is unstable"]
impl<A, T: Iterator<A>> Iterator<A> for Take<T> {
#[inline]
fn next(&mut self) -> Option<A> {
if self.n != 0 {
self.n -= 1;
self.iter.next()
} else {
None
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (lower, upper) = self.iter.size_hint();
let lower = cmp::min(lower, self.n);
let upper = match upper {
Some(x) if x < self.n => Some(x),
_ => Some(self.n)
};
(lower, upper)
}
}
#[experimental = "trait is experimental"]
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Take<T> {
#[inline]
fn indexable(&self) -> uint {
cmp::min(self.iter.indexable(), self.n)
}
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
if index >= self.n {
None
} else {
self.iter.idx(index)
}
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[unstable = "waiting for unboxed closures"]
pub struct Scan<A, B, I, St, F> where I: Iterator<A>, F: FnMut(&mut St, A) -> Option<B> {
iter: I,
f: F,
pub state: St,
}
#[stable]
impl<A, B, I, St, F> Clone for Scan<A, B, I, St, F> where
I: Clone + Iterator<A>,
St: Clone,
F: Clone + FnMut(&mut St, A) -> Option<B>,
{
fn clone(&self) -> Scan<A, B, I, St, F> {
Scan {
iter: self.iter.clone(),
f: self.f.clone(),
state: self.state.clone(),
}
}
}
#[unstable = "trait is unstable"]
impl<A, B, I, St, F> Iterator<B> for Scan<A, B, I, St, F> where
I: Iterator<A>,
F: FnMut(&mut St, A) -> Option<B>,
{
#[inline]
fn next(&mut self) -> Option<B> {
self.iter.next().and_then(|a| (self.f)(&mut self.state, a))
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (_, upper) = self.iter.size_hint();
(0, upper) }
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[unstable = "waiting for unboxed closures"]
pub struct FlatMap<A, B, I, U, F> where I: Iterator<A>, U: Iterator<B>, F: FnMut(A) -> U {
iter: I,
f: F,
frontiter: Option<U>,
backiter: Option<U>,
}
#[stable]
impl<A, B, I, U, F> Clone for FlatMap<A, B, I, U, F> where
I: Clone + Iterator<A>,
U: Clone + Iterator<B>,
F: Clone + FnMut(A) -> U,
{
fn clone(&self) -> FlatMap<A, B, I, U, F> {
FlatMap {
iter: self.iter.clone(),
f: self.f.clone(),
frontiter: self.frontiter.clone(),
backiter: self.backiter.clone(),
}
}
}
#[unstable = "trait is unstable"]
impl<A, B, I, U, F> Iterator<B> for FlatMap<A, B, I, U, F> where
I: Iterator<A>,
U: Iterator<B>,
F: FnMut(A) -> U,
{
#[inline]
fn next(&mut self) -> Option<B> {
loop {
for inner in self.frontiter.iter_mut() {
for x in *inner {
return Some(x)
}
}
match self.iter.next().map(|x| (self.f)(x)) {
None => return self.backiter.as_mut().and_then(|it| it.next()),
next => self.frontiter = next,
}
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (flo, fhi) = self.frontiter.as_ref().map_or((0, Some(0)), |it| it.size_hint());
let (blo, bhi) = self.backiter.as_ref().map_or((0, Some(0)), |it| it.size_hint());
let lo = flo.saturating_add(blo);
match (self.iter.size_hint(), fhi, bhi) {
((0, Some(0)), Some(a), Some(b)) => (lo, a.checked_add(b)),
_ => (lo, None)
}
}
}
#[unstable = "trait is unstable"]
impl<A, B, I, U, F> DoubleEndedIterator<B> for FlatMap<A, B, I, U, F> where
I: DoubleEndedIterator<A>,
U: DoubleEndedIterator<B>,
F: FnMut(A) -> U,
{
#[inline]
fn next_back(&mut self) -> Option<B> {
loop {
for inner in self.backiter.iter_mut() {
match inner.next_back() {
None => (),
y => return y
}
}
match self.iter.next_back().map(|x| (self.f)(x)) {
None => return self.frontiter.as_mut().and_then(|it| it.next_back()),
next => self.backiter = next,
}
}
}
}
#[deriving(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[stable]
pub struct Fuse<T> {
iter: T,
done: bool
}
#[unstable = "trait is unstable"]
impl<A, T: Iterator<A>> Iterator<A> for Fuse<T> {
#[inline]
fn next(&mut self) -> Option<A> {
if self.done {
None
} else {
match self.iter.next() {
None => {
self.done = true;
None
}
x => x
}
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
if self.done {
(0, Some(0))
} else {
self.iter.size_hint()
}
}
}
#[unstable = "trait is unstable"]
impl<A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Fuse<T> {
#[inline]
fn next_back(&mut self) -> Option<A> {
if self.done {
None
} else {
match self.iter.next_back() {
None => {
self.done = true;
None
}
x => x
}
}
}
}
#[experimental = "trait is experimental"]
impl<A, T: RandomAccessIterator<A>> RandomAccessIterator<A> for Fuse<T> {
#[inline]
fn indexable(&self) -> uint {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
self.iter.idx(index)
}
}
#[experimental = "seems marginal"]
impl<T> Fuse<T> {
#[inline]
pub fn reset_fuse(&mut self) {
self.done = false
}
}
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
#[unstable = "waiting for unboxed closures"]
pub struct Inspect<A, I, F> where I: Iterator<A>, F: FnMut(&A) {
iter: I,
f: F,
}
#[stable]
impl<A, I, F> Clone for Inspect<A, I, F> where
I: Clone + Iterator<A>,
F: Clone + FnMut(&A),
{
fn clone(&self) -> Inspect<A, I, F> {
Inspect {
iter: self.iter.clone(),
f: self.f.clone(),
}
}
}
impl<A, I, F> Inspect<A, I, F> where I: Iterator<A>, F: FnMut(&A) {
#[inline]
fn do_inspect(&mut self, elt: Option<A>) -> Option<A> {
match elt {
Some(ref a) => (self.f)(a),
None => ()
}
elt
}
}
#[unstable = "trait is unstable"]
impl<A, I, F> Iterator<A> for Inspect<A, I, F> where I: Iterator<A>, F: FnMut(&A) {
#[inline]
fn next(&mut self) -> Option<A> {
let next = self.iter.next();
self.do_inspect(next)
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
self.iter.size_hint()
}
}
#[unstable = "trait is unstable"]
impl<A, I, F> DoubleEndedIterator<A> for Inspect<A, I, F> where
I: DoubleEndedIterator<A>,
F: FnMut(&A),
{
#[inline]
fn next_back(&mut self) -> Option<A> {
let next = self.iter.next_back();
self.do_inspect(next)
}
}
#[experimental = "trait is experimental"]
impl<A, I, F> RandomAccessIterator<A> for Inspect<A, I, F> where
I: RandomAccessIterator<A>,
F: FnMut(&A),
{
#[inline]
fn indexable(&self) -> uint {
self.iter.indexable()
}
#[inline]
fn idx(&mut self, index: uint) -> Option<A> {
let element = self.iter.idx(index);
self.do_inspect(element)
}
}
#[experimental]
pub struct Unfold<A, St, F> where F: FnMut(&mut St) -> Option<A> {
f: F,
pub state: St,
}
#[stable]
impl<A, St, F> Clone for Unfold<A, St, F> where
F: Clone + FnMut(&mut St) -> Option<A>,
St: Clone,
{
fn clone(&self) -> Unfold<A, St, F> {
Unfold {
f: self.f.clone(),
state: self.state.clone(),
}
}
}
#[experimental]
impl<A, St, F> Unfold<A, St, F> where F: FnMut(&mut St) -> Option<A> {
#[inline]
pub fn new(initial_state: St, f: F) -> Unfold<A, St, F> {
Unfold {
f: f,
state: initial_state
}
}
}
#[experimental]
impl<A, St, F> Iterator<A> for Unfold<A, St, F> where F: FnMut(&mut St) -> Option<A> {
#[inline]
fn next(&mut self) -> Option<A> {
(self.f)(&mut self.state)
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
(0, None)
}
}
#[deriving(Clone, Copy)]
#[unstable = "may be renamed"]
pub struct Counter<A> {
state: A,
step: A,
}
#[inline]
#[unstable = "may be renamed"]
pub fn count<A>(start: A, step: A) -> Counter<A> {
Counter{state: start, step: step}
}
#[unstable = "trait is unstable"]
impl<A: Add<A, A> + Clone> Iterator<A> for Counter<A> {
#[inline]
fn next(&mut self) -> Option<A> {
let result = self.state.clone();
self.state = self.state.clone() + self.step.clone();
Some(result)
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
(uint::MAX, None) }
}
#[deriving(Clone, Copy)]
#[unstable = "may be refactored due to numerics reform or ops reform"]
pub struct Range<A> {
state: A,
stop: A,
one: A,
}
#[inline]
pub fn range<A: Int>(start: A, stop: A) -> Range<A> {
Range {
state: start,
stop: stop,
one: Int::one(),
}
}
#[unstable = "trait is unstable"]
impl<A: Int + ToPrimitive> Iterator<A> for Range<A> {
#[inline]
fn next(&mut self) -> Option<A> {
if self.state < self.stop {
let result = self.state.clone();
self.state = self.state + self.one;
Some(result)
} else {
None
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let bound = match self.state.to_i64() {
Some(a) => {
let sz = self.stop.to_i64().map(|b| b.checked_sub(a));
match sz {
Some(Some(bound)) => bound.to_uint(),
_ => None,
}
},
None => match self.state.to_u64() {
Some(a) => {
let sz = self.stop.to_u64().map(|b| b.checked_sub(a));
match sz {
Some(Some(bound)) => bound.to_uint(),
_ => None
}
},
None => None
}
};
match bound {
Some(b) => (b, Some(b)),
None => (0, None)
}
}
}
#[unstable = "trait is unstable"]
impl<A: Int + ToPrimitive> DoubleEndedIterator<A> for Range<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
if self.stop > self.state {
self.stop = self.stop - self.one;
Some(self.stop.clone())
} else {
None
}
}
}
#[deriving(Clone)]
#[unstable = "may be refactored due to numerics reform or ops reform"]
pub struct RangeInclusive<A> {
range: Range<A>,
done: bool,
}
#[inline]
#[unstable = "may be refactored due to numerics reform or ops reform"]
pub fn range_inclusive<A: Int>(start: A, stop: A) -> RangeInclusive<A> {
RangeInclusive {
range: range(start, stop),
done: false,
}
}
#[unstable = "trait is unstable"]
impl<A: Int + ToPrimitive> Iterator<A> for RangeInclusive<A> {
#[inline]
fn next(&mut self) -> Option<A> {
match self.range.next() {
Some(x) => Some(x),
None => {
if !self.done && self.range.state == self.range.stop {
self.done = true;
Some(self.range.stop.clone())
} else {
None
}
}
}
}
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) {
let (lo, hi) = self.range.size_hint();
if self.done {
(lo, hi)
} else {
let lo = lo.saturating_add(1);
let hi = match hi {
Some(x) => x.checked_add(1),
None => None
};
(lo, hi)
}
}
}
#[unstable = "trait is unstable"]
impl<A: Int + ToPrimitive> DoubleEndedIterator<A> for RangeInclusive<A> {
#[inline]
fn next_back(&mut self) -> Option<A> {
if self.range.stop > self.range.state {
let result = self.range.stop.clone();
self.range.stop = self.range.stop - self.range.one;
Some(result)
} else if !self.done && self.range.state == self.range.stop {
self.done = true;
Some(self.range.stop.clone())
} else {
None
}
}
}
#[deriving(Clone)]
#[unstable = "may be refactored due to numerics reform or ops reform"]
pub struct RangeStep<A> {
state: A,
stop: A,
step: A,
rev: bool,
}
#[inline]
#[unstable = "may be refactored due to numerics reform or ops reform"]
pub fn range_step<A: Int>(start: A, stop: A, step: A) -> RangeStep<A> {
let rev = step < Int::zero();
RangeStep{state: start, stop: stop, step: step, rev: rev}
}
#[unstable = "trait is unstable"]
impl<A: Int> Iterator<A> for RangeStep<A> {
#[inline]
fn next(&mut self) -> Option<A> {
if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) {
let result = self.state;
match self.state.checked_add(self.step) {
Some(x) => self.state = x,
None => self.state = self.stop.clone()
}
Some(result)
} else {
None
}
}
}
#[deriving(Clone)]
#[unstable = "may be refactored due to numerics reform or ops reform"]
pub struct RangeStepInclusive<A> {
state: A,
stop: A,
step: A,
rev: bool,
done: bool,
}
#[inline]
#[unstable = "may be refactored due to numerics reform or ops reform"]
pub fn range_step_inclusive<A: Int>(start: A, stop: A, step: A) -> RangeStepInclusive<A> {
let rev = step < Int::zero();
RangeStepInclusive {
state: start,
stop: stop,
step: step,
rev: rev,
done: false,
}
}
#[unstable = "trait is unstable"]
impl<A: Int> Iterator<A> for RangeStepInclusive<A> {
#[inline]
fn next(&mut self) -> Option<A> {
if !self.done && ((self.rev && self.state >= self.stop) ||
(!self.rev && self.state <= self.stop)) {
let result = self.state;
match self.state.checked_add(self.step) {
Some(x) => self.state = x,
None => self.done = true
}
Some(result)
} else {
None
}
}
}
#[unstable = "Trait is unstable."]
pub trait Step: Ord {
fn step(&mut self);
fn step_back(&mut self);
fn steps_between(a: &Self, b: &Self) -> Option<uint>;
}
macro_rules! step_impl {
($($t:ty)*) => ($(
#[unstable = "Trait is unstable."]
impl Step for $t {
#[inline]
fn step(&mut self) { *self += 1; }
#[inline]
fn step_back(&mut self) { *self -= 1; }
#[inline]
fn steps_between(a: &$t, b: &$t) -> Option<uint> {
debug_assert!(a < b);
Some((*a - *b) as uint)
}
}
)*)
}
macro_rules! step_impl_no_between {
($($t:ty)*) => ($(
#[unstable = "Trait is unstable."]
impl Step for $t {
#[inline]
fn step(&mut self) { *self += 1; }
#[inline]
fn step_back(&mut self) { *self -= 1; }
#[inline]
fn steps_between(_a: &$t, _b: &$t) -> Option<uint> {
None
}
}
)*)
}
step_impl!(uint u8 u16 u32 int i8 i16 i32);
#[cfg(target_word_size = "64")]
step_impl!(u64 i64);
#[cfg(target_word_size = "32")]
step_impl_no_between!(u64 i64);
#[deriving(Clone)]
#[stable]
pub struct Repeat<A> {
element: A
}
impl<A: Clone> Repeat<A> {
#[inline]
#[deprecated = "use iter::repeat instead"]
pub fn new(elt: A) -> Repeat<A> {
Repeat{element: elt}
}
}
#[unstable = "trait is unstable"]
impl<A: Clone> Iterator<A> for Repeat<A> {
#[inline]
fn next(&mut self) -> Option<A> { self.idx(0) }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { (uint::MAX, None) }
}
#[unstable = "trait is unstable"]
impl<A: Clone> DoubleEndedIterator<A> for Repeat<A> {
#[inline]
fn next_back(&mut self) -> Option<A> { self.idx(0) }
}
#[experimental = "trait is experimental"]
impl<A: Clone> RandomAccessIterator<A> for Repeat<A> {
#[inline]
fn indexable(&self) -> uint { uint::MAX }
#[inline]
fn idx(&mut self, _: uint) -> Option<A> { Some(self.element.clone()) }
}
type IterateState<T, F> = (F, Option<T>, bool);
#[experimental]
pub type Iterate<T, F> = Unfold<T, IterateState<T, F>, fn(&mut IterateState<T, F>) -> Option<T>>;
#[experimental]
pub fn iterate<T, F>(seed: T, f: F) -> Iterate<T, F> where
T: Clone,
F: FnMut(T) -> T,
{
fn next<T, F>(st: &mut IterateState<T, F>) -> Option<T> where
T: Clone,
F: FnMut(T) -> T,
{
let &(ref mut f, ref mut val, ref mut first) = st;
if *first {
*first = false;
} else {
match val.take() {
Some(x) => {
*val = Some((*f)(x))
}
None => {}
}
}
val.clone()
}
let next: fn(&mut IterateState<T,F>) -> Option<T> = next;
Unfold::new((f, Some(seed), true), next)
}
#[inline]
#[stable]
pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
Repeat{element: elt}
}
#[experimental = "likely to be removed after cmp reform"]
pub mod order {
use cmp;
use cmp::{Eq, Ord, PartialOrd, PartialEq};
use cmp::Ordering::{Equal, Less, Greater};
use option::Option;
use option::Option::{Some, None};
use super::Iterator;
pub fn equals<A: Eq, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
loop {
match (a.next(), b.next()) {
(None, None) => return true,
(None, _) | (_, None) => return false,
(Some(x), Some(y)) => if x != y { return false },
}
}
}
pub fn cmp<A: Ord, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> cmp::Ordering {
loop {
match (a.next(), b.next()) {
(None, None) => return Equal,
(None, _ ) => return Less,
(_ , None) => return Greater,
(Some(x), Some(y)) => match x.cmp(&y) {
Equal => (),
non_eq => return non_eq,
},
}
}
}
pub fn partial_cmp<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S)
-> Option<cmp::Ordering> {
loop {
match (a.next(), b.next()) {
(None, None) => return Some(Equal),
(None, _ ) => return Some(Less),
(_ , None) => return Some(Greater),
(Some(x), Some(y)) => match x.partial_cmp(&y) {
Some(Equal) => (),
non_eq => return non_eq,
},
}
}
}
pub fn eq<A, B, L, R>(mut a: L, mut b: R) -> bool where
A: PartialEq<B>,
L: Iterator<A>,
R: Iterator<B>,
{
loop {
match (a.next(), b.next()) {
(None, None) => return true,
(None, _) | (_, None) => return false,
(Some(x), Some(y)) => if !x.eq(&y) { return false },
}
}
}
pub fn ne<A, B, L, R>(mut a: L, mut b: R) -> bool where
A: PartialEq<B>,
L: Iterator<A>,
R: Iterator<B>,
{
loop {
match (a.next(), b.next()) {
(None, None) => return false,
(None, _) | (_, None) => return true,
(Some(x), Some(y)) => if x.ne(&y) { return true },
}
}
}
pub fn lt<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
loop {
match (a.next(), b.next()) {
(None, None) => return false,
(None, _ ) => return true,
(_ , None) => return false,
(Some(x), Some(y)) => if x.ne(&y) { return x.lt(&y) },
}
}
}
pub fn le<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
loop {
match (a.next(), b.next()) {
(None, None) => return true,
(None, _ ) => return true,
(_ , None) => return false,
(Some(x), Some(y)) => if x.ne(&y) { return x.le(&y) },
}
}
}
pub fn gt<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
loop {
match (a.next(), b.next()) {
(None, None) => return false,
(None, _ ) => return false,
(_ , None) => return true,
(Some(x), Some(y)) => if x.ne(&y) { return x.gt(&y) },
}
}
}
pub fn ge<A: PartialOrd, T: Iterator<A>, S: Iterator<A>>(mut a: T, mut b: S) -> bool {
loop {
match (a.next(), b.next()) {
(None, None) => return true,
(None, _ ) => return false,
(_ , None) => return true,
(Some(x), Some(y)) => if x.ne(&y) { return x.ge(&y) },
}
}
}
}