Skip to content

Commit 5cf99c5

Browse files
committed
dlist pop needs copy after all (#3024)
1 parent de48b7d commit 5cf99c5

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/libcore/dlist.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -304,20 +304,6 @@ impl extensions<T> for dlist<T> {
304304
tl.map(|nobe| self.unlink(nobe));
305305
tl
306306
}
307-
/// Remove data from the head of the list. O(1).
308-
fn pop() -> option<T> {
309-
do option::map_consume(self.pop_n()) |nobe| {
310-
let dlist_node(@{ data: x, _ }) <- nobe;
311-
x
312-
}
313-
}
314-
/// Remove data from the tail of the list. O(1).
315-
fn pop_tail() -> option<T> {
316-
do option::map_consume(self.pop_tail_n()) |nobe| {
317-
let dlist_node(@{ data: x, _ }) <- nobe;
318-
x
319-
}
320-
}
321307
/// Get the node at the list's head. O(1).
322308
pure fn peek_n() -> option<dlist_node<T>> { self.hd }
323309
/// Get the node at the list's tail. O(1).
@@ -399,7 +385,7 @@ impl extensions<T> for dlist<T> {
399385
// Cute as it would be to simply detach the list and proclaim "O(1)!",
400386
// the GC would still be a hidden O(n). Better to be honest about it.
401387
while !self.is_empty() {
402-
let _ = self.pop();
388+
let _ = self.pop_n();
403389
}
404390
}
405391

@@ -457,6 +443,10 @@ impl extensions<T> for dlist<T> {
457443
}
458444

459445
impl extensions<T: copy> for dlist<T> {
446+
/// Remove data from the head of the list. O(1).
447+
fn pop() -> option<T> { self.pop_n().map (|nobe| nobe.data) }
448+
/// Remove data from the tail of the list. O(1).
449+
fn pop_tail() -> option<T> { self.pop_tail_n().map (|nobe| nobe.data) }
460450
/// Get data at the list's head. O(1).
461451
pure fn peek() -> option<T> { self.peek_n().map (|nobe| nobe.data) }
462452
/// Get data at the list's tail. O(1).
@@ -622,6 +612,13 @@ mod tests {
622612
a.assert_consistent(); assert a.is_empty();
623613
}
624614
#[test]
615+
fn test_dlist_clear() {
616+
let a = from_vec(~[5,4,3,2,1]);
617+
a.clear();
618+
assert a.len() == 0;
619+
a.assert_consistent();
620+
}
621+
#[test]
625622
fn test_dlist_is_empty() {
626623
let empty = new_dlist::<int>();
627624
let full1 = from_vec(~[1,2,3]);

0 commit comments

Comments
 (0)