Move some code over to iterator-for to see how it performs.

This commit is contained in:
Marijn Haverbeke 2012-03-27 15:14:12 +02:00
parent cbad6925c1
commit b5a4fa9456
27 changed files with 194 additions and 180 deletions

View file

@ -173,14 +173,14 @@ fn set(v: bitv, i: uint, x: bool) {
#[doc = "Returns true if all bits are 1"]
fn is_true(v: bitv) -> bool {
for i: uint in to_vec(v) { if i != 1u { ret false; } }
for each(v) {|i| if !i { ret false; } }
ret true;
}
#[doc = "Returns true if all bits are 0"]
fn is_false(v: bitv) -> bool {
for i: uint in to_vec(v) { if i == 1u { ret false; } }
for each(v) {|i| if i { ret false; } }
ret true;
}
@ -198,6 +198,12 @@ fn to_vec(v: bitv) -> [uint] {
ret vec::from_fn::<uint>(v.nbits, sub);
}
fn each(v: bitv, f: fn(bool) -> bool) {
let mut i = 0u;
while i < v.nbits {
if !f(get(v, i)) { break; }
}
}
#[doc = "
Converts the bitvector to a string.
@ -207,7 +213,7 @@ is either '0' or '1'.
"]
fn to_str(v: bitv) -> str {
let mut rs = "";
for i: uint in to_vec(v) { if i == 1u { rs += "1"; } else { rs += "0"; } }
for each(v) {|i| if i { rs += "1"; } else { rs += "0"; } }
ret rs;
}

View file

@ -208,7 +208,7 @@ fn getopts(args: [str], opts: [opt]) -> result unsafe {
}
}
let mut name_pos = 0u;
for nm: name in names {
for vec::each(names) {|nm|
name_pos += 1u;
let optid = alt find_opt(opts, nm) {
some(id) { id }
@ -290,7 +290,7 @@ Used when an option accepts multiple values.
"]
fn opt_strs(m: match, nm: str) -> [str] {
let mut acc: [str] = [];
for v: optval in opt_vals(m, nm) {
for vec::each(opt_vals(m, nm)) {|v|
alt v { val(s) { acc += [s]; } _ { } }
}
ret acc;

View file

@ -63,7 +63,7 @@ fn sha1() -> sha1 {
fn add_input(st: sha1state, msg: [u8]) {
// FIXME: Should be typestate precondition
assert (!st.computed);
for element: u8 in msg {
for vec::each(msg) {|element|
st.msg_block[st.msg_block_idx] = element;
st.msg_block_idx += 1u;
st.len_low += 8u32;
@ -161,7 +161,7 @@ fn sha1() -> sha1 {
fn mk_result(st: sha1state) -> [u8] {
if !st.computed { pad_msg(st); st.computed = true; }
let mut rs: [u8] = [];
for hpart: u32 in st.h {
for vec::each(st.h) {|hpart|
let a = (hpart >> 24u32 & 0xFFu32) as u8;
let b = (hpart >> 16u32 & 0xFFu32) as u8;
let c = (hpart >> 8u32 & 0xFFu32) as u8;
@ -238,7 +238,7 @@ fn sha1() -> sha1 {
fn result_str() -> str {
let r = mk_result(self);
let mut s = "";
for b: u8 in r { s += uint::to_str(b as uint, 16u); }
for vec::each(r) {|b| s += uint::to_str(b as uint, 16u); }
ret s;
}
}
@ -327,7 +327,7 @@ mod tests {
// Test that it works when accepting the message all at once
let sh = sha1::sha1();
for t: test in tests {
for vec::each(tests) {|t|
sh.input_str(t.input);
let out = sh.result();
check_vec_eq(t.output, out);
@ -336,7 +336,7 @@ mod tests {
// Test that it works when accepting the message in pieces
for t: test in tests {
for vec::each(tests) {|t|
let len = str::len(t.input);
let mut left = len;
while left > 0u {

View file

@ -254,7 +254,11 @@ mod test_qsort {
let immut_names = vec::from_mut(names);
let pairs = vec::zip(expected, immut_names);
for (a, b) in pairs { #debug("%d %d", a, b); assert (a == b); }
for vec::each(pairs) {|p|
let (a, b) = p;
#debug("%d %d", a, b);
assert (a == b);
}
}
}

View file

@ -37,7 +37,7 @@ fn color_supported() -> bool {
"screen-bce", "xterm-256color"];
ret alt os::getenv("TERM") {
option::some(env) {
for term: str in supported_terms {
for vec::each(supported_terms) {|term|
if str::eq(term, env) { ret true; }
}
false

View file

@ -183,7 +183,7 @@ fn print_failures(st: console_test_state) {
st.out.write_line("\nfailures:");
let failures = vec::map(copy st.failures) {|test| test.name};
let failures = sort::merge_sort(str::le, failures);
for name in failures {
for vec::each(failures) {|name|
st.out.write_line(#fmt[" %s", name]);
}
}
@ -492,7 +492,7 @@ mod tests {
{
let testfn = fn~() { };
let mut tests = [];
for name: str in names {
for vec::each(names) {|name|
let test = {name: name, fn: testfn, ignore: false,
should_fail: false};
tests += [test];
@ -510,7 +510,7 @@ mod tests {
let pairs = vec::zip(expected, filtered);
for (a, b) in pairs { assert (a == b.name); }
for vec::each(pairs) {|p| let (a, b) = p; assert (a == b.name); }
}
}