Skip to content

Commit 7158102

Browse files
committed
auto merge of #5810 : thestinger/rust/iterator, r=graydon
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.
2 parents 9f337a5 + 8bf9fc5 commit 7158102

File tree

5 files changed

+216
-164
lines changed

5 files changed

+216
-164
lines changed

src/libcore/core.rc

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ pub mod from_str;
176176
#[path = "num/num.rs"]
177177
pub mod num;
178178
pub mod iter;
179+
pub mod iterator;
179180
pub mod to_str;
180181
pub mod to_bytes;
181182
pub mod clone;

src/libcore/iterator.rs

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

src/libstd/serialize.rs

+15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use core::hashmap::{HashMap, HashSet};
2121
use core::trie::{TrieMap, TrieSet};
2222
use deque::Deque;
2323
use dlist::DList;
24+
#[cfg(stage1)]
25+
#[cfg(stage2)]
26+
#[cfg(stage3)]
2427
use treemap::{TreeMap, TreeSet};
2528

2629
pub trait Encoder {
@@ -738,6 +741,9 @@ impl<D: Decoder> Decodable<D> for TrieSet {
738741
}
739742
}
740743

744+
#[cfg(stage1)]
745+
#[cfg(stage2)]
746+
#[cfg(stage3)]
741747
impl<
742748
E: Encoder,
743749
K: Encodable<E> + Eq + TotalOrd,
@@ -755,6 +761,9 @@ impl<
755761
}
756762
}
757763

764+
#[cfg(stage1)]
765+
#[cfg(stage2)]
766+
#[cfg(stage3)]
758767
impl<
759768
D: Decoder,
760769
K: Decodable<D> + Eq + TotalOrd,
@@ -773,6 +782,9 @@ impl<
773782
}
774783
}
775784

785+
#[cfg(stage1)]
786+
#[cfg(stage2)]
787+
#[cfg(stage3)]
776788
impl<
777789
S: Encoder,
778790
T: Encodable<S> + Eq + TotalOrd
@@ -788,6 +800,9 @@ impl<
788800
}
789801
}
790802

803+
#[cfg(stage1)]
804+
#[cfg(stage2)]
805+
#[cfg(stage3)]
791806
impl<
792807
D: Decoder,
793808
T: Decodable<D> + Eq + TotalOrd

src/libstd/std.rc

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ pub mod rope;
7676
pub mod smallintmap;
7777
pub mod sort;
7878
pub mod dlist;
79+
#[cfg(stage1)]
80+
#[cfg(stage2)]
81+
#[cfg(stage3)]
7982
pub mod treemap;
8083

8184
// And ... other stuff

0 commit comments

Comments
 (0)