Skip to content

Commit 99200dd

Browse files
committed
---
yaml --- r: 4609 b: refs/heads/master c: ae4c17e h: refs/heads/master i: 4607: 90c3a45 v: v3
1 parent 9fb11e6 commit 99200dd

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 9638f522bda78220beac63c9142a4b02dc807141
2+
refs/heads/master: ae4c17e2677e7db41c47c958936e82fd56ca2a62

trunk/src/lib/ivec.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ fn map[@T, @U](f: &block(&T) -> U , v: &[mutable? T]) -> [U] {
208208
ret result;
209209
}
210210

211+
fn map2[@T, @U, @V](f: &block(&T, &U) -> V, v0: &[T], v1: &[U])
212+
-> [V] {
213+
let v0_len = len[T](v0);
214+
if v0_len != len[U](v1) { fail; }
215+
let u: [V] = ~[];
216+
let i = 0u;
217+
while i < v0_len { u += ~[f({ v0.(i) }, { v1.(i) })]; i += 1u; }
218+
ret u;
219+
}
220+
211221
fn filter_map[@T, @U](f: &block(&T) -> option::t[U],
212222
v: &[mutable? T]) -> [U] {
213223
let result = ~[];
@@ -255,6 +265,18 @@ fn find[@T](f: &block(&T) -> bool , v: &[T]) -> option::t[T] {
255265
ret none;
256266
}
257267

268+
fn position[@T](x: &T, v: &[T]) -> option::t[uint] {
269+
let i: uint = 0u;
270+
while i < len(v) { if x == v.(i) { ret some[uint](i); } i += 1u; }
271+
ret none[uint];
272+
}
273+
274+
fn position_pred[T](f: fn(&T) -> bool , v: &[T]) -> option::t[uint] {
275+
let i: uint = 0u;
276+
while i < len(v) { if f(v.(i)) { ret some[uint](i); } i += 1u; }
277+
ret none[uint];
278+
}
279+
258280
fn unzip[@T, @U](v: &[{_0: T, _1: U}]) -> {_0: [T], _1: [U]} {
259281
let sz = len(v);
260282
if sz == 0u {

trunk/src/test/stdtest/ivec.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
use std;
33
import std::ivec;
4+
import std::ivec::*;
45
import std::option;
56
import std::option::none;
67
import std::option::some;
@@ -235,6 +236,17 @@ fn test_map() {
235236
assert (w.(4) == 25u);
236237
}
237238

239+
#[test]
240+
fn test_map2() {
241+
fn times(x: &int, y: &int) -> int { ret x * y; }
242+
let f = times;
243+
let v0 = ~[1, 2, 3, 4, 5];
244+
let v1 = ~[5, 4, 3, 2, 1];
245+
let u = ivec::map2[int, int, int](f, v0, v1);
246+
let i = 0;
247+
while i < 5 { assert (v0.(i) * v1.(i) == u.(i)); i += 1; }
248+
}
249+
238250
#[test]
239251
fn test_filter_map() {
240252
// Test on-stack filter-map.
@@ -251,6 +263,23 @@ fn test_filter_map() {
251263
assert (w.(0) == 1u);
252264
assert (w.(1) == 9u);
253265
assert (w.(2) == 25u);
266+
267+
fn halve(i: &int) -> option::t[int] {
268+
if i % 2 == 0 {
269+
ret option::some[int](i / 2);
270+
} else { ret option::none[int]; }
271+
}
272+
fn halve_for_sure(i: &int) -> int { ret i / 2; }
273+
let all_even: [int] = ~[0, 2, 8, 6];
274+
let all_odd1: [int] = ~[1, 7, 3];
275+
let all_odd2: [int] = ~[];
276+
let mix: [int] = ~[9, 2, 6, 7, 1, 0, 0, 3];
277+
let mix_dest: [int] = ~[1, 3, 0, 0];
278+
assert (filter_map(halve, all_even) == map(halve_for_sure, all_even));
279+
assert (filter_map(halve, all_odd1) == ~[]);
280+
assert (filter_map(halve, all_odd2) == ~[]);
281+
assert (filter_map(halve, mix) == mix_dest);
282+
254283
}
255284

256285
#[test]
@@ -296,6 +325,24 @@ fn test_zip_unzip() {
296325
assert ({_0: 3, _1: 6} == {_0: u1._0.(2), _1: u1._1.(2)});
297326
}
298327

328+
#[test]
329+
fn test_position() {
330+
let v1: [int] = ~[1, 2, 3, 3, 2, 5];
331+
assert (position(1, v1) == option::some[uint](0u));
332+
assert (position(2, v1) == option::some[uint](1u));
333+
assert (position(5, v1) == option::some[uint](5u));
334+
assert (position(4, v1) == option::none[uint]);
335+
}
336+
337+
#[test]
338+
fn test_position_pred() {
339+
fn less_than_three(i: &int) -> bool { ret i < 3; }
340+
fn is_eighteen(i: &int) -> bool { ret i == 18; }
341+
let v1: [int] = ~[5, 4, 3, 2, 1];
342+
assert (position_pred(less_than_three, v1) == option::some[uint](3u));
343+
assert (position_pred(is_eighteen, v1) == option::none[uint]);
344+
}
345+
299346
// Local Variables:
300347
// mode: rust;
301348
// fill-column: 78;

0 commit comments

Comments
 (0)