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, MutableSeq};
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)
254 impl<T> MutableSeq<T> for DList<T> {
255 fn push(&mut self, elt: T) {
256 self.push_back_node(box Node::new(elt))
258 fn pop(&mut self) -> Option<T> {
259 self.pop_back_node().map(|box Node{value, ..}| value)
263 impl<T> Default for DList<T> {
265 fn default() -> DList<T> { DList::new() }
269 /// Create an empty DList
271 pub fn new() -> DList<T> {
272 DList{list_head: None, list_tail: Rawlink::none(), length: 0}
275 /// Move the last element to the front of the list.
277 /// If the list is empty, do nothing.
282 /// use std::collections::DList;
284 /// let mut dl = DList::new();
289 /// dl.rotate_forward();
291 /// for e in dl.iter() {
292 /// println!("{}", e); // prints 3, then 1, then 2
296 pub fn rotate_forward(&mut self) {
297 self.pop_back_node().map(|tail| {
298 self.push_front_node(tail)
302 /// Move the first element to the back of the list.
304 /// If the list is empty, do nothing.
309 /// use std::collections::DList;
311 /// let mut dl = DList::new();
316 /// dl.rotate_backward();
318 /// for e in dl.iter() {
319 /// println!("{}", e); // prints 2, then 3, then 1
323 pub fn rotate_backward(&mut self) {
324 self.pop_front_node().map(|head| {
325 self.push_back_node(head)
329 /// Add all elements from `other` to the end of the list
336 /// use std::collections::DList;
338 /// let mut a = DList::new();
339 /// let mut b = DList::new();
347 /// for e in a.iter() {
348 /// println!("{}", e); // prints 1, then 2, then 3, then 4
351 pub fn append(&mut self, mut other: DList<T>) {
352 match self.list_tail.resolve() {
353 None => *self = other,
355 // Carefully empty `other`.
356 let o_tail = other.list_tail.take();
357 let o_length = other.length;
358 match other.list_head.take() {
361 tail.next = link_with_prev(node, self.list_tail);
362 self.list_tail = o_tail;
363 self.length += o_length;
370 /// Add all elements from `other` to the beginning of the list
377 /// use std::collections::DList;
379 /// let mut a = DList::new();
380 /// let mut b = DList::new();
388 /// for e in a.iter() {
389 /// println!("{}", e); // prints 3, then 4, then 1, then 2
393 pub fn prepend(&mut self, mut other: DList<T>) {
394 mem::swap(self, &mut other);
398 /// Insert `elt` before the first `x` in the list where `f(x, elt)` is true,
406 /// use std::collections::DList;
408 /// let mut a: DList<int> = DList::new();
414 /// // insert 11 before the first odd number in the list
415 /// a.insert_when(11, |&e, _| e % 2 == 1);
417 /// for e in a.iter() {
418 /// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
421 pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
423 let mut it = self.mut_iter();
425 match it.peek_next() {
427 Some(x) => if f(x, &elt) { break }
435 /// Merge DList `other` into this DList, using the function `f`.
436 /// Iterate the both DList with `a` from self and `b` from `other`, and
437 /// put `a` in the result if `f(a, b)` is true, else `b`.
440 pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
442 let mut it = self.mut_iter();
444 let take_a = match (it.peek_next(), other.front()) {
445 (_ , None) => return,
447 (Some(ref mut x), Some(y)) => f(*x, y),
452 it.insert_next_node(other.pop_front_node().unwrap());
460 /// Provide a forward iterator
462 pub fn iter<'a>(&'a self) -> Items<'a, T> {
463 Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
466 /// Provide a forward iterator with mutable references
468 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
469 let head_raw = match self.list_head {
470 Some(ref mut h) => Rawlink::some(&mut **h),
471 None => Rawlink::none(),
476 tail: self.list_tail,
482 /// Consume the list into an iterator yielding elements by value
484 pub fn move_iter(self) -> MoveItems<T> {
485 MoveItems{list: self}
489 impl<T: Ord> DList<T> {
490 /// Insert `elt` sorted in ascending order
494 pub fn insert_ordered(&mut self, elt: T) {
495 self.insert_when(elt, |a, b| a >= b)
500 impl<T> Drop for DList<T> {
502 // Dissolve the dlist in backwards direction
503 // Just dropping the list_head can lead to stack exhaustion
504 // when length is >> 1_000_000
505 let mut tail = self.list_tail;
507 match tail.resolve() {
510 prev.next.take(); // release Box<Node<T>>
516 self.list_head = None;
517 self.list_tail = Rawlink::none();
522 impl<'a, A> Iterator<&'a A> for Items<'a, A> {
524 fn next(&mut self) -> Option<&'a A> {
528 self.head.as_ref().map(|head| {
530 self.head = &head.next;
536 fn size_hint(&self) -> (uint, Option<uint>) {
537 (self.nelem, Some(self.nelem))
541 impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
543 fn next_back(&mut self) -> Option<&'a A> {
547 self.tail.resolve_immut().as_ref().map(|prev| {
549 self.tail = prev.prev;
555 impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
557 impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
559 fn next(&mut self) -> Option<&'a mut A> {
563 self.head.resolve().map(|next| {
565 self.head = match next.next {
566 Some(ref mut node) => Rawlink::some(&mut **node),
567 None => Rawlink::none(),
574 fn size_hint(&self) -> (uint, Option<uint>) {
575 (self.nelem, Some(self.nelem))
579 impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
581 fn next_back(&mut self) -> Option<&'a mut A> {
585 self.tail.resolve().map(|prev| {
587 self.tail = prev.prev;
593 impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
595 /// Allow mutating the DList while iterating
596 pub trait ListInsertion<A> {
597 /// Insert `elt` just after to the element most recently returned by `.next()`
599 /// The inserted element does not appear in the iteration.
600 fn insert_next(&mut self, elt: A);
602 /// Provide a reference to the next element, without changing the iterator
603 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
606 // private methods for MutItems
607 impl<'a, A> MutItems<'a, A> {
608 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
609 // Insert before `self.head` so that it is between the
610 // previously yielded element and self.head.
612 // The inserted node will not appear in further iteration.
613 match self.head.resolve() {
614 None => { self.list.push_back_node(ins_node); }
616 let prev_node = match node.prev.resolve() {
617 None => return self.list.push_front_node(ins_node),
620 let node_own = prev_node.next.take_unwrap();
621 ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
622 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
623 self.list.length += 1;
629 impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
631 fn insert_next(&mut self, elt: A) {
632 self.insert_next_node(box Node::new(elt))
636 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
640 self.head.resolve().map(|head| &mut head.value)
644 impl<A> Iterator<A> for MoveItems<A> {
646 fn next(&mut self) -> Option<A> { self.list.pop_front() }
649 fn size_hint(&self) -> (uint, Option<uint>) {
650 (self.list.length, Some(self.list.length))
654 impl<A> DoubleEndedIterator<A> for MoveItems<A> {
656 fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
659 impl<A> FromIterator<A> for DList<A> {
660 fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
661 let mut ret = DList::new();
662 ret.extend(iterator);
667 impl<A> Extendable<A> for DList<A> {
668 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
669 for elt in iterator { self.push_back(elt); }
673 impl<A: PartialEq> PartialEq for DList<A> {
674 fn eq(&self, other: &DList<A>) -> bool {
675 self.len() == other.len() &&
676 iter::order::eq(self.iter(), other.iter())
679 fn ne(&self, other: &DList<A>) -> bool {
680 self.len() != other.len() ||
681 iter::order::ne(self.iter(), other.iter())
685 impl<A: PartialOrd> PartialOrd for DList<A> {
686 fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
687 iter::order::partial_cmp(self.iter(), other.iter())
691 impl<A: Clone> Clone for DList<A> {
692 fn clone(&self) -> DList<A> {
693 self.iter().map(|x| x.clone()).collect()
697 impl<A: fmt::Show> fmt::Show for DList<A> {
698 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
699 try!(write!(f, "["));
701 for (i, e) in self.iter().enumerate() {
702 if i != 0 { try!(write!(f, ", ")); }
703 try!(write!(f, "{}", *e));
717 use {Deque, MutableSeq};
718 use super::{DList, Node, ListInsertion};
721 pub fn check_links<T>(list: &DList<T>) {
723 let mut last_ptr: Option<&Node<T>> = None;
724 let mut node_ptr: &Node<T>;
725 match list.list_head {
726 None => { assert_eq!(0u, list.length); return }
727 Some(ref node) => node_ptr = &**node,
730 match (last_ptr, node_ptr.prev.resolve_immut()) {
732 (None , _ ) => fail!("prev link for list_head"),
733 (Some(p), Some(pptr)) => {
734 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
736 _ => fail!("prev link is none, not good"),
738 match node_ptr.next {
740 last_ptr = Some(node_ptr);
750 assert_eq!(len, list.length);
755 let mut m: DList<Box<int>> = DList::new();
756 assert_eq!(m.pop_front(), None);
757 assert_eq!(m.pop_back(), None);
758 assert_eq!(m.pop_front(), None);
760 assert_eq!(m.pop_front(), Some(box 1));
763 assert_eq!(m.len(), 2);
764 assert_eq!(m.pop_front(), Some(box 2));
765 assert_eq!(m.pop_front(), Some(box 3));
766 assert_eq!(m.len(), 0);
767 assert_eq!(m.pop_front(), None);
772 assert_eq!(m.pop_front(), Some(box 1));
774 let mut n = DList::new();
778 assert_eq!(n.front().unwrap(), &3);
779 let x = n.front_mut().unwrap();
784 assert_eq!(n.back().unwrap(), &2);
785 let y = n.back_mut().unwrap();
789 assert_eq!(n.pop_front(), Some(0));
790 assert_eq!(n.pop_front(), Some(1));
794 fn generate_test() -> DList<int> {
795 list_from(&[0i,1,2,3,4,5,6])
799 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
800 v.iter().map(|x| (*x).clone()).collect()
806 let mut m = DList::new();
807 let mut n = DList::new();
810 assert_eq!(m.len(), 1);
811 assert_eq!(m.pop_back(), Some(2));
815 let mut m = DList::new();
816 let n = DList::new();
819 assert_eq!(m.len(), 1);
820 assert_eq!(m.pop_back(), Some(2));
824 let v = vec![1i,2,3,4,5];
825 let u = vec![9i,8,1,2,3,4,5];
826 let mut m = list_from(v.as_slice());
827 m.append(list_from(u.as_slice()));
829 let sum = v.append(u.as_slice());
830 assert_eq!(sum.len(), m.len());
831 for elt in sum.move_iter() {
832 assert_eq!(m.pop_front(), Some(elt))
839 let mut m = DList::new();
840 let mut n = DList::new();
843 assert_eq!(m.len(), 1);
844 assert_eq!(m.pop_back(), Some(2));
848 let v = vec![1i,2,3,4,5];
849 let u = vec![9i,8,1,2,3,4,5];
850 let mut m = list_from(v.as_slice());
851 m.prepend(list_from(u.as_slice()));
853 let sum = u.append(v.as_slice());
854 assert_eq!(sum.len(), m.len());
855 for elt in sum.move_iter() {
856 assert_eq!(m.pop_front(), Some(elt))
862 let mut n: DList<int> = DList::new();
863 n.rotate_backward(); check_links(&n);
864 assert_eq!(n.len(), 0);
865 n.rotate_forward(); check_links(&n);
866 assert_eq!(n.len(), 0);
868 let v = vec![1i,2,3,4,5];
869 let mut m = list_from(v.as_slice());
870 m.rotate_backward(); check_links(&m);
871 m.rotate_forward(); check_links(&m);
872 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
873 m.rotate_forward(); check_links(&m);
874 m.rotate_forward(); check_links(&m);
875 m.pop_front(); check_links(&m);
876 m.rotate_forward(); check_links(&m);
877 m.rotate_backward(); check_links(&m);
878 m.push_front(9); check_links(&m);
879 m.rotate_forward(); check_links(&m);
880 assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
885 let m = generate_test();
886 for (i, elt) in m.iter().enumerate() {
887 assert_eq!(i as int, *elt);
889 let mut n = DList::new();
890 assert_eq!(n.iter().next(), None);
892 let mut it = n.iter();
893 assert_eq!(it.size_hint(), (1, Some(1)));
894 assert_eq!(it.next().unwrap(), &4);
895 assert_eq!(it.size_hint(), (0, Some(0)));
896 assert_eq!(it.next(), None);
900 fn test_iterator_clone() {
901 let mut n = DList::new();
905 let mut it = n.iter();
907 let mut jt = it.clone();
908 assert_eq!(it.next(), jt.next());
909 assert_eq!(it.next_back(), jt.next_back());
910 assert_eq!(it.next(), jt.next());
914 fn test_iterator_double_end() {
915 let mut n = DList::new();
916 assert_eq!(n.iter().next(), None);
920 let mut it = n.iter();
921 assert_eq!(it.size_hint(), (3, Some(3)));
922 assert_eq!(it.next().unwrap(), &6);
923 assert_eq!(it.size_hint(), (2, Some(2)));
924 assert_eq!(it.next_back().unwrap(), &4);
925 assert_eq!(it.size_hint(), (1, Some(1)));
926 assert_eq!(it.next_back().unwrap(), &5);
927 assert_eq!(it.next_back(), None);
928 assert_eq!(it.next(), None);
933 let m = generate_test();
934 for (i, elt) in m.iter().rev().enumerate() {
935 assert_eq!((6 - i) as int, *elt);
937 let mut n = DList::new();
938 assert_eq!(n.iter().rev().next(), None);
940 let mut it = n.iter().rev();
941 assert_eq!(it.size_hint(), (1, Some(1)));
942 assert_eq!(it.next().unwrap(), &4);
943 assert_eq!(it.size_hint(), (0, Some(0)));
944 assert_eq!(it.next(), None);
949 let mut m = generate_test();
950 let mut len = m.len();
951 for (i, elt) in m.mut_iter().enumerate() {
952 assert_eq!(i as int, *elt);
956 let mut n = DList::new();
957 assert!(n.mut_iter().next().is_none());
960 let mut it = n.mut_iter();
961 assert_eq!(it.size_hint(), (2, Some(2)));
962 assert!(it.next().is_some());
963 assert!(it.next().is_some());
964 assert_eq!(it.size_hint(), (0, Some(0)));
965 assert!(it.next().is_none());
969 fn test_iterator_mut_double_end() {
970 let mut n = DList::new();
971 assert!(n.mut_iter().next_back().is_none());
975 let mut it = n.mut_iter();
976 assert_eq!(it.size_hint(), (3, Some(3)));
977 assert_eq!(*it.next().unwrap(), 6);
978 assert_eq!(it.size_hint(), (2, Some(2)));
979 assert_eq!(*it.next_back().unwrap(), 4);
980 assert_eq!(it.size_hint(), (1, Some(1)));
981 assert_eq!(*it.next_back().unwrap(), 5);
982 assert!(it.next_back().is_none());
983 assert!(it.next().is_none());
987 fn test_insert_prev() {
988 let mut m = list_from(&[0i,2,4,6,8]);
991 let mut it = m.mut_iter();
997 it.insert_next(*elt + 1);
998 match it.peek_next() {
999 Some(x) => assert_eq!(*x, *elt + 2),
1000 None => assert_eq!(8, *elt),
1009 assert_eq!(m.len(), 3 + len * 2);
1010 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
1015 let mut m = list_from([0i, 1, 3, 5, 6, 7, 2]);
1016 let n = list_from([-1i, 0, 0, 7, 7, 9]);
1017 let len = m.len() + n.len();
1018 m.merge(n, |a, b| a <= b);
1019 assert_eq!(m.len(), len);
1021 let res = m.move_iter().collect::<Vec<int>>();
1022 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
1026 fn test_insert_ordered() {
1027 let mut n = DList::new();
1028 n.insert_ordered(1i);
1029 assert_eq!(n.len(), 1);
1030 assert_eq!(n.pop_front(), Some(1));
1032 let mut m = DList::new();
1035 m.insert_ordered(3);
1037 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
1041 fn test_mut_rev_iter() {
1042 let mut m = generate_test();
1043 for (i, elt) in m.mut_iter().rev().enumerate() {
1044 assert_eq!((6-i) as int, *elt);
1046 let mut n = DList::new();
1047 assert!(n.mut_iter().rev().next().is_none());
1049 let mut it = n.mut_iter().rev();
1050 assert!(it.next().is_some());
1051 assert!(it.next().is_none());
1056 let n = list_from([1i,2,3]);
1059 assert_eq!(&[&1,&2,&3], n.iter().collect::<Vec<&int>>().as_slice());
1065 let mut n: DList<u8> = list_from([]);
1066 let mut m = list_from([]);
1073 let n = list_from([2i,3,4]);
1074 let m = list_from([1i,2,3]);
1080 let n: DList<int> = list_from([]);
1081 let m = list_from([1i,2,3]);
1090 let nan = 0.0f64/0.0;
1091 let n = list_from([nan]);
1092 let m = list_from([nan]);
1098 let n = list_from([nan]);
1099 let one = list_from([1.0f64]);
1100 assert!(!(n < one));
1101 assert!(!(n > one));
1102 assert!(!(n <= one));
1103 assert!(!(n >= one));
1105 let u = list_from([1.0f64,2.0,nan]);
1106 let v = list_from([1.0f64,2.0,3.0]);
1112 let s = list_from([1.0f64,2.0,4.0,2.0]);
1113 let t = list_from([1.0f64,2.0,3.0,2.0]);
1116 assert!(!(s <= one));
1122 for _ in range(0u, 25) {
1131 let list: DList<int> = range(0i, 10).collect();
1132 assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1134 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1137 assert!(list.to_string().as_slice() == "[just, one, test, more]");
1141 fn fuzz_test(sz: int) {
1142 let mut m: DList<int> = DList::new();
1144 for i in range(0, sz) {
1146 let r: u8 = rand::random();
1170 for (a, &b) in m.move_iter().zip(v.iter()) {
1174 assert_eq!(i, v.len());
1178 fn bench_collect_into(b: &mut test::Bencher) {
1179 let v = &[0i, ..64];
1181 let _: DList<int> = v.iter().map(|x| *x).collect();
1186 fn bench_push_front(b: &mut test::Bencher) {
1187 let mut m: DList<int> = DList::new();
1194 fn bench_push_back(b: &mut test::Bencher) {
1195 let mut m: DList<int> = DList::new();
1202 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1203 let mut m: DList<int> = DList::new();
1211 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1212 let mut m: DList<int> = DList::new();
1220 fn bench_rotate_forward(b: &mut test::Bencher) {
1221 let mut m: DList<int> = DList::new();
1230 fn bench_rotate_backward(b: &mut test::Bencher) {
1231 let mut m: DList<int> = DList::new();
1235 m.rotate_backward();
1240 fn bench_iter(b: &mut test::Bencher) {
1241 let v = &[0i, ..128];
1242 let m: DList<int> = v.iter().map(|&x|x).collect();
1244 assert!(m.iter().count() == 128);
1248 fn bench_iter_mut(b: &mut test::Bencher) {
1249 let v = &[0i, ..128];
1250 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1252 assert!(m.mut_iter().count() == 128);
1256 fn bench_iter_rev(b: &mut test::Bencher) {
1257 let v = &[0i, ..128];
1258 let m: DList<int> = v.iter().map(|&x|x).collect();
1260 assert!(m.iter().rev().count() == 128);
1264 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1265 let v = &[0i, ..128];
1266 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1268 assert!(m.mut_iter().rev().count() == 128);