From 6212729315be2ac80785ffcecfe0a80c9955c4cf Mon Sep 17 00:00:00 2001 From: blake2-ppc Date: Mon, 9 Sep 2013 04:46:32 +0200 Subject: std::vec: Change fn unzip to take an iterator argument Remove unzip_slice since it's redundant. Old unzip is equivalent to the `|x| unzip(x.move_iter())` --- src/libstd/vec.rs | 36 +++++++++--------------------------- 1 file changed, 9 insertions(+), 27 deletions(-) (limited to 'src/libstd/vec.rs') diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 4cc5c4f14ff..10a0bf27836 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -389,37 +389,19 @@ impl<'self,T:Clone> VectorVector for &'self [&'self [T]] { } } -// FIXME: if issue #586 gets implemented, could have a postcondition -// saying the two result lists have the same length -- or, could -// return a nominal record with a constraint saying that, instead of -// returning a tuple (contingent on issue #869) /** - * Convert a vector of pairs into a pair of vectors, by reference. As unzip(). - */ -pub fn unzip_slice(v: &[(T, U)]) -> (~[T], ~[U]) { - let mut ts = ~[]; - let mut us = ~[]; - for p in v.iter() { - let (t, u) = (*p).clone(); - ts.push(t); - us.push(u); - } - (ts, us) -} - -/** - * Convert a vector of pairs into a pair of vectors. + * Convert an iterator of pairs into a pair of vectors. * * Returns a tuple containing two vectors where the i-th element of the first - * vector contains the first element of the i-th tuple of the input vector, + * vector contains the first element of the i-th tuple of the input iterator, * and the i-th element of the second vector contains the second element - * of the i-th tuple of the input vector. + * of the i-th tuple of the input iterator. */ -pub fn unzip(v: ~[(T, U)]) -> (~[T], ~[U]) { - let mut ts = ~[]; - let mut us = ~[]; - for p in v.move_iter() { - let (t, u) = p; +pub fn unzip>(mut iter: V) -> (~[T], ~[U]) { + let (lo, _) = iter.size_hint(); + let mut ts = with_capacity(lo); + let mut us = with_capacity(lo); + for (t, u) in iter { ts.push(t); us.push(u); } @@ -2891,7 +2873,7 @@ mod tests { fn test_zip_unzip() { let z1 = ~[(1, 4), (2, 5), (3, 6)]; - let (left, right) = unzip(z1); + let (left, right) = unzip(z1.iter().map(|&x| x)); assert_eq!((1, 4), (left[0], right[0])); assert_eq!((2, 5), (left[1], right[1])); -- cgit 1.4.1-3-g733a5