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-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;
|
|
|
|
use std::cmp;
|
2013-07-09 20:49:32 -05:00
|
|
|
use std::ptr;
|
2013-07-09 09:55:04 -05:00
|
|
|
use std::util;
|
|
|
|
use std::iterator::FromIterator;
|
2012-09-19 18:52:32 -05:00
|
|
|
|
2013-07-10 08:27:15 -05:00
|
|
|
use container::Deque;
|
|
|
|
|
2013-07-09 09:55:04 -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 08:47:52 -05:00
|
|
|
/// DList iterator
|
2013-07-09 09:55:04 -05:00
|
|
|
pub struct ForwardIterator<'self, T> {
|
2013-07-11 08:47:52 -05:00
|
|
|
priv list: &'self DList<T>,
|
2013-07-09 09:55:04 -05:00
|
|
|
priv next: &'self Link<T>,
|
2013-07-09 09:55:04 -05:00
|
|
|
priv nelem: uint,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
/// DList reverse iterator
|
2013-07-09 09:55:04 -05:00
|
|
|
pub struct ReverseIterator<'self, T> {
|
2013-07-11 08:47:52 -05:00
|
|
|
priv list: &'self DList<T>,
|
2013-07-09 20:49:32 -05:00
|
|
|
priv next: 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 08:47:52 -05:00
|
|
|
/// DList mutable iterator
|
2013-07-09 09:55:04 -05:00
|
|
|
pub struct MutForwardIterator<'self, T> {
|
2013-07-11 08:47:52 -05:00
|
|
|
priv list: &'self mut DList<T>,
|
2013-07-09 20:49:32 -05:00
|
|
|
priv curs: Rawlink<Node<T>>,
|
2013-07-09 09:55:04 -05:00
|
|
|
priv nelem: uint,
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
/// DList mutable reverse iterator
|
2013-07-09 09:55:04 -05:00
|
|
|
pub struct MutReverseIterator<'self, T> {
|
2013-07-11 08:47:52 -05:00
|
|
|
priv list: &'self mut DList<T>,
|
2013-07-09 20:49:32 -05:00
|
|
|
priv next: 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-09 09:55:04 -05:00
|
|
|
pub struct ConsumeIterator<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-11 08:47:52 -05:00
|
|
|
/// DList reverse consuming iterator
|
2013-07-09 09:55:04 -05:00
|
|
|
pub struct ConsumeRevIterator<T> {
|
2013-07-11 08:47:52 -05:00
|
|
|
priv list: DList<T>
|
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-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)
|
|
|
|
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)
|
|
|
|
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)
|
|
|
|
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-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-10 08:27:15 -05:00
|
|
|
fn front<'a>(&'a self) -> Option<&'a T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list_head.chain_ref(|x| Some(&x.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-10 08:27:15 -05:00
|
|
|
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
match self.list_head {
|
|
|
|
None => None,
|
|
|
|
Some(ref mut head) => Some(&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-10 08:27:15 -05:00
|
|
|
fn back<'a>(&'a self) -> Option<&'a T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.list_tail.resolve_immut() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => None,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(tail) => Some(&tail.value),
|
2012-07-17 18:03:54 -05:00
|
|
|
}
|
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-10 08:27:15 -05:00
|
|
|
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.list_tail.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => None,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(tail) => Some(&mut tail.value),
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
|
|
|
/// Add an element last in the list
|
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-10 08:27:15 -05:00
|
|
|
fn push_back(&mut self, elt: T) {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.list_tail.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => return self.push_front(elt),
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(tail) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
let mut new_tail = ~Node{value: elt, next: None, prev: self.list_tail};
|
2013-07-09 20:49:32 -05:00
|
|
|
self.list_tail = Rawlink::some(new_tail);
|
2013-07-09 09:55:04 -05:00
|
|
|
tail.next = Some(new_tail);
|
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
self.length += 1;
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
|
|
|
/// Remove the last element and return it, or None if the list is empty
|
|
|
|
///
|
|
|
|
/// O(1)
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-07-10 08:27:15 -05:00
|
|
|
fn pop_back(&mut self) -> Option<T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.list_tail.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => None,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(tail) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.length -= 1;
|
2013-07-09 20:49:32 -05:00
|
|
|
let tail_own = match tail.prev.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => {
|
2013-07-09 20:49:32 -05:00
|
|
|
self.list_tail = Rawlink::none();
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list_head.swap_unwrap()
|
|
|
|
},
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(tail_prev) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list_tail = tail.prev;
|
2013-07-09 20:49:32 -05:00
|
|
|
tail_prev.next.swap_unwrap()
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(tail_own.value)
|
2013-06-23 16:57:39 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
2013-06-23 16:57:39 -05:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
/// Add an element first in the list
|
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-10 08:27:15 -05:00
|
|
|
fn push_front(&mut self, elt: T) {
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut new_head = ~Node{value: elt, next: None, prev: Rawlink::none()};
|
2013-07-09 09:55:04 -05:00
|
|
|
match self.list_head {
|
|
|
|
None => {
|
2013-07-09 20:49:32 -05:00
|
|
|
self.list_tail = Rawlink::some(new_head);
|
2013-07-09 09:55:04 -05:00
|
|
|
self.list_head = Some(new_head);
|
2013-06-23 16:57:39 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
Some(ref mut head) => {
|
2013-07-09 20:49:32 -05:00
|
|
|
head.prev = Rawlink::some(new_head);
|
2013-07-09 09:55:04 -05:00
|
|
|
util::swap(head, &mut new_head);
|
|
|
|
head.next = Some(new_head);
|
2013-06-23 16:57:39 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
self.length += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove the first element and return it, or None if the list is empty
|
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-10 08:27:15 -05:00
|
|
|
fn pop_front(&mut self) -> Option<T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
match util::replace(&mut self.list_head, None) {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => None,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(old_head) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.length -= 1;
|
2013-07-09 20:49:32 -05:00
|
|
|
match *old_head {
|
2013-07-09 09:55:04 -05:00
|
|
|
Node{value: value, next: Some(next), prev: _} => {
|
2013-07-09 20:49:32 -05:00
|
|
|
self.list_head = link_with_prev(next, Rawlink::none());
|
2013-07-09 09:55:04 -05:00
|
|
|
Some(value)
|
|
|
|
}
|
|
|
|
Node{value: value, next: None, prev: _} => {
|
2013-07-09 20:49:32 -05:00
|
|
|
self.list_tail = Rawlink::none();
|
2013-07-09 09:55:04 -05:00
|
|
|
Some(value)
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 21:37:57 -05:00
|
|
|
}
|
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
|
|
|
|
|
|
|
/// Add all elements from `other` to the end of the list
|
|
|
|
///
|
|
|
|
/// O(1)
|
2013-07-11 08:47:52 -05:00
|
|
|
pub fn append(&mut self, 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-07-09 09:55:04 -05:00
|
|
|
match other {
|
2013-07-11 08:47:52 -05:00
|
|
|
DList{list_head: None, list_tail: _, length: _} => return,
|
|
|
|
DList{list_head: Some(node), list_tail: o_tail, length: o_length} => {
|
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-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)
|
|
|
|
#[inline]
|
2013-07-09 20:49:32 -05:00
|
|
|
pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) {
|
2013-07-09 09:55:04 -05:00
|
|
|
{
|
|
|
|
let mut it = self.mut_iter();
|
|
|
|
loop {
|
|
|
|
match it.next() {
|
|
|
|
None => break,
|
|
|
|
Some(x) => if f(x, &elt) { it.insert_before(elt); return }
|
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
self.push_back(elt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Merge, using the function `f`; take `a` if `f(a, b)` is true, else `b`.
|
|
|
|
///
|
|
|
|
/// O(max(N, M))
|
2013-07-11 08:47:52 -05:00
|
|
|
pub fn merge(&mut self, mut other: DList<T>, f: &fn(&T, &T) -> bool) {
|
2013-07-09 09:55:04 -05:00
|
|
|
{
|
|
|
|
let mut it = self.mut_iter();
|
|
|
|
loop {
|
2013-07-10 08:27:15 -05:00
|
|
|
match (it.next(), other.front()) {
|
2013-07-09 09:55:04 -05:00
|
|
|
(None , _ ) => break,
|
|
|
|
(_ , None ) => return,
|
|
|
|
(Some(x), Some(y)) => if f(x, y) { loop }
|
|
|
|
}
|
|
|
|
it.insert_before(other.pop_front().unwrap());
|
2012-09-21 21:37:57 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
self.append(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Provide a forward iterator
|
|
|
|
pub fn iter<'a>(&'a self) -> ForwardIterator<'a, T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
ForwardIterator{nelem: self.len(), list: self, next: &self.list_head}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide a reverse iterator
|
|
|
|
pub fn rev_iter<'a>(&'a self) -> ReverseIterator<'a, T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
ReverseIterator{nelem: self.len(), list: self, next: self.list_tail}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide a forward iterator with mutable references
|
|
|
|
pub fn mut_iter<'a>(&'a mut self) -> MutForwardIterator<'a, T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
MutForwardIterator{nelem: self.len(), list: self, curs: Rawlink::none()}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide a reverse iterator with mutable references
|
|
|
|
pub fn mut_rev_iter<'a>(&'a mut self) -> MutReverseIterator<'a, T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
MutReverseIterator{nelem: self.len(), list: self, next: self.list_tail}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Consume the list into an iterator yielding elements by value
|
|
|
|
pub fn consume_iter(self) -> ConsumeIterator<T> {
|
|
|
|
ConsumeIterator{list: self}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume the list into an iterator yielding elements by value, in reverse
|
|
|
|
pub fn consume_rev_iter(self) -> ConsumeRevIterator<T> {
|
|
|
|
ConsumeRevIterator{list: self}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert sorted in ascending order
|
|
|
|
///
|
|
|
|
/// O(N)
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<T: cmp::TotalOrd> DList<T> {
|
2013-07-09 09:55:04 -05:00
|
|
|
fn insert_ordered(&mut self, elt: T) {
|
2013-07-09 20:49:32 -05:00
|
|
|
self.insert_when(elt, |a, b| a.cmp(b) != cmp::Less);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self, A> Iterator<&'self A> for ForwardIterator<'self, A> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'self A> {
|
|
|
|
match *self.next {
|
|
|
|
None => None,
|
|
|
|
Some(ref next) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.nelem -= 1;
|
2013-07-09 09:55:04 -05:00
|
|
|
self.next = &next.next;
|
|
|
|
Some(&next.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
// MutForwardIterator is different because it implements ListInsertion,
|
2013-07-09 09:55:04 -05:00
|
|
|
// and can modify the list during traversal, used in insert_when and merge.
|
|
|
|
impl<'self, A> Iterator<&'self mut A> for MutForwardIterator<'self, A> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'self mut A> {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.curs.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => {
|
|
|
|
match self.list.list_head {
|
|
|
|
None => None,
|
|
|
|
Some(ref mut head) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.nelem -= 1;
|
2013-07-09 20:49:32 -05:00
|
|
|
self.curs = Rawlink::some(*head);
|
2013-07-09 09:55:04 -05:00
|
|
|
Some(&mut head.value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(curs) => {
|
|
|
|
match curs.next {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => None,
|
|
|
|
Some(ref mut head) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.nelem -= 1;
|
2013-07-09 20:49:32 -05:00
|
|
|
self.curs = Rawlink::some(*head);
|
2013-07-09 09:55:04 -05:00
|
|
|
Some(&mut head.value)
|
|
|
|
}
|
|
|
|
}
|
2012-09-21 21:37:57 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'self, A> Iterator<&'self A> for ReverseIterator<'self, A> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'self A> {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.next.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => None,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(prev) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.nelem -= 1;
|
2013-07-09 09:55:04 -05:00
|
|
|
self.next = prev.prev;
|
|
|
|
Some(&prev.value)
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
|
|
|
}
|
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))
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
impl<'self, A> Iterator<&'self mut A> for MutReverseIterator<'self, A> {
|
|
|
|
#[inline]
|
|
|
|
fn next(&mut self) -> Option<&'self mut A> {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.next.resolve() {
|
2013-07-09 09:55:04 -05:00
|
|
|
None => None,
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(prev) => {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.nelem -= 1;
|
2013-07-09 09:55:04 -05:00
|
|
|
self.next = prev.prev;
|
|
|
|
Some(&mut prev.value)
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
}
|
|
|
|
|
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-01-24 17:40:46 -06:00
|
|
|
}
|
2013-07-09 09:55:04 -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-09 09:55:04 -05:00
|
|
|
/// Insert `elt` just previous to the most recently yielded element
|
|
|
|
fn insert_before(&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
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
impl<'self, A> ListInsertion<A> for MutForwardIterator<'self, A> {
|
2013-07-09 09:55:04 -05:00
|
|
|
fn insert_before(&mut self, elt: A) {
|
2013-07-09 20:49:32 -05:00
|
|
|
match self.curs.resolve() {
|
2013-07-09 20:49:32 -05:00
|
|
|
None => { self.list.push_front(elt); self.next(); }
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(node) => {
|
|
|
|
let prev_node = match node.prev.resolve() {
|
2013-07-09 20:49:32 -05:00
|
|
|
None => return self.list.push_front(elt),
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(prev) => prev,
|
2013-07-09 09:55:04 -05:00
|
|
|
};
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut ins_node = ~Node{value: elt, next: None, prev: Rawlink::none()};
|
|
|
|
let node_own = prev_node.next.swap_unwrap();
|
|
|
|
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-09 20:49:32 -05:00
|
|
|
|
|
|
|
fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
|
|
|
|
match self.curs.resolve() {
|
2013-07-10 08:27:15 -05:00
|
|
|
None => self.list.front_mut(),
|
2013-07-09 20:49:32 -05:00
|
|
|
Some(curs) => match curs.next {
|
|
|
|
None => None,
|
|
|
|
Some(ref mut node) => Some(&mut node.value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
impl<A> Iterator<A> for ConsumeIterator<A> {
|
|
|
|
fn next(&mut self) -> Option<A> { self.list.pop_front() }
|
|
|
|
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
|
|
|
|
2013-07-09 09:55:04 -05:00
|
|
|
impl<A> Iterator<A> for ConsumeRevIterator<A> {
|
|
|
|
fn next(&mut self) -> Option<A> { self.list.pop_back() }
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
(self.list.length, Some(self.list.length))
|
|
|
|
}
|
|
|
|
}
|
2013-01-24 17:40:46 -06:00
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<A, T: Iterator<A>> FromIterator<A, T> for DList<A> {
|
|
|
|
fn from_iterator(iterator: &mut T) -> DList<A> {
|
|
|
|
let mut ret = DList::new();
|
2013-07-09 09:55:04 -05:00
|
|
|
for iterator.advance |elt| { ret.push_back(elt); }
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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() &&
|
|
|
|
self.iter().zip(other.iter()).all(|(a, b)| a.eq(b))
|
|
|
|
}
|
2013-07-11 08:47:52 -05:00
|
|
|
fn ne(&self, other: &DList<A>) -> bool {
|
2013-07-09 09:55:04 -05:00
|
|
|
!self.eq(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 08:47:52 -05:00
|
|
|
impl<A: Clone> Clone for DList<A> {
|
|
|
|
fn clone(&self) -> DList<A> {
|
2013-07-09 09:55:04 -05:00
|
|
|
self.iter().transform(|x| x.clone()).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-07-11 08:47:52 -05:00
|
|
|
pub fn check_links<T>(list: &DList<T>) {
|
2013-07-09 09:55:04 -05:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
loop {
|
2013-07-09 20:49:32 -05:00
|
|
|
match (last_ptr, node_ptr.prev.resolve_immut()) {
|
2013-07-09 09:55:04 -05:00
|
|
|
(None , None ) => {}
|
|
|
|
(None , _ ) => fail!("prev link for list_head"),
|
|
|
|
(Some(p), Some(pptr)) => {
|
2013-07-09 20:49:32 -05:00
|
|
|
assert_eq!(p as *Node<T>, pptr as *Node<T>);
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
_ => fail!("prev link is none, not good"),
|
|
|
|
}
|
|
|
|
match node_ptr.next {
|
|
|
|
Some(ref next) => {
|
|
|
|
last_ptr = Some(node_ptr);
|
|
|
|
node_ptr = &**next;
|
|
|
|
len += 1;
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
len += 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert_eq!(len, list.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
2013-07-09 20:49:32 -05:00
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use std::rand;
|
|
|
|
use std::int;
|
|
|
|
use extra::test;
|
2013-03-08 17:19:30 -06:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_basic() {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new::<~int>();
|
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-11 08:47:52 -05:00
|
|
|
fn list_from<T: Copy>(v: &[T]) -> DList<T> {
|
2013-07-09 20:49:32 -05:00
|
|
|
v.iter().transform(|x| copy *x).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());
|
|
|
|
for sum.consume_iter().advance |elt| {
|
|
|
|
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());
|
|
|
|
for sum.consume_iter().advance |elt| {
|
|
|
|
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_iterator() {
|
|
|
|
let m = generate_test();
|
|
|
|
for m.iter().enumerate().advance |(i, elt)| {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rev_iter() {
|
|
|
|
let m = generate_test();
|
|
|
|
for m.rev_iter().enumerate().advance |(i, elt)| {
|
|
|
|
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();
|
|
|
|
for m.mut_iter().enumerate().advance |(i, elt)| {
|
|
|
|
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);
|
|
|
|
let mut it = n.mut_iter();
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert!(it.next().is_some());
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-09 20:49:32 -05:00
|
|
|
it.insert_before(-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) => {
|
|
|
|
it.insert_before(*elt + 1);
|
|
|
|
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-09 20:49:32 -05:00
|
|
|
it.insert_before(0);
|
|
|
|
it.insert_before(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);
|
|
|
|
assert_eq!(m.consume_iter().collect::<~[int]>(), ~[-2,1,0,3,2,5,4,7,6,9,0,1,8]);
|
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);
|
|
|
|
let res = m.consume_iter().collect::<~[int]>();
|
|
|
|
assert_eq!(res, ~[-1, 0, 0, 1, 0, 3, 5, 6, 7, 2, 7, 7, 9]);
|
|
|
|
}
|
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);
|
|
|
|
assert_eq!(~[2,3,4], m.consume_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();
|
|
|
|
for m.mut_rev_iter().enumerate().advance |(i, elt)| {
|
|
|
|
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-07-09 09:55:04 -05:00
|
|
|
}
|
2012-12-27 19:53:04 -06:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_fuzz() {
|
|
|
|
for 25.times {
|
|
|
|
fuzz_test(3);
|
|
|
|
fuzz_test(16);
|
|
|
|
fuzz_test(189);
|
|
|
|
}
|
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|
2013-07-09 20:49:32 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
fn fuzz_test(sz: int) {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new::<int>();
|
2013-07-09 20:49:32 -05:00
|
|
|
let mut v = ~[];
|
|
|
|
for int::range(0i, sz) |i| {
|
|
|
|
check_links(&m);
|
|
|
|
let r: u8 = rand::random();
|
|
|
|
match r % 6 {
|
|
|
|
0 => {
|
|
|
|
m.pop_back();
|
|
|
|
if v.len() > 0 { v.pop(); }
|
|
|
|
}
|
|
|
|
1 => {
|
|
|
|
m.pop_front();
|
|
|
|
if v.len() > 0 { v.shift(); }
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
for m.consume_iter().zip(v.iter()).advance |(a, &b)| {
|
|
|
|
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];
|
|
|
|
do b.iter {
|
2013-07-11 08:47:52 -05:00
|
|
|
let _: DList<int> = v.iter().transform(|&x|x).collect();
|
2013-07-09 09:55:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_collect_into_vec(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..64];
|
|
|
|
do b.iter {
|
|
|
|
let _: ~[int] = v.iter().transform(|&x|x).collect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_front(b: &mut test::BenchHarness) {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new::<int>();
|
2013-07-09 09:55:04 -05:00
|
|
|
do b.iter {
|
|
|
|
m.push_front(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_front_vec_size10(b: &mut test::BenchHarness) {
|
|
|
|
let mut m = ~[0, ..10];
|
|
|
|
do b.iter {
|
|
|
|
m.unshift(0);
|
|
|
|
m.pop(); // to keep it fair, dont' grow the vec
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_back(b: &mut test::BenchHarness) {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new::<int>();
|
2013-07-09 09:55:04 -05:00
|
|
|
do b.iter {
|
|
|
|
m.push_back(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_back_vec(b: &mut test::BenchHarness) {
|
|
|
|
let mut m = ~[];
|
|
|
|
do b.iter {
|
|
|
|
m.push(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_back_pop_back(b: &mut test::BenchHarness) {
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m = DList::new::<int>();
|
2013-07-09 09:55:04 -05:00
|
|
|
do b.iter {
|
|
|
|
m.push_back(0);
|
|
|
|
m.pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_push_back_pop_back_vec(b: &mut test::BenchHarness) {
|
|
|
|
let mut m = ~[];
|
|
|
|
do b.iter {
|
|
|
|
m.push(0);
|
|
|
|
m.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-07-11 08:47:52 -05:00
|
|
|
let m: DList<int> = v.iter().transform(|&x|x).collect();
|
2013-07-09 09:55:04 -05:00
|
|
|
do b.iter {
|
|
|
|
for m.iter().advance |_| {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_mut(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m: DList<int> = v.iter().transform(|&x|x).collect();
|
2013-07-09 09:55:04 -05:00
|
|
|
do b.iter {
|
|
|
|
for m.mut_iter().advance |_| {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_rev(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-07-11 08:47:52 -05:00
|
|
|
let m: DList<int> = v.iter().transform(|&x|x).collect();
|
2013-07-09 09:55:04 -05:00
|
|
|
do b.iter {
|
|
|
|
for m.rev_iter().advance |_| {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_mut_rev(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
2013-07-11 08:47:52 -05:00
|
|
|
let mut m: DList<int> = v.iter().transform(|&x|x).collect();
|
2013-07-09 09:55:04 -05:00
|
|
|
do b.iter {
|
|
|
|
for m.mut_rev_iter().advance |_| {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[bench]
|
|
|
|
fn bench_iter_vec(b: &mut test::BenchHarness) {
|
|
|
|
let v = &[0, ..128];
|
|
|
|
do b.iter {
|
|
|
|
for v.iter().advance |_| {}
|
2012-07-17 18:03:54 -05:00
|
|
|
}
|
2012-07-02 15:47:55 -05:00
|
|
|
}
|
2012-06-29 23:21:15 -05:00
|
|
|
}
|
2013-07-09 09:55:04 -05:00
|
|
|
|