diff options
| author | Kevin Ballard <kevin@sb.org> | 2014-05-03 18:10:04 -0700 |
|---|---|---|
| committer | Kevin Ballard <kevin@sb.org> | 2014-05-08 12:06:21 -0700 |
| commit | 3296bd7e4686bf4b76d9b9ac5c9c7f61ef4eb9e7 (patch) | |
| tree | bf754254629c4597314e035ba0ceca82e162f75c /src/libstd | |
| parent | 21dae8e1e0916afd2f789a2f1affdfe580d9ca0e (diff) | |
| download | rust-3296bd7e4686bf4b76d9b9ac5c9c7f61ef4eb9e7.tar.gz rust-3296bd7e4686bf4b76d9b9ac5c9c7f61ef4eb9e7.zip | |
Rename slice::unzip() to vec::unzip()
unzip() has nothing to do with slices, so it belongs in vec.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/slice.rs | 31 | ||||
| -rw-r--r-- | src/libstd/sync/deque.rs | 4 | ||||
| -rw-r--r-- | src/libstd/vec.rs | 33 |
3 files changed, 35 insertions, 33 deletions
diff --git a/src/libstd/slice.rs b/src/libstd/slice.rs index a93f209459a..17c09b0a427 100644 --- a/src/libstd/slice.rs +++ b/src/libstd/slice.rs @@ -155,25 +155,6 @@ impl<'a, T: Clone, V: Vector<T>> VectorVector<T> for &'a [V] { } } -/** - * 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 iterator, - * and the i-th element of the second vector contains the second element - * of the i-th tuple of the input iterator. - */ -pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (~[T], ~[U]) { - let (lo, _) = iter.size_hint(); - let mut ts = Vec::with_capacity(lo); - let mut us = Vec::with_capacity(lo); - for (t, u) in iter { - ts.push(t); - us.push(u); - } - (ts.move_iter().collect(), us.move_iter().collect()) -} - /// An Iterator that yields the element swaps needed to produce /// a sequence of all possible permutations for an indexed sequence of /// elements. Each permutation is only a single swap apart. @@ -1243,18 +1224,6 @@ mod tests { } #[test] - fn test_zip_unzip() { - let z1 = vec![(1, 4), (2, 5), (3, 6)]; - - let (left, right) = unzip(z1.iter().map(|&x| x)); - - let (left, right) = (left.as_slice(), right.as_slice()); - assert_eq!((1, 4), (left[0], right[0])); - assert_eq!((2, 5), (left[1], right[1])); - assert_eq!((3, 6), (left[2], right[2])); - } - - #[test] fn test_element_swaps() { let mut v = [1, 2, 3]; for (i, (a, b)) in ElementSwaps::new(v.len()).enumerate() { diff --git a/src/libstd/sync/deque.rs b/src/libstd/sync/deque.rs index d06062f02ac..8dfd691e6ff 100644 --- a/src/libstd/sync/deque.rs +++ b/src/libstd/sync/deque.rs @@ -407,7 +407,7 @@ mod tests { use rand::Rng; use sync::atomics::{AtomicBool, INIT_ATOMIC_BOOL, SeqCst, AtomicUint, INIT_ATOMIC_UINT}; - use slice; + use vec; #[test] fn smoke() { @@ -603,7 +603,7 @@ mod tests { let mut pool = BufferPool::<(int, uint)>::new(); let (mut w, s) = pool.deque(); - let (threads, hits) = slice::unzip(range(0, NTHREADS).map(|_| { + let (threads, hits) = vec::unzip(range(0, NTHREADS).map(|_| { let s = s.clone(); let unique_box = box AtomicUint::new(0); let thread_box = unsafe { diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs index 9d83a5848fb..257dcc0fcfe 100644 --- a/src/libstd/vec.rs +++ b/src/libstd/vec.rs @@ -1436,10 +1436,31 @@ impl<T> Drop for MoveItems<T> { } } +/** + * 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 iterator, + * and the i-th element of the second vector contains the second element + * of the i-th tuple of the input iterator. + */ +pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (Vec<T>, Vec<U>) { + let (lo, _) = iter.size_hint(); + let mut ts = Vec::with_capacity(lo); + let mut us = Vec::with_capacity(lo); + for (t, u) in iter { + ts.push(t); + us.push(u); + } + (ts, us) +} + + #[cfg(test)] mod tests { use prelude::*; use mem::size_of; + use super::unzip; #[test] fn test_small_vec_struct() { @@ -1687,4 +1708,16 @@ mod tests { assert_eq!([&[1], &[2, 3]].connect_vec(&0), vec![1, 0, 2, 3]); assert_eq!([&[1], &[2], &[3]].connect_vec(&0), vec![1, 0, 2, 0, 3]); } + + #[test] + fn test_zip_unzip() { + let z1 = vec![(1, 4), (2, 5), (3, 6)]; + + let (left, right) = unzip(z1.iter().map(|&x| x)); + + let (left, right) = (left.as_slice(), right.as_slice()); + assert_eq!((1, 4), (left[0], right[0])); + assert_eq!((2, 5), (left[1], right[1])); + assert_eq!((3, 6), (left[2], right[2])); + } } |
