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
16 //! `use 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 {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 /// An iterator over references to the items of a `DList`.
53 pub struct Items<'a, T:'a> {
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 /// An iterator over mutable references to the items of a `DList`.
65 pub struct MutItems<'a, T:'a> {
66 list: &'a mut DList<T>,
67 head: Rawlink<Node<T>>,
68 tail: Rawlink<Node<T>>,
72 /// An iterator over mutable references to the items of a `DList`.
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> {
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> {
133 /// Returns `true` if the `DList` is empty.
135 /// This operation should compute in O(1) time.
137 fn is_empty(&self) -> bool {
138 self.list_head.is_none()
141 /// Returns the length of the `DList`.
143 /// This operation should compute in O(1) time.
145 fn len(&self) -> uint {
150 impl<T> Mutable for DList<T> {
151 /// Removes all elements from the `DList`.
153 /// This operation should compute in O(n) time.
155 fn clear(&mut self) {
162 /// Add a Node first in the list
164 fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
165 match self.list_head {
167 self.list_tail = Rawlink::some(&mut *new_head);
168 self.list_head = link_with_prev(new_head, Rawlink::none());
170 Some(ref mut head) => {
171 new_head.prev = Rawlink::none();
172 head.prev = Rawlink::some(&mut *new_head);
173 mem::swap(head, &mut new_head);
174 head.next = Some(new_head);
180 /// Remove the first Node and return it, or None if the list is empty
182 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
183 self.list_head.take().map(|mut front_node| {
185 match front_node.next.take() {
186 Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
187 None => self.list_tail = Rawlink::none()
193 /// Add a Node last in the list
195 fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
196 match self.list_tail.resolve() {
197 None => return self.push_front_node(new_tail),
199 self.list_tail = Rawlink::some(&mut *new_tail);
200 tail.next = link_with_prev(new_tail, Rawlink::some(tail));
206 /// Remove the last Node and return it, or None if the list is empty
208 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
209 self.list_tail.resolve().map_or(None, |tail| {
211 self.list_tail = tail.prev;
212 match tail.prev.resolve() {
213 None => self.list_head.take(),
214 Some(tail_prev) => tail_prev.next.take()
220 impl<T> Deque<T> for DList<T> {
221 /// Provides a reference to the front element, or `None` if the list is
224 fn front<'a>(&'a self) -> Option<&'a T> {
225 self.list_head.as_ref().map(|head| &head.value)
228 /// Provides a mutable reference to the front element, or `None` if the list
231 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
232 self.list_head.as_mut().map(|head| &mut head.value)
235 /// Provides a reference to the back element, or `None` if the list is
238 fn back<'a>(&'a self) -> Option<&'a T> {
239 self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value)
242 /// Provides a mutable reference to the back element, or `None` if the list
245 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
246 self.list_tail.resolve().map(|tail| &mut tail.value)
249 /// Adds an element first in the list.
251 /// This operation should compute in O(1) time.
252 fn push_front(&mut self, elt: T) {
253 self.push_front_node(box Node::new(elt))
256 /// Removes the first element and returns it, or `None` if the list is
259 /// This operation should compute in O(1) time.
260 fn pop_front(&mut self) -> Option<T> {
261 self.pop_front_node().map(|box Node{value, ..}| value)
265 impl<T> MutableSeq<T> for DList<T> {
266 fn push(&mut self, elt: T) {
267 self.push_back_node(box Node::new(elt))
269 fn pop(&mut self) -> Option<T> {
270 self.pop_back_node().map(|box Node{value, ..}| value)
274 impl<T> Default for DList<T> {
276 fn default() -> DList<T> { DList::new() }
280 /// Creates an empty `DList`.
282 pub fn new() -> DList<T> {
283 DList{list_head: None, list_tail: Rawlink::none(), length: 0}
286 /// Moves the last element to the front of the list.
288 /// If the list is empty, does nothing.
293 /// use std::collections::DList;
295 /// let mut dl = DList::new();
300 /// dl.rotate_forward();
302 /// for e in dl.iter() {
303 /// println!("{}", e); // prints 3, then 1, then 2
307 pub fn rotate_forward(&mut self) {
308 self.pop_back_node().map(|tail| {
309 self.push_front_node(tail)
313 /// Moves the first element to the back of the list.
315 /// If the list is empty, does nothing.
320 /// use std::collections::DList;
322 /// let mut dl = DList::new();
327 /// dl.rotate_backward();
329 /// for e in dl.iter() {
330 /// println!("{}", e); // prints 2, then 3, then 1
334 pub fn rotate_backward(&mut self) {
335 self.pop_front_node().map(|head| {
336 self.push_back_node(head)
340 /// Adds all elements from `other` to the end of the list.
342 /// This operation should compute in O(1) time.
347 /// use std::collections::DList;
349 /// let mut a = DList::new();
350 /// let mut b = DList::new();
358 /// for e in a.iter() {
359 /// println!("{}", e); // prints 1, then 2, then 3, then 4
362 pub fn append(&mut self, mut other: DList<T>) {
363 match self.list_tail.resolve() {
364 None => *self = other,
366 // Carefully empty `other`.
367 let o_tail = other.list_tail.take();
368 let o_length = other.length;
369 match other.list_head.take() {
372 tail.next = link_with_prev(node, self.list_tail);
373 self.list_tail = o_tail;
374 self.length += o_length;
381 /// Adds all elements from `other` to the beginning of the list.
383 /// This operation should compute in O(1) time.
388 /// use std::collections::DList;
390 /// let mut a = DList::new();
391 /// let mut b = DList::new();
399 /// for e in a.iter() {
400 /// println!("{}", e); // prints 3, then 4, then 1, then 2
404 pub fn prepend(&mut self, mut other: DList<T>) {
405 mem::swap(self, &mut other);
409 /// Inserts `elt` before the first `x` in the list where `f(x, elt)` is
410 /// true, or at the end.
412 /// This operation should compute in O(N) time.
417 /// use std::collections::DList;
419 /// let mut a: DList<int> = DList::new();
425 /// // insert 11 before the first odd number in the list
426 /// a.insert_when(11, |&e, _| e % 2 == 1);
428 /// for e in a.iter() {
429 /// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
432 pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
434 let mut it = self.mut_iter();
436 match it.peek_next() {
438 Some(x) => if f(x, &elt) { break }
446 /// Merges `other` into this `DList`, using the function `f`.
448 /// Iterates both `DList`s with `a` from self and `b` from `other`, and
449 /// put `a` in the result if `f(a, b)` is true, and otherwise `b`.
451 /// This operation should compute in O(max(N, M)) time.
452 pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
454 let mut it = self.mut_iter();
456 let take_a = match (it.peek_next(), other.front()) {
457 (_ , None) => return,
459 (Some(ref mut x), Some(y)) => f(*x, y),
464 it.insert_next_node(other.pop_front_node().unwrap());
472 /// Provides a forward iterator.
474 pub fn iter<'a>(&'a self) -> Items<'a, T> {
475 Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
478 /// Provides a forward iterator with mutable references.
480 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
481 let head_raw = match self.list_head {
482 Some(ref mut h) => Rawlink::some(&mut **h),
483 None => Rawlink::none(),
488 tail: self.list_tail,
494 /// Consumes the list into an iterator yielding elements by value.
496 pub fn move_iter(self) -> MoveItems<T> {
497 MoveItems{list: self}
501 impl<T: Ord> DList<T> {
502 /// Inserts `elt` sorted in ascending order.
504 /// This operation should compute in O(N) time.
506 pub fn insert_ordered(&mut self, elt: T) {
507 self.insert_when(elt, |a, b| a >= b)
512 impl<T> Drop for DList<T> {
514 // Dissolve the dlist in backwards direction
515 // Just dropping the list_head can lead to stack exhaustion
516 // when length is >> 1_000_000
517 let mut tail = self.list_tail;
519 match tail.resolve() {
522 prev.next.take(); // release Box<Node<T>>
528 self.list_head = None;
529 self.list_tail = Rawlink::none();
534 impl<'a, A> Iterator<&'a A> for Items<'a, A> {
536 fn next(&mut self) -> Option<&'a A> {
540 self.head.as_ref().map(|head| {
542 self.head = &head.next;
548 fn size_hint(&self) -> (uint, Option<uint>) {
549 (self.nelem, Some(self.nelem))
553 impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
555 fn next_back(&mut self) -> Option<&'a A> {
559 self.tail.resolve_immut().as_ref().map(|prev| {
561 self.tail = prev.prev;
567 impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
569 impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
571 fn next(&mut self) -> Option<&'a mut A> {
575 self.head.resolve().map(|next| {
577 self.head = match next.next {
578 Some(ref mut node) => Rawlink::some(&mut **node),
579 None => Rawlink::none(),
586 fn size_hint(&self) -> (uint, Option<uint>) {
587 (self.nelem, Some(self.nelem))
591 impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
593 fn next_back(&mut self) -> Option<&'a mut A> {
597 self.tail.resolve().map(|prev| {
599 self.tail = prev.prev;
605 impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
607 /// Allows mutating a `DList` while iterating.
608 pub trait ListInsertion<A> {
609 /// Inserts `elt` just after to the element most recently returned by
612 /// The inserted element does not appear in the iteration.
613 fn insert_next(&mut self, elt: A);
615 /// Provides a reference to the next element, without changing the iterator
616 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
619 // private methods for MutItems
620 impl<'a, A> MutItems<'a, A> {
621 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
622 // Insert before `self.head` so that it is between the
623 // previously yielded element and self.head.
625 // The inserted node will not appear in further iteration.
626 match self.head.resolve() {
627 None => { self.list.push_back_node(ins_node); }
629 let prev_node = match node.prev.resolve() {
630 None => return self.list.push_front_node(ins_node),
633 let node_own = prev_node.next.take().unwrap();
634 ins_node.next = link_with_prev(node_own, Rawlink::some(&mut *ins_node));
635 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
636 self.list.length += 1;
642 impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
644 fn insert_next(&mut self, elt: A) {
645 self.insert_next_node(box Node::new(elt))
649 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
653 self.head.resolve().map(|head| &mut head.value)
657 impl<A> Iterator<A> for MoveItems<A> {
659 fn next(&mut self) -> Option<A> { self.list.pop_front() }
662 fn size_hint(&self) -> (uint, Option<uint>) {
663 (self.list.length, Some(self.list.length))
667 impl<A> DoubleEndedIterator<A> for MoveItems<A> {
669 fn next_back(&mut self) -> Option<A> { self.list.pop() }
672 impl<A> FromIterator<A> for DList<A> {
673 fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
674 let mut ret = DList::new();
675 ret.extend(iterator);
680 impl<A> Extendable<A> for DList<A> {
681 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
682 for elt in iterator { self.push(elt); }
686 impl<A: PartialEq> PartialEq for DList<A> {
687 fn eq(&self, other: &DList<A>) -> bool {
688 self.len() == other.len() &&
689 iter::order::eq(self.iter(), other.iter())
692 fn ne(&self, other: &DList<A>) -> bool {
693 self.len() != other.len() ||
694 iter::order::ne(self.iter(), other.iter())
698 impl<A: Eq> Eq for DList<A> {}
700 impl<A: PartialOrd> PartialOrd for DList<A> {
701 fn partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
702 iter::order::partial_cmp(self.iter(), other.iter())
706 impl<A: Ord> Ord for DList<A> {
708 fn cmp(&self, other: &DList<A>) -> Ordering {
709 iter::order::cmp(self.iter(), other.iter())
713 impl<A: Clone> Clone for DList<A> {
714 fn clone(&self) -> DList<A> {
715 self.iter().map(|x| x.clone()).collect()
719 impl<A: fmt::Show> fmt::Show for DList<A> {
720 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
721 try!(write!(f, "["));
723 for (i, e) in self.iter().enumerate() {
724 if i != 0 { try!(write!(f, ", ")); }
725 try!(write!(f, "{}", *e));
732 impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
733 fn hash(&self, state: &mut S) {
734 self.len().hash(state);
735 for elt in self.iter() {
749 use {Deque, MutableSeq};
750 use super::{DList, Node, ListInsertion};
753 pub fn check_links<T>(list: &DList<T>) {
755 let mut last_ptr: Option<&Node<T>> = None;
756 let mut node_ptr: &Node<T>;
757 match list.list_head {
758 None => { assert_eq!(0u, list.length); return }
759 Some(ref node) => node_ptr = &**node,
762 match (last_ptr, node_ptr.prev.resolve_immut()) {
764 (None , _ ) => fail!("prev link for list_head"),
765 (Some(p), Some(pptr)) => {
766 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
768 _ => fail!("prev link is none, not good"),
770 match node_ptr.next {
772 last_ptr = Some(node_ptr);
782 assert_eq!(len, list.length);
787 let mut m: DList<Box<int>> = DList::new();
788 assert_eq!(m.pop_front(), None);
789 assert_eq!(m.pop(), None);
790 assert_eq!(m.pop_front(), None);
792 assert_eq!(m.pop_front(), Some(box 1));
795 assert_eq!(m.len(), 2);
796 assert_eq!(m.pop_front(), Some(box 2));
797 assert_eq!(m.pop_front(), Some(box 3));
798 assert_eq!(m.len(), 0);
799 assert_eq!(m.pop_front(), None);
804 assert_eq!(m.pop_front(), Some(box 1));
806 let mut n = DList::new();
810 assert_eq!(n.front().unwrap(), &3);
811 let x = n.front_mut().unwrap();
816 assert_eq!(n.back().unwrap(), &2);
817 let y = n.back_mut().unwrap();
821 assert_eq!(n.pop_front(), Some(0));
822 assert_eq!(n.pop_front(), Some(1));
826 fn generate_test() -> DList<int> {
827 list_from(&[0i,1,2,3,4,5,6])
831 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
832 v.iter().map(|x| (*x).clone()).collect()
838 let mut m = DList::new();
839 let mut n = DList::new();
842 assert_eq!(m.len(), 1);
843 assert_eq!(m.pop(), Some(2));
847 let mut m = DList::new();
848 let n = DList::new();
851 assert_eq!(m.len(), 1);
852 assert_eq!(m.pop(), Some(2));
856 let v = vec![1i,2,3,4,5];
857 let u = vec![9i,8,1,2,3,4,5];
858 let mut m = list_from(v.as_slice());
859 m.append(list_from(u.as_slice()));
861 let sum = v.append(u.as_slice());
862 assert_eq!(sum.len(), m.len());
863 for elt in sum.move_iter() {
864 assert_eq!(m.pop_front(), Some(elt))
871 let mut m = DList::new();
872 let mut n = DList::new();
875 assert_eq!(m.len(), 1);
876 assert_eq!(m.pop(), Some(2));
880 let v = vec![1i,2,3,4,5];
881 let u = vec![9i,8,1,2,3,4,5];
882 let mut m = list_from(v.as_slice());
883 m.prepend(list_from(u.as_slice()));
885 let sum = u.append(v.as_slice());
886 assert_eq!(sum.len(), m.len());
887 for elt in sum.move_iter() {
888 assert_eq!(m.pop_front(), Some(elt))
894 let mut n: DList<int> = DList::new();
895 n.rotate_backward(); check_links(&n);
896 assert_eq!(n.len(), 0);
897 n.rotate_forward(); check_links(&n);
898 assert_eq!(n.len(), 0);
900 let v = vec![1i,2,3,4,5];
901 let mut m = list_from(v.as_slice());
902 m.rotate_backward(); check_links(&m);
903 m.rotate_forward(); check_links(&m);
904 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
905 m.rotate_forward(); check_links(&m);
906 m.rotate_forward(); check_links(&m);
907 m.pop_front(); check_links(&m);
908 m.rotate_forward(); check_links(&m);
909 m.rotate_backward(); check_links(&m);
910 m.push_front(9); check_links(&m);
911 m.rotate_forward(); check_links(&m);
912 assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
917 let m = generate_test();
918 for (i, elt) in m.iter().enumerate() {
919 assert_eq!(i as int, *elt);
921 let mut n = DList::new();
922 assert_eq!(n.iter().next(), None);
924 let mut it = n.iter();
925 assert_eq!(it.size_hint(), (1, Some(1)));
926 assert_eq!(it.next().unwrap(), &4);
927 assert_eq!(it.size_hint(), (0, Some(0)));
928 assert_eq!(it.next(), None);
932 fn test_iterator_clone() {
933 let mut n = DList::new();
937 let mut it = n.iter();
939 let mut jt = it.clone();
940 assert_eq!(it.next(), jt.next());
941 assert_eq!(it.next_back(), jt.next_back());
942 assert_eq!(it.next(), jt.next());
946 fn test_iterator_double_end() {
947 let mut n = DList::new();
948 assert_eq!(n.iter().next(), None);
952 let mut it = n.iter();
953 assert_eq!(it.size_hint(), (3, Some(3)));
954 assert_eq!(it.next().unwrap(), &6);
955 assert_eq!(it.size_hint(), (2, Some(2)));
956 assert_eq!(it.next_back().unwrap(), &4);
957 assert_eq!(it.size_hint(), (1, Some(1)));
958 assert_eq!(it.next_back().unwrap(), &5);
959 assert_eq!(it.next_back(), None);
960 assert_eq!(it.next(), None);
965 let m = generate_test();
966 for (i, elt) in m.iter().rev().enumerate() {
967 assert_eq!((6 - i) as int, *elt);
969 let mut n = DList::new();
970 assert_eq!(n.iter().rev().next(), None);
972 let mut it = n.iter().rev();
973 assert_eq!(it.size_hint(), (1, Some(1)));
974 assert_eq!(it.next().unwrap(), &4);
975 assert_eq!(it.size_hint(), (0, Some(0)));
976 assert_eq!(it.next(), None);
981 let mut m = generate_test();
982 let mut len = m.len();
983 for (i, elt) in m.mut_iter().enumerate() {
984 assert_eq!(i as int, *elt);
988 let mut n = DList::new();
989 assert!(n.mut_iter().next().is_none());
992 let mut it = n.mut_iter();
993 assert_eq!(it.size_hint(), (2, Some(2)));
994 assert!(it.next().is_some());
995 assert!(it.next().is_some());
996 assert_eq!(it.size_hint(), (0, Some(0)));
997 assert!(it.next().is_none());
1001 fn test_iterator_mut_double_end() {
1002 let mut n = DList::new();
1003 assert!(n.mut_iter().next_back().is_none());
1007 let mut it = n.mut_iter();
1008 assert_eq!(it.size_hint(), (3, Some(3)));
1009 assert_eq!(*it.next().unwrap(), 6);
1010 assert_eq!(it.size_hint(), (2, Some(2)));
1011 assert_eq!(*it.next_back().unwrap(), 4);
1012 assert_eq!(it.size_hint(), (1, Some(1)));
1013 assert_eq!(*it.next_back().unwrap(), 5);
1014 assert!(it.next_back().is_none());
1015 assert!(it.next().is_none());
1019 fn test_insert_prev() {
1020 let mut m = list_from(&[0i,2,4,6,8]);
1023 let mut it = m.mut_iter();
1029 it.insert_next(*elt + 1);
1030 match it.peek_next() {
1031 Some(x) => assert_eq!(*x, *elt + 2),
1032 None => assert_eq!(8, *elt),
1041 assert_eq!(m.len(), 3 + len * 2);
1042 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
1047 let mut m = list_from([0i, 1, 3, 5, 6, 7, 2]);
1048 let n = list_from([-1i, 0, 0, 7, 7, 9]);
1049 let len = m.len() + n.len();
1050 m.merge(n, |a, b| a <= b);
1051 assert_eq!(m.len(), len);
1053 let res = m.move_iter().collect::<Vec<int>>();
1054 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
1058 fn test_insert_ordered() {
1059 let mut n = DList::new();
1060 n.insert_ordered(1i);
1061 assert_eq!(n.len(), 1);
1062 assert_eq!(n.pop_front(), Some(1));
1064 let mut m = DList::new();
1067 m.insert_ordered(3);
1069 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
1073 fn test_mut_rev_iter() {
1074 let mut m = generate_test();
1075 for (i, elt) in m.mut_iter().rev().enumerate() {
1076 assert_eq!((6-i) as int, *elt);
1078 let mut n = DList::new();
1079 assert!(n.mut_iter().rev().next().is_none());
1081 let mut it = n.mut_iter().rev();
1082 assert!(it.next().is_some());
1083 assert!(it.next().is_none());
1088 let n = list_from([1i,2,3]);
1091 let a: &[_] = &[&1,&2,&3];
1092 assert_eq!(a, n.iter().collect::<Vec<&int>>().as_slice());
1098 let mut n: DList<u8> = list_from([]);
1099 let mut m = list_from([]);
1106 let n = list_from([2i,3,4]);
1107 let m = list_from([1i,2,3]);
1113 let mut x = DList::new();
1114 let mut y = DList::new();
1116 assert!(hash::hash(&x) == hash::hash(&y));
1126 assert!(hash::hash(&x) == hash::hash(&y));
1131 let n: DList<int> = list_from([]);
1132 let m = list_from([1i,2,3]);
1141 let nan = 0.0f64/0.0;
1142 let n = list_from([nan]);
1143 let m = list_from([nan]);
1149 let n = list_from([nan]);
1150 let one = list_from([1.0f64]);
1151 assert!(!(n < one));
1152 assert!(!(n > one));
1153 assert!(!(n <= one));
1154 assert!(!(n >= one));
1156 let u = list_from([1.0f64,2.0,nan]);
1157 let v = list_from([1.0f64,2.0,3.0]);
1163 let s = list_from([1.0f64,2.0,4.0,2.0]);
1164 let t = list_from([1.0f64,2.0,3.0,2.0]);
1167 assert!(!(s <= one));
1173 for _ in range(0u, 25) {
1182 let list: DList<int> = range(0i, 10).collect();
1183 assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1185 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1188 assert!(list.to_string().as_slice() == "[just, one, test, more]");
1192 fn fuzz_test(sz: int) {
1193 let mut m: DList<int> = DList::new();
1195 for i in range(0, sz) {
1197 let r: u8 = rand::random();
1221 for (a, &b) in m.move_iter().zip(v.iter()) {
1225 assert_eq!(i, v.len());
1229 fn bench_collect_into(b: &mut test::Bencher) {
1230 let v = &[0i, ..64];
1232 let _: DList<int> = v.iter().map(|x| *x).collect();
1237 fn bench_push_front(b: &mut test::Bencher) {
1238 let mut m: DList<int> = DList::new();
1245 fn bench_push_back(b: &mut test::Bencher) {
1246 let mut m: DList<int> = DList::new();
1253 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1254 let mut m: DList<int> = DList::new();
1262 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1263 let mut m: DList<int> = DList::new();
1271 fn bench_rotate_forward(b: &mut test::Bencher) {
1272 let mut m: DList<int> = DList::new();
1281 fn bench_rotate_backward(b: &mut test::Bencher) {
1282 let mut m: DList<int> = DList::new();
1286 m.rotate_backward();
1291 fn bench_iter(b: &mut test::Bencher) {
1292 let v = &[0i, ..128];
1293 let m: DList<int> = v.iter().map(|&x|x).collect();
1295 assert!(m.iter().count() == 128);
1299 fn bench_iter_mut(b: &mut test::Bencher) {
1300 let v = &[0i, ..128];
1301 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1303 assert!(m.mut_iter().count() == 128);
1307 fn bench_iter_rev(b: &mut test::Bencher) {
1308 let v = &[0i, ..128];
1309 let m: DList<int> = v.iter().map(|&x|x).collect();
1311 assert!(m.iter().rev().count() == 128);
1315 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1316 let v = &[0i, ..128];
1317 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1319 assert!(m.mut_iter().rev().count() == 128);