Skip to content

Commit 1156013

Browse files
committed
---
yaml --- r: 56208 b: refs/heads/auto c: 0ff568a h: refs/heads/master v: v3
1 parent d9e312b commit 1156013

File tree

4 files changed

+96
-9
lines changed

4 files changed

+96
-9
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1414
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1515
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1616
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
17-
refs/heads/auto: a35376e24ec28d79f4e29ec34061ba23353c3d57
17+
refs/heads/auto: 0ff568a3c1d4cc51109aacc88a598e6d3cfe5ea0
1818
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1919
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c

branches/auto/src/libcore/iterator.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub trait IteratorUtil<A> {
2222
// FIXME: #5898: should be called map
2323
fn transform<'r, B>(self, f: &'r fn(A) -> B) -> MapIterator<'r, A, B, Self>;
2424
fn filter<'r>(self, predicate: &'r fn(&A) -> bool) -> FilterIterator<'r, A, Self>;
25+
fn enumerate(self) -> EnumerateIterator<Self>;
2526
fn advance(&mut self, f: &fn(A) -> bool);
2627
}
2728

@@ -42,6 +43,11 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
4243
FilterIterator{iter: self, predicate: predicate}
4344
}
4445

46+
#[inline(always)]
47+
fn enumerate(self) -> EnumerateIterator<T> {
48+
EnumerateIterator{iter: self, count: 0}
49+
}
50+
4551
/// A shim implementing the `for` loop iteration protocol for iterator objects
4652
#[inline]
4753
fn advance(&mut self, f: &fn(A) -> bool) {
@@ -104,3 +110,22 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for MapIterator<'self, A, B, T> {
104110
}
105111
}
106112
}
113+
114+
pub struct EnumerateIterator<T> {
115+
priv iter: T,
116+
priv count: uint
117+
}
118+
119+
impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<T> {
120+
#[inline]
121+
fn next(&mut self) -> Option<(uint, A)> {
122+
match self.iter.next() {
123+
Some(a) => {
124+
let ret = Some((self.count, a));
125+
self.count += 1;
126+
ret
127+
}
128+
_ => None
129+
}
130+
}
131+
}

branches/auto/src/libcore/vec.rs

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
1818
use clone::Clone;
1919
use iter::BaseIter;
2020
use iter;
21+
#[cfg(stage1)]
22+
#[cfg(stage2)]
23+
#[cfg(stage3)]
24+
use iterator::Iterator;
2125
use kinds::Copy;
2226
use libc;
2327
use option::{None, Option, Some};
@@ -1919,6 +1923,7 @@ impl<'self,T> ImmutableVector<T> for &'self [T] {
19191923
#[cfg(stage3)]
19201924
pub trait ImmutableVector<'self, T> {
19211925
fn slice(&self, start: uint, end: uint) -> &'self [T];
1926+
fn iter(self) -> VecIterator<'self, T>;
19221927
fn head(&self) -> &'self T;
19231928
fn head_opt(&self) -> Option<&'self T>;
19241929
fn tail(&self) -> &'self [T];
@@ -1949,6 +1954,15 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
19491954
slice(*self, start, end)
19501955
}
19511956

1957+
#[inline]
1958+
fn iter(self) -> VecIterator<'self, T> {
1959+
unsafe {
1960+
let p = vec::raw::to_ptr(self);
1961+
VecIterator{ptr: p, end: p.offset(self.len()),
1962+
lifetime: cast::transmute(p)}
1963+
}
1964+
}
1965+
19521966
/// Returns the first element of a vector, failing if the vector is empty.
19531967
#[inline]
19541968
fn head(&self) -> &'self T { head(*self) }
@@ -2795,7 +2809,33 @@ impl<A:Clone> Clone for ~[A] {
27952809
}
27962810
}
27972811

2798-
// ___________________________________________________________________________
2812+
// could be implemented with &[T] with .slice(), but this avoids bounds checks
2813+
#[cfg(stage1)]
2814+
#[cfg(stage2)]
2815+
#[cfg(stage3)]
2816+
pub struct VecIterator<'self, T> {
2817+
priv ptr: *T,
2818+
priv end: *T,
2819+
priv lifetime: &'self T // FIXME: #5922
2820+
}
2821+
2822+
#[cfg(stage1)]
2823+
#[cfg(stage2)]
2824+
#[cfg(stage3)]
2825+
impl<'self, T> Iterator<&'self T> for VecIterator<'self, T> {
2826+
#[inline]
2827+
fn next(&mut self) -> Option<&'self T> {
2828+
unsafe {
2829+
if self.ptr == self.end {
2830+
None
2831+
} else {
2832+
let old = self.ptr;
2833+
self.ptr = self.ptr.offset(1);
2834+
Some(cast::transmute(old))
2835+
}
2836+
}
2837+
}
2838+
}
27992839

28002840
#[cfg(test)]
28012841
mod tests {
@@ -4421,6 +4461,29 @@ mod tests {
44214461
[1, 2, 3, 4, 5, 5, 5, 5].cmp(& &[1, 2, 3, 4, 5, 6]) == Less;
44224462
[2, 2].cmp(& &[1, 2, 3, 4]) == Greater;
44234463
}
4464+
4465+
#[test]
4466+
fn test_iterator() {
4467+
use iterator::*;
4468+
let xs = [1, 2, 5, 10, 11];
4469+
let ys = [1, 2, 5, 10, 11, 19];
4470+
let mut it = xs.iter();
4471+
let mut i = 0;
4472+
for it.advance |&x| {
4473+
assert_eq!(x, ys[i]);
4474+
i += 1;
4475+
}
4476+
}
4477+
4478+
#[test]
4479+
fn test_iterator_enumerate() {
4480+
use iterator::*;
4481+
let xs = [0u,1,2,3,4,5];
4482+
let mut it = xs.iter().enumerate();
4483+
for it.advance |(i, &x): (uint, &uint)| {
4484+
assert_eq!(i, x);
4485+
}
4486+
}
44244487
}
44254488

44264489
// Local Variables:

branches/auto/src/libstd/workcache.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -30,12 +30,11 @@ use core::to_bytes;
3030

3131
/**
3232
*
33-
* This is a loose clone of the [fbuild build system](https://github.com/felix-lang/fbuild),
34-
* made a touch more generic (not wired to special cases on files) and much
35-
* less metaprogram-y due to rust's comparative weakness there, relative to
36-
* python.
33+
* This is a loose clone of the fbuild build system, made a touch more
34+
* generic (not wired to special cases on files) and much less metaprogram-y
35+
* due to rust's comparative weakness there, relative to python.
3736
*
38-
* It's based around _imperative builds_ that happen to have some function
37+
* It's based around _imperative bulids_ that happen to have some function
3938
* calls cached. That is, it's _just_ a mechanism for describing cached
4039
* functions. This makes it much simpler and smaller than a "build system"
4140
* that produces an IR and evaluates it. The evaluation order is normal
@@ -55,7 +54,7 @@ use core::to_bytes;
5554
* Works are conceptually single units, but we store them most of the time
5655
* in maps of the form (type,name) => value. These are WorkMaps.
5756
*
58-
* A cached function divides the works it's interested in into inputs and
57+
* A cached function divides the works it's interested up into inputs and
5958
* outputs, and subdivides those into declared (input) works and
6059
* discovered (input and output) works.
6160
*

0 commit comments

Comments
 (0)