Skip to content

Commit 8bf9fc5

Browse files
committed
initial iterator object library
1 parent 65ff441 commit 8bf9fc5

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)