diff options
| author | Patrick Walton <[email protected]> | 2011-03-18 13:53:49 -0700 |
|---|---|---|
| committer | Patrick Walton <[email protected]> | 2011-03-18 13:53:49 -0700 |
| commit | 368eb4bab615feb99e203eecdcec6d0be02f5b42 (patch) | |
| tree | 4c6349efdd41baf1caa8d1a774166f0f6b5372b0 /src/lib/_vec.rs | |
| parent | rustc: When encountering "mutable" as a tycon, parse it, drop it on the floor... (diff) | |
| download | rust-368eb4bab615feb99e203eecdcec6d0be02f5b42.tar.xz rust-368eb4bab615feb99e203eecdcec6d0be02f5b42.zip | |
Add some mutable variants of vector functions to the standard library
Diffstat (limited to 'src/lib/_vec.rs')
| -rw-r--r-- | src/lib/_vec.rs | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs index 80cd242d..ce052283 100644 --- a/src/lib/_vec.rs +++ b/src/lib/_vec.rs @@ -20,6 +20,7 @@ native "rust" mod rustrt { * want to invoke this as vec_alloc[vec[U], U]. */ fn vec_alloc[T, U](uint n_elts) -> vec[U]; + fn vec_alloc_mut[T, U](uint n_elts) -> vec[mutable U]; fn refcount[T](vec[T] v) -> uint; @@ -30,6 +31,10 @@ fn alloc[T](uint n_elts) -> vec[T] { ret rustrt.vec_alloc[vec[T], T](n_elts); } +fn alloc_mut[T](uint n_elts) -> vec[mutable T] { + ret rustrt.vec_alloc_mut[vec[mutable T], T](n_elts); +} + fn refcount[T](vec[T] v) -> uint { auto r = rustrt.refcount[T](v); if (r == dbg.const_refcount) { @@ -52,6 +57,16 @@ fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] { ret v; } +fn init_fn_mut[T](&init_op[T] op, uint n_elts) -> vec[mutable T] { + let vec[mutable T] v = alloc_mut[T](n_elts); + let uint i = 0u; + while (i < n_elts) { + v += vec(mutable op(i)); + i += 1u; + } + ret v; +} + fn init_elt[T](&T t, uint n_elts) -> vec[T] { /** * FIXME (issue #81): should be: @@ -69,6 +84,16 @@ fn init_elt[T](&T t, uint n_elts) -> vec[T] { ret v; } +fn init_elt_mut[T](&T t, uint n_elts) -> vec[mutable T] { + let vec[mutable T] v = alloc_mut[T](n_elts); + let uint i = n_elts; + while (i > 0u) { + i -= 1u; + v += vec(mutable t); + } + ret v; +} + fn buf[T](vec[T] v) -> vbuf { ret rustrt.vec_buf[T](v, 0u); } |