Add some of stdtest::vec into stdtest::ivec
Add missing functions to std::ivec as needed
This commit is contained in:
parent
9638f522bd
commit
ae4c17e267
2 changed files with 69 additions and 0 deletions
|
@ -208,6 +208,16 @@ fn map[@T, @U](f: &block(&T) -> U , v: &[mutable? T]) -> [U] {
|
|||
ret result;
|
||||
}
|
||||
|
||||
fn map2[@T, @U, @V](f: &block(&T, &U) -> V, v0: &[T], v1: &[U])
|
||||
-> [V] {
|
||||
let v0_len = len[T](v0);
|
||||
if v0_len != len[U](v1) { fail; }
|
||||
let u: [V] = ~[];
|
||||
let i = 0u;
|
||||
while i < v0_len { u += ~[f({ v0.(i) }, { v1.(i) })]; i += 1u; }
|
||||
ret u;
|
||||
}
|
||||
|
||||
fn filter_map[@T, @U](f: &block(&T) -> option::t[U],
|
||||
v: &[mutable? T]) -> [U] {
|
||||
let result = ~[];
|
||||
|
@ -255,6 +265,18 @@ fn find[@T](f: &block(&T) -> bool , v: &[T]) -> option::t[T] {
|
|||
ret none;
|
||||
}
|
||||
|
||||
fn position[@T](x: &T, v: &[T]) -> option::t[uint] {
|
||||
let i: uint = 0u;
|
||||
while i < len(v) { if x == v.(i) { ret some[uint](i); } i += 1u; }
|
||||
ret none[uint];
|
||||
}
|
||||
|
||||
fn position_pred[T](f: fn(&T) -> bool , v: &[T]) -> option::t[uint] {
|
||||
let i: uint = 0u;
|
||||
while i < len(v) { if f(v.(i)) { ret some[uint](i); } i += 1u; }
|
||||
ret none[uint];
|
||||
}
|
||||
|
||||
fn unzip[@T, @U](v: &[{_0: T, _1: U}]) -> {_0: [T], _1: [U]} {
|
||||
let sz = len(v);
|
||||
if sz == 0u {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
|
||||
use std;
|
||||
import std::ivec;
|
||||
import std::ivec::*;
|
||||
import std::option;
|
||||
import std::option::none;
|
||||
import std::option::some;
|
||||
|
@ -235,6 +236,17 @@ fn test_map() {
|
|||
assert (w.(4) == 25u);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_map2() {
|
||||
fn times(x: &int, y: &int) -> int { ret x * y; }
|
||||
let f = times;
|
||||
let v0 = ~[1, 2, 3, 4, 5];
|
||||
let v1 = ~[5, 4, 3, 2, 1];
|
||||
let u = ivec::map2[int, int, int](f, v0, v1);
|
||||
let i = 0;
|
||||
while i < 5 { assert (v0.(i) * v1.(i) == u.(i)); i += 1; }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_filter_map() {
|
||||
// Test on-stack filter-map.
|
||||
|
@ -251,6 +263,23 @@ fn test_filter_map() {
|
|||
assert (w.(0) == 1u);
|
||||
assert (w.(1) == 9u);
|
||||
assert (w.(2) == 25u);
|
||||
|
||||
fn halve(i: &int) -> option::t[int] {
|
||||
if i % 2 == 0 {
|
||||
ret option::some[int](i / 2);
|
||||
} else { ret option::none[int]; }
|
||||
}
|
||||
fn halve_for_sure(i: &int) -> int { ret i / 2; }
|
||||
let all_even: [int] = ~[0, 2, 8, 6];
|
||||
let all_odd1: [int] = ~[1, 7, 3];
|
||||
let all_odd2: [int] = ~[];
|
||||
let mix: [int] = ~[9, 2, 6, 7, 1, 0, 0, 3];
|
||||
let mix_dest: [int] = ~[1, 3, 0, 0];
|
||||
assert (filter_map(halve, all_even) == map(halve_for_sure, all_even));
|
||||
assert (filter_map(halve, all_odd1) == ~[]);
|
||||
assert (filter_map(halve, all_odd2) == ~[]);
|
||||
assert (filter_map(halve, mix) == mix_dest);
|
||||
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -296,6 +325,24 @@ fn test_zip_unzip() {
|
|||
assert ({_0: 3, _1: 6} == {_0: u1._0.(2), _1: u1._1.(2)});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position() {
|
||||
let v1: [int] = ~[1, 2, 3, 3, 2, 5];
|
||||
assert (position(1, v1) == option::some[uint](0u));
|
||||
assert (position(2, v1) == option::some[uint](1u));
|
||||
assert (position(5, v1) == option::some[uint](5u));
|
||||
assert (position(4, v1) == option::none[uint]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_pred() {
|
||||
fn less_than_three(i: &int) -> bool { ret i < 3; }
|
||||
fn is_eighteen(i: &int) -> bool { ret i == 18; }
|
||||
let v1: [int] = ~[5, 4, 3, 2, 1];
|
||||
assert (position_pred(less_than_three, v1) == option::some[uint](3u));
|
||||
assert (position_pred(is_eighteen, v1) == option::none[uint]);
|
||||
}
|
||||
|
||||
// Local Variables:
|
||||
// mode: rust;
|
||||
// fill-column: 78;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue