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;
32 use std::hash::{Writer, Hash};
34 use {Collection, Mutable, Deque, MutableSeq};
36 /// A doubly-linked list.
40 list_tail: Rawlink<Node<T>>,
43 type Link<T> = Option<Box<Node<T>>>;
44 struct Rawlink<T> { p: *mut T }
48 prev: Rawlink<Node<T>>,
52 /// Double-ended DList iterator
53 pub struct Items<'a, T> {
55 tail: Rawlink<Node<T>>,
59 // FIXME #11820: the &'a Option<> of the Link stops clone working.
60 impl<'a, T> Clone for Items<'a, T> {
61 fn clone(&self) -> Items<'a, T> { *self }
64 /// Double-ended mutable DList iterator
65 pub struct MutItems<'a, T> {
66 list: &'a mut DList<T>,
67 head: Rawlink<Node<T>>,
68 tail: Rawlink<Node<T>>,
72 /// DList consuming iterator
74 pub struct MoveItems<T> {
78 /// Rawlink is a type like Option<T> but for holding a raw pointer
80 /// Like Option::None for Rawlink
81 fn none() -> Rawlink<T> {
82 Rawlink{p: ptr::mut_null()}
85 /// Like Option::Some for Rawlink
86 fn some(n: &mut T) -> Rawlink<T> {
90 /// Convert the `Rawlink` into an Option value
91 fn resolve_immut<'a>(&self) -> Option<&'a T> {
93 mem::transmute(self.p.to_option())
97 /// Convert the `Rawlink` into an Option value
98 fn resolve<'a>(&mut self) -> Option<&'a mut T> {
102 Some(unsafe { mem::transmute(self.p) })
106 /// Return the `Rawlink` and replace with `Rawlink::none()`
107 fn take(&mut self) -> Rawlink<T> {
108 mem::replace(self, Rawlink::none())
112 impl<T> Clone for Rawlink<T> {
114 fn clone(&self) -> Rawlink<T> {
120 fn new(v: T) -> Node<T> {
121 Node{value: v, next: None, prev: Rawlink::none()}
125 /// Set the .prev field on `next`, then return `Some(next)`
126 fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
132 impl<T> Collection for DList<T> {
135 fn is_empty(&self) -> bool {
136 self.list_head.is_none()
140 fn len(&self) -> uint {
145 impl<T> Mutable for DList<T> {
146 /// Remove all elements from the DList
150 fn clear(&mut self) {
157 /// Add a Node first in the list
159 fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
160 match self.list_head {
162 self.list_tail = Rawlink::some(&mut *new_head);
163 self.list_head = link_with_prev(new_head, Rawlink::none());
165 Some(ref mut head) => {
166 new_head.prev = Rawlink::none();
167 head.prev = Rawlink::some(&mut *new_head);
168 mem::swap(head, &mut new_head);
169 head.next = Some(new_head);
175 /// Remove the first Node and return it, or None if the list is empty
177 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
178 self.list_head.take().map(|mut front_node| {
180 match front_node.next.take() {
181 Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
182 None => self.list_tail = Rawlink::none()
188 /// Add a Node last in the list
190 fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
191 match self.list_tail.resolve() {
192 None => return self.push_front_node(new_tail),
194 self.list_tail = Rawlink::some(&mut *new_tail);
195 tail.next = link_with_prev(new_tail, Rawlink::some(tail));
201 /// Remove the last Node and return it, or None if the list is empty
203 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
204 self.list_tail.resolve().map_or(None, |tail| {
206 self.list_tail = tail.prev;
207 match tail.prev.resolve() {
208 None => self.list_head.take(),
209 Some(tail_prev) => tail_prev.next.take()
215 impl<T> Deque<T> for DList<T> {
216 /// Provide a reference to the front element, or None if the list is empty
218 fn front<'a>(&'a self) -> Option<&'a T> {
219 self.list_head.as_ref().map(|head| &head.value)
222 /// Provide a mutable reference to the front element, or None if the list is empty
224 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
225 self.list_head.as_mut().map(|head| &mut head.value)
228 /// Provide a reference to the back element, or None if the list is empty
230 fn back<'a>(&'a self) -> Option<&'a T> {
231 self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value)
234 /// Provide a mutable reference to the back element, or None if the list is empty
236 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
237 self.list_tail.resolve().map(|tail| &mut tail.value)
240 /// Add an element first in the list
243 fn push_front(&mut self, elt: T) {
244 self.push_front_node(box Node::new(elt))
247 /// Remove the first element and return it, or None if the list is empty
250 fn pop_front(&mut self) -> Option<T> {
251 self.pop_front_node().map(|box Node{value, ..}| value)
255 impl<T> MutableSeq<T> for DList<T> {
256 fn push(&mut self, elt: T) {
257 self.push_back_node(box Node::new(elt))
259 fn pop(&mut self) -> Option<T> {
260 self.pop_back_node().map(|box Node{value, ..}| value)
264 impl<T> Default for DList<T> {
266 fn default() -> DList<T> { DList::new() }
270 /// Create an empty DList
272 pub fn new() -> DList<T> {
273 DList{list_head: None, list_tail: Rawlink::none(), length: 0}
276 /// Move the last element to the front of the list.
278 /// If the list is empty, do nothing.
283 /// use std::collections::DList;
285 /// let mut dl = DList::new();
290 /// dl.rotate_forward();
292 /// for e in dl.iter() {
293 /// println!("{}", e); // prints 3, then 1, then 2
297 pub fn rotate_forward(&mut self) {
298 self.pop_back_node().map(|tail| {
299 self.push_front_node(tail)
303 /// Move the first element to the back of the list.
305 /// If the list is empty, do nothing.
310 /// use std::collections::DList;
312 /// let mut dl = DList::new();
317 /// dl.rotate_backward();
319 /// for e in dl.iter() {
320 /// println!("{}", e); // prints 2, then 3, then 1
324 pub fn rotate_backward(&mut self) {
325 self.pop_front_node().map(|head| {
326 self.push_back_node(head)
330 /// Add all elements from `other` to the end of the list
337 /// use std::collections::DList;
339 /// let mut a = DList::new();
340 /// let mut b = DList::new();
348 /// for e in a.iter() {
349 /// println!("{}", e); // prints 1, then 2, then 3, then 4
352 pub fn append(&mut self, mut other: DList<T>) {
353 match self.list_tail.resolve() {
354 None => *self = other,
356 // Carefully empty `other`.
357 let o_tail = other.list_tail.take();
358 let o_length = other.length;
359 match other.list_head.take() {
362 tail.next = link_with_prev(node, self.list_tail);
363 self.list_tail = o_tail;
364 self.length += o_length;
371 /// Add all elements from `other` to the beginning of the list
378 /// use std::collections::DList;
380 /// let mut a = DList::new();
381 /// let mut b = DList::new();
389 /// for e in a.iter() {
390 /// println!("{}", e); // prints 3, then 4, then 1, then 2
394 pub fn prepend(&mut self, mut other: DList<T>) {
395 mem::swap(self, &mut other);
399 /// Insert `elt` before the first `x` in the list where `f(x, elt)` is true,
407 /// use std::collections::DList;
409 /// let mut a: DList<int> = DList::new();
415 /// // insert 11 before the first odd number in the list
416 /// a.insert_when(11, |&e, _| e % 2 == 1);
418 /// for e in a.iter() {
419 /// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
422 pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
424 let mut it = self.mut_iter();
426 match it.peek_next() {
428 Some(x) => if f(x, &elt) { break }
436 /// Merge DList `other` into this DList, using the function `f`.
437 /// Iterate the both DList with `a` from self and `b` from `other`, and
438 /// put `a` in the result if `f(a, b)` is true, else `b`.
441 pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
443 let mut it = self.mut_iter();
445 let take_a = match (it.peek_next(), other.front()) {
446 (_ , None) => return,
448 (Some(ref mut x), Some(y)) => f(*x, y),
453 it.insert_next_node(other.pop_front_node().unwrap());
461 /// Provide a forward iterator
463 pub fn iter<'a>(&'a self) -> Items<'a, T> {
464 Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
467 /// Provide a forward iterator with mutable references
469 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
470 let head_raw = match self.list_head {
471 Some(ref mut h) => Rawlink::some(&mut **h),
472 None => Rawlink::none(),
477 tail: self.list_tail,
483 /// Consume the list into an iterator yielding elements by value
485 pub fn move_iter(self) -> MoveItems<T> {
486 MoveItems{list: self}
490 impl<T: Ord> DList<T> {
491 /// Insert `elt` sorted in ascending order
495 pub fn insert_ordered(&mut self, elt: T) {
496 self.insert_when(elt, |a, b| a >= b)
501 impl<T> Drop for DList<T> {
503 // Dissolve the dlist in backwards direction
504 // Just dropping the list_head can lead to stack exhaustion
505 // when length is >> 1_000_000
506 let mut tail = self.list_tail;
508 match tail.resolve() {
511 prev.next.take(); // release Box<Node<T>>
517 self.list_head = None;
518 self.list_tail = Rawlink::none();
523 impl<'a, A> Iterator<&'a A> for Items<'a, A> {
525 fn next(&mut self) -> Option<&'a A> {
529 self.head.as_ref().map(|head| {
531 self.head = &head.next;
537 fn size_hint(&self) -> (uint, Option<uint>) {
538 (self.nelem, Some(self.nelem))
542 impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
544 fn next_back(&mut self) -> Option<&'a A> {
548 self.tail.resolve_immut().as_ref().map(|prev| {
550 self.tail = prev.prev;
556 impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
558 impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
560 fn next(&mut self) -> Option<&'a mut A> {
564 self.head.resolve().map(|next| {
566 self.head = match next.next {
567 Some(ref mut node) => Rawlink::some(&mut **node),
568 None => Rawlink::none(),
575 fn size_hint(&self) -> (uint, Option<uint>) {
576 (self.nelem, Some(self.nelem))
580 impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
582 fn next_back(&mut self) -> Option<&'a mut A> {
586 self.tail.resolve().map(|prev| {
588 self.tail = prev.prev;
594 impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
596 /// Allow mutating the DList while iterating
597 pub trait ListInsertion<A> {
598 /// Insert `elt` just after to the element most recently returned by `.next()`
600 /// The inserted element does not appear in the iteration.
601 fn insert_next(&mut self, elt: A);
603 /// Provide a reference to the next element, without changing the iterator
604 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
607 // private methods for MutItems
608 impl<'a, A> MutItems<'a, A> {
609 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
610 // Insert before `self.head` so that it is between the
611 // previously yielded element and self.head.
613 // The inserted node will not appear in further iteration.
614 match self.head.resolve() {
615 None => { self.list.push_back_node(ins_node); }
617 let prev_node = match node.prev.resolve() {
618 None => return self.list.push_front_node(ins_node),
621 let node_own = prev_node.next.take_unwrap();
622 ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
623 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
624 self.list.length += 1;
630 impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
632 fn insert_next(&mut self, elt: A) {
633 self.insert_next_node(box Node::new(elt))
637 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
641 self.head.resolve().map(|head| &mut head.value)
645 impl<A> Iterator<A> for MoveItems<A> {
647 fn next(&mut self) -> Option<A> { self.list.pop_front() }
650 fn size_hint(&self) -> (uint, Option<uint>) {
651 (self.list.length, Some(self.list.length))
655 impl<A> DoubleEndedIterator<A> for MoveItems<A> {
657 fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
660 impl<A> FromIterator<A> for DList<A> {
661 fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
662 let mut ret = DList::new();
663 ret.extend(iterator);
668 impl<A> Extendable<A> for DList<A> {
669 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
670 for elt in iterator { self.push_back(elt); }
674 impl<A: PartialEq> PartialEq for DList<A> {
675 fn eq(&self, other: &DList<A>) -> bool {
676 self.len() == other.len() &&
677 iter::order::eq(self.iter(), other.iter())
680 fn ne(&self, other: &DList<A>) -> bool {
681 self.len() != other.len() ||
682 iter::order::ne(self.iter(), other.iter())
686 impl<A: PartialOrd> PartialOrd for DList<A> {
687 fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
688 iter::order::partial_cmp(self.iter(), other.iter())
692 impl<A: Clone> Clone for DList<A> {
693 fn clone(&self) -> DList<A> {
694 self.iter().map(|x| x.clone()).collect()
698 impl<A: fmt::Show> fmt::Show for DList<A> {
699 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
700 try!(write!(f, "["));
702 for (i, e) in self.iter().enumerate() {
703 if i != 0 { try!(write!(f, ", ")); }
704 try!(write!(f, "{}", *e));
711 impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
712 fn hash(&self, state: &mut S) {
713 self.len().hash(state);
714 for elt in self.iter() {
728 use {Deque, MutableSeq};
729 use super::{DList, Node, ListInsertion};
732 pub fn check_links<T>(list: &DList<T>) {
734 let mut last_ptr: Option<&Node<T>> = None;
735 let mut node_ptr: &Node<T>;
736 match list.list_head {
737 None => { assert_eq!(0u, list.length); return }
738 Some(ref node) => node_ptr = &**node,
741 match (last_ptr, node_ptr.prev.resolve_immut()) {
743 (None , _ ) => fail!("prev link for list_head"),
744 (Some(p), Some(pptr)) => {
745 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
747 _ => fail!("prev link is none, not good"),
749 match node_ptr.next {
751 last_ptr = Some(node_ptr);
761 assert_eq!(len, list.length);
766 let mut m: DList<Box<int>> = DList::new();
767 assert_eq!(m.pop_front(), None);
768 assert_eq!(m.pop_back(), None);
769 assert_eq!(m.pop_front(), None);
771 assert_eq!(m.pop_front(), Some(box 1));
774 assert_eq!(m.len(), 2);
775 assert_eq!(m.pop_front(), Some(box 2));
776 assert_eq!(m.pop_front(), Some(box 3));
777 assert_eq!(m.len(), 0);
778 assert_eq!(m.pop_front(), None);
783 assert_eq!(m.pop_front(), Some(box 1));
785 let mut n = DList::new();
789 assert_eq!(n.front().unwrap(), &3);
790 let x = n.front_mut().unwrap();
795 assert_eq!(n.back().unwrap(), &2);
796 let y = n.back_mut().unwrap();
800 assert_eq!(n.pop_front(), Some(0));
801 assert_eq!(n.pop_front(), Some(1));
805 fn generate_test() -> DList<int> {
806 list_from(&[0i,1,2,3,4,5,6])
810 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
811 v.iter().map(|x| (*x).clone()).collect()
817 let mut m = DList::new();
818 let mut n = DList::new();
821 assert_eq!(m.len(), 1);
822 assert_eq!(m.pop_back(), Some(2));
826 let mut m = DList::new();
827 let n = DList::new();
830 assert_eq!(m.len(), 1);
831 assert_eq!(m.pop_back(), Some(2));
835 let v = vec![1i,2,3,4,5];
836 let u = vec![9i,8,1,2,3,4,5];
837 let mut m = list_from(v.as_slice());
838 m.append(list_from(u.as_slice()));
840 let sum = v.append(u.as_slice());
841 assert_eq!(sum.len(), m.len());
842 for elt in sum.move_iter() {
843 assert_eq!(m.pop_front(), Some(elt))
850 let mut m = DList::new();
851 let mut n = DList::new();
854 assert_eq!(m.len(), 1);
855 assert_eq!(m.pop_back(), Some(2));
859 let v = vec![1i,2,3,4,5];
860 let u = vec![9i,8,1,2,3,4,5];
861 let mut m = list_from(v.as_slice());
862 m.prepend(list_from(u.as_slice()));
864 let sum = u.append(v.as_slice());
865 assert_eq!(sum.len(), m.len());
866 for elt in sum.move_iter() {
867 assert_eq!(m.pop_front(), Some(elt))
873 let mut n: DList<int> = DList::new();
874 n.rotate_backward(); check_links(&n);
875 assert_eq!(n.len(), 0);
876 n.rotate_forward(); check_links(&n);
877 assert_eq!(n.len(), 0);
879 let v = vec![1i,2,3,4,5];
880 let mut m = list_from(v.as_slice());
881 m.rotate_backward(); check_links(&m);
882 m.rotate_forward(); check_links(&m);
883 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
884 m.rotate_forward(); check_links(&m);
885 m.rotate_forward(); check_links(&m);
886 m.pop_front(); check_links(&m);
887 m.rotate_forward(); check_links(&m);
888 m.rotate_backward(); check_links(&m);
889 m.push_front(9); check_links(&m);
890 m.rotate_forward(); check_links(&m);
891 assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
896 let m = generate_test();
897 for (i, elt) in m.iter().enumerate() {
898 assert_eq!(i as int, *elt);
900 let mut n = DList::new();
901 assert_eq!(n.iter().next(), None);
903 let mut it = n.iter();
904 assert_eq!(it.size_hint(), (1, Some(1)));
905 assert_eq!(it.next().unwrap(), &4);
906 assert_eq!(it.size_hint(), (0, Some(0)));
907 assert_eq!(it.next(), None);
911 fn test_iterator_clone() {
912 let mut n = DList::new();
916 let mut it = n.iter();
918 let mut jt = it.clone();
919 assert_eq!(it.next(), jt.next());
920 assert_eq!(it.next_back(), jt.next_back());
921 assert_eq!(it.next(), jt.next());
925 fn test_iterator_double_end() {
926 let mut n = DList::new();
927 assert_eq!(n.iter().next(), None);
931 let mut it = n.iter();
932 assert_eq!(it.size_hint(), (3, Some(3)));
933 assert_eq!(it.next().unwrap(), &6);
934 assert_eq!(it.size_hint(), (2, Some(2)));
935 assert_eq!(it.next_back().unwrap(), &4);
936 assert_eq!(it.size_hint(), (1, Some(1)));
937 assert_eq!(it.next_back().unwrap(), &5);
938 assert_eq!(it.next_back(), None);
939 assert_eq!(it.next(), None);
944 let m = generate_test();
945 for (i, elt) in m.iter().rev().enumerate() {
946 assert_eq!((6 - i) as int, *elt);
948 let mut n = DList::new();
949 assert_eq!(n.iter().rev().next(), None);
951 let mut it = n.iter().rev();
952 assert_eq!(it.size_hint(), (1, Some(1)));
953 assert_eq!(it.next().unwrap(), &4);
954 assert_eq!(it.size_hint(), (0, Some(0)));
955 assert_eq!(it.next(), None);
960 let mut m = generate_test();
961 let mut len = m.len();
962 for (i, elt) in m.mut_iter().enumerate() {
963 assert_eq!(i as int, *elt);
967 let mut n = DList::new();
968 assert!(n.mut_iter().next().is_none());
971 let mut it = n.mut_iter();
972 assert_eq!(it.size_hint(), (2, Some(2)));
973 assert!(it.next().is_some());
974 assert!(it.next().is_some());
975 assert_eq!(it.size_hint(), (0, Some(0)));
976 assert!(it.next().is_none());
980 fn test_iterator_mut_double_end() {
981 let mut n = DList::new();
982 assert!(n.mut_iter().next_back().is_none());
986 let mut it = n.mut_iter();
987 assert_eq!(it.size_hint(), (3, Some(3)));
988 assert_eq!(*it.next().unwrap(), 6);
989 assert_eq!(it.size_hint(), (2, Some(2)));
990 assert_eq!(*it.next_back().unwrap(), 4);
991 assert_eq!(it.size_hint(), (1, Some(1)));
992 assert_eq!(*it.next_back().unwrap(), 5);
993 assert!(it.next_back().is_none());
994 assert!(it.next().is_none());
998 fn test_insert_prev() {
999 let mut m = list_from(&[0i,2,4,6,8]);
1002 let mut it = m.mut_iter();
1008 it.insert_next(*elt + 1);
1009 match it.peek_next() {
1010 Some(x) => assert_eq!(*x, *elt + 2),
1011 None => assert_eq!(8, *elt),
1020 assert_eq!(m.len(), 3 + len * 2);
1021 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
1026 let mut m = list_from([0i, 1, 3, 5, 6, 7, 2]);
1027 let n = list_from([-1i, 0, 0, 7, 7, 9]);
1028 let len = m.len() + n.len();
1029 m.merge(n, |a, b| a <= b);
1030 assert_eq!(m.len(), len);
1032 let res = m.move_iter().collect::<Vec<int>>();
1033 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
1037 fn test_insert_ordered() {
1038 let mut n = DList::new();
1039 n.insert_ordered(1i);
1040 assert_eq!(n.len(), 1);
1041 assert_eq!(n.pop_front(), Some(1));
1043 let mut m = DList::new();
1046 m.insert_ordered(3);
1048 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
1052 fn test_mut_rev_iter() {
1053 let mut m = generate_test();
1054 for (i, elt) in m.mut_iter().rev().enumerate() {
1055 assert_eq!((6-i) as int, *elt);
1057 let mut n = DList::new();
1058 assert!(n.mut_iter().rev().next().is_none());
1060 let mut it = n.mut_iter().rev();
1061 assert!(it.next().is_some());
1062 assert!(it.next().is_none());
1067 let n = list_from([1i,2,3]);
1070 assert_eq!(&[&1,&2,&3], n.iter().collect::<Vec<&int>>().as_slice());
1076 let mut n: DList<u8> = list_from([]);
1077 let mut m = list_from([]);
1084 let n = list_from([2i,3,4]);
1085 let m = list_from([1i,2,3]);
1091 let mut x = DList::new();
1092 let mut y = DList::new();
1094 assert!(hash::hash(&x) == hash::hash(&y));
1104 assert!(hash::hash(&x) == hash::hash(&y));
1109 let n: DList<int> = list_from([]);
1110 let m = list_from([1i,2,3]);
1119 let nan = 0.0f64/0.0;
1120 let n = list_from([nan]);
1121 let m = list_from([nan]);
1127 let n = list_from([nan]);
1128 let one = list_from([1.0f64]);
1129 assert!(!(n < one));
1130 assert!(!(n > one));
1131 assert!(!(n <= one));
1132 assert!(!(n >= one));
1134 let u = list_from([1.0f64,2.0,nan]);
1135 let v = list_from([1.0f64,2.0,3.0]);
1141 let s = list_from([1.0f64,2.0,4.0,2.0]);
1142 let t = list_from([1.0f64,2.0,3.0,2.0]);
1145 assert!(!(s <= one));
1151 for _ in range(0u, 25) {
1160 let list: DList<int> = range(0i, 10).collect();
1161 assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1163 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1166 assert!(list.to_string().as_slice() == "[just, one, test, more]");
1170 fn fuzz_test(sz: int) {
1171 let mut m: DList<int> = DList::new();
1173 for i in range(0, sz) {
1175 let r: u8 = rand::random();
1199 for (a, &b) in m.move_iter().zip(v.iter()) {
1203 assert_eq!(i, v.len());
1207 fn bench_collect_into(b: &mut test::Bencher) {
1208 let v = &[0i, ..64];
1210 let _: DList<int> = v.iter().map(|x| *x).collect();
1215 fn bench_push_front(b: &mut test::Bencher) {
1216 let mut m: DList<int> = DList::new();
1223 fn bench_push_back(b: &mut test::Bencher) {
1224 let mut m: DList<int> = DList::new();
1231 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1232 let mut m: DList<int> = DList::new();
1240 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1241 let mut m: DList<int> = DList::new();
1249 fn bench_rotate_forward(b: &mut test::Bencher) {
1250 let mut m: DList<int> = DList::new();
1259 fn bench_rotate_backward(b: &mut test::Bencher) {
1260 let mut m: DList<int> = DList::new();
1264 m.rotate_backward();
1269 fn bench_iter(b: &mut test::Bencher) {
1270 let v = &[0i, ..128];
1271 let m: DList<int> = v.iter().map(|&x|x).collect();
1273 assert!(m.iter().count() == 128);
1277 fn bench_iter_mut(b: &mut test::Bencher) {
1278 let v = &[0i, ..128];
1279 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1281 assert!(m.mut_iter().count() == 128);
1285 fn bench_iter_rev(b: &mut test::Bencher) {
1286 let v = &[0i, ..128];
1287 let m: DList<int> = v.iter().map(|&x|x).collect();
1289 assert!(m.iter().rev().count() == 128);
1293 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1294 let v = &[0i, ..128];
1295 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1297 assert!(m.mut_iter().rev().count() == 128);