Skip to content

Commit 694de53

Browse files
committed
Make vec::pop efficient
1 parent 2dbaa05 commit 694de53

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

src/libcore/vec.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,27 +294,18 @@ fn shift<T: copy>(&v: [const T]) -> T {
294294
ret e;
295295
}
296296

297-
// TODO: Write this, unsafely, in a way that's not O(n).
298297
/*
299298
Function: pop
300299
301300
Remove the last element from a vector and return it
302301
*/
303-
fn pop<T: copy>(&v: [const T]) -> T {
302+
fn pop<T>(&v: [const T]) -> T unsafe {
304303
let ln = len(v);
305-
assert (ln > 0u);
306-
ln -= 1u;
307-
let e = v[ln];
308-
v = slice(v, 0u, ln);
309-
ret e;
310-
// FIXME use this implementation after the next snapshot (27.01.2012)
311-
/* let new_ln = len(v) - 1u;
312-
assert (new_ln > 0u);
313-
let valptr = ptr::mut_addr_of(v[new_ln]);
304+
assert ln > 0u;
305+
let valptr = ptr::mut_addr_of(v[ln - 1u]);
314306
let val <- *valptr;
315-
unsafe::set_len(v, new_ln);
307+
unsafe::set_len(v, ln - 1u);
316308
val
317-
*/
318309
}
319310

320311
/*

0 commit comments

Comments
 (0)