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())`
This commit is contained in:
blake2-ppc 2013-09-09 04:46:32 +02:00
parent 79e78c4b0c
commit 6212729315
3 changed files with 14 additions and 32 deletions

View File

@ -112,7 +112,7 @@ pub fn parse_config_(
process_output: Process process_output: Process
) -> Result<Config, ~str> { ) -> Result<Config, ~str> {
let args = args.tail(); let args = args.tail();
let opts = vec::unzip(opts()).first(); let opts = vec::unzip(opts().move_iter()).first();
match getopts::getopts(args, opts) { match getopts::getopts(args, opts) {
Ok(matches) => { Ok(matches) => {
if matches.free.len() == 1 { if matches.free.len() == 1 {

View File

@ -148,7 +148,7 @@ mod test {
// Unfortunately this does not actually test the block_on early-break // Unfortunately this does not actually test the block_on early-break
// codepath in select -- racing between the sender and the receiver in // codepath in select -- racing between the sender and the receiver in
// separate tasks is necessary to get around the optimistic check. // separate tasks is necessary to get around the optimistic check.
let (ports, chans) = unzip(from_fn(num_ports, |_| oneshot::<()>())); let (ports, chans) = unzip(range(0, num_ports).map(|_| oneshot::<()>()));
let mut dead_chans = ~[]; let mut dead_chans = ~[];
let mut ports = ports; let mut ports = ports;
for (i, chan) in chans.move_iter().enumerate() { for (i, chan) in chans.move_iter().enumerate() {
@ -165,7 +165,7 @@ mod test {
// Same thing with streams instead. // Same thing with streams instead.
// FIXME(#7971): This should be in a macro but borrowck isn't smart enough. // FIXME(#7971): This should be in a macro but borrowck isn't smart enough.
let (ports, chans) = unzip(from_fn(num_ports, |_| stream::<()>())); let (ports, chans) = unzip(range(0, num_ports).map(|_| stream::<()>()));
let mut dead_chans = ~[]; let mut dead_chans = ~[];
let mut ports = ports; let mut ports = ports;
for (i, chan) in chans.move_iter().enumerate() { for (i, chan) in chans.move_iter().enumerate() {
@ -209,7 +209,7 @@ mod test {
// Sends 10 buffered packets, and uses select to retrieve them all. // Sends 10 buffered packets, and uses select to retrieve them all.
// Puts the port in a different spot in the vector each time. // Puts the port in a different spot in the vector each time.
do run_in_newsched_task { do run_in_newsched_task {
let (ports, _) = unzip(from_fn(10, |_| stream())); let (ports, _) = unzip(range(0u, 10).map(|_| stream::<int>()));
let (port, chan) = stream(); let (port, chan) = stream();
do 10.times { chan.send(31337); } do 10.times { chan.send(31337); }
let mut ports = ports; let mut ports = ports;
@ -327,7 +327,7 @@ mod test {
let (p,c) = oneshot(); let (p,c) = oneshot();
let c = Cell::new(c); let c = Cell::new(c);
do task::spawn { do task::spawn {
let (dead_ps, dead_cs) = unzip(from_fn(5, |_| oneshot::<()>())); let (dead_ps, dead_cs) = unzip(range(0u, 5).map(|_| oneshot::<()>()));
let mut ports = dead_ps; let mut ports = dead_ps;
select(ports); // should get killed; nothing should leak select(ports); // should get killed; nothing should leak
c.take().send(()); // must not happen c.take().send(()); // must not happen

View File

@ -389,37 +389,19 @@ impl<'self,T:Clone> VectorVector<T> 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(). * Convert an iterator of pairs into a pair of vectors.
*/
pub fn unzip_slice<T:Clone,U:Clone>(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.
* *
* Returns a tuple containing two vectors where the i-th element of the first * 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 * 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<T,U>(v: ~[(T, U)]) -> (~[T], ~[U]) { pub fn unzip<T, U, V: Iterator<(T, U)>>(mut iter: V) -> (~[T], ~[U]) {
let mut ts = ~[]; let (lo, _) = iter.size_hint();
let mut us = ~[]; let mut ts = with_capacity(lo);
for p in v.move_iter() { let mut us = with_capacity(lo);
let (t, u) = p; for (t, u) in iter {
ts.push(t); ts.push(t);
us.push(u); us.push(u);
} }
@ -2891,7 +2873,7 @@ mod tests {
fn test_zip_unzip() { fn test_zip_unzip() {
let z1 = ~[(1, 4), (2, 5), (3, 6)]; 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!((1, 4), (left[0], right[0]));
assert_eq!((2, 5), (left[1], right[1])); assert_eq!((2, 5), (left[1], right[1]));