diff options
| author | bors <bors@rust-lang.org> | 2013-05-13 17:40:46 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-05-13 17:40:46 -0700 |
| commit | 7d81c80c65f8ed8f06d703b9d0635c8af521ea76 (patch) | |
| tree | 01917c8253a9eefd68982bfa6bcf402e54789cef /src | |
| parent | ad5bfd600d1611c9d1bb933a383d0db2fa0ee7bf (diff) | |
| parent | 1d1c502dd4727401865258fe60b4f858947c40a0 (diff) | |
auto merge of #6388 : recrack/rust/each2_mut, r=pcwalton
- vec.rs :add 'each2_mut function' - testsuit : run-pass/vec-each2_mut.rs
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/vec.rs | 33 | ||||
| -rw-r--r-- | src/test/run-pass/vec-each2_mut.rs | 38 |
2 files changed, 69 insertions, 2 deletions
diff --git a/src/libcore/vec.rs b/src/libcore/vec.rs index e56144ebc0a..21c3dac16f6 100644 --- a/src/libcore/vec.rs +++ b/src/libcore/vec.rs @@ -1681,8 +1681,8 @@ pub fn eachi_reverse<'r,T>(v: &'r [T], */ #[inline] pub fn _each2<U, T>(v1: &[U], v2: &[T], f: &fn(u: &U, t: &T) -> bool) -> bool { - assert!(len(v1) == len(v2)); - for uint::range(0u, len(v1)) |i| { + assert!(v1.len() == v2.len()); + for uint::range(0u, v1.len()) |i| { if !f(&v1[i], &v2[i]) { return false; } @@ -1700,6 +1700,35 @@ pub fn each2<U, T>(v1: &[U], v2: &[T], f: &fn(u: &U, t: &T) -> bool) -> bool { } /** + * + * 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!(v1.len() == v2.len()); + for uint::range(0u, v1.len()) |i| { + if !f(&mut v1[i], &mut v2[i]) { + return false; + } + } + return true; +} + +#[cfg(stage0)] +pub fn each2_mut<U, T>(v1: &mut [U], v2: &mut [T], f: &fn(u: &mut U, t: &mut T) -> bool) { + _each2_mut(v1, v2, f); +} + +#[cfg(not(stage0))] +pub fn each2_mut<U, T>(v1: &mut [U], v2: &mut [T], f: &fn(u: &mut U, t: &mut T) -> bool) -> bool { + _each2_mut(v1, v2, f) +} + +/** * Iterate over all permutations of vector `v`. * * Permutations are produced in lexicographic order with respect to the order diff --git a/src/test/run-pass/vec-each2_mut.rs b/src/test/run-pass/vec-each2_mut.rs new file mode 100644 index 00000000000..3c6b7da9f14 --- /dev/null +++ b/src/test/run-pass/vec-each2_mut.rs @@ -0,0 +1,38 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// -*- rust -*- +fn main(){ + let mut t1 = ~[]; + t1.push('a'); + + let mut t2 = ~[]; + t2.push('b'); + + for vec::each2_mut(t1, t2) | i1, i2 | { + assert!(*i1 == 'a'); + assert!(*i2 == 'b'); + } + + for vec::each2(t1, t2) | i1, i2 | { + io::println(fmt!("after t1: %?, t2: %?", i1, i2)); + } + + for vec::each2_mut(t1, t2) | i1, i2 | { + *i1 = 'b'; + *i2 = 'a'; + assert!(*i1 == 'b'); + assert!(*i2 == 'a'); + } + + for vec::each2(t1, t2) | i1, i2 | { + io::println(fmt!("before t1: %?, t2: %?", i1, i2)); + } +} |
