Skip to content

Commit

Permalink
Move some code over to iterator-for to see how it performs.
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Mar 27, 2012
1 parent cbad692 commit b5a4fa9
Show file tree
Hide file tree
Showing 27 changed files with 194 additions and 180 deletions.
6 changes: 3 additions & 3 deletions src/libcore/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn lefts<T: copy, U>(eithers: [either<T, U>]) -> [T] {
#[doc = "Extracts from a vector of either all the left values"];

let mut result: [T] = [];
for elt: either<T, U> in eithers {
for vec::each(eithers) {|elt|
alt elt { left(l) { result += [l]; } _ {/* fallthrough */ } }
}
ret result;
Expand All @@ -35,7 +35,7 @@ fn rights<T, U: copy>(eithers: [either<T, U>]) -> [U] {
#[doc = "Extracts from a vector of either all the right values"];

let mut result: [U] = [];
for elt: either<T, U> in eithers {
for vec::each(eithers) {|elt|
alt elt { right(r) { result += [r]; } _ {/* fallthrough */ } }
}
ret result;
Expand All @@ -52,7 +52,7 @@ fn partition<T: copy, U: copy>(eithers: [either<T, U>])

let mut lefts: [T] = [];
let mut rights: [U] = [];
for elt: either<T, U> in eithers {
for vec::each(eithers) {|elt|
alt elt { left(l) { lefts += [l]; } right(r) { rights += [r]; } }
}
ret {lefts: lefts, rights: rights};
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/extfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ mod rt {
ret padstr + s;
}
fn have_flag(flags: [flag], f: flag) -> bool {
for candidate: flag in flags { if candidate == f { ret true; } }
for vec::each(flags) {|candidate| if candidate == f { ret true; } }
ret false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ fn mk_file_writer(path: str, flags: [fileflag])
fn wb() -> c_int { O_WRONLY as c_int }

let mut fflags: c_int = wb();
for f: fileflag in flags {
for vec::each(flags) {|f|
alt f {
append { fflags |= O_APPEND as c_int; }
create { fflags |= O_CREAT as c_int; }
Expand Down Expand Up @@ -521,7 +521,7 @@ impl of writer for mem_buffer {
fn write(v: [const u8]) {
// Fast path.
if self.pos == vec::len(self.buf) {
for b: u8 in v { self.buf += [mut b]; }
for vec::each(v) {|b| self.buf += [mut b]; }
self.pos += vec::len(v);
ret;
}
Expand Down
9 changes: 5 additions & 4 deletions src/libcore/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ native mod rustrt {

fn env() -> [(str,str)] {
let mut pairs = [];
for p in rustrt::rust_env_pairs() {
for vec::each(rustrt::rust_env_pairs()) {|p|
let vs = str::splitn_char(p, '=', 1u);
assert vec::len(vs) == 2u;
pairs += [(vs[0], vs[1])];
Expand Down Expand Up @@ -464,7 +464,7 @@ fn list_dir(p: path) -> [str] {
p += path::path_sep();
}
let mut full_paths: [str] = [];
for filename: str in rustrt::rust_list_files(p + star()) {
for vec::each(rustrt::rust_list_files(p + star())) {|filename|
if !str::eq(filename, ".") {
if !str::eq(filename, "..") {
full_paths += [p + filename];
Expand Down Expand Up @@ -645,7 +645,8 @@ mod tests {
fn test_env_getenv() {
let e = env();
assert vec::len(e) > 0u;
for (n, v) in e {
for vec::each(e) {|p|
let (n, v) = p;
log(debug, n);
let v2 = getenv(n);
// MingW seems to set some funky environment variables like
Expand Down Expand Up @@ -734,7 +735,7 @@ mod tests {
// Just assuming that we've got some contents in the current directory
assert (vec::len(dirs) > 0u);

for dir in dirs { log(debug, dir); }
for vec::each(dirs) {|dir| log(debug, dir); }
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ checking for overflow:
fn map<T,U:copy,V:copy>(ts: [T], op: fn(T) -> result<V,U>) -> result<[V],U> {
let mut vs: [V] = [];
vec::reserve(vs, vec::len(ts));
for t in ts {
for vec::each(ts) {|t|
alt op(t) {
ok(v) { vs += [v]; }
err(u) { ret err(u); }
Expand Down
8 changes: 5 additions & 3 deletions src/libcore/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn with_argv<T>(prog: str, args: [str],
cb: fn(**libc::c_char) -> T) -> T unsafe {
let mut argptrs = str::as_c_str(prog) {|b| [b] };
let mut tmps = [];
for arg in args {
for vec::each(args) {|arg|
let t = @arg;
tmps += [t];
argptrs += str::as_c_str(*t) {|b| [b] };
Expand All @@ -102,7 +102,8 @@ fn with_envp<T>(env: option<[(str,str)]>,
let mut tmps = [];
let mut ptrs = [];

for (k,v) in es {
for vec::each(es) {|e|
let (k,v) = e;
let t = @(#fmt("%s=%s", k, v));
vec::push(tmps, t);
ptrs += str::as_c_str(*t) {|b| [b]};
Expand All @@ -125,7 +126,8 @@ fn with_envp<T>(env: option<[(str,str)]>,
alt env {
some (es) {
let mut blk : [u8] = [];
for (k,v) in es {
for vec::each(es) {|e|
let (k,v) = e;
let t = #fmt("%s=%s", k, v);
let mut v : [u8] = ::unsafe::reinterpret_cast(t);
blk += v;
Expand Down
28 changes: 20 additions & 8 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export
all, any,
all_between, any_between,
map,
each,
bytes_iter,
chars_iter,
split_char_iter,
Expand Down Expand Up @@ -176,14 +177,14 @@ fn from_char(ch: char) -> str {
fn from_chars(chs: [char]) -> str {
let mut buf = "";
reserve(buf, chs.len());
for ch in chs { push_char(buf, ch); }
for vec::each(chs) {|ch| push_char(buf, ch); }
ret buf;
}

#[doc = "Concatenate a vector of strings"]
fn concat(v: [str]) -> str {
let mut s: str = "";
for ss: str in v { s += ss; }
for vec::each(v) {|ss| s += ss; }
ret s;
}

Expand All @@ -192,7 +193,7 @@ Concatenate a vector of strings, placing a given separator between each
"]
fn connect(v: [str], sep: str) -> str {
let mut s = "", first = true;
for ss: str in v {
for vec::each(v) {|ss|
if first { first = false; } else { s += sep; }
s += ss;
}
Expand Down Expand Up @@ -538,7 +539,7 @@ fn hash(&&s: str) -> uint {
// djb hash.
// FIXME: replace with murmur.
let mut u: uint = 5381u;
for c: u8 in s { u *= 33u; u += c as uint; }
for each(s) {|c| u *= 33u; u += c as uint; }
ret u;
}

Expand Down Expand Up @@ -581,6 +582,16 @@ fn bytes_iter(ss: str, it: fn(u8)) {
}
}

#[doc = "Iterate over the bytes in a string"]
#[inline(always)]
fn each(s: str, it: fn(u8) -> bool) {
let mut i = 0u, l = len(s);
while (i < l) {
if !it(s[i]) { break; }
i += 1u;
}
}

#[doc = "Iterate over the characters in a string"]
fn chars_iter(s: str, it: fn(char)) {
let mut pos = 0u;
Expand Down Expand Up @@ -938,7 +949,7 @@ fn rfind_between(s: str, start: uint, end: uint, f: fn(char) -> bool)
// Utility used by various searching functions
fn match_at(haystack: str, needle: str, at: uint) -> bool {
let mut i = at;
for c in needle { if haystack[i] != c { ret false; } i += 1u; }
for each(needle) {|c| if haystack[i] != c { ret false; } i += 1u; }
ret true;
}

Expand Down Expand Up @@ -1074,7 +1085,7 @@ fn is_ascii(s: str) -> bool {
}

#[doc = "Returns true if the string has length 0"]
pure fn is_empty(s: str) -> bool { for c: u8 in s { ret false; } ret true; }
pure fn is_empty(s: str) -> bool { len(s) == 0u }

#[doc = "Returns true if the string has length greater than 0"]
pure fn is_not_empty(s: str) -> bool { !is_empty(s) }
Expand Down Expand Up @@ -1588,7 +1599,7 @@ mod unsafe {

#[doc = "Appends a vector of bytes to a string. (Not UTF-8 safe)."]
unsafe fn push_bytes(&s: str, bytes: [u8]) {
for byte in bytes { rustrt::rust_str_push(s, byte); }
for vec::each(bytes) {|byte| rustrt::rust_str_push(s, byte); }
}

#[doc = "
Expand Down Expand Up @@ -2472,7 +2483,8 @@ mod tests {
0xd801_u16, 0xdc95_u16, 0xd801_u16, 0xdc86_u16,
0x000a_u16 ]) ];

for (s, u) in pairs {
for vec::each(pairs) {|p|
let (s, u) = p;
assert to_utf16(s) == u;
assert from_utf16(u) == s;
assert from_utf16(to_utf16(s)) == s;
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/to_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl <A: to_str copy, B: to_str copy, C: to_str copy> of to_str for (A, B, C){
impl <A: to_str> of to_str for [A] {
fn to_str() -> str {
let mut acc = "[", first = true;
for elt in self {
for vec::each(self) {|elt|
if first { first = false; }
else { acc += ", "; }
acc += elt.to_str();
Expand Down
24 changes: 12 additions & 12 deletions src/libcore/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ Apply a function to each element of a vector and return the results
fn map<T, U>(v: [T], f: fn(T) -> U) -> [U] {
let mut result = [];
reserve(result, len(v));
for elem: T in v { result += [f(elem)]; }
for each(v) {|elem| result += [f(elem)]; }
ret result;
}

Expand All @@ -420,7 +420,7 @@ of each result vector
"]
fn flat_map<T, U>(v: [T], f: fn(T) -> [U]) -> [U] {
let mut result = [];
for elem: T in v { result += f(elem); }
for each(v) {|elem| result += f(elem); }
ret result;
}

Expand All @@ -446,7 +446,7 @@ the resulting vector.
fn filter_map<T, U: copy>(v: [T], f: fn(T) -> option<U>)
-> [U] {
let mut result = [];
for elem: T in v {
for each(v) {|elem|
alt f(elem) {
none {/* no-op */ }
some(result_elem) { result += [result_elem]; }
Expand All @@ -464,7 +464,7 @@ only those elements for which `f` returned true.
"]
fn filter<T: copy>(v: [T], f: fn(T) -> bool) -> [T] {
let mut result = [];
for elem: T in v {
for each(v) {|elem|
if f(elem) { result += [elem]; }
}
ret result;
Expand All @@ -477,7 +477,7 @@ Flattens a vector of vectors of T into a single vector of T.
"]
fn concat<T: copy>(v: [const [const T]]) -> [T] {
let mut r = [];
for inner in v { r += from_const(inner); }
for each(v) {|inner| r += from_const(inner); }
ret r;
}

Expand All @@ -487,7 +487,7 @@ Concatenate a vector of vectors, placing a given separator between each
fn connect<T: copy>(v: [const [const T]], sep: T) -> [T] {
let mut r: [T] = [];
let mut first = true;
for inner in v {
for each(v) {|inner|
if first { first = false; } else { push(r, sep); }
r += from_const(inner);
}
Expand Down Expand Up @@ -518,7 +518,7 @@ Return true if a predicate matches any elements
If the vector contains no elements then false is returned.
"]
fn any<T>(v: [T], f: fn(T) -> bool) -> bool {
for elem: T in v { if f(elem) { ret true; } }
for each(v) {|elem| if f(elem) { ret true; } }
ret false;
}

Expand All @@ -544,7 +544,7 @@ Return true if a predicate matches all elements
If the vector contains no elements then true is returned.
"]
fn all<T>(v: [T], f: fn(T) -> bool) -> bool {
for elem: T in v { if !f(elem) { ret false; } }
for each(v) {|elem| if !f(elem) { ret false; } }
ret true;
}

Expand All @@ -563,14 +563,14 @@ fn all2<T, U>(v0: [const T], v1: [const U], f: fn(T, U) -> bool) -> bool {

#[doc = "Return true if a vector contains an element with the given value"]
fn contains<T>(v: [const T], x: T) -> bool {
for elt: T in v { if x == elt { ret true; } }
for each(v) {|elt| if x == elt { ret true; } }
ret false;
}

#[doc = "Returns the number of elements that are equal to a given value"]
fn count<T>(v: [const T], x: T) -> uint {
let mut cnt = 0u;
for elt: T in v { if x == elt { cnt += 1u; } }
for each(v) {|elt| if x == elt { cnt += 1u; } }
ret cnt;
}

Expand Down Expand Up @@ -701,7 +701,7 @@ of the i-th tuple of the input vector.
"]
fn unzip<T: copy, U: copy>(v: [const (T, U)]) -> ([T], [U]) {
let mut as = [], bs = [];
for (a, b) in v { as += [a]; bs += [b]; }
for each(v) {|p| let (a, b) = p; as += [a]; bs += [b]; }
ret (as, bs);
}

Expand Down Expand Up @@ -807,7 +807,7 @@ Iterates over a vector's elements and indices
fn eachi<T>(v: [const T], f: fn(uint, T) -> bool) unsafe {
let mut i = 0u, l = len(v);
let mut p = ptr::offset(unsafe::to_ptr(v), 0u);
while i > l {
while i < l {
if !f(i, *p) { break; }
p = ptr::offset(p, 1u);
i += 1u;
Expand Down
12 changes: 9 additions & 3 deletions src/libstd/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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.
Expand All @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/libstd/getopts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit b5a4fa9

Please sign in to comment.