about summary refs log tree commit diff
path: root/src/lib
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2011-06-18 16:29:45 -0700
committerPatrick Walton <pcwalton@mimiga.net>2011-06-18 16:29:45 -0700
commit5d90b1df4b9933adc20736a39a2b9a747760e0ab (patch)
treef27f0ac912917319aa427369e928af7f8dad6f54 /src/lib
parentef65542b1d9b80272e1f8c49db47b45622c10ef8 (diff)
downloadrust-5d90b1df4b9933adc20736a39a2b9a747760e0ab.tar.gz
rust-5d90b1df4b9933adc20736a39a2b9a747760e0ab.zip
stdlib: Add ivec::grow() and ivec::grow_fn()
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/ivec.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/ivec.rs b/src/lib/ivec.rs
index 7e50d3170a0..93c6896d9b7 100644
--- a/src/lib/ivec.rs
+++ b/src/lib/ivec.rs
@@ -106,6 +106,28 @@ fn slice_mut[T](&T[mutable?] v, uint start, uint end) -> T[mutable] {
 // TODO
 
 
+// Appending
+
+/// Expands the given vector in-place by appending `n` copies of `initval`.
+fn grow[T](&mutable T[] v, uint n, &T initval) {
+    let uint i = 0u;
+    while (i < n) {
+        v += ~[initval];
+        i += 1u;
+    }
+}
+
+/// Calls `f` `n` times and appends the results of these calls to the given
+/// vector.
+fn grow_fn[T](&mutable T[] v, uint n, fn(uint)->T init_fn) {
+    let uint i = 0u;
+    while (i < n) {
+        v += ~[init_fn(i)];
+        i += 1u;
+    }
+}
+
+
 mod unsafe {
     fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {
         ret rustrt::ivec_copy_from_buf(v, ptr, count);