2013-07-10 09:06:09 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2013-07-09 09:55:04 -05:00
|
|
|
|
|
|
|
//! A doubly-linked list with owned nodes.
|
|
|
|
//!
|
2013-07-11 08:47:52 -05:00
|
|
|
//! The DList allows pushing and popping elements at either end.
|
2013-07-11 09:17:51 -05:00
|
|
|
//!
|
|
|
|
//! DList implements the trait Deque. It should be imported with `use
|
|
|
|
//! extra::container::Deque`.
|
2013-07-09 09:55:04 -05:00
|
|
|
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
// DList is constructed like a singly-linked list over the field `next`.
|
2013-07-09 09:55:04 -05:00
|
|
|
// including the last link being None; each Node owns its `next` field.
|
2012-12-03 18:48:01 -06:00
|
|
|
//
|
2013-07-11 08:47:52 -05:00
|
|
|
// Backlinks over DList::prev are raw pointers that form a full chain in
|
2013-07-09 09:55:04 -05:00
|
|
|
// the reverse direction.
|
2012-12-03 18:48:01 -06:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
use std::cast;
|
2013-07-09 20:49:32 -05:00
|
|
|
use std::ptr;
|
2013-07-09 09:55:04 -05:00
|
|
|
use std::util;
|
2014-01-23 13:41:57 -06:00
|
|
|
use std::iter::Rev;
|
2013-09-08 10:01:16 -05:00
|
|
|
use std::iter;
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2013-07-10 08:27:15 -05:00
|
|
|
use container::Deque;
|
|
|
|
|
2013-07-11 09:17:51 -05:00
|
|
|
/// A doubly-linked list.
|
2013-07-11 08:47:52 -05:00
|
|
|
pub struct DList<T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
priv length: uint,
|
|
|
|
priv list_head: Link<T>,
|
2013-07-09 20:49:32 -05:00
|
|
|
priv list_tail: Rawlink<Node<T>>,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
type Link<T> = Option<~Node<T>>;
|
2013-07-09 20:49:32 -05:00
|
|
|
struct Rawlink<T> { priv p: *mut T }
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
struct Node<T> {
|
|
|
|
priv next: Link<T>,
|
2013-07-09 20:49:32 -05:00
|
|
|
priv prev: Rawlink<Node<T>>,
|
2013-07-09 09:55:04 -05:00
|
|
|
priv value: T,
|
|
|
|
}
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2013-07-11 20:24:59 -05:00
|
|
|
/// Double-ended DList iterator
|
2013-07-18 11:46:37 -05:00
|
|
|
#[deriving(Clone)]
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct Items<'a, T> {
|
2013-12-10 01:16:18 -06:00
|
|
|
priv head: &'a Link<T>,
|
2013-07-11 20:24:59 -05:00
|
|
|
priv tail: Rawlink<Node<T>>,
|
2013-07-09 09:55:04 -05:00
|
|
|
priv nelem: uint,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-12-23 16:41:37 -06:00
|
|
|
|
2013-07-11 21:23:15 -05:00
|
|
|
/// Double-ended mutable DList iterator
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct MutItems<'a, T> {
|
2013-12-10 01:16:18 -06:00
|
|
|
priv list: &'a mut DList<T>,
|
2013-07-11 21:23:15 -05:00
|
|
|
priv head: Rawlink<Node<T>>,
|
|
|
|
priv tail: Rawlink<Node<T>>,
|
2013-07-09 09:55:04 -05:00
|
|
|
priv nelem: uint,
|
2013-01-24 17:40:46 -06:00
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
/// DList consuming iterator
|
2013-07-18 11:46:37 -05:00
|
|
|
#[deriving(Clone)]
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct MoveItems<T> {
|
2013-07-11 08:47:52 -05:00
|
|
|
priv list: DList<T>
|
2012-08-27 16:22:25 -05:00
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
/// Rawlink is a type like Option<T> but for holding a raw pointer
|
|
|
|
impl<T> Rawlink<T> {
|
|
|
|
/// Like Option::None for Rawlink
|
|
|
|
fn none() -> Rawlink<T> {
|
|
|
|
Rawlink{p: ptr::mut_null()}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like Option::Some for Rawlink
|
|
|
|
fn some(n: &mut T) -> Rawlink<T> {
|
|
|
|
Rawlink{p: ptr::to_mut_unsafe_ptr(n)}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the `Rawlink` into an Option value
|
|
|
|
fn resolve_immut(&self) -> Option<&T> {
|
|
|
|
unsafe { self.p.to_option() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert the `Rawlink` into an Option value
|
|
|
|
fn resolve(&mut self) -> Option<&mut T> {
|
|
|
|
if self.p.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(unsafe { cast::transmute(self.p) })
|
|
|
|
}
|
|
|
|
}
|
2013-08-04 20:38:06 -05:00
|
|
|
|
|
|
|
/// Return the `Rawlink` and replace with `Rawlink::none()`
|
|
|
|
fn take(&mut self) -> Rawlink<T> {
|
|
|
|
util::replace(self, Rawlink::none())
|
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
|
2013-07-18 11:46:37 -05:00
|
|
|
impl<T> Clone for Rawlink<T> {
|
|
|
|
#[inline]
|
|
|
|
fn clone(&self) -> Rawlink<T> {
|
|
|
|
Rawlink{p: self.p}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
impl<T> Node<T> {
|
|
|
|
fn new(v: T) -> Node<T> {
|
|
|
|
Node{value: v, next: None, prev: Rawlink::none()}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
/// Set the .prev field on `next`, then return `Some(next)`
|
|
|
|
fn link_with_prev<T>(mut next: ~Node<T>, prev: Rawlink<Node<T>>) -> Link<T> {
|
|
|
|
next.prev = prev;
|
|
|
|
Some(next)
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<T> Container for DList<T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
/// O(1)
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.list_head.is_none()
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
/// O(1)
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn len(&self) -> uint {
|
|
|
|
self.length
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<T> Mutable for DList<T> {
|
|
|
|
/// Remove all elements from the DList
|
2013-07-09 09:55:04 -05:00
|
|
|
///
|
|
|
|
/// O(N)
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn clear(&mut self) {
|
2013-07-11 08:47:52 -05:00
|
|
|
*self = DList::new()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
// private methods
|
|
|
|
impl<T> DList<T> {
|
|
|
|
/// Add a Node first in the list
|
|
|
|
#[inline]
|
|
|
|
fn push_front_node(&mut self, mut new_head: ~Node<T>) {
|
|
|
|
match self.list_head {
|
|
|
|
None => {
|
|
|
|
self.list_tail = Rawlink::some(new_head);
|
|
|
|
self.list_head = link_with_prev(new_head, Rawlink::none());
|
|
|
|
}
|
|
|
|
Some(ref mut head) => {
|
|
|
|
new_head.prev = Rawlink::none();
|
|
|
|
head.prev = Rawlink::some(new_head);
|
|
|
|
util::swap(head, &mut new_head);
|
|
|
|
head.next = Some(new_head);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.length += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the first Node and return it, or None if the list is empty
|
|
|
|
#[inline]
|
|
|
|
fn pop_front_node(&mut self) -> Option<~Node<T>> {
|
2013-11-20 17:46:49 -06:00
|
|
|
self.list_head.take().map(|mut front_node| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.length -= 1;
|
|
|
|
match front_node.next.take() {
|
|
|
|
Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
|
|
|
|
None => self.list_tail = Rawlink::none()
|
|
|
|
}
|
|
|
|
front_node
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a Node last in the list
|
|
|
|
#[inline]
|
|
|
|
fn push_back_node(&mut self, mut new_tail: ~Node<T>) {
|
|
|
|
match self.list_tail.resolve() {
|
|
|
|
None => return self.push_front_node(new_tail),
|
|
|
|
Some(tail) => {
|
|
|
|
self.list_tail = Rawlink::some(new_tail);
|
|
|
|
tail.next = link_with_prev(new_tail, Rawlink::some(tail));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.length += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the last Node and return it, or None if the list is empty
|
|
|
|
#[inline]
|
|
|
|
fn pop_back_node(&mut self) -> Option<~Node<T>> {
|
2013-12-06 12:51:10 -06:00
|
|
|
self.list_tail.resolve().map_or(None, |tail| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.length -= 1;
|
|
|
|
self.list_tail = tail.prev;
|
|
|
|
match tail.prev.resolve() {
|
2013-07-21 15:37:14 -05:00
|
|
|
None => self.list_head.take(),
|
|
|
|
Some(tail_prev) => tail_prev.next.take()
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<T> Deque<T> for DList<T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
/// Provide a reference to the front element, or None if the list is empty
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-10 08:27:15 -05:00
|
|
|
fn front<'a>(&'a self) -> Option<&'a T> {
|
2013-09-20 01:08:47 -05:00
|
|
|
self.list_head.as_ref().map(|head| &head.value)
|
2012-07-17 18:03:54 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
/// Provide a mutable reference to the front element, or None if the list is empty
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-10 08:27:15 -05:00
|
|
|
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
2013-09-20 01:08:47 -05:00
|
|
|
self.list_head.as_mut().map(|head| &mut head.value)
|
2013-02-04 16:02:01 -06:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
|
|
|
/// Provide a reference to the back element, or None if the list is empty
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-10 08:27:15 -05:00
|
|
|
fn back<'a>(&'a self) -> Option<&'a T> {
|
2013-09-20 01:08:47 -05:00
|
|
|
let tmp = self.list_tail.resolve_immut(); // FIXME: #3511: shouldn't need variable
|
|
|
|
tmp.as_ref().map(|tail| &tail.value)
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide a mutable reference to the back element, or None if the list is empty
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-10 08:27:15 -05:00
|
|
|
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
2013-11-16 16:29:39 -06:00
|
|
|
let tmp: Option<&'a mut Node<T>> =
|
|
|
|
self.list_tail.resolve(); // FIXME: #3511: shouldn't need variable
|
|
|
|
tmp.map(|tail| &mut tail.value)
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
/// Add an element first in the list
|
2013-07-09 09:55:04 -05:00
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-21 12:31:40 -05:00
|
|
|
fn push_front(&mut self, elt: T) {
|
|
|
|
self.push_front_node(~Node::new(elt))
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
/// Remove the first element and return it, or None if the list is empty
|
2013-07-09 09:55:04 -05:00
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-21 12:31:40 -05:00
|
|
|
fn pop_front(&mut self) -> Option<T> {
|
2013-11-28 14:22:53 -06:00
|
|
|
self.pop_front_node().map(|~Node{value, ..}| value)
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-06-23 16:57:39 -05:00
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
/// Add an element last in the list
|
2013-07-09 09:55:04 -05:00
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-21 12:31:40 -05:00
|
|
|
fn push_back(&mut self, elt: T) {
|
|
|
|
self.push_back_node(~Node::new(elt))
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
/// Remove the last element and return it, or None if the list is empty
|
2013-07-09 09:55:04 -05:00
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-21 12:31:40 -05:00
|
|
|
fn pop_back(&mut self) -> Option<T> {
|
2013-11-28 14:22:53 -06:00
|
|
|
self.pop_back_node().map(|~Node{value, ..}| value)
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-10 08:27:15 -05:00
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<T> DList<T> {
|
|
|
|
/// Create an empty DList
|
2013-07-10 08:27:15 -05:00
|
|
|
#[inline]
|
2013-07-11 08:47:52 -05:00
|
|
|
pub fn new() -> DList<T> {
|
|
|
|
DList{list_head: None, list_tail: Rawlink::none(), length: 0}
|
2013-07-10 08:27:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
/// Move the last element to the front of the list.
|
|
|
|
///
|
|
|
|
/// If the list is empty, do nothing.
|
|
|
|
#[inline]
|
2013-07-22 18:31:30 -05:00
|
|
|
pub fn rotate_forward(&mut self) {
|
2013-11-20 17:46:49 -06:00
|
|
|
self.pop_back_node().map(|tail| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.push_front_node(tail)
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Move the first element to the back of the list.
|
|
|
|
///
|
|
|
|
/// If the list is empty, do nothing.
|
|
|
|
#[inline]
|
2013-07-22 18:31:30 -05:00
|
|
|
pub fn rotate_backward(&mut self) {
|
2013-11-20 17:46:49 -06:00
|
|
|
self.pop_front_node().map(|head| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.push_back_node(head)
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
/// Add all elements from `other` to the end of the list
|
|
|
|
///
|
|
|
|
/// O(1)
|
2013-08-04 20:38:06 -05:00
|
|
|
pub fn append(&mut self, mut other: DList<T>) {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.list_tail.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => *self = other,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(tail) => {
|
2013-08-04 20:38:06 -05:00
|
|
|
// Carefully empty `other`.
|
|
|
|
let o_tail = other.list_tail.take();
|
|
|
|
let o_length = other.length;
|
|
|
|
match other.list_head.take() {
|
|
|
|
None => return,
|
|
|
|
Some(node) => {
|
2013-07-09 20:49:32 -05:00
|
|
|
tail.next = link_with_prev(node, self.list_tail);
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list_tail = o_tail;
|
|
|
|
self.length += o_length;
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 21:37:57 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add all elements from `other` to the beginning of the list
|
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-11 08:47:52 -05:00
|
|
|
pub fn prepend(&mut self, mut other: DList<T>) {
|
2013-07-09 09:55:04 -05:00
|
|
|
util::swap(self, &mut other);
|
|
|
|
self.append(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert `elt` before the first `x` in the list where `f(x, elt)` is true,
|
|
|
|
/// or at the end.
|
|
|
|
///
|
|
|
|
/// O(N)
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
|
2013-07-09 09:55:04 -05:00
|
|
|
{
|
|
|
|
let mut it = self.mut_iter();
|
|
|
|
loop {
|
2013-07-11 21:23:15 -05:00
|
|
|
match it.peek_next() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => break,
|
2013-07-11 21:23:15 -05:00
|
|
|
Some(x) => if f(x, &elt) { break }
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-11 21:23:15 -05:00
|
|
|
it.next();
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-11 21:23:15 -05:00
|
|
|
it.insert_next(elt);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 19:31:08 -05:00
|
|
|
/// Merge DList `other` into this DList, using the function `f`.
|
|
|
|
/// Iterate the both DList with `a` from self and `b` from `other`, and
|
|
|
|
/// put `a` in the result if `f(a, b)` is true, else `b`.
|
2013-07-09 09:55:04 -05:00
|
|
|
///
|
|
|
|
/// O(max(N, M))
|
2013-11-18 23:54:13 -06:00
|
|
|
pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
|
2013-07-09 09:55:04 -05:00
|
|
|
{
|
|
|
|
let mut it = self.mut_iter();
|
|
|
|
loop {
|
2013-07-11 21:23:15 -05:00
|
|
|
let take_a = match (it.peek_next(), other.front()) {
|
|
|
|
(_ , None) => return,
|
|
|
|
(None, _ ) => break,
|
|
|
|
(Some(ref mut x), Some(y)) => f(*x, y),
|
2013-07-11 19:31:08 -05:00
|
|
|
};
|
|
|
|
if take_a {
|
2013-07-11 21:23:15 -05:00
|
|
|
it.next();
|
2013-07-11 19:31:08 -05:00
|
|
|
} else {
|
2013-07-21 12:31:40 -05:00
|
|
|
it.insert_next_node(other.pop_front_node().unwrap());
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-09-21 21:37:57 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
self.append(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Provide a forward iterator
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn iter<'a>(&'a self) -> Items<'a, T> {
|
|
|
|
Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide a reverse iterator
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2014-01-23 13:41:57 -06:00
|
|
|
pub fn rev_iter<'a>(&'a self) -> Rev<Items<'a, T>> {
|
|
|
|
self.iter().rev()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide a forward iterator with mutable references
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
|
2013-07-11 21:23:15 -05:00
|
|
|
let head_raw = match self.list_head {
|
|
|
|
Some(ref mut h) => Rawlink::some(*h),
|
|
|
|
None => Rawlink::none(),
|
|
|
|
};
|
2014-01-14 21:32:24 -06:00
|
|
|
MutItems{
|
2013-07-11 21:23:15 -05:00
|
|
|
nelem: self.len(),
|
|
|
|
head: head_raw,
|
|
|
|
tail: self.list_tail,
|
|
|
|
list: self
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
/// Provide a reverse iterator with mutable references
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2014-01-23 13:41:57 -06:00
|
|
|
pub fn mut_rev_iter<'a>(&'a mut self) -> Rev<MutItems<'a, T>> {
|
|
|
|
self.mut_iter().rev()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Consume the list into an iterator yielding elements by value
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn move_iter(self) -> MoveItems<T> {
|
|
|
|
MoveItems{list: self}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume the list into an iterator yielding elements by value, in reverse
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2014-01-23 13:41:57 -06:00
|
|
|
pub fn move_rev_iter(self) -> Rev<MoveItems<T>> {
|
|
|
|
self.move_iter().rev()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-21 14:05:48 -05:00
|
|
|
impl<T: Ord> DList<T> {
|
2013-07-11 19:31:08 -05:00
|
|
|
/// Insert `elt` sorted in ascending order
|
|
|
|
///
|
|
|
|
/// O(N)
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-11 19:31:08 -05:00
|
|
|
pub fn insert_ordered(&mut self, elt: T) {
|
2013-07-21 14:05:48 -05:00
|
|
|
self.insert_when(elt, |a, b| a >= b)
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 20:38:06 -05:00
|
|
|
#[unsafe_destructor]
|
|
|
|
impl<T> Drop for DList<T> {
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self) {
|
2013-08-04 20:38:06 -05:00
|
|
|
// Dissolve the dlist in backwards direction
|
|
|
|
// Just dropping the list_head can lead to stack exhaustion
|
|
|
|
// when length is >> 1_000_000
|
2013-09-16 20:18:07 -05:00
|
|
|
let mut tail = self.list_tail;
|
2013-08-04 20:38:06 -05:00
|
|
|
loop {
|
|
|
|
match tail.resolve() {
|
|
|
|
None => break,
|
|
|
|
Some(prev) => {
|
|
|
|
prev.next.take(); // release ~Node<T>
|
|
|
|
tail = prev.prev;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-09-16 20:18:07 -05:00
|
|
|
self.length = 0;
|
|
|
|
self.list_head = None;
|
|
|
|
self.list_tail = Rawlink::none();
|
2013-08-04 20:38:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a, A> Iterator<&'a A> for Items<'a, A> {
|
2013-07-09 09:55:04 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next(&mut self) -> Option<&'a A> {
|
2013-07-11 20:24:59 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
self.head.as_ref().map(|head| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.head = &head.next;
|
|
|
|
&head.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-07-09 09:55:04 -05:00
|
|
|
(self.nelem, Some(self.nelem))
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next_back(&mut self) -> Option<&'a A> {
|
2013-07-11 20:24:59 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
let tmp = self.tail.resolve_immut(); // FIXME: #3511: shouldn't need variable
|
2013-11-20 17:46:49 -06:00
|
|
|
tmp.as_ref().map(|prev| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.tail = prev.prev;
|
|
|
|
&prev.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-11 20:24:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
|
2013-09-01 11:20:24 -05:00
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
|
2013-07-09 09:55:04 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next(&mut self) -> Option<&'a mut A> {
|
2013-07-11 21:23:15 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
self.head.resolve().map(|next| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.head = match next.next {
|
|
|
|
Some(ref mut node) => Rawlink::some(&mut **node),
|
|
|
|
None => Rawlink::none(),
|
|
|
|
};
|
|
|
|
&mut next.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
2013-07-09 09:55:04 -05:00
|
|
|
(self.nelem, Some(self.nelem))
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
|
2013-07-09 09:55:04 -05:00
|
|
|
#[inline]
|
2013-12-10 01:16:18 -06:00
|
|
|
fn next_back(&mut self) -> Option<&'a mut A> {
|
2013-07-11 21:23:15 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
self.tail.resolve().map(|prev| {
|
2013-07-21 12:31:40 -05:00
|
|
|
self.nelem -= 1;
|
|
|
|
self.tail = prev.prev;
|
|
|
|
&mut prev.value
|
2013-11-20 17:46:49 -06:00
|
|
|
})
|
2013-01-24 17:40:46 -06:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
|
2013-07-11 21:23:15 -05:00
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
/// Allow mutating the DList while iterating
|
2013-07-09 20:49:32 -05:00
|
|
|
pub trait ListInsertion<A> {
|
2013-07-22 10:51:11 -05:00
|
|
|
/// Insert `elt` just after to the element most recently returned by `.next()`
|
|
|
|
///
|
|
|
|
/// The inserted element does not appear in the iteration.
|
2013-07-11 21:23:15 -05:00
|
|
|
fn insert_next(&mut self, elt: A);
|
2013-07-09 20:49:32 -05:00
|
|
|
|
|
|
|
/// Provide a reference to the next element, without changing the iterator
|
|
|
|
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
// private methods for MutItems
|
|
|
|
impl<'a, A> MutItems<'a, A> {
|
2013-07-21 12:31:40 -05:00
|
|
|
fn insert_next_node(&mut self, mut ins_node: ~Node<A>) {
|
|
|
|
// Insert before `self.head` so that it is between the
|
2013-07-11 21:23:15 -05:00
|
|
|
// previously yielded element and self.head.
|
2013-07-21 12:31:40 -05:00
|
|
|
//
|
|
|
|
// The inserted node will not appear in further iteration.
|
2013-07-11 21:23:15 -05:00
|
|
|
match self.head.resolve() {
|
2013-07-21 12:31:40 -05:00
|
|
|
None => { self.list.push_back_node(ins_node); }
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(node) => {
|
|
|
|
let prev_node = match node.prev.resolve() {
|
2013-07-21 12:31:40 -05:00
|
|
|
None => return self.list.push_front_node(ins_node),
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(prev) => prev,
|
2013-07-09 09:55:04 -05:00
|
|
|
};
|
2013-07-16 14:47:01 -05:00
|
|
|
let node_own = prev_node.next.take_unwrap();
|
2013-07-09 20:49:32 -05:00
|
|
|
ins_node.next = link_with_prev(node_own, Rawlink::some(ins_node));
|
|
|
|
prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list.length += 1;
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
}
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
|
2013-07-21 12:31:40 -05:00
|
|
|
#[inline]
|
|
|
|
fn insert_next(&mut self, elt: A) {
|
|
|
|
self.insert_next_node(~Node::new(elt))
|
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 20:49:32 -05:00
|
|
|
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
|
2013-07-22 10:51:11 -05:00
|
|
|
if self.nelem == 0 {
|
|
|
|
return None
|
|
|
|
}
|
2013-09-20 01:08:47 -05:00
|
|
|
self.head.resolve().map(|head| &mut head.value)
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<A> Iterator<A> for MoveItems<A> {
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn next(&mut self) -> Option<A> { self.list.pop_front() }
|
2013-07-14 16:03:54 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-07-09 09:55:04 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
(self.list.length, Some(self.list.length))
|
2012-07-17 18:03:54 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<A> DoubleEndedIterator<A> for MoveItems<A> {
|
2013-07-14 16:03:54 -05:00
|
|
|
#[inline]
|
2013-07-11 20:50:05 -05:00
|
|
|
fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
std: Move the iterator param on FromIterator and Extendable to the method.
If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:
fn foo<E: Extendable<int, Range<int>> +
Extendable<int, Map<&'self int, int, VecIterator<int>>>
(e: &mut E, ...) { ... }
since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).
This patch changes it to:
fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
2013-08-13 08:08:14 -05:00
|
|
|
impl<A> FromIterator<A> for DList<A> {
|
|
|
|
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> DList<A> {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut ret = DList::new();
|
2013-07-29 19:06:49 -05:00
|
|
|
ret.extend(iterator);
|
2013-07-09 09:55:04 -05:00
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
std: Move the iterator param on FromIterator and Extendable to the method.
If they are on the trait then it is extremely annoying to use them as
generic parameters to a function, e.g. with the iterator param on the trait
itself, if one was to pass an Extendable<int> to a function that filled it
either from a Range or a Map<VecIterator>, one needs to write something
like:
fn foo<E: Extendable<int, Range<int>> +
Extendable<int, Map<&'self int, int, VecIterator<int>>>
(e: &mut E, ...) { ... }
since using a generic, i.e. `foo<E: Extendable<int, I>, I: Iterator<int>>`
means that `foo` takes 2 type parameters, and the caller has to specify them
(which doesn't work anyway, as they'll mismatch with the iterators used in
`foo` itself).
This patch changes it to:
fn foo<E: Extendable<int>>(e: &mut E, ...) { ... }
2013-08-13 08:08:14 -05:00
|
|
|
impl<A> Extendable<A> for DList<A> {
|
|
|
|
fn extend<T: Iterator<A>>(&mut self, iterator: &mut T) {
|
2013-08-03 11:45:23 -05:00
|
|
|
for elt in *iterator { self.push_back(elt); }
|
2013-07-29 19:06:49 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<A: Eq> Eq for DList<A> {
|
|
|
|
fn eq(&self, other: &DList<A>) -> bool {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.len() == other.len() &&
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::eq(self.iter(), other.iter())
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-14 16:03:54 -05:00
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
fn ne(&self, other: &DList<A>) -> bool {
|
2013-08-29 10:11:11 -05:00
|
|
|
self.len() != other.len() ||
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::ne(self.iter(), other.iter())
|
2013-08-08 15:07:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A: Eq + Ord> Ord for DList<A> {
|
|
|
|
fn lt(&self, other: &DList<A>) -> bool {
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::lt(self.iter(), other.iter())
|
2013-08-08 15:07:21 -05:00
|
|
|
}
|
|
|
|
fn le(&self, other: &DList<A>) -> bool {
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::le(self.iter(), other.iter())
|
2013-08-08 15:07:21 -05:00
|
|
|
}
|
|
|
|
fn gt(&self, other: &DList<A>) -> bool {
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::gt(self.iter(), other.iter())
|
2013-08-08 15:07:21 -05:00
|
|
|
}
|
|
|
|
fn ge(&self, other: &DList<A>) -> bool {
|
2013-09-08 10:01:16 -05:00
|
|
|
iter::order::ge(self.iter(), other.iter())
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<A: Clone> Clone for DList<A> {
|
|
|
|
fn clone(&self) -> DList<A> {
|
2013-08-09 22:09:47 -05:00
|
|
|
self.iter().map(|x| x.clone()).collect()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2014-01-07 00:33:50 -06:00
|
|
|
mod tests {
|
|
|
|
use container::Deque;
|
|
|
|
use extra::test;
|
|
|
|
use std::rand;
|
|
|
|
use super::{DList, Node, ListInsertion};
|
|
|
|
|
|
|
|
pub fn check_links<T>(list: &DList<T>) {
|
|
|
|
let mut len = 0u;
|
|
|
|
let mut last_ptr: Option<&Node<T>> = None;
|
|
|
|
let mut node_ptr: &Node<T>;
|
|
|
|
match list.list_head {
|
|
|
|
None => { assert_eq!(0u, list.length); return }
|
|
|
|
Some(ref node) => node_ptr = &**node,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2014-01-07 00:33:50 -06:00
|
|
|
loop {
|
|
|
|
match (last_ptr, node_ptr.prev.resolve_immut()) {
|
|
|
|
(None , None ) => {}
|
|
|
|
(None , _ ) => fail!("prev link for list_head"),
|
|
|
|
(Some(p), Some(pptr)) => {
|
|
|
|
assert_eq!(p as *Node<T>, pptr as *Node<T>);
|
|
|
|
}
|
|
|
|
_ => fail!("prev link is none, not good"),
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2014-01-07 00:33:50 -06:00
|
|
|
match node_ptr.next {
|
|
|
|
Some(ref next) => {
|
|
|
|
last_ptr = Some(node_ptr);
|
|
|
|
node_ptr = &**next;
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
2014-01-07 00:33:50 -06:00
|
|
|
assert_eq!(len, list.length);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-03-08 17:19:30 -06:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<~int> = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.pop_front(), None);
|
|
|
|
assert_eq!(m.pop_back(), None);
|
|
|
|
assert_eq!(m.pop_front(), None);
|
|
|
|
m.push_front(~1);
|
|
|
|
assert_eq!(m.pop_front(), Some(~1));
|
|
|
|
m.push_back(~2);
|
|
|
|
m.push_back(~3);
|
|
|
|
assert_eq!(m.len(), 2);
|
|
|
|
assert_eq!(m.pop_front(), Some(~2));
|
|
|
|
assert_eq!(m.pop_front(), Some(~3));
|
|
|
|
assert_eq!(m.len(), 0);
|
|
|
|
assert_eq!(m.pop_front(), None);
|
|
|
|
m.push_back(~1);
|
|
|
|
m.push_back(~3);
|
|
|
|
m.push_back(~5);
|
|
|
|
m.push_back(~7);
|
|
|
|
assert_eq!(m.pop_front(), Some(~1));
|
2013-05-21 19:24:31 -05:00
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
n.push_front(2);
|
|
|
|
n.push_front(3);
|
|
|
|
{
|
2013-07-10 08:27:15 -05:00
|
|
|
assert_eq!(n.front().unwrap(), &3);
|
|
|
|
let x = n.front_mut().unwrap();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(*x, 3);
|
|
|
|
*x = 0;
|
|
|
|
}
|
|
|
|
{
|
2013-07-10 08:27:15 -05:00
|
|
|
assert_eq!(n.back().unwrap(), &2);
|
|
|
|
let y = n.back_mut().unwrap();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(*y, 2);
|
|
|
|
*y = 1;
|
|
|
|
}
|
|
|
|
assert_eq!(n.pop_front(), Some(0));
|
|
|
|
assert_eq!(n.pop_front(), Some(1));
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[cfg(test)]
|
2013-07-11 08:47:52 -05:00
|
|
|
fn generate_test() -> DList<int> {
|
2013-07-09 20:49:32 -05:00
|
|
|
list_from(&[0,1,2,3,4,5,6])
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[cfg(test)]
|
2013-07-02 14:47:32 -05:00
|
|
|
fn list_from<T: Clone>(v: &[T]) -> DList<T> {
|
2013-08-09 22:09:47 -05:00
|
|
|
v.iter().map(|x| (*x).clone()).collect()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_append() {
|
|
|
|
{
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new();
|
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
n.push_back(2);
|
|
|
|
m.append(n);
|
|
|
|
assert_eq!(m.len(), 1);
|
|
|
|
assert_eq!(m.pop_back(), Some(2));
|
|
|
|
check_links(&m);
|
|
|
|
}
|
|
|
|
{
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new();
|
|
|
|
let n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
m.push_back(2);
|
|
|
|
m.append(n);
|
|
|
|
assert_eq!(m.len(), 1);
|
|
|
|
assert_eq!(m.pop_back(), Some(2));
|
|
|
|
check_links(&m);
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
let v = ~[1,2,3,4,5];
|
|
|
|
let u = ~[9,8,1,2,3,4,5];
|
|
|
|
let mut m = list_from(v);
|
|
|
|
m.append(list_from(u));
|
|
|
|
check_links(&m);
|
|
|
|
let sum = v + u;
|
|
|
|
assert_eq!(sum.len(), m.len());
|
2013-08-07 21:21:36 -05:00
|
|
|
for elt in sum.move_iter() {
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.pop_front(), Some(elt))
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_prepend() {
|
|
|
|
{
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new();
|
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
n.push_back(2);
|
|
|
|
m.prepend(n);
|
|
|
|
assert_eq!(m.len(), 1);
|
|
|
|
assert_eq!(m.pop_back(), Some(2));
|
|
|
|
check_links(&m);
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
let v = ~[1,2,3,4,5];
|
|
|
|
let u = ~[9,8,1,2,3,4,5];
|
|
|
|
let mut m = list_from(v);
|
|
|
|
m.prepend(list_from(u));
|
|
|
|
check_links(&m);
|
|
|
|
let sum = u + v;
|
|
|
|
assert_eq!(sum.len(), m.len());
|
2013-08-07 21:21:36 -05:00
|
|
|
for elt in sum.move_iter() {
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.pop_front(), Some(elt))
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
#[test]
|
|
|
|
fn test_rotate() {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut n: DList<int> = DList::new();
|
2013-07-22 18:31:30 -05:00
|
|
|
n.rotate_backward(); check_links(&n);
|
2013-07-21 12:31:40 -05:00
|
|
|
assert_eq!(n.len(), 0);
|
2013-07-22 18:31:30 -05:00
|
|
|
n.rotate_forward(); check_links(&n);
|
2013-07-21 12:31:40 -05:00
|
|
|
assert_eq!(n.len(), 0);
|
|
|
|
|
|
|
|
let v = ~[1,2,3,4,5];
|
|
|
|
let mut m = list_from(v);
|
2013-07-22 18:31:30 -05:00
|
|
|
m.rotate_backward(); check_links(&m);
|
|
|
|
m.rotate_forward(); check_links(&m);
|
2013-07-21 12:31:40 -05:00
|
|
|
assert_eq!(v.iter().collect::<~[&int]>(), m.iter().collect());
|
2013-07-22 18:31:30 -05:00
|
|
|
m.rotate_forward(); check_links(&m);
|
|
|
|
m.rotate_forward(); check_links(&m);
|
2013-07-21 12:31:40 -05:00
|
|
|
m.pop_front(); check_links(&m);
|
2013-07-22 18:31:30 -05:00
|
|
|
m.rotate_forward(); check_links(&m);
|
|
|
|
m.rotate_backward(); check_links(&m);
|
2013-07-21 12:31:40 -05:00
|
|
|
m.push_front(9); check_links(&m);
|
2013-07-22 18:31:30 -05:00
|
|
|
m.rotate_forward(); check_links(&m);
|
2013-08-07 21:21:36 -05:00
|
|
|
assert_eq!(~[3,9,5,1,2], m.move_iter().collect());
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
|
|
|
let m = generate_test();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, elt) in m.iter().enumerate() {
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(i as int, *elt);
|
|
|
|
}
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(n.iter().next(), None);
|
|
|
|
n.push_front(4);
|
|
|
|
let mut it = n.iter();
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next().unwrap(), &4);
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:46:37 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_clone() {
|
|
|
|
let mut n = DList::new();
|
|
|
|
n.push_back(2);
|
|
|
|
n.push_back(3);
|
|
|
|
n.push_back(4);
|
|
|
|
let mut it = n.iter();
|
|
|
|
it.next();
|
|
|
|
let mut jt = it.clone();
|
|
|
|
assert_eq!(it.next(), jt.next());
|
|
|
|
assert_eq!(it.next_back(), jt.next_back());
|
|
|
|
assert_eq!(it.next(), jt.next());
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:23:15 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_double_end() {
|
|
|
|
let mut n = DList::new();
|
|
|
|
assert_eq!(n.iter().next(), None);
|
|
|
|
n.push_front(4);
|
|
|
|
n.push_front(5);
|
|
|
|
n.push_front(6);
|
|
|
|
let mut it = n.iter();
|
|
|
|
assert_eq!(it.size_hint(), (3, Some(3)));
|
|
|
|
assert_eq!(it.next().unwrap(), &6);
|
|
|
|
assert_eq!(it.size_hint(), (2, Some(2)));
|
|
|
|
assert_eq!(it.next_back().unwrap(), &4);
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next_back().unwrap(), &5);
|
|
|
|
assert_eq!(it.next_back(), None);
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_rev_iter() {
|
|
|
|
let m = generate_test();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, elt) in m.rev_iter().enumerate() {
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!((6 - i) as int, *elt);
|
|
|
|
}
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(n.rev_iter().next(), None);
|
|
|
|
n.push_front(4);
|
|
|
|
let mut it = n.rev_iter();
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next().unwrap(), &4);
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert_eq!(it.next(), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mut_iter() {
|
|
|
|
let mut m = generate_test();
|
|
|
|
let mut len = m.len();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, elt) in m.mut_iter().enumerate() {
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(i as int, *elt);
|
|
|
|
len -= 1;
|
|
|
|
}
|
|
|
|
assert_eq!(len, 0);
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert!(n.mut_iter().next().is_none());
|
|
|
|
n.push_front(4);
|
2013-07-11 21:23:15 -05:00
|
|
|
n.push_back(5);
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut it = n.mut_iter();
|
2013-07-11 21:23:15 -05:00
|
|
|
assert_eq!(it.size_hint(), (2, Some(2)));
|
|
|
|
assert!(it.next().is_some());
|
2013-07-09 20:49:32 -05:00
|
|
|
assert!(it.next().is_some());
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
2013-07-11 21:23:15 -05:00
|
|
|
#[test]
|
|
|
|
fn test_iterator_mut_double_end() {
|
|
|
|
let mut n = DList::new();
|
|
|
|
assert!(n.mut_iter().next_back().is_none());
|
|
|
|
n.push_front(4);
|
|
|
|
n.push_front(5);
|
|
|
|
n.push_front(6);
|
|
|
|
let mut it = n.mut_iter();
|
|
|
|
assert_eq!(it.size_hint(), (3, Some(3)));
|
|
|
|
assert_eq!(*it.next().unwrap(), 6);
|
|
|
|
assert_eq!(it.size_hint(), (2, Some(2)));
|
|
|
|
assert_eq!(*it.next_back().unwrap(), 4);
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(*it.next_back().unwrap(), 5);
|
|
|
|
assert!(it.next_back().is_none());
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
2013-07-09 20:49:32 -05:00
|
|
|
fn test_insert_prev() {
|
|
|
|
let mut m = list_from(&[0,2,4,6,8]);
|
2013-07-09 20:49:32 -05:00
|
|
|
let len = m.len();
|
|
|
|
{
|
|
|
|
let mut it = m.mut_iter();
|
2013-07-11 21:23:15 -05:00
|
|
|
it.insert_next(-2);
|
2013-07-09 20:49:32 -05:00
|
|
|
loop {
|
|
|
|
match it.next() {
|
|
|
|
None => break,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(elt) => {
|
2013-07-11 21:23:15 -05:00
|
|
|
it.insert_next(*elt + 1);
|
2013-07-09 20:49:32 -05:00
|
|
|
match it.peek_next() {
|
|
|
|
Some(x) => assert_eq!(*x, *elt + 2),
|
|
|
|
None => assert_eq!(8, *elt),
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-11 21:23:15 -05:00
|
|
|
it.insert_next(0);
|
|
|
|
it.insert_next(1);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
check_links(&m);
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(m.len(), 3 + len * 2);
|
2013-08-07 21:21:36 -05:00
|
|
|
assert_eq!(m.move_iter().collect::<~[int]>(), ~[-2,0,1,2,3,4,5,6,7,8,9,0,1]);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_merge() {
|
|
|
|
let mut m = list_from([0, 1, 3, 5, 6, 7, 2]);
|
|
|
|
let n = list_from([-1, 0, 0, 7, 7, 9]);
|
|
|
|
let len = m.len() + n.len();
|
|
|
|
m.merge(n, |a, b| a <= b);
|
|
|
|
assert_eq!(m.len(), len);
|
|
|
|
check_links(&m);
|
2013-08-07 21:21:36 -05:00
|
|
|
let res = m.move_iter().collect::<~[int]>();
|
2013-07-11 19:31:08 -05:00
|
|
|
assert_eq!(res, ~[-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_insert_ordered() {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
n.insert_ordered(1);
|
|
|
|
assert_eq!(n.len(), 1);
|
|
|
|
assert_eq!(n.pop_front(), Some(1));
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
m.push_back(2);
|
|
|
|
m.push_back(4);
|
|
|
|
m.insert_ordered(3);
|
|
|
|
check_links(&m);
|
2013-08-07 21:21:36 -05:00
|
|
|
assert_eq!(~[2,3,4], m.move_iter().collect::<~[int]>());
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_mut_rev_iter() {
|
|
|
|
let mut m = generate_test();
|
2013-08-03 11:45:23 -05:00
|
|
|
for (i, elt) in m.mut_rev_iter().enumerate() {
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!((6-i) as int, *elt);
|
|
|
|
}
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut n = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
assert!(n.mut_rev_iter().next().is_none());
|
|
|
|
n.push_front(4);
|
|
|
|
let mut it = n.mut_rev_iter();
|
|
|
|
assert!(it.next().is_some());
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_send() {
|
|
|
|
let n = list_from([1,2,3]);
|
|
|
|
do spawn {
|
|
|
|
check_links(&n);
|
|
|
|
assert_eq!(~[&1,&2,&3], n.iter().collect::<~[&int]>());
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_eq() {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut n: DList<u8> = list_from([]);
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut m = list_from([]);
|
|
|
|
assert_eq!(&n, &m);
|
|
|
|
n.push_front(1);
|
|
|
|
assert!(n != m);
|
|
|
|
m.push_back(1);
|
|
|
|
assert_eq!(&n, &m);
|
2013-08-29 10:11:11 -05:00
|
|
|
|
|
|
|
let n = list_from([2,3,4]);
|
|
|
|
let m = list_from([1,2,3]);
|
|
|
|
assert!(n != m);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-12-27 19:53:04 -06:00
|
|
|
|
2013-08-08 15:07:21 -05:00
|
|
|
#[test]
|
|
|
|
fn test_ord() {
|
|
|
|
let n: DList<int> = list_from([]);
|
|
|
|
let m = list_from([1,2,3]);
|
|
|
|
assert!(n < m);
|
|
|
|
assert!(m > n);
|
|
|
|
assert!(n <= n);
|
|
|
|
assert!(n >= n);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ord_nan() {
|
|
|
|
let nan = 0.0/0.0;
|
|
|
|
let n = list_from([nan]);
|
|
|
|
let m = list_from([nan]);
|
|
|
|
assert!(!(n < m));
|
|
|
|
assert!(!(n > m));
|
|
|
|
assert!(!(n <= m));
|
|
|
|
assert!(!(n >= m));
|
|
|
|
|
|
|
|
let n = list_from([nan]);
|
|
|
|
let one = list_from([1.0]);
|
|
|
|
assert!(!(n < one));
|
|
|
|
assert!(!(n > one));
|
|
|
|
assert!(!(n <= one));
|
|
|
|
assert!(!(n >= one));
|
|
|
|
|
|
|
|
let u = list_from([1.0,2.0,nan]);
|
|
|
|
let v = list_from([1.0,2.0,3.0]);
|
|
|
|
assert!(!(u < v));
|
|
|
|
assert!(!(u > v));
|
|
|
|
assert!(!(u <= v));
|
|
|
|
assert!(!(u >= v));
|
|
|
|
|
|
|
|
let s = list_from([1.0,2.0,4.0,2.0]);
|
|
|
|
let t = list_from([1.0,2.0,3.0,2.0]);
|
|
|
|
assert!(!(s < t));
|
|
|
|
assert!(s > one);
|
|
|
|
assert!(!(s <= one));
|
|
|
|
assert!(s >= one);
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_fuzz() {
|
2013-11-21 21:20:48 -06:00
|
|
|
25.times(|| {
|
2013-07-09 20:49:32 -05:00
|
|
|
fuzz_test(3);
|
|
|
|
fuzz_test(16);
|
|
|
|
fuzz_test(189);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
fn fuzz_test(sz: int) {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<int> = DList::new();
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut v = ~[];
|
2013-08-03 11:45:23 -05:00
|
|
|
for i in range(0, sz) {
|
2013-07-09 20:49:32 -05:00
|
|
|
check_links(&m);
|
|
|
|
let r: u8 = rand::random();
|
|
|
|
match r % 6 {
|
|
|
|
0 => {
|
|
|
|
m.pop_back();
|
2013-12-23 09:20:52 -06:00
|
|
|
v.pop();
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
m.pop_front();
|
2013-12-23 09:40:42 -06:00
|
|
|
v.shift();
|
2013-07-09 20:49:32 -05:00
|
|
|
}
|
|
|
|
2 | 4 => {
|
|
|
|
m.push_front(-i);
|
|
|
|
v.unshift(-i);
|
|
|
|
}
|
|
|
|
3 | 5 | _ => {
|
|
|
|
m.push_back(i);
|
|
|
|
v.push(i);
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
check_links(&m);
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut i = 0u;
|
2013-08-07 21:21:36 -05:00
|
|
|
for (a, &b) in m.move_iter().zip(v.iter()) {
|
2013-07-09 20:49:32 -05:00
|
|
|
i += 1;
|
|
|
|
assert_eq!(a, b);
|
|
|
|
}
|
|
|
|
assert_eq!(i, v.len());
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_collect_into(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..64];
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-08-09 22:09:47 -05:00
|
|
|
let _: DList<int> = v.iter().map(|x| *x).collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_front(b: &mut test::BenchHarness) {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<int> = DList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-09 09:55:04 -05:00
|
|
|
m.push_front(0);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_back(b: &mut test::BenchHarness) {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<int> = DList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-09 09:55:04 -05:00
|
|
|
m.push_back(0);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-07-21 14:05:48 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
#[bench]
|
|
|
|
fn bench_push_back_pop_back(b: &mut test::BenchHarness) {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<int> = DList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-09 09:55:04 -05:00
|
|
|
m.push_back(0);
|
|
|
|
m.pop_back();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
2013-07-21 12:31:40 -05:00
|
|
|
#[bench]
|
|
|
|
fn bench_push_front_pop_front(b: &mut test::BenchHarness) {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<int> = DList::new();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-21 12:31:40 -05:00
|
|
|
m.push_front(0);
|
|
|
|
m.pop_front();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-21 12:31:40 -05:00
|
|
|
}
|
|
|
|
|
2013-07-21 14:05:48 -05:00
|
|
|
#[bench]
|
2013-07-22 18:31:30 -05:00
|
|
|
fn bench_rotate_forward(b: &mut test::BenchHarness) {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<int> = DList::new();
|
2013-07-21 14:05:48 -05:00
|
|
|
m.push_front(0);
|
|
|
|
m.push_front(1);
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-22 18:31:30 -05:00
|
|
|
m.rotate_forward();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-21 14:05:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
2013-07-22 18:31:30 -05:00
|
|
|
fn bench_rotate_backward(b: &mut test::BenchHarness) {
|
2013-08-08 13:38:10 -05:00
|
|
|
let mut m: DList<int> = DList::new();
|
2013-07-21 14:05:48 -05:00
|
|
|
m.push_front(0);
|
|
|
|
m.push_front(1);
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-07-22 18:31:30 -05:00
|
|
|
m.rotate_backward();
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-21 14:05:48 -05:00
|
|
|
}
|
2013-07-21 12:31:40 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
#[bench]
|
|
|
|
fn bench_iter(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-08-09 22:09:47 -05:00
|
|
|
let m: DList<int> = v.iter().map(|&x|x).collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-08-09 22:30:03 -05:00
|
|
|
assert!(m.iter().len() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_mut(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-08-09 22:09:47 -05:00
|
|
|
let mut m: DList<int> = v.iter().map(|&x|x).collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-08-09 22:30:03 -05:00
|
|
|
assert!(m.mut_iter().len() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_rev(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-08-09 22:09:47 -05:00
|
|
|
let m: DList<int> = v.iter().map(|&x|x).collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-08-09 22:30:03 -05:00
|
|
|
assert!(m.rev_iter().len() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_mut_rev(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-08-09 22:09:47 -05:00
|
|
|
let mut m: DList<int> = v.iter().map(|&x|x).collect();
|
2013-11-21 21:20:48 -06:00
|
|
|
b.iter(|| {
|
2013-08-09 22:30:03 -05:00
|
|
|
assert!(m.mut_rev_iter().len() == 128);
|
2013-11-21 21:20:48 -06:00
|
|
|
})
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|