Make vec::pop efficient
This commit is contained in:
parent
2dbaa05af8
commit
694de53d28
1 changed files with 4 additions and 13 deletions
|
@ -294,27 +294,18 @@ fn shift<T: copy>(&v: [const T]) -> T {
|
|||
ret e;
|
||||
}
|
||||
|
||||
// TODO: Write this, unsafely, in a way that's not O(n).
|
||||
/*
|
||||
Function: pop
|
||||
|
||||
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);
|
||||
assert (ln > 0u);
|
||||
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]);
|
||||
assert ln > 0u;
|
||||
let valptr = ptr::mut_addr_of(v[ln - 1u]);
|
||||
let val <- *valptr;
|
||||
unsafe::set_len(v, new_ln);
|
||||
unsafe::set_len(v, ln - 1u);
|
||||
val
|
||||
*/
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in a new issue