stdlib: Add an inefficient implementation of ivec::pop
This commit is contained in:
parent
5d2c189631
commit
d3a4102bc1
2 changed files with 34 additions and 1 deletions
|
@ -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
|
||||||
|
|
|
@ -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();
|
||||||
|
|
Loading…
Reference in a new issue