Skip to content

Commit

Permalink
iterator: use an IteratorUtil trait
Browse files Browse the repository at this point in the history
  • Loading branch information
thestinger committed Apr 16, 2013
1 parent a581926 commit f82c964
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
69 changes: 37 additions & 32 deletions src/libcore/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,46 @@
use prelude::*;

pub trait Iterator<T> {
pub trait Iterator<A> {
/// Advance the iterator and return the next value. Return `None` when the end is reached.
fn next(&mut self) -> Option<T>;
fn next(&mut self) -> Option<A>;
}

/// A shim implementing the `for` loop iteration protocol for iterator objects
#[inline]
pub fn advance<T, U: Iterator<T>>(iter: &mut U, f: &fn(T) -> bool) {
loop {
match iter.next() {
Some(x) => {
if !f(x) { return }
pub trait IteratorUtil<A> {
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>;
// FIXME: #5898: should be called map
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, Self>;
fn advance(&mut self, f: &fn(A) -> bool);
}

impl<A, T: Iterator<A>> IteratorUtil<A> for T {
#[inline(always)]
fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<T, U> {
ZipIterator{a: self, b: other}
}

// FIXME: #5898: should be called map
#[inline(always)]
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, T> {
MapIterator{iter: self, f: f}
}

#[inline(always)]
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, T> {
FilterIterator{iter: self, predicate: predicate}
}

/// A shim implementing the `for` loop iteration protocol for iterator objects
#[inline]
fn advance(&mut self, f: &fn(A) -> bool) {
loop {
match self.next() {
Some(x) => {
if !f(x) { return }
}
None => return
}
None => return
}
}
}
Expand All @@ -35,13 +61,6 @@ pub struct ZipIterator<T, U> {
priv b: U
}

pub impl<A, B, T: Iterator<A>, U: Iterator<B>> ZipIterator<T, U> {
#[inline(always)]
fn new(a: T, b: U) -> ZipIterator<T, U> {
ZipIterator{a: a, b: b}
}
}

impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> {
#[inline]
fn next(&mut self) -> Option<(A, B)> {
Expand All @@ -57,17 +76,10 @@ pub struct FilterIterator<'self, A, T> {
priv predicate: &'self fn(&A) -> bool
}

pub impl<'self, A, T: Iterator<A>> FilterIterator<'self, A, T> {
#[inline(always)]
fn new(iter: T, predicate: &'self fn(&A) -> bool) -> FilterIterator<'self, A, T> {
FilterIterator{iter: iter, predicate: predicate}
}
}

impl<'self, A, T: Iterator<A>> Iterator<A> for FilterIterator<'self, A, T> {
#[inline]
fn next(&mut self) -> Option<A> {
for advance(self) |x| {
for self.iter.advance |x| {
if (self.predicate)(&x) {
return Some(x);
} else {
Expand All @@ -83,13 +95,6 @@ pub struct MapIterator<'self, A, B, T> {
priv f: &'self fn(A) -> B
}

pub impl<'self, A, B, T: Iterator<A>> MapIterator<'self, A, B, T> {
#[inline(always)]
fn new(iter: T, f: &'self fn(A) -> B) -> MapIterator<'self, A, B, T> {
MapIterator{iter: iter, f: f}
}
}

impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
#[inline]
fn next(&mut self) -> Option<B> {
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/treemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ mod test_treemap {
(&x5, &y5)];
let mut i = 0;

for advance(&mut b) |x| {
for b.advance |x| {
assert!(expected[i] == x);
i += 1;

Expand All @@ -1005,7 +1005,7 @@ mod test_treemap {
}
}

for advance(&mut b) |x| {
for b.advance |x| {
assert!(expected[i] == x);
i += 1;
}
Expand Down Expand Up @@ -1209,7 +1209,7 @@ mod test_set {

let x = x;
let y = y;
let mut z = ZipIterator::new(x.iter(), y.iter());
let mut z = x.iter().zip(y.iter());

// FIXME: #5801: this needs a type hint to compile...
let result: Option<(&uint, & &'static str)> = z.next();
Expand Down

5 comments on commit f82c964

@bors
Copy link
Contributor

@bors bors commented on f82c964 Apr 16, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on f82c964 Apr 16, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging thestinger/rust/iterator = f82c964 into auto

@bors
Copy link
Contributor

@bors bors commented on f82c964 Apr 16, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thestinger/rust/iterator = f82c964 merged ok, testing candidate = 76e77af

@bors
Copy link
Contributor

@bors bors commented on f82c964 Apr 16, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on f82c964 Apr 16, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = 76e77af

Please sign in to comment.