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(&self) -> Option<&T> {
91 unsafe { self.p.to_option() }
94 /// Convert the `Rawlink` into an Option value
95 fn resolve(&mut self) -> Option<&mut T> {
99 Some(unsafe { mem::transmute(self.p) })
103 /// Return the `Rawlink` and replace with `Rawlink::none()`
104 fn take(&mut self) -> Rawlink<T> {
105 mem::replace(self, Rawlink::none())
109 impl<T> Clone for Rawlink<T> {
111 fn clone(&self) -> Rawlink<T> {
117 fn new(v: T) -> Node<T> {
118 Node{value: v, next: None, prev: Rawlink::none()}
122 /// Set the .prev field on `next`, then return `Some(next)`
123 fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
129 impl<T> Collection for DList<T> {
132 fn is_empty(&self) -> bool {
133 self.list_head.is_none()
137 fn len(&self) -> uint {
142 impl<T> Mutable for DList<T> {
143 /// Remove all elements from the DList
147 fn clear(&mut self) {
154 /// Add a Node first in the list
156 fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
157 match self.list_head {
159 self.list_tail = Rawlink::some(&mut *new_head);
160 self.list_head = link_with_prev(new_head, Rawlink::none());
162 Some(ref mut head) => {
163 new_head.prev = Rawlink::none();
164 head.prev = Rawlink::some(&mut *new_head);
165 mem::swap(head, &mut new_head);
166 head.next = Some(new_head);
172 /// Remove the first Node and return it, or None if the list is empty
174 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
175 self.list_head.take().map(|mut front_node| {
177 match front_node.next.take() {
178 Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
179 None => self.list_tail = Rawlink::none()
185 /// Add a Node last in the list
187 fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
188 match self.list_tail.resolve() {
189 None => return self.push_front_node(new_tail),
191 self.list_tail = Rawlink::some(&mut *new_tail);
192 tail.next = link_with_prev(new_tail, Rawlink::some(tail));
198 /// Remove the last Node and return it, or None if the list is empty
200 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
201 self.list_tail.resolve().map_or(None, |tail| {
203 self.list_tail = tail.prev;
204 match tail.prev.resolve() {
205 None => self.list_head.take(),
206 Some(tail_prev) => tail_prev.next.take()
212 impl<T> Deque<T> for DList<T> {
213 /// Provide a reference to the front element, or None if the list is empty
215 fn front<'a>(&'a self) -> Option<&'a T> {
216 self.list_head.as_ref().map(|head| &head.value)
219 /// Provide a mutable reference to the front element, or None if the list is empty
221 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
222 self.list_head.as_mut().map(|head| &mut head.value)
225 /// Provide a reference to the back element, or None if the list is empty
227 fn back<'a>(&'a self) -> Option<&'a T> {
228 self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value)
231 /// Provide a mutable reference to the back element, or None if the list is empty
233 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
234 self.list_tail.resolve().map(|tail| &mut tail.value)
237 /// Add an element first in the list
240 fn push_front(&mut self, elt: T) {
241 self.push_front_node(box Node::new(elt))
244 /// Remove the first element and return it, or None if the list is empty
247 fn pop_front(&mut self) -> Option<T> {
248 self.pop_front_node().map(|box Node{value, ..}| value)
251 /// Add an element last in the list
254 fn push_back(&mut self, elt: T) {
255 self.push_back_node(box Node::new(elt))
258 /// Remove the last element and return it, or None if the list is empty
261 fn pop_back(&mut self) -> Option<T> {
262 self.pop_back_node().map(|box Node{value, ..}| value)
266 impl<T> Default for DList<T> {
268 fn default() -> DList<T> { DList::new() }
272 /// Create an empty DList
274 pub fn new() -> DList<T> {
275 DList{list_head: None, list_tail: Rawlink::none(), length: 0}
278 /// Move the last element to the front of the list.
280 /// If the list is empty, do nothing.
285 /// use std::collections::{DList, Deque};
287 /// let mut dl = DList::new();
288 /// dl.push_back(1i);
292 /// dl.rotate_forward();
294 /// for e in dl.iter() {
295 /// println!("{}", e); // prints 3, then 1, then 2
299 pub fn rotate_forward(&mut self) {
300 self.pop_back_node().map(|tail| {
301 self.push_front_node(tail)
305 /// Move the first element to the back of the list.
307 /// If the list is empty, do nothing.
312 /// use std::collections::{DList, Deque};
314 /// let mut dl = DList::new();
315 /// dl.push_back(1i);
319 /// dl.rotate_backward();
321 /// for e in dl.iter() {
322 /// println!("{}", e); // prints 2, then 3, then 1
326 pub fn rotate_backward(&mut self) {
327 self.pop_front_node().map(|head| {
328 self.push_back_node(head)
332 /// Add all elements from `other` to the end of the list
339 /// use std::collections::{DList, Deque};
341 /// let mut a = DList::new();
342 /// let mut b = DList::new();
350 /// for e in a.iter() {
351 /// println!("{}", e); // prints 1, then 2, then 3, then 4
354 pub fn append(&mut self, mut other: DList<T>) {
355 match self.list_tail.resolve() {
356 None => *self = other,
358 // Carefully empty `other`.
359 let o_tail = other.list_tail.take();
360 let o_length = other.length;
361 match other.list_head.take() {
364 tail.next = link_with_prev(node, self.list_tail);
365 self.list_tail = o_tail;
366 self.length += o_length;
373 /// Add all elements from `other` to the beginning of the list
380 /// use std::collections::{DList, Deque};
382 /// let mut a = DList::new();
383 /// let mut b = DList::new();
391 /// for e in a.iter() {
392 /// println!("{}", e); // prints 3, then 4, then 1, then 2
396 pub fn prepend(&mut self, mut other: DList<T>) {
397 mem::swap(self, &mut other);
401 /// Insert `elt` before the first `x` in the list where `f(x, elt)` is true,
409 /// use std::collections::{DList, Deque};
411 /// let mut a: DList<int> = DList::new();
417 /// // insert 11 before the first odd number in the list
418 /// a.insert_when(11, |&e, _| e % 2 == 1);
420 /// for e in a.iter() {
421 /// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
424 pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
426 let mut it = self.mut_iter();
428 match it.peek_next() {
430 Some(x) => if f(x, &elt) { break }
438 /// Merge DList `other` into this DList, using the function `f`.
439 /// Iterate the both DList with `a` from self and `b` from `other`, and
440 /// put `a` in the result if `f(a, b)` is true, else `b`.
443 pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
445 let mut it = self.mut_iter();
447 let take_a = match (it.peek_next(), other.front()) {
448 (_ , None) => return,
450 (Some(ref mut x), Some(y)) => f(*x, y),
455 it.insert_next_node(other.pop_front_node().unwrap());
463 /// Provide a forward iterator
465 pub fn iter<'a>(&'a self) -> Items<'a, T> {
466 Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
469 /// Provide a forward iterator with mutable references
471 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
472 let head_raw = match self.list_head {
473 Some(ref mut h) => Rawlink::some(&mut **h),
474 None => Rawlink::none(),
479 tail: self.list_tail,
485 /// Consume the list into an iterator yielding elements by value
487 pub fn move_iter(self) -> MoveItems<T> {
488 MoveItems{list: self}
492 impl<T: Ord> DList<T> {
493 /// Insert `elt` sorted in ascending order
497 pub fn insert_ordered(&mut self, elt: T) {
498 self.insert_when(elt, |a, b| a >= b)
503 impl<T> Drop for DList<T> {
505 // Dissolve the dlist in backwards direction
506 // Just dropping the list_head can lead to stack exhaustion
507 // when length is >> 1_000_000
508 let mut tail = self.list_tail;
510 match tail.resolve() {
513 prev.next.take(); // release Box<Node<T>>
519 self.list_head = None;
520 self.list_tail = Rawlink::none();
525 impl<'a, A> Iterator<&'a A> for Items<'a, A> {
527 fn next(&mut self) -> Option<&'a A> {
531 self.head.as_ref().map(|head| {
533 self.head = &head.next;
539 fn size_hint(&self) -> (uint, Option<uint>) {
540 (self.nelem, Some(self.nelem))
544 impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
546 fn next_back(&mut self) -> Option<&'a A> {
550 self.tail.resolve_immut().as_ref().map(|prev| {
552 self.tail = prev.prev;
558 impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
560 impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
562 fn next(&mut self) -> Option<&'a mut A> {
566 self.head.resolve().map(|next| {
568 self.head = match next.next {
569 Some(ref mut node) => Rawlink::some(&mut **node),
570 None => Rawlink::none(),
577 fn size_hint(&self) -> (uint, Option<uint>) {
578 (self.nelem, Some(self.nelem))
582 impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
584 fn next_back(&mut self) -> Option<&'a mut A> {
588 self.tail.resolve().map(|prev| {
590 self.tail = prev.prev;
596 impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
598 /// Allow mutating the DList while iterating
599 pub trait ListInsertion<A> {
600 /// Insert `elt` just after to the element most recently returned by `.next()`
602 /// The inserted element does not appear in the iteration.
603 fn insert_next(&mut self, elt: A);
605 /// Provide a reference to the next element, without changing the iterator
606 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
609 // private methods for MutItems
610 impl<'a, A> MutItems<'a, A> {
611 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
612 // Insert before `self.head` so that it is between the
613 // previously yielded element and self.head.
615 // The inserted node will not appear in further iteration.
616 match self.head.resolve() {
617 None => { self.list.push_back_node(ins_node); }
619 let prev_node = match node.prev.resolve() {
620 None => return self.list.push_front_node(ins_node),
623 let node_own = prev_node.next.take_unwrap();
624 ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
625 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
626 self.list.length += 1;
632 impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
634 fn insert_next(&mut self, elt: A) {
635 self.insert_next_node(box Node::new(elt))
639 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
643 self.head.resolve().map(|head| &mut head.value)
647 impl<A> Iterator<A> for MoveItems<A> {
649 fn next(&mut self) -> Option<A> { self.list.pop_front() }
652 fn size_hint(&self) -> (uint, Option<uint>) {
653 (self.list.length, Some(self.list.length))
657 impl<A> DoubleEndedIterator<A> for MoveItems<A> {
659 fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
662 impl<A> FromIterator<A> for DList<A> {
663 fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
664 let mut ret = DList::new();
665 ret.extend(iterator);
670 impl<A> Extendable<A> for DList<A> {
671 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
672 for elt in iterator { self.push_back(elt); }
676 impl<A: PartialEq> PartialEq for DList<A> {
677 fn eq(&self, other: &DList<A>) -> bool {
678 self.len() == other.len() &&
679 iter::order::eq(self.iter(), other.iter())
682 fn ne(&self, other: &DList<A>) -> bool {
683 self.len() != other.len() ||
684 iter::order::ne(self.iter(), other.iter())
688 impl<A: PartialOrd> PartialOrd for DList<A> {
689 fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
690 iter::order::partial_cmp(self.iter(), other.iter())
694 impl<A: Clone> Clone for DList<A> {
695 fn clone(&self) -> DList<A> {
696 self.iter().map(|x| x.clone()).collect()
700 impl<A: fmt::Show> fmt::Show for DList<A> {
701 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
702 try!(write!(f, "["));
704 for (i, e) in self.iter().enumerate() {
705 if i != 0 { try!(write!(f, ", ")); }
706 try!(write!(f, "{}", *e));
721 use super::{DList, Node, ListInsertion};
724 pub fn check_links<T>(list: &DList<T>) {
726 let mut last_ptr: Option<&Node<T>> = None;
727 let mut node_ptr: &Node<T>;
728 match list.list_head {
729 None => { assert_eq!(0u, list.length); return }
730 Some(ref node) => node_ptr = &**node,
733 match (last_ptr, node_ptr.prev.resolve_immut()) {
735 (None , _ ) => fail!("prev link for list_head"),
736 (Some(p), Some(pptr)) => {
737 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
739 _ => fail!("prev link is none, not good"),
741 match node_ptr.next {
743 last_ptr = Some(node_ptr);
753 assert_eq!(len, list.length);
758 let mut m: DList<Box<int>> = DList::new();
759 assert_eq!(m.pop_front(), None);
760 assert_eq!(m.pop_back(), None);
761 assert_eq!(m.pop_front(), None);
763 assert_eq!(m.pop_front(), Some(box 1));
766 assert_eq!(m.len(), 2);
767 assert_eq!(m.pop_front(), Some(box 2));
768 assert_eq!(m.pop_front(), Some(box 3));
769 assert_eq!(m.len(), 0);
770 assert_eq!(m.pop_front(), None);
775 assert_eq!(m.pop_front(), Some(box 1));
777 let mut n = DList::new();
781 assert_eq!(n.front().unwrap(), &3);
782 let x = n.front_mut().unwrap();
787 assert_eq!(n.back().unwrap(), &2);
788 let y = n.back_mut().unwrap();
792 assert_eq!(n.pop_front(), Some(0));
793 assert_eq!(n.pop_front(), Some(1));
797 fn generate_test() -> DList<int> {
798 list_from(&[0i,1,2,3,4,5,6])
802 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
803 v.iter().map(|x| (*x).clone()).collect()
809 let mut m = DList::new();
810 let mut n = DList::new();
813 assert_eq!(m.len(), 1);
814 assert_eq!(m.pop_back(), Some(2));
818 let mut m = DList::new();
819 let n = DList::new();
822 assert_eq!(m.len(), 1);
823 assert_eq!(m.pop_back(), Some(2));
827 let v = vec![1i,2,3,4,5];
828 let u = vec![9i,8,1,2,3,4,5];
829 let mut m = list_from(v.as_slice());
830 m.append(list_from(u.as_slice()));
832 let sum = v.append(u.as_slice());
833 assert_eq!(sum.len(), m.len());
834 for elt in sum.move_iter() {
835 assert_eq!(m.pop_front(), Some(elt))
842 let mut m = DList::new();
843 let mut n = DList::new();
846 assert_eq!(m.len(), 1);
847 assert_eq!(m.pop_back(), Some(2));
851 let v = vec![1i,2,3,4,5];
852 let u = vec![9i,8,1,2,3,4,5];
853 let mut m = list_from(v.as_slice());
854 m.prepend(list_from(u.as_slice()));
856 let sum = u.append(v.as_slice());
857 assert_eq!(sum.len(), m.len());
858 for elt in sum.move_iter() {
859 assert_eq!(m.pop_front(), Some(elt))
865 let mut n: DList<int> = DList::new();
866 n.rotate_backward(); check_links(&n);
867 assert_eq!(n.len(), 0);
868 n.rotate_forward(); check_links(&n);
869 assert_eq!(n.len(), 0);
871 let v = vec![1i,2,3,4,5];
872 let mut m = list_from(v.as_slice());
873 m.rotate_backward(); check_links(&m);
874 m.rotate_forward(); check_links(&m);
875 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
876 m.rotate_forward(); check_links(&m);
877 m.rotate_forward(); check_links(&m);
878 m.pop_front(); check_links(&m);
879 m.rotate_forward(); check_links(&m);
880 m.rotate_backward(); check_links(&m);
881 m.push_front(9); check_links(&m);
882 m.rotate_forward(); check_links(&m);
883 assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
888 let m = generate_test();
889 for (i, elt) in m.iter().enumerate() {
890 assert_eq!(i as int, *elt);
892 let mut n = DList::new();
893 assert_eq!(n.iter().next(), None);
895 let mut it = n.iter();
896 assert_eq!(it.size_hint(), (1, Some(1)));
897 assert_eq!(it.next().unwrap(), &4);
898 assert_eq!(it.size_hint(), (0, Some(0)));
899 assert_eq!(it.next(), None);
903 fn test_iterator_clone() {
904 let mut n = DList::new();
908 let mut it = n.iter();
910 let mut jt = it.clone();
911 assert_eq!(it.next(), jt.next());
912 assert_eq!(it.next_back(), jt.next_back());
913 assert_eq!(it.next(), jt.next());
917 fn test_iterator_double_end() {
918 let mut n = DList::new();
919 assert_eq!(n.iter().next(), None);
923 let mut it = n.iter();
924 assert_eq!(it.size_hint(), (3, Some(3)));
925 assert_eq!(it.next().unwrap(), &6);
926 assert_eq!(it.size_hint(), (2, Some(2)));
927 assert_eq!(it.next_back().unwrap(), &4);
928 assert_eq!(it.size_hint(), (1, Some(1)));
929 assert_eq!(it.next_back().unwrap(), &5);
930 assert_eq!(it.next_back(), None);
931 assert_eq!(it.next(), None);
936 let m = generate_test();
937 for (i, elt) in m.iter().rev().enumerate() {
938 assert_eq!((6 - i) as int, *elt);
940 let mut n = DList::new();
941 assert_eq!(n.iter().rev().next(), None);
943 let mut it = n.iter().rev();
944 assert_eq!(it.size_hint(), (1, Some(1)));
945 assert_eq!(it.next().unwrap(), &4);
946 assert_eq!(it.size_hint(), (0, Some(0)));
947 assert_eq!(it.next(), None);
952 let mut m = generate_test();
953 let mut len = m.len();
954 for (i, elt) in m.mut_iter().enumerate() {
955 assert_eq!(i as int, *elt);
959 let mut n = DList::new();
960 assert!(n.mut_iter().next().is_none());
963 let mut it = n.mut_iter();
964 assert_eq!(it.size_hint(), (2, Some(2)));
965 assert!(it.next().is_some());
966 assert!(it.next().is_some());
967 assert_eq!(it.size_hint(), (0, Some(0)));
968 assert!(it.next().is_none());
972 fn test_iterator_mut_double_end() {
973 let mut n = DList::new();
974 assert!(n.mut_iter().next_back().is_none());
978 let mut it = n.mut_iter();
979 assert_eq!(it.size_hint(), (3, Some(3)));
980 assert_eq!(*it.next().unwrap(), 6);
981 assert_eq!(it.size_hint(), (2, Some(2)));
982 assert_eq!(*it.next_back().unwrap(), 4);
983 assert_eq!(it.size_hint(), (1, Some(1)));
984 assert_eq!(*it.next_back().unwrap(), 5);
985 assert!(it.next_back().is_none());
986 assert!(it.next().is_none());
990 fn test_insert_prev() {
991 let mut m = list_from(&[0i,2,4,6,8]);
994 let mut it = m.mut_iter();
1000 it.insert_next(*elt + 1);
1001 match it.peek_next() {
1002 Some(x) => assert_eq!(*x, *elt + 2),
1003 None => assert_eq!(8, *elt),
1012 assert_eq!(m.len(), 3 + len * 2);
1013 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
1018 let mut m = list_from([0i, 1, 3, 5, 6, 7, 2]);
1019 let n = list_from([-1i, 0, 0, 7, 7, 9]);
1020 let len = m.len() + n.len();
1021 m.merge(n, |a, b| a <= b);
1022 assert_eq!(m.len(), len);
1024 let res = m.move_iter().collect::<Vec<int>>();
1025 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
1029 fn test_insert_ordered() {
1030 let mut n = DList::new();
1031 n.insert_ordered(1i);
1032 assert_eq!(n.len(), 1);
1033 assert_eq!(n.pop_front(), Some(1));
1035 let mut m = DList::new();
1038 m.insert_ordered(3);
1040 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
1044 fn test_mut_rev_iter() {
1045 let mut m = generate_test();
1046 for (i, elt) in m.mut_iter().rev().enumerate() {
1047 assert_eq!((6-i) as int, *elt);
1049 let mut n = DList::new();
1050 assert!(n.mut_iter().rev().next().is_none());
1052 let mut it = n.mut_iter().rev();
1053 assert!(it.next().is_some());
1054 assert!(it.next().is_none());
1059 let n = list_from([1i,2,3]);
1062 assert_eq!(&[&1,&2,&3], n.iter().collect::<Vec<&int>>().as_slice());
1068 let mut n: DList<u8> = list_from([]);
1069 let mut m = list_from([]);
1076 let n = list_from([2i,3,4]);
1077 let m = list_from([1i,2,3]);
1083 let n: DList<int> = list_from([]);
1084 let m = list_from([1i,2,3]);
1093 let nan = 0.0f64/0.0;
1094 let n = list_from([nan]);
1095 let m = list_from([nan]);
1101 let n = list_from([nan]);
1102 let one = list_from([1.0f64]);
1103 assert!(!(n < one));
1104 assert!(!(n > one));
1105 assert!(!(n <= one));
1106 assert!(!(n >= one));
1108 let u = list_from([1.0f64,2.0,nan]);
1109 let v = list_from([1.0f64,2.0,3.0]);
1115 let s = list_from([1.0f64,2.0,4.0,2.0]);
1116 let t = list_from([1.0f64,2.0,3.0,2.0]);
1119 assert!(!(s <= one));
1125 for _ in range(0u, 25) {
1134 let list: DList<int> = range(0i, 10).collect();
1135 assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1137 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1140 assert!(list.to_string().as_slice() == "[just, one, test, more]");
1144 fn fuzz_test(sz: int) {
1145 let mut m: DList<int> = DList::new();
1147 for i in range(0, sz) {
1149 let r: u8 = rand::random();
1173 for (a, &b) in m.move_iter().zip(v.iter()) {
1177 assert_eq!(i, v.len());
1181 fn bench_collect_into(b: &mut test::Bencher) {
1182 let v = &[0i, ..64];
1184 let _: DList<int> = v.iter().map(|x| *x).collect();
1189 fn bench_push_front(b: &mut test::Bencher) {
1190 let mut m: DList<int> = DList::new();
1197 fn bench_push_back(b: &mut test::Bencher) {
1198 let mut m: DList<int> = DList::new();
1205 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1206 let mut m: DList<int> = DList::new();
1214 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1215 let mut m: DList<int> = DList::new();
1223 fn bench_rotate_forward(b: &mut test::Bencher) {
1224 let mut m: DList<int> = DList::new();
1233 fn bench_rotate_backward(b: &mut test::Bencher) {
1234 let mut m: DList<int> = DList::new();
1238 m.rotate_backward();
1243 fn bench_iter(b: &mut test::Bencher) {
1244 let v = &[0i, ..128];
1245 let m: DList<int> = v.iter().map(|&x|x).collect();
1247 assert!(m.iter().count() == 128);
1251 fn bench_iter_mut(b: &mut test::Bencher) {
1252 let v = &[0i, ..128];
1253 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1255 assert!(m.mut_iter().count() == 128);
1259 fn bench_iter_rev(b: &mut test::Bencher) {
1260 let v = &[0i, ..128];
1261 let m: DList<int> = v.iter().map(|&x|x).collect();
1263 assert!(m.iter().rev().count() == 128);
1267 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1268 let v = &[0i, ..128];
1269 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1271 assert!(m.mut_iter().rev().count() == 128);