libcore: Add vec any2 and all2 functions.

This commit is contained in:
Erick Tryzelaar
2011-12-19 09:46:09 -08:00
parent f9df32adac
commit 6b1c60d312
2 changed files with 49 additions and 0 deletions

View File

@@ -507,6 +507,24 @@ fn any<T>(v: [T], f: block(T) -> bool) -> bool {
ret false;
}
/*
Function: any2
Return true if a predicate matches any elements in both vectors.
If the vectors contains no elements then false is returned.
*/
fn any2<T, U>(v0: [T], v1: [U], f: block(T, U) -> bool) -> bool {
let v0_len = len(v0);
let v1_len = len(v1);
let i = 0u;
while i < v0_len && i < v1_len {
if f(v0[i], v1[i]) { ret true; };
i += 1u;
}
ret false;
}
/*
Function: all
@@ -519,6 +537,21 @@ fn all<T>(v: [T], f: block(T) -> bool) -> bool {
ret true;
}
/*
Function: all2
Return true if a predicate matches all elements in both vectors.
If the vectors are not the same size then false is returned.
*/
fn all2<T, U>(v0: [T], v1: [U], f: block(T, U) -> bool) -> bool {
let v0_len = len(v0);
if v0_len != len(v1) { ret false; }
let i = 0u;
while i < v0_len { if !f(v0[i], v1[i]) { ret false; }; i += 1u; }
ret true;
}
/*
Function: member