Skip to content

Commit

Permalink
auto merge of #5810 : thestinger/rust/iterator, r=graydon
Browse files Browse the repository at this point in the history
The current protocol is very comparable to Python, where `.__iter__()` returns an iterator object which implements `.__next__()` and throws `StopIteration` on completion. `Option` is much cleaner than using a exceptions as a flow control hack though. It requires that the container is frozen so there's no worry about invalidating them.

Advantages over internal iterators, which are functions that are passed closures and directly implement the iteration protocol:

* Iteration is stateful, so you can interleave iteration over arbitrary containers. That's needed to implement algorithms like zip, merge, set union, set intersection, set difference and symmetric difference. I already used this internally in the `TreeMap` and `TreeSet` implementations, but regions and traits weren't solid enough to make it generic yet.
* They provide a universal, generic interface. The same trait is used for a forward/reverse iterator, an iterator over a range, etc. Internal iterators end up resulting in a trait for each possible way you could iterate.
* They can be composed with adaptors like `ZipIterator`, which also implement the same trait themselves.

The disadvantage is that they're a pain to write without support from the compiler for compiling something like `yield` to a state machine. :)

This can coexist alongside internal iterators since both can use the current `for` protocol. It's easier to write an internal iterator, but external ones are far more powerful/useful so they should probably be provided whenever possible by the standard library.

## Current issues

#5801 is somewhat annoying since explicit type hints are required.

I just wanted to get the essentials working well, so I haven't put much thought into making the naming concise (free functions vs. static `new` methods, etc.).

Making an `Iterable` trait seems like it will have to be a long-term goal, requiring type system extensions. At least without resorting to objects which would probably be unacceptably slow.
  • Loading branch information
bors committed Apr 13, 2013
2 parents 9f337a5 + 8bf9fc5 commit 7158102
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 164 deletions.
1 change: 1 addition & 0 deletions src/libcore/core.rc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub mod from_str;
#[path = "num/num.rs"]
pub mod num;
pub mod iter;
pub mod iterator;
pub mod to_str;
pub mod to_bytes;
pub mod clone;
Expand Down
101 changes: 101 additions & 0 deletions src/libcore/iterator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// 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.

//! Composable iterator objects

use prelude::*;

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

/// 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 }
}
None => return
}
}
}

pub struct ZipIterator<T, U> {
priv a: T,
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)> {
match (self.a.next(), self.b.next()) {
(Some(x), Some(y)) => Some((x, y)),
_ => None
}
}
}

pub struct FilterIterator<'self, A, T> {
priv iter: T,
priv predicate: &'self fn(&A) -> bool
}

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| {
if (self.predicate)(&x) {
return Some(x);
} else {
loop
}
}
None
}
}

pub struct MapIterator<'self, A, B, T> {
priv iter: T,
priv f: &'self fn(A) -> B
}

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> {
match self.iter.next() {
Some(a) => Some((self.f)(a)),
_ => None
}
}
}
15 changes: 15 additions & 0 deletions src/libstd/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use core::hashmap::{HashMap, HashSet};
use core::trie::{TrieMap, TrieSet};
use deque::Deque;
use dlist::DList;
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
use treemap::{TreeMap, TreeSet};

pub trait Encoder {
Expand Down Expand Up @@ -738,6 +741,9 @@ impl<D: Decoder> Decodable<D> for TrieSet {
}
}

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
impl<
E: Encoder,
K: Encodable<E> + Eq + TotalOrd,
Expand All @@ -755,6 +761,9 @@ impl<
}
}

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
impl<
D: Decoder,
K: Decodable<D> + Eq + TotalOrd,
Expand All @@ -773,6 +782,9 @@ impl<
}
}

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
impl<
S: Encoder,
T: Encodable<S> + Eq + TotalOrd
Expand All @@ -788,6 +800,9 @@ impl<
}
}

#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
impl<
D: Decoder,
T: Decodable<D> + Eq + TotalOrd
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/std.rc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ pub mod rope;
pub mod smallintmap;
pub mod sort;
pub mod dlist;
#[cfg(stage1)]
#[cfg(stage2)]
#[cfg(stage3)]
pub mod treemap;

// And ... other stuff
Expand Down
Loading

0 comments on commit 7158102

Please sign in to comment.