mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-12 15:04:03 +00:00
Add reverse iterators to std::vec
This commit is contained in:
parent
f1f0e6c06c
commit
7a7940daca
@ -663,6 +663,35 @@ fn iter2<T>(v: [mutable? T], f: block(uint, T)) {
|
||||
for x in v { f(i, x); i += 1u; }
|
||||
}
|
||||
|
||||
/*
|
||||
Function: riter
|
||||
|
||||
Iterates over a vector in reverse
|
||||
|
||||
Iterates over vector `v` and, for each element, calls function `f` with the
|
||||
element's value.
|
||||
|
||||
*/
|
||||
fn riter<T>(v: [mutable? T], f: block(T)) {
|
||||
riter2(v) { |_i, v| f(v) }
|
||||
}
|
||||
|
||||
/*
|
||||
Function: riter2
|
||||
|
||||
Iterates over a vector's elements and indexes in reverse
|
||||
|
||||
Iterates over vector `v` and, for each element, calls function `f` with the
|
||||
element's value and index.
|
||||
*/
|
||||
fn riter2<T>(v: [mutable? T], f: block(uint, T)) {
|
||||
let i = len(v);
|
||||
while 0u < i {
|
||||
i -= 1u;
|
||||
f(i, v[i]);
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
Function: to_ptr
|
||||
|
||||
|
@ -316,6 +316,34 @@ fn iter_nonempty() {
|
||||
assert i == 6;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn riter_empty() {
|
||||
let i = 0;
|
||||
vec::riter::<int>([], { |_v| i += 1 });
|
||||
assert i == 0;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn riter_nonempty() {
|
||||
let i = 0;
|
||||
vec::riter([1, 2, 3], { |v|
|
||||
if i == 0 { assert v == 3; }
|
||||
i += v
|
||||
});
|
||||
assert i == 6;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn riter2() {
|
||||
let i = 0;
|
||||
vec::riter2([0, 1, 2], { |j, v|
|
||||
if i == 0 { assert v == 2; }
|
||||
assert j == v as uint;
|
||||
i += v;
|
||||
});
|
||||
assert i == 3;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_any_and_all() {
|
||||
assert (vec::any(is_three, [1u, 2u, 3u]));
|
||||
|
Loading…
Reference in New Issue
Block a user