diff options
| author | Graydon Hoare <[email protected]> | 2010-07-05 14:42:12 -0700 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2010-07-05 14:42:12 -0700 |
| commit | f360d222c897d36ea751051ca87c0e588fa653b2 (patch) | |
| tree | baf79fe74df23434d17f2f32732153511a4dd13b /src/lib | |
| parent | Divide vec fill by element size when reporting vec len. (diff) | |
| download | rust-f360d222c897d36ea751051ca87c0e588fa653b2.tar.xz rust-f360d222c897d36ea751051ca87c0e588fa653b2.zip | |
Uint-ify various bits of _str and _vec, enrich _vec a bit.
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/_str.rs | 4 | ||||
| -rw-r--r-- | src/lib/_vec.rs | 42 |
2 files changed, 38 insertions, 8 deletions
diff --git a/src/lib/_str.rs b/src/lib/_str.rs index ac27f294..f1de80f1 100644 --- a/src/lib/_str.rs +++ b/src/lib/_str.rs @@ -4,13 +4,13 @@ native "rust" mod rustrt { type sbuf; fn str_buf(str s) -> sbuf; fn str_len(str s) -> uint; - fn str_alloc(int n_bytes) -> str; + fn str_alloc(uint n_bytes) -> str; } fn is_utf8(vec[u8] v) -> bool { } -fn alloc(int n_bytes) -> str { +fn alloc(uint n_bytes) -> str { ret rustrt.str_alloc(n_bytes); } diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs index 86733fb5..3074df2c 100644 --- a/src/lib/_vec.rs +++ b/src/lib/_vec.rs @@ -1,21 +1,39 @@ import vbuf = rustrt.vbuf; +import op = util.operator; native "rust" mod rustrt { type vbuf; fn vec_buf[T](vec[T] v) -> vbuf; fn vec_len[T](vec[T] v) -> uint; - fn vec_alloc[T](int n_elts) -> vec[T]; + fn vec_alloc[T](uint n_elts) -> vec[T]; } -fn alloc[T](int n_elts) -> vec[T] { +fn alloc[T](uint n_elts) -> vec[T] { ret rustrt.vec_alloc[T](n_elts); } -fn init[T](&T t, int n_elts) -> vec[T] { +type init_op[T] = fn(uint i) -> T; + +fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] { let vec[T] v = alloc[T](n_elts); - let int i = n_elts; - while (i > 0) { - i -= 1; + let uint i = n_elts; + while (i > uint(0)) { + i -= uint(1); + v += vec(op(i)); + } + ret v; +} + +fn init_elt[T](&T t, uint n_elts) -> vec[T] { + // FIXME: should be: + // fn elt_op[X](X x, uint i) -> X { ret x; } + // auto inner = bind elt_op[T](t, _); + // ret init_fn[T](inner, n_elts); + // but this does not work presently. + let vec[T] v = alloc[T](n_elts); + let uint i = n_elts; + while (i > uint(0)) { + i -= uint(1); v += vec(t); } ret v; @@ -39,3 +57,15 @@ fn grow[T](mutable vec[T] v, int n, T initval) -> vec[T] { } ret v; } + +fn map[T,U](&op[T,U] f, &vec[T] v) -> vec[U] { + // FIXME: should be + // let vec[U] u = alloc[U](len[T](v)); + // but this does not work presently. + let vec[U] u = vec(); + for (T ve in v) { + u += vec(f[T,U](ve)); + } + ret u; +} + |