Make vec::pop efficient

This commit is contained in:
Marijn Haverbeke 2012-02-01 12:08:44 +01:00
parent 2dbaa05af8
commit 694de53d28

View file

@ -294,27 +294,18 @@ fn shift<T: copy>(&v: [const T]) -> T {
ret e; ret e;
} }
// TODO: Write this, unsafely, in a way that's not O(n).
/* /*
Function: pop Function: pop
Remove the last element from a vector and return it Remove the last element from a vector and return it
*/ */
fn pop<T: copy>(&v: [const T]) -> T { fn pop<T>(&v: [const T]) -> T unsafe {
let ln = len(v); let ln = len(v);
assert (ln > 0u); assert ln > 0u;
ln -= 1u; let valptr = ptr::mut_addr_of(v[ln - 1u]);
let e = v[ln];
v = slice(v, 0u, ln);
ret e;
// FIXME use this implementation after the next snapshot (27.01.2012)
/* let new_ln = len(v) - 1u;
assert (new_ln > 0u);
let valptr = ptr::mut_addr_of(v[new_ln]);
let val <- *valptr; let val <- *valptr;
unsafe::set_len(v, new_ln); unsafe::set_len(v, ln - 1u);
val val
*/
} }
/* /*