diff options
| author | Daniel Micay <danielmicay@gmail.com> | 2013-06-07 14:43:02 -0400 |
|---|---|---|
| committer | Daniel Micay <danielmicay@gmail.com> | 2013-06-07 14:43:02 -0400 |
| commit | d945543ebf446913a44375169aa23686ffaa6205 (patch) | |
| tree | 5ffb15c6376e9a94ffa98fa81ffb8382b79b57e3 /src/libstd | |
| parent | 18019a1304c5cf6ec6f04e43b030602c8fec0e01 (diff) | |
| parent | 54d914a9a932a0e9e65195f0f5be91e5e8b7f342 (diff) | |
| download | rust-d945543ebf446913a44375169aa23686ffaa6205.tar.gz rust-d945543ebf446913a44375169aa23686ffaa6205.zip | |
Merge branch 'each-fn-kill' of https://github.com/huonw/rust into each-fn-kill
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/iterator.rs | 39 | ||||
| -rw-r--r-- | src/libstd/pipes.rs | 12 | ||||
| -rw-r--r-- | src/libstd/prelude.rs | 1 | ||||
| -rw-r--r-- | src/libstd/trie.rs | 3 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 91 |
5 files changed, 34 insertions, 112 deletions
diff --git a/src/libstd/iterator.rs b/src/libstd/iterator.rs index 7f723e44c2b..4bc545f607d 100644 --- a/src/libstd/iterator.rs +++ b/src/libstd/iterator.rs @@ -55,7 +55,7 @@ pub trait IteratorUtil<A> { /// assert_eq!(it.next().get(), &1); /// assert!(it.next().is_none()); /// ~~~ - fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<Self, U>; + fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<A, Self, U>; /// Creates an iterator which iterates over both this and the specified /// iterators simultaneously, yielding the two elements as pairs. When @@ -73,7 +73,7 @@ pub trait IteratorUtil<A> { /// assert_eq!(it.next().get(), (&0, &1)); /// assert!(it.next().is_none()); /// ~~~ - fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<Self, U>; + fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<A, Self, B, U>; // FIXME: #5898: should be called map /// Creates a new iterator which will apply the specified function to each @@ -139,7 +139,7 @@ pub trait IteratorUtil<A> { /// assert_eq!(it.next().get(), (1, &200)); /// assert!(it.next().is_none()); /// ~~~ - fn enumerate(self) -> EnumerateIterator<Self>; + fn enumerate(self) -> EnumerateIterator<A, Self>; /// Creates an iterator which invokes the predicate on elements until it /// returns true. Once the predicate returns true, all further elements are @@ -349,12 +349,12 @@ pub trait IteratorUtil<A> { /// In the future these will be default methods instead of a utility trait. impl<A, T: Iterator<A>> IteratorUtil<A> for T { #[inline(always)] - fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<T, U> { + fn chain<U: Iterator<A>>(self, other: U) -> ChainIterator<A, T, U> { ChainIterator{a: self, b: other, flag: false} } #[inline(always)] - fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<T, U> { + fn zip<B, U: Iterator<B>>(self, other: U) -> ZipIterator<A, T, B, U> { ZipIterator{a: self, b: other} } @@ -375,7 +375,7 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T { } #[inline(always)] - fn enumerate(self) -> EnumerateIterator<T> { + fn enumerate(self) -> EnumerateIterator<A, T> { EnumerateIterator{iter: self, count: 0} } @@ -570,13 +570,14 @@ impl<A: Ord, T: Iterator<A>> OrdIterator<A> for T { } /// An iterator which strings two iterators together -pub struct ChainIterator<T, U> { +// FIXME #6967: Dummy A parameter to get around type inference bug +pub struct ChainIterator<A, T, U> { priv a: T, priv b: U, priv flag: bool } -impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<T, U> { +impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<A, T, U> { #[inline] fn next(&mut self) -> Option<A> { if self.flag { @@ -593,12 +594,13 @@ impl<A, T: Iterator<A>, U: Iterator<A>> Iterator<A> for ChainIterator<T, U> { } /// An iterator which iterates two other iterators simultaneously -pub struct ZipIterator<T, U> { +// FIXME #6967: Dummy A & B parameters to get around type inference bug +pub struct ZipIterator<A, T, B, U> { priv a: T, priv b: U } -impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<T, U> { +impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<(A, B)> for ZipIterator<A, T, B, U> { #[inline] fn next(&mut self) -> Option<(A, B)> { match (self.a.next(), self.b.next()) { @@ -664,12 +666,13 @@ impl<'self, A, B, T: Iterator<A>> Iterator<B> for FilterMapIterator<'self, A, B, } /// An iterator which yields the current count and the element during iteration -pub struct EnumerateIterator<T> { +// FIXME #6967: Dummy A parameter to get around type inference bug +pub struct EnumerateIterator<A, T> { priv iter: T, priv count: uint } -impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<T> { +impl<A, T: Iterator<A>> Iterator<(uint, A)> for EnumerateIterator<A, T> { #[inline] fn next(&mut self) -> Option<(uint, A)> { match self.iter.next() { @@ -887,7 +890,7 @@ mod tests { let expected = [0, 1, 2, 3, 4, 5, 30, 40, 50, 60]; let mut it = xs.iter().chain(ys.iter()); let mut i = 0; - for it.advance |&x: &uint| { + for it.advance |&x| { assert_eq!(x, expected[i]); i += 1; } @@ -896,7 +899,7 @@ mod tests { let ys = Counter::new(30u, 10).take(4); let mut it = xs.iter().transform(|&x| x).chain(ys); let mut i = 0; - for it.advance |x: uint| { + for it.advance |x| { assert_eq!(x, expected[i]); i += 1; } @@ -906,7 +909,7 @@ mod tests { #[test] fn test_filter_map() { let mut it = Counter::new(0u, 1u).take(10) - .filter_map(|x: uint| if x.is_even() { Some(x*x) } else { None }); + .filter_map(|x| if x.is_even() { Some(x*x) } else { None }); assert_eq!(it.collect::<~[uint]>(), ~[0*0, 2*2, 4*4, 6*6, 8*8]); } @@ -914,7 +917,7 @@ mod tests { fn test_iterator_enumerate() { let xs = [0u, 1, 2, 3, 4, 5]; let mut it = xs.iter().enumerate(); - for it.advance |(i, &x): (uint, &uint)| { + for it.advance |(i, &x)| { assert_eq!(i, x); } } @@ -925,7 +928,7 @@ mod tests { let ys = [0u, 1, 2, 3, 5, 13]; let mut it = xs.iter().take_while(|&x| *x < 15u); let mut i = 0; - for it.advance |&x: &uint| { + for it.advance |&x| { assert_eq!(x, ys[i]); i += 1; } @@ -938,7 +941,7 @@ mod tests { let ys = [15, 16, 17, 19]; let mut it = xs.iter().skip_while(|&x| *x < 15u); let mut i = 0; - for it.advance |&x: &uint| { + for it.advance |&x| { assert_eq!(x, ys[i]); i += 1; } diff --git a/src/libstd/pipes.rs b/src/libstd/pipes.rs index fe1b834813e..1137540ae70 100644 --- a/src/libstd/pipes.rs +++ b/src/libstd/pipes.rs @@ -87,6 +87,7 @@ bounded and unbounded protocols allows for less code duplication. use container::Container; use cast::{forget, transmute, transmute_copy}; use either::{Either, Left, Right}; +use iterator::IteratorUtil; use kinds::Owned; use libc; use ops::Drop; @@ -96,8 +97,7 @@ use unstable::intrinsics; use ptr; use ptr::RawPtr; use task; -use vec; -use vec::OwnedVector; +use vec::{OwnedVector, MutableVector}; use util::replace; static SPIN_COUNT: uint = 0; @@ -600,7 +600,7 @@ pub fn wait_many<T: Selectable>(pkts: &mut [T]) -> uint { let mut data_avail = false; let mut ready_packet = pkts.len(); - for vec::eachi_mut(pkts) |i, p| { + for pkts.mut_iter().enumerate().advance |(i, p)| { unsafe { let p = &mut *p.header(); let old = p.mark_blocked(this); @@ -622,7 +622,7 @@ pub fn wait_many<T: Selectable>(pkts: &mut [T]) -> uint { let event = wait_event(this) as *PacketHeader; let mut pos = None; - for vec::eachi_mut(pkts) |i, p| { + for pkts.mut_iter().enumerate().advance |(i, p)| { if p.header() == event { pos = Some(i); break; @@ -640,7 +640,7 @@ pub fn wait_many<T: Selectable>(pkts: &mut [T]) -> uint { debug!("%?", &mut pkts[ready_packet]); - for vec::each_mut(pkts) |p| { + for pkts.mut_iter().advance |p| { unsafe { (*p.header()).unblock() } @@ -853,7 +853,7 @@ pub fn select<T:Owned,Tb:Owned>(mut endpoints: ~[RecvPacketBuffered<T, Tb>]) Option<T>, ~[RecvPacketBuffered<T, Tb>]) { let mut endpoint_headers = ~[]; - for vec::each_mut(endpoints) |endpoint| { + for endpoints.mut_iter().advance |endpoint| { endpoint_headers.push(endpoint.header()); } diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index bb10cec30ed..89992003026 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -82,4 +82,3 @@ pub use io::{Reader, ReaderUtil, Writer, WriterUtil}; // Reexported runtime types pub use comm::{stream, Port, Chan, GenericChan, GenericSmartChan, GenericPort, Peekable}; pub use task::spawn; - diff --git a/src/libstd/trie.rs b/src/libstd/trie.rs index aea03b437ca..7899edbfcb9 100644 --- a/src/libstd/trie.rs +++ b/src/libstd/trie.rs @@ -11,6 +11,7 @@ //! An ordered map and set for integer keys implemented as a radix trie use prelude::*; +use iterator::IteratorUtil; use uint; use util::{swap, replace}; use vec; @@ -277,7 +278,7 @@ impl<T> TrieNode<T> { } fn mutate_values<'a>(&'a mut self, f: &fn(&uint, &mut T) -> bool) -> bool { - for vec::each_mut(self.children) |child| { + for self.children.mut_iter().advance |child| { match *child { Internal(ref mut x) => if !x.mutate_values(f) { return false diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 8340b956b65..d8424f4a29e 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -19,7 +19,7 @@ use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater}; use clone::Clone; use old_iter::BaseIter; use old_iter; -use iterator::Iterator; +use iterator::{Iterator, IteratorUtil}; use iter::FromIter; use kinds::Copy; use libc; @@ -1568,28 +1568,6 @@ pub fn each<'r,T>(v: &'r [T], f: &fn(&'r T) -> bool) -> bool { return !broke; } -/// Like `each()`, but for the case where you have -/// a vector with mutable contents and you would like -/// to mutate the contents as you iterate. -#[inline(always)] -pub fn each_mut<'r,T>(v: &'r mut [T], f: &fn(elem: &'r mut T) -> bool) -> bool { - let mut broke = false; - do as_mut_buf(v) |p, n| { - let mut n = n; - let mut p = p; - while n > 0 { - unsafe { - let q: &'r mut T = cast::transmute_mut_region(&mut *p); - if !f(q) { break; } - p = p.offset(1); - } - n -= 1; - } - broke = n > 0; - } - return !broke; -} - /// Like `each()`, but for the case where you have a vector that *may or may /// not* have mutable contents. #[inline(always)] @@ -1621,24 +1599,6 @@ pub fn eachi<'r,T>(v: &'r [T], f: &fn(uint, v: &'r T) -> bool) -> bool { } /** - * Iterates over a mutable vector's elements and indices - * - * Return true to continue, false to break. - */ -#[inline(always)] -pub fn eachi_mut<'r,T>(v: &'r mut [T], - f: &fn(uint, v: &'r mut T) -> bool) -> bool { - let mut i = 0; - for each_mut(v) |p| { - if !f(i, p) { - return false; - } - i += 1; - } - return true; -} - -/** * Iterates over a vector's elements in reverse * * Return true to continue, false to break. @@ -1667,44 +1627,6 @@ pub fn eachi_reverse<'r,T>(v: &'r [T], } /** - * Iterates over two vectors simultaneously - * - * # Failure - * - * Both vectors must have the same length - */ -#[inline] -pub fn each2<U, T>(v1: &[U], v2: &[T], f: &fn(u: &U, t: &T) -> bool) -> bool { - assert_eq!(v1.len(), v2.len()); - for uint::range(0u, v1.len()) |i| { - if !f(&v1[i], &v2[i]) { - return false; - } - } - return true; -} - -/** - * - * Iterates over two vector with mutable. - * - * # Failure - * - * Both vectors must have the same length - */ -#[inline] -pub fn each2_mut<U, T>(v1: &mut [U], v2: &mut [T], - f: &fn(u: &mut U, t: &mut T) -> bool) -> bool { - assert_eq!(v1.len(), v2.len()); - for uint::range(0u, v1.len()) |i| { - if !f(&mut v1[i], &mut v2[i]) { - return false; - } - } - return true; -} - -/** * Iterate over all permutations of vector `v`. * * Permutations are produced in lexicographic order with respect to the order @@ -2738,7 +2660,7 @@ impl<A> old_iter::BaseIter<A> for @[A] { impl<'self,A> old_iter::MutableIter<A> for &'self mut [A] { #[inline(always)] fn each_mut<'a>(&'a mut self, blk: &fn(v: &'a mut A) -> bool) -> bool { - each_mut(*self, blk) + self.mut_iter().advance(blk) } } @@ -2746,7 +2668,7 @@ impl<'self,A> old_iter::MutableIter<A> for &'self mut [A] { impl<A> old_iter::MutableIter<A> for ~[A] { #[inline(always)] fn each_mut<'a>(&'a mut self, blk: &fn(v: &'a mut A) -> bool) -> bool { - each_mut(*self, blk) + self.mut_iter().advance(blk) } } @@ -2754,7 +2676,7 @@ impl<A> old_iter::MutableIter<A> for ~[A] { impl<A> old_iter::MutableIter<A> for @mut [A] { #[inline(always)] fn each_mut(&mut self, blk: &fn(v: &mut A) -> bool) -> bool { - each_mut(*self, blk) + self.mut_iter().advance(blk) } } @@ -2786,7 +2708,7 @@ impl<'self,A> old_iter::ExtendedIter<A> for &'self [A] { impl<'self,A> old_iter::ExtendedMutableIter<A> for &'self mut [A] { #[inline(always)] pub fn eachi_mut(&mut self, blk: &fn(uint, v: &mut A) -> bool) -> bool { - eachi_mut(*self, blk) + self.mut_iter().enumerate().advance(|(i, v)| blk(i, v)) } } @@ -3646,16 +3568,13 @@ mod tests { fn test_each_ret_len0() { let mut a0 : [int, .. 0] = []; assert_eq!(each(a0, |_p| fail!()), true); - assert_eq!(each_mut(a0, |_p| fail!()), true); } #[test] fn test_each_ret_len1() { let mut a1 = [17]; assert_eq!(each(a1, |_p| true), true); - assert_eq!(each_mut(a1, |_p| true), true); assert_eq!(each(a1, |_p| false), false); - assert_eq!(each_mut(a1, |_p| false), false); } |
