Skip to content

Commit 449f1a4

Browse files
committed
---
yaml --- r: 3233 b: refs/heads/master c: ca2ff9c h: refs/heads/master i: 3231: 5551c55 v: v3
1 parent 7e1080d commit 449f1a4

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 5d90b1df4b9933adc20736a39a2b9a747760e0ab
2+
refs/heads/master: ca2ff9c50dc0af698d50fce3304909d60448bd8c

trunk/src/lib/ivec.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn to_ptr[T](&T[] v) -> *T {
2929
ret rustrt::ivec_to_ptr(v);
3030
}
3131

32-
fn len[T](&T[] v) -> uint {
32+
fn len[T](&T[mutable?] v) -> uint {
3333
ret rusti::ivec_len(v);
3434
}
3535

@@ -110,23 +110,42 @@ fn slice_mut[T](&T[mutable?] v, uint start, uint end) -> T[mutable] {
110110

111111
/// Expands the given vector in-place by appending `n` copies of `initval`.
112112
fn grow[T](&mutable T[] v, uint n, &T initval) {
113+
reserve(v, len(v) + n);
113114
let uint i = 0u;
114115
while (i < n) {
115116
v += ~[initval];
116117
i += 1u;
117118
}
118119
}
119120

121+
// TODO: Remove me once we have slots.
122+
fn grow_mut[T](&mutable T[mutable] v, uint n, &T initval) {
123+
reserve(v, len(v) + n);
124+
let uint i = 0u;
125+
while (i < n) {
126+
v += ~[mutable initval];
127+
i += 1u;
128+
}
129+
}
130+
120131
/// Calls `f` `n` times and appends the results of these calls to the given
121132
/// vector.
122133
fn grow_fn[T](&mutable T[] v, uint n, fn(uint)->T init_fn) {
134+
reserve(v, len(v) + n);
123135
let uint i = 0u;
124136
while (i < n) {
125137
v += ~[init_fn(i)];
126138
i += 1u;
127139
}
128140
}
129141

142+
/// Sets the element at position `index` to `val`. If `index` is past the end
143+
/// of the vector, expands the vector by replicating `initval` to fill the
144+
/// intervening space.
145+
fn grow_set[T](&mutable T[mutable] v, uint index, &T initval, &T val) {
146+
if (index >= len(v)) { grow_mut(v, index - len(v) + 1u, initval); }
147+
v.(index) = val;
148+
}
130149

131150
mod unsafe {
132151
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {

trunk/src/test/run-pass/lib-ivec.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ fn test_grow_fn() {
133133
assert (v.(2) == 4u);
134134
}
135135

136+
fn test_grow_set() {
137+
auto v = ~[ mutable 1, 2, 3 ];
138+
ivec::grow_set(v, 4u, 4, 5);
139+
assert (ivec::len(v) == 5u);
140+
assert (v.(0) == 1);
141+
assert (v.(1) == 2);
142+
assert (v.(2) == 3);
143+
assert (v.(3) == 4);
144+
assert (v.(4) == 5);
145+
}
146+
136147
fn main() {
137148
test_reserve_and_on_heap();
138149
test_unsafe_ptrs();
@@ -146,5 +157,6 @@ fn main() {
146157
// Appending
147158
test_grow();
148159
test_grow_fn();
160+
test_grow_set();
149161
}
150162

0 commit comments

Comments
 (0)