diff options
| author | Patrick Walton <[email protected]> | 2011-04-15 17:44:52 -0700 |
|---|---|---|
| committer | Patrick Walton <[email protected]> | 2011-04-15 17:46:16 -0700 |
| commit | 3e922e2ecf808cae8a59ea9d9e0d75abc09b3290 (patch) | |
| tree | 43ba6b909b1402ae7553634dfa47455aa2cdfaaa /src/lib/_vec.rs | |
| parent | Produce PIC code and use -S in the Makefile. (diff) | |
| download | rust-3e922e2ecf808cae8a59ea9d9e0d75abc09b3290.tar.xz rust-3e922e2ecf808cae8a59ea9d9e0d75abc09b3290.zip | |
stdlib: Add some vector functions: empty, empty_mut, grow_set, and freeze. Change _vec.grow to use uint arguments.
Diffstat (limited to 'src/lib/_vec.rs')
| -rw-r--r-- | src/lib/_vec.rs | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/lib/_vec.rs b/src/lib/_vec.rs index 87a97359..ac2ffc8b 100644 --- a/src/lib/_vec.rs +++ b/src/lib/_vec.rs @@ -55,6 +55,16 @@ unsafe fn vec_from_vbuf[T](vbuf v, uint n_elts) -> vec[T] { ret rustrt.vec_from_vbuf[T](v, n_elts); } +// FIXME: Remove me; this is a botch to get around rustboot's bad typechecker. +fn empty[T]() -> vec[T] { + ret alloc[T](0u); +} + +// FIXME: Remove me; this is a botch to get around rustboot's bad typechecker. +fn empty_mut[T]() -> vec[mutable T] { + ret alloc_mut[T](0u); +} + type init_op[T] = fn(uint i) -> T; fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] { @@ -175,14 +185,22 @@ fn unshift[T](&mutable vec[mutable? T] v, &T t) { v = res; } -fn grow[T](&mutable vec[mutable? T] v, int n, &T initval) { - let int i = n; - while (i > 0) { - i -= 1; +fn grow[T](&mutable vec[mutable? T] v, uint n, &T initval) { + let uint i = n; + while (i > 0u) { + i -= 1u; v += vec(initval); } } +fn grow_set[T](&mutable vec[mutable T] v, uint index, &T initval, &T val) { + auto length = _vec.len[mutable T](v); + if (index >= length) { + grow[mutable T](v, index - length + 1u, initval); + } + v.(index) = val; +} + fn map[T, U](&option.operator[T,U] f, &vec[mutable? T] v) -> vec[U] { let vec[U] u = alloc[U](len[T](v)); for (T ve in v) { @@ -262,6 +280,15 @@ fn plus_option[T](&vec[T] v, &option.t[T] o) -> () { } } +// TODO: Remove in favor of built-in "freeze" operation when it's implemented. +fn freeze[T](vec[mutable T] v) -> vec[T] { + let vec[T] result = vec(); + for (T elem in v) { + result += vec(elem); + } + ret result; +} + // Local Variables: // mode: rust; // fill-column: 78; |