1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A doubly-linked list with owned nodes.
13 //! The DList allows pushing and popping elements at either end.
15 //! DList implements the trait Deque. It should be imported with `use
16 //! collections::Deque`.
18 // DList is constructed like a singly-linked list over the field `next`.
19 // including the last link being None; each Node owns its `next` field.
21 // Backlinks over DList::prev are raw pointers that form a full chain in
22 // the reverse direction.
26 use alloc::boxed::Box;
27 use core::default::Default;
33 use {Collection, Mutable, Deque};
35 /// A doubly-linked list.
39 list_tail: Rawlink<Node<T>>,
42 type Link<T> = Option<Box<Node<T>>>;
43 struct Rawlink<T> { p: *mut T }
47 prev: Rawlink<Node<T>>,
51 /// Double-ended DList iterator
52 pub struct Items<'a, T> {
54 tail: Rawlink<Node<T>>,
58 // FIXME #11820: the &'a Option<> of the Link stops clone working.
59 impl<'a, T> Clone for Items<'a, T> {
60 fn clone(&self) -> Items<'a, T> { *self }
63 /// Double-ended mutable DList iterator
64 pub struct MutItems<'a, T> {
65 list: &'a mut DList<T>,
66 head: Rawlink<Node<T>>,
67 tail: Rawlink<Node<T>>,
71 /// DList consuming iterator
73 pub struct MoveItems<T> {
77 /// Rawlink is a type like Option<T> but for holding a raw pointer
79 /// Like Option::None for Rawlink
80 fn none() -> Rawlink<T> {
81 Rawlink{p: ptr::mut_null()}
84 /// Like Option::Some for Rawlink
85 fn some(n: &mut T) -> Rawlink<T> {
89 /// Convert the `Rawlink` into an Option value
90 fn resolve_immut<'a>(&self) -> Option<&'a T> {
92 mem::transmute(self.p.to_option())
96 /// Convert the `Rawlink` into an Option value
97 fn resolve<'a>(&mut self) -> Option<&'a mut T> {
101 Some(unsafe { mem::transmute(self.p) })
105 /// Return the `Rawlink` and replace with `Rawlink::none()`
106 fn take(&mut self) -> Rawlink<T> {
107 mem::replace(self, Rawlink::none())
111 impl<T> Clone for Rawlink<T> {
113 fn clone(&self) -> Rawlink<T> {
119 fn new(v: T) -> Node<T> {
120 Node{value: v, next: None, prev: Rawlink::none()}
124 /// Set the .prev field on `next`, then return `Some(next)`
125 fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
131 impl<T> Collection for DList<T> {
134 fn is_empty(&self) -> bool {
135 self.list_head.is_none()
139 fn len(&self) -> uint {
144 impl<T> Mutable for DList<T> {
145 /// Remove all elements from the DList
149 fn clear(&mut self) {
156 /// Add a Node first in the list
158 fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
159 match self.list_head {
161 self.list_tail = Rawlink::some(&mut *new_head);
162 self.list_head = link_with_prev(new_head, Rawlink::none());
164 Some(ref mut head) => {
165 new_head.prev = Rawlink::none();
166 head.prev = Rawlink::some(&mut *new_head);
167 mem::swap(head, &mut new_head);
168 head.next = Some(new_head);
174 /// Remove the first Node and return it, or None if the list is empty
176 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
177 self.list_head.take().map(|mut front_node| {
179 match front_node.next.take() {
180 Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
181 None => self.list_tail = Rawlink::none()
187 /// Add a Node last in the list
189 fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
190 match self.list_tail.resolve() {
191 None => return self.push_front_node(new_tail),
193 self.list_tail = Rawlink::some(&mut *new_tail);
194 tail.next = link_with_prev(new_tail, Rawlink::some(tail));
200 /// Remove the last Node and return it, or None if the list is empty
202 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
203 self.list_tail.resolve().map_or(None, |tail| {
205 self.list_tail = tail.prev;
206 match tail.prev.resolve() {
207 None => self.list_head.take(),
208 Some(tail_prev) => tail_prev.next.take()
214 impl<T> Deque<T> for DList<T> {
215 /// Provide a reference to the front element, or None if the list is empty
217 fn front<'a>(&'a self) -> Option<&'a T> {
218 self.list_head.as_ref().map(|head| &head.value)
221 /// Provide a mutable reference to the front element, or None if the list is empty
223 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
224 self.list_head.as_mut().map(|head| &mut head.value)
227 /// Provide a reference to the back element, or None if the list is empty
229 fn back<'a>(&'a self) -> Option<&'a T> {
230 self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value)
233 /// Provide a mutable reference to the back element, or None if the list is empty
235 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
236 self.list_tail.resolve().map(|tail| &mut tail.value)
239 /// Add an element first in the list
242 fn push_front(&mut self, elt: T) {
243 self.push_front_node(box Node::new(elt))
246 /// Remove the first element and return it, or None if the list is empty
249 fn pop_front(&mut self) -> Option<T> {
250 self.pop_front_node().map(|box Node{value, ..}| value)
253 /// Add an element last in the list
256 fn push_back(&mut self, elt: T) {
257 self.push_back_node(box Node::new(elt))
260 /// Remove the last element and return it, or None if the list is empty
263 fn pop_back(&mut self) -> Option<T> {
264 self.pop_back_node().map(|box Node{value, ..}| value)
268 impl<T> Default for DList<T> {
270 fn default() -> DList<T> { DList::new() }
274 /// Create an empty DList
276 pub fn new() -> DList<T> {
277 DList{list_head: None, list_tail: Rawlink::none(), length: 0}
280 /// Move the last element to the front of the list.
282 /// If the list is empty, do nothing.
287 /// use std::collections::{DList, Deque};
289 /// let mut dl = DList::new();
290 /// dl.push_back(1i);
294 /// dl.rotate_forward();
296 /// for e in dl.iter() {
297 /// println!("{}", e); // prints 3, then 1, then 2
301 pub fn rotate_forward(&mut self) {
302 self.pop_back_node().map(|tail| {
303 self.push_front_node(tail)
307 /// Move the first element to the back of the list.
309 /// If the list is empty, do nothing.
314 /// use std::collections::{DList, Deque};
316 /// let mut dl = DList::new();
317 /// dl.push_back(1i);
321 /// dl.rotate_backward();
323 /// for e in dl.iter() {
324 /// println!("{}", e); // prints 2, then 3, then 1
328 pub fn rotate_backward(&mut self) {
329 self.pop_front_node().map(|head| {
330 self.push_back_node(head)
334 /// Add all elements from `other` to the end of the list
341 /// use std::collections::{DList, Deque};
343 /// let mut a = DList::new();
344 /// let mut b = DList::new();
352 /// for e in a.iter() {
353 /// println!("{}", e); // prints 1, then 2, then 3, then 4
356 pub fn append(&mut self, mut other: DList<T>) {
357 match self.list_tail.resolve() {
358 None => *self = other,
360 // Carefully empty `other`.
361 let o_tail = other.list_tail.take();
362 let o_length = other.length;
363 match other.list_head.take() {
366 tail.next = link_with_prev(node, self.list_tail);
367 self.list_tail = o_tail;
368 self.length += o_length;
375 /// Add all elements from `other` to the beginning of the list
382 /// use std::collections::{DList, Deque};
384 /// let mut a = DList::new();
385 /// let mut b = DList::new();
393 /// for e in a.iter() {
394 /// println!("{}", e); // prints 3, then 4, then 1, then 2
398 pub fn prepend(&mut self, mut other: DList<T>) {
399 mem::swap(self, &mut other);
403 /// Insert `elt` before the first `x` in the list where `f(x, elt)` is true,
411 /// use std::collections::{DList, Deque};
413 /// let mut a: DList<int> = DList::new();
419 /// // insert 11 before the first odd number in the list
420 /// a.insert_when(11, |&e, _| e % 2 == 1);
422 /// for e in a.iter() {
423 /// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
426 pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
428 let mut it = self.mut_iter();
430 match it.peek_next() {
432 Some(x) => if f(x, &elt) { break }
440 /// Merge DList `other` into this DList, using the function `f`.
441 /// Iterate the both DList with `a` from self and `b` from `other`, and
442 /// put `a` in the result if `f(a, b)` is true, else `b`.
445 pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
447 let mut it = self.mut_iter();
449 let take_a = match (it.peek_next(), other.front()) {
450 (_ , None) => return,
452 (Some(ref mut x), Some(y)) => f(*x, y),
457 it.insert_next_node(other.pop_front_node().unwrap());
465 /// Provide a forward iterator
467 pub fn iter<'a>(&'a self) -> Items<'a, T> {
468 Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
471 /// Provide a forward iterator with mutable references
473 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
474 let head_raw = match self.list_head {
475 Some(ref mut h) => Rawlink::some(&mut **h),
476 None => Rawlink::none(),
481 tail: self.list_tail,
487 /// Consume the list into an iterator yielding elements by value
489 pub fn move_iter(self) -> MoveItems<T> {
490 MoveItems{list: self}
494 impl<T: Ord> DList<T> {
495 /// Insert `elt` sorted in ascending order
499 pub fn insert_ordered(&mut self, elt: T) {
500 self.insert_when(elt, |a, b| a >= b)
505 impl<T> Drop for DList<T> {
507 // Dissolve the dlist in backwards direction
508 // Just dropping the list_head can lead to stack exhaustion
509 // when length is >> 1_000_000
510 let mut tail = self.list_tail;
512 match tail.resolve() {
515 prev.next.take(); // release Box<Node<T>>
521 self.list_head = None;
522 self.list_tail = Rawlink::none();
527 impl<'a, A> Iterator<&'a A> for Items<'a, A> {
529 fn next(&mut self) -> Option<&'a A> {
533 self.head.as_ref().map(|head| {
535 self.head = &head.next;
541 fn size_hint(&self) -> (uint, Option<uint>) {
542 (self.nelem, Some(self.nelem))
546 impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
548 fn next_back(&mut self) -> Option<&'a A> {
552 self.tail.resolve_immut().as_ref().map(|prev| {
554 self.tail = prev.prev;
560 impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
562 impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
564 fn next(&mut self) -> Option<&'a mut A> {
568 self.head.resolve().map(|next| {
570 self.head = match next.next {
571 Some(ref mut node) => Rawlink::some(&mut **node),
572 None => Rawlink::none(),
579 fn size_hint(&self) -> (uint, Option<uint>) {
580 (self.nelem, Some(self.nelem))
584 impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
586 fn next_back(&mut self) -> Option<&'a mut A> {
590 self.tail.resolve().map(|prev| {
592 self.tail = prev.prev;
598 impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
600 /// Allow mutating the DList while iterating
601 pub trait ListInsertion<A> {
602 /// Insert `elt` just after to the element most recently returned by `.next()`
604 /// The inserted element does not appear in the iteration.
605 fn insert_next(&mut self, elt: A);
607 /// Provide a reference to the next element, without changing the iterator
608 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
611 // private methods for MutItems
612 impl<'a, A> MutItems<'a, A> {
613 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
614 // Insert before `self.head` so that it is between the
615 // previously yielded element and self.head.
617 // The inserted node will not appear in further iteration.
618 match self.head.resolve() {
619 None => { self.list.push_back_node(ins_node); }
621 let prev_node = match node.prev.resolve() {
622 None => return self.list.push_front_node(ins_node),
625 let node_own = prev_node.next.take_unwrap();
626 ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
627 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
628 self.list.length += 1;
634 impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
636 fn insert_next(&mut self, elt: A) {
637 self.insert_next_node(box Node::new(elt))
641 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
645 self.head.resolve().map(|head| &mut head.value)
649 impl<A> Iterator<A> for MoveItems<A> {
651 fn next(&mut self) -> Option<A> { self.list.pop_front() }
654 fn size_hint(&self) -> (uint, Option<uint>) {
655 (self.list.length, Some(self.list.length))
659 impl<A> DoubleEndedIterator<A> for MoveItems<A> {
661 fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
664 impl<A> FromIterator<A> for DList<A> {
665 fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
666 let mut ret = DList::new();
667 ret.extend(iterator);
672 impl<A> Extendable<A> for DList<A> {
673 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
674 for elt in iterator { self.push_back(elt); }
678 impl<A: PartialEq> PartialEq for DList<A> {
679 fn eq(&self, other: &DList<A>) -> bool {
680 self.len() == other.len() &&
681 iter::order::eq(self.iter(), other.iter())
684 fn ne(&self, other: &DList<A>) -> bool {
685 self.len() != other.len() ||
686 iter::order::ne(self.iter(), other.iter())
690 impl<A: PartialOrd> PartialOrd for DList<A> {
691 fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
692 iter::order::partial_cmp(self.iter(), other.iter())
696 impl<A: Clone> Clone for DList<A> {
697 fn clone(&self) -> DList<A> {
698 self.iter().map(|x| x.clone()).collect()
702 impl<A: fmt::Show> fmt::Show for DList<A> {
703 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
704 try!(write!(f, "["));
706 for (i, e) in self.iter().enumerate() {
707 if i != 0 { try!(write!(f, ", ")); }
708 try!(write!(f, "{}", *e));
723 use super::{DList, Node, ListInsertion};
726 pub fn check_links<T>(list: &DList<T>) {
728 let mut last_ptr: Option<&Node<T>> = None;
729 let mut node_ptr: &Node<T>;
730 match list.list_head {
731 None => { assert_eq!(0u, list.length); return }
732 Some(ref node) => node_ptr = &**node,
735 match (last_ptr, node_ptr.prev.resolve_immut()) {
737 (None , _ ) => fail!("prev link for list_head"),
738 (Some(p), Some(pptr)) => {
739 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
741 _ => fail!("prev link is none, not good"),
743 match node_ptr.next {
745 last_ptr = Some(node_ptr);
755 assert_eq!(len, list.length);
760 let mut m: DList<Box<int>> = DList::new();
761 assert_eq!(m.pop_front(), None);
762 assert_eq!(m.pop_back(), None);
763 assert_eq!(m.pop_front(), None);
765 assert_eq!(m.pop_front(), Some(box 1));
768 assert_eq!(m.len(), 2);
769 assert_eq!(m.pop_front(), Some(box 2));
770 assert_eq!(m.pop_front(), Some(box 3));
771 assert_eq!(m.len(), 0);
772 assert_eq!(m.pop_front(), None);
777 assert_eq!(m.pop_front(), Some(box 1));
779 let mut n = DList::new();
783 assert_eq!(n.front().unwrap(), &3);
784 let x = n.front_mut().unwrap();
789 assert_eq!(n.back().unwrap(), &2);
790 let y = n.back_mut().unwrap();
794 assert_eq!(n.pop_front(), Some(0));
795 assert_eq!(n.pop_front(), Some(1));
799 fn generate_test() -> DList<int> {
800 list_from(&[0i,1,2,3,4,5,6])
804 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
805 v.iter().map(|x| (*x).clone()).collect()
811 let mut m = DList::new();
812 let mut n = DList::new();
815 assert_eq!(m.len(), 1);
816 assert_eq!(m.pop_back(), Some(2));
820 let mut m = DList::new();
821 let n = DList::new();
824 assert_eq!(m.len(), 1);
825 assert_eq!(m.pop_back(), Some(2));
829 let v = vec![1i,2,3,4,5];
830 let u = vec![9i,8,1,2,3,4,5];
831 let mut m = list_from(v.as_slice());
832 m.append(list_from(u.as_slice()));
834 let sum = v.append(u.as_slice());
835 assert_eq!(sum.len(), m.len());
836 for elt in sum.move_iter() {
837 assert_eq!(m.pop_front(), Some(elt))
844 let mut m = DList::new();
845 let mut n = DList::new();
848 assert_eq!(m.len(), 1);
849 assert_eq!(m.pop_back(), Some(2));
853 let v = vec![1i,2,3,4,5];
854 let u = vec![9i,8,1,2,3,4,5];
855 let mut m = list_from(v.as_slice());
856 m.prepend(list_from(u.as_slice()));
858 let sum = u.append(v.as_slice());
859 assert_eq!(sum.len(), m.len());
860 for elt in sum.move_iter() {
861 assert_eq!(m.pop_front(), Some(elt))
867 let mut n: DList<int> = DList::new();
868 n.rotate_backward(); check_links(&n);
869 assert_eq!(n.len(), 0);
870 n.rotate_forward(); check_links(&n);
871 assert_eq!(n.len(), 0);
873 let v = vec![1i,2,3,4,5];
874 let mut m = list_from(v.as_slice());
875 m.rotate_backward(); check_links(&m);
876 m.rotate_forward(); check_links(&m);
877 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
878 m.rotate_forward(); check_links(&m);
879 m.rotate_forward(); check_links(&m);
880 m.pop_front(); check_links(&m);
881 m.rotate_forward(); check_links(&m);
882 m.rotate_backward(); check_links(&m);
883 m.push_front(9); check_links(&m);
884 m.rotate_forward(); check_links(&m);
885 assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
890 let m = generate_test();
891 for (i, elt) in m.iter().enumerate() {
892 assert_eq!(i as int, *elt);
894 let mut n = DList::new();
895 assert_eq!(n.iter().next(), None);
897 let mut it = n.iter();
898 assert_eq!(it.size_hint(), (1, Some(1)));
899 assert_eq!(it.next().unwrap(), &4);
900 assert_eq!(it.size_hint(), (0, Some(0)));
901 assert_eq!(it.next(), None);
905 fn test_iterator_clone() {
906 let mut n = DList::new();
910 let mut it = n.iter();
912 let mut jt = it.clone();
913 assert_eq!(it.next(), jt.next());
914 assert_eq!(it.next_back(), jt.next_back());
915 assert_eq!(it.next(), jt.next());
919 fn test_iterator_double_end() {
920 let mut n = DList::new();
921 assert_eq!(n.iter().next(), None);
925 let mut it = n.iter();
926 assert_eq!(it.size_hint(), (3, Some(3)));
927 assert_eq!(it.next().unwrap(), &6);
928 assert_eq!(it.size_hint(), (2, Some(2)));
929 assert_eq!(it.next_back().unwrap(), &4);
930 assert_eq!(it.size_hint(), (1, Some(1)));
931 assert_eq!(it.next_back().unwrap(), &5);
932 assert_eq!(it.next_back(), None);
933 assert_eq!(it.next(), None);
938 let m = generate_test();
939 for (i, elt) in m.iter().rev().enumerate() {
940 assert_eq!((6 - i) as int, *elt);
942 let mut n = DList::new();
943 assert_eq!(n.iter().rev().next(), None);
945 let mut it = n.iter().rev();
946 assert_eq!(it.size_hint(), (1, Some(1)));
947 assert_eq!(it.next().unwrap(), &4);
948 assert_eq!(it.size_hint(), (0, Some(0)));
949 assert_eq!(it.next(), None);
954 let mut m = generate_test();
955 let mut len = m.len();
956 for (i, elt) in m.mut_iter().enumerate() {
957 assert_eq!(i as int, *elt);
961 let mut n = DList::new();
962 assert!(n.mut_iter().next().is_none());
965 let mut it = n.mut_iter();
966 assert_eq!(it.size_hint(), (2, Some(2)));
967 assert!(it.next().is_some());
968 assert!(it.next().is_some());
969 assert_eq!(it.size_hint(), (0, Some(0)));
970 assert!(it.next().is_none());
974 fn test_iterator_mut_double_end() {
975 let mut n = DList::new();
976 assert!(n.mut_iter().next_back().is_none());
980 let mut it = n.mut_iter();
981 assert_eq!(it.size_hint(), (3, Some(3)));
982 assert_eq!(*it.next().unwrap(), 6);
983 assert_eq!(it.size_hint(), (2, Some(2)));
984 assert_eq!(*it.next_back().unwrap(), 4);
985 assert_eq!(it.size_hint(), (1, Some(1)));
986 assert_eq!(*it.next_back().unwrap(), 5);
987 assert!(it.next_back().is_none());
988 assert!(it.next().is_none());
992 fn test_insert_prev() {
993 let mut m = list_from(&[0i,2,4,6,8]);
996 let mut it = m.mut_iter();
1002 it.insert_next(*elt + 1);
1003 match it.peek_next() {
1004 Some(x) => assert_eq!(*x, *elt + 2),
1005 None => assert_eq!(8, *elt),
1014 assert_eq!(m.len(), 3 + len * 2);
1015 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
1020 let mut m = list_from([0i, 1, 3, 5, 6, 7, 2]);
1021 let n = list_from([-1i, 0, 0, 7, 7, 9]);
1022 let len = m.len() + n.len();
1023 m.merge(n, |a, b| a <= b);
1024 assert_eq!(m.len(), len);
1026 let res = m.move_iter().collect::<Vec<int>>();
1027 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
1031 fn test_insert_ordered() {
1032 let mut n = DList::new();
1033 n.insert_ordered(1i);
1034 assert_eq!(n.len(), 1);
1035 assert_eq!(n.pop_front(), Some(1));
1037 let mut m = DList::new();
1040 m.insert_ordered(3);
1042 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
1046 fn test_mut_rev_iter() {
1047 let mut m = generate_test();
1048 for (i, elt) in m.mut_iter().rev().enumerate() {
1049 assert_eq!((6-i) as int, *elt);
1051 let mut n = DList::new();
1052 assert!(n.mut_iter().rev().next().is_none());
1054 let mut it = n.mut_iter().rev();
1055 assert!(it.next().is_some());
1056 assert!(it.next().is_none());
1061 let n = list_from([1i,2,3]);
1064 assert_eq!(&[&1,&2,&3], n.iter().collect::<Vec<&int>>().as_slice());
1070 let mut n: DList<u8> = list_from([]);
1071 let mut m = list_from([]);
1078 let n = list_from([2i,3,4]);
1079 let m = list_from([1i,2,3]);
1085 let n: DList<int> = list_from([]);
1086 let m = list_from([1i,2,3]);
1095 let nan = 0.0f64/0.0;
1096 let n = list_from([nan]);
1097 let m = list_from([nan]);
1103 let n = list_from([nan]);
1104 let one = list_from([1.0f64]);
1105 assert!(!(n < one));
1106 assert!(!(n > one));
1107 assert!(!(n <= one));
1108 assert!(!(n >= one));
1110 let u = list_from([1.0f64,2.0,nan]);
1111 let v = list_from([1.0f64,2.0,3.0]);
1117 let s = list_from([1.0f64,2.0,4.0,2.0]);
1118 let t = list_from([1.0f64,2.0,3.0,2.0]);
1121 assert!(!(s <= one));
1127 for _ in range(0u, 25) {
1136 let list: DList<int> = range(0i, 10).collect();
1137 assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1139 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1142 assert!(list.to_string().as_slice() == "[just, one, test, more]");
1146 fn fuzz_test(sz: int) {
1147 let mut m: DList<int> = DList::new();
1149 for i in range(0, sz) {
1151 let r: u8 = rand::random();
1175 for (a, &b) in m.move_iter().zip(v.iter()) {
1179 assert_eq!(i, v.len());
1183 fn bench_collect_into(b: &mut test::Bencher) {
1184 let v = &[0i, ..64];
1186 let _: DList<int> = v.iter().map(|x| *x).collect();
1191 fn bench_push_front(b: &mut test::Bencher) {
1192 let mut m: DList<int> = DList::new();
1199 fn bench_push_back(b: &mut test::Bencher) {
1200 let mut m: DList<int> = DList::new();
1207 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1208 let mut m: DList<int> = DList::new();
1216 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1217 let mut m: DList<int> = DList::new();
1225 fn bench_rotate_forward(b: &mut test::Bencher) {
1226 let mut m: DList<int> = DList::new();
1235 fn bench_rotate_backward(b: &mut test::Bencher) {
1236 let mut m: DList<int> = DList::new();
1240 m.rotate_backward();
1245 fn bench_iter(b: &mut test::Bencher) {
1246 let v = &[0i, ..128];
1247 let m: DList<int> = v.iter().map(|&x|x).collect();
1249 assert!(m.iter().count() == 128);
1253 fn bench_iter_mut(b: &mut test::Bencher) {
1254 let v = &[0i, ..128];
1255 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1257 assert!(m.mut_iter().count() == 128);
1261 fn bench_iter_rev(b: &mut test::Bencher) {
1262 let v = &[0i, ..128];
1263 let m: DList<int> = v.iter().map(|&x|x).collect();
1265 assert!(m.iter().rev().count() == 128);
1269 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1270 let v = &[0i, ..128];
1271 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1273 assert!(m.mut_iter().rev().count() == 128);