stdlib: Add an inefficient implementation of ivec::pop

This commit is contained in:
Patrick Walton 2011-07-04 21:20:18 -07:00
parent 5d2c189631
commit d3a4102bc1
2 changed files with 34 additions and 1 deletions

View file

@ -104,7 +104,17 @@ fn slice_mut[T](&T[mutable?] v, uint start, uint end) -> T[mutable] {
// Mutators // Mutators
// TODO // TODO: Write this, unsafely, in a way that's not O(n).
fn pop[T](&mutable T[mutable?] v) -> T {
auto ln = len(v);
assert (ln > 0u);
ln -= 1u;
auto e = v.(ln);
v = slice(v, 0u, ln);
ret e;
}
// TODO: More.
// Appending // Appending

View file

@ -106,6 +106,26 @@ fn test_slice() {
assert (v.(4) == 6); assert (v.(4) == 6);
} }
fn test_pop() {
// Test on-stack pop.
auto v = ~[ 1, 2, 3 ];
auto e = ivec::pop(v);
assert (ivec::len(v) == 2u);
assert (v.(0) == 1);
assert (v.(1) == 2);
assert (e == 3);
// Test on-heap pop.
v = ~[ 1, 2, 3, 4, 5 ];
e = ivec::pop(v);
assert (ivec::len(v) == 4u);
assert (v.(0) == 1);
assert (v.(1) == 2);
assert (v.(2) == 3);
assert (v.(3) == 4);
assert (e == 5);
}
fn test_grow() { fn test_grow() {
// Test on-stack grow(). // Test on-stack grow().
auto v = ~[]; auto v = ~[];
@ -154,6 +174,9 @@ fn main() {
test_last(); test_last();
test_slice(); test_slice();
// Mutators
test_pop();
// Appending // Appending
test_grow(); test_grow();
test_grow_fn(); test_grow_fn();