Skip to content

Commit ccf6353

Browse files
committed
auto merge of #6589 : thestinger/rust/iterator, r=thestinger
2 parents 3ee479f + 9b6b0e1 commit ccf6353

File tree

1 file changed

+27
-76
lines changed

1 file changed

+27
-76
lines changed

src/libcore/iterator.rs

Lines changed: 27 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ pub trait IteratorUtil<A> {
4747
fn advance(&mut self, f: &fn(A) -> bool);
4848
#[cfg(not(stage0))]
4949
fn advance(&mut self, f: &fn(A) -> bool) -> bool;
50-
fn to_vec(self) -> ~[A];
51-
fn nth(&mut self, n: uint) -> A;
52-
fn first(&mut self) -> A;
53-
fn last(&mut self) -> A;
50+
#[cfg(not(stage0))]
51+
fn to_vec(&mut self) -> ~[A];
52+
fn nth(&mut self, n: uint) -> Option<A>;
53+
fn last(&mut self) -> Option<A>;
5454
fn fold<B>(&mut self, start: B, f: &fn(B, A) -> B) -> B;
5555
fn count(&mut self) -> uint;
5656
fn all(&mut self, f: &fn(&A) -> bool) -> bool;
@@ -147,47 +147,30 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
147147
}
148148
}
149149

150+
#[cfg(not(stage0))]
150151
#[inline(always)]
151-
fn to_vec(self) -> ~[A] {
152-
let mut v = ~[];
153-
let mut it = self;
154-
for it.advance() |x| { v.push(x); }
155-
return v;
152+
fn to_vec(&mut self) -> ~[A] {
153+
iter::to_vec::<A>(|f| self.advance(f))
156154
}
157155

158-
/// Get `n`th element of an iterator.
156+
/// Return the `n`th item yielded by an iterator.
159157
#[inline(always)]
160-
fn nth(&mut self, n: uint) -> A {
161-
let mut i = n;
158+
fn nth(&mut self, mut n: uint) -> Option<A> {
162159
loop {
163160
match self.next() {
164-
Some(x) => { if i == 0 { return x; }}
165-
None => { fail!("cannot get %uth element", n) }
161+
Some(x) => if n == 0 { return Some(x) },
162+
None => return None
166163
}
167-
i -= 1;
164+
n -= 1;
168165
}
169166
}
170167

171-
// Get first elemet of an iterator.
168+
/// Return the last item yielded by an iterator.
172169
#[inline(always)]
173-
fn first(&mut self) -> A {
174-
match self.next() {
175-
Some(x) => x ,
176-
None => fail!("cannot get first element")
177-
}
178-
}
179-
180-
// Get last element of an iterator.
181-
//
182-
// If the iterator have an infinite length, this method won't return.
183-
#[inline(always)]
184-
fn last(&mut self) -> A {
185-
let mut elm = match self.next() {
186-
Some(x) => x,
187-
None => fail!("cannot get last element")
188-
};
189-
for self.advance |e| { elm = e; }
190-
return elm;
170+
fn last(&mut self) -> Option<A> {
171+
let mut last = None;
172+
for self.advance |x| { last = Some(x); }
173+
last
191174
}
192175

193176
/// Reduce an iterator to an accumulated value
@@ -203,7 +186,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
203186
return accum;
204187
}
205188

206-
/// Count the number of an iterator elemenrs
189+
/// Count the number of items yielded by an iterator
207190
#[inline(always)]
208191
fn count(&mut self) -> uint { self.fold(0, |cnt, _x| cnt + 1) }
209192

@@ -344,17 +327,13 @@ pub struct FilterMapIterator<'self, A, B, T> {
344327
impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B, T> {
345328
#[inline]
346329
fn next(&mut self) -> Option<B> {
347-
loop {
348-
match self.iter.next() {
349-
None => { return None; }
350-
Some(a) => {
351-
match (self.f)(a) {
352-
Some(b) => { return Some(b); }
353-
None => { loop; }
354-
}
355-
}
330+
for self.iter.advance |x| {
331+
match (self.f)(x) {
332+
Some(y) => return Some(y),
333+
None => ()
356334
}
357335
}
336+
None
358337
}
359338
}
360339

@@ -579,7 +558,7 @@ mod tests {
579558

580559
#[test]
581560
fn test_filter_map() {
582-
let it = Counter::new(0u, 1u).take(10)
561+
let mut it = Counter::new(0u, 1u).take(10)
583562
.filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None });
584563
assert_eq!(it.to_vec(), ~[0*0, 2*2, 4*4, 6*6, 8*8]);
585564
}
@@ -689,43 +668,15 @@ mod tests {
689668
fn test_iterator_nth() {
690669
let v = &[0, 1, 2, 3, 4];
691670
for uint::range(0, v.len()) |i| {
692-
assert_eq!(v.iter().nth(i), &v[i]);
671+
assert_eq!(v.iter().nth(i).unwrap(), &v[i]);
693672
}
694673
}
695674

696-
#[test]
697-
#[should_fail]
698-
fn test_iterator_nth_fail() {
699-
let v = &[0, 1, 2, 3, 4];
700-
v.iter().nth(5);
701-
}
702-
703-
#[test]
704-
fn test_iterator_first() {
705-
let v = &[0, 1, 2, 3, 4];
706-
assert_eq!(v.iter().first(), &0);
707-
assert_eq!(v.slice(2, 5).iter().first(), &2);
708-
}
709-
710-
#[test]
711-
#[should_fail]
712-
fn test_iterator_first_fail() {
713-
let v: &[uint] = &[];
714-
v.iter().first();
715-
}
716-
717675
#[test]
718676
fn test_iterator_last() {
719677
let v = &[0, 1, 2, 3, 4];
720-
assert_eq!(v.iter().last(), &4);
721-
assert_eq!(v.slice(0, 1).iter().last(), &0);
722-
}
723-
724-
#[test]
725-
#[should_fail]
726-
fn test_iterator_last_fail() {
727-
let v: &[uint] = &[];
728-
v.iter().last();
678+
assert_eq!(v.iter().last().unwrap(), &4);
679+
assert_eq!(v.slice(0, 1).iter().last().unwrap(), &0);
729680
}
730681

731682
#[test]

0 commit comments

Comments
 (0)