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::owned::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(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(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(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.
282 pub fn rotate_forward(&mut self) {
283 self.pop_back_node().map(|tail| {
284 self.push_front_node(tail)
288 /// Move the first element to the back of the list.
290 /// If the list is empty, do nothing.
292 pub fn rotate_backward(&mut self) {
293 self.pop_front_node().map(|head| {
294 self.push_back_node(head)
298 /// Add all elements from `other` to the end of the list
301 pub fn append(&mut self, mut other: DList<T>) {
302 match self.list_tail.resolve() {
303 None => *self = other,
305 // Carefully empty `other`.
306 let o_tail = other.list_tail.take();
307 let o_length = other.length;
308 match other.list_head.take() {
311 tail.next = link_with_prev(node, self.list_tail);
312 self.list_tail = o_tail;
313 self.length += o_length;
320 /// Add all elements from `other` to the beginning of the list
324 pub fn prepend(&mut self, mut other: DList<T>) {
325 mem::swap(self, &mut other);
329 /// Insert `elt` before the first `x` in the list where `f(x, elt)` is true,
333 pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
335 let mut it = self.mut_iter();
337 match it.peek_next() {
339 Some(x) => if f(x, &elt) { break }
347 /// Merge DList `other` into this DList, using the function `f`.
348 /// Iterate the both DList with `a` from self and `b` from `other`, and
349 /// put `a` in the result if `f(a, b)` is true, else `b`.
352 pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
354 let mut it = self.mut_iter();
356 let take_a = match (it.peek_next(), other.front()) {
357 (_ , None) => return,
359 (Some(ref mut x), Some(y)) => f(*x, y),
364 it.insert_next_node(other.pop_front_node().unwrap());
372 /// Provide a forward iterator
374 pub fn iter<'a>(&'a self) -> Items<'a, T> {
375 Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
378 /// Provide a forward iterator with mutable references
380 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
381 let head_raw = match self.list_head {
382 Some(ref mut h) => Rawlink::some(*h),
383 None => Rawlink::none(),
388 tail: self.list_tail,
394 /// Consume the list into an iterator yielding elements by value
396 pub fn move_iter(self) -> MoveItems<T> {
397 MoveItems{list: self}
401 impl<T: Ord> DList<T> {
402 /// Insert `elt` sorted in ascending order
406 pub fn insert_ordered(&mut self, elt: T) {
407 self.insert_when(elt, |a, b| a >= b)
412 impl<T> Drop for DList<T> {
414 // Dissolve the dlist in backwards direction
415 // Just dropping the list_head can lead to stack exhaustion
416 // when length is >> 1_000_000
417 let mut tail = self.list_tail;
419 match tail.resolve() {
422 prev.next.take(); // release Box<Node<T>>
428 self.list_head = None;
429 self.list_tail = Rawlink::none();
434 impl<'a, A> Iterator<&'a A> for Items<'a, A> {
436 fn next(&mut self) -> Option<&'a A> {
440 self.head.as_ref().map(|head| {
442 self.head = &head.next;
448 fn size_hint(&self) -> (uint, Option<uint>) {
449 (self.nelem, Some(self.nelem))
453 impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
455 fn next_back(&mut self) -> Option<&'a A> {
459 self.tail.resolve_immut().as_ref().map(|prev| {
461 self.tail = prev.prev;
467 impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
469 impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
471 fn next(&mut self) -> Option<&'a mut A> {
475 self.head.resolve().map(|next| {
477 self.head = match next.next {
478 Some(ref mut node) => Rawlink::some(&mut **node),
479 None => Rawlink::none(),
486 fn size_hint(&self) -> (uint, Option<uint>) {
487 (self.nelem, Some(self.nelem))
491 impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
493 fn next_back(&mut self) -> Option<&'a mut A> {
497 self.tail.resolve().map(|prev| {
499 self.tail = prev.prev;
505 impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
507 /// Allow mutating the DList while iterating
508 pub trait ListInsertion<A> {
509 /// Insert `elt` just after to the element most recently returned by `.next()`
511 /// The inserted element does not appear in the iteration.
512 fn insert_next(&mut self, elt: A);
514 /// Provide a reference to the next element, without changing the iterator
515 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
518 // private methods for MutItems
519 impl<'a, A> MutItems<'a, A> {
520 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
521 // Insert before `self.head` so that it is between the
522 // previously yielded element and self.head.
524 // The inserted node will not appear in further iteration.
525 match self.head.resolve() {
526 None => { self.list.push_back_node(ins_node); }
528 let prev_node = match node.prev.resolve() {
529 None => return self.list.push_front_node(ins_node),
532 let node_own = prev_node.next.take_unwrap();
533 ins_node.next = link_with_prev(node_own, Rawlink::some(ins_node));
534 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
535 self.list.length += 1;
541 impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
543 fn insert_next(&mut self, elt: A) {
544 self.insert_next_node(box Node::new(elt))
548 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
552 self.head.resolve().map(|head| &mut head.value)
556 impl<A> Iterator<A> for MoveItems<A> {
558 fn next(&mut self) -> Option<A> { self.list.pop_front() }
561 fn size_hint(&self) -> (uint, Option<uint>) {
562 (self.list.length, Some(self.list.length))
566 impl<A> DoubleEndedIterator<A> for MoveItems<A> {
568 fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
571 impl<A> FromIterator<A> for DList<A> {
572 fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
573 let mut ret = DList::new();
574 ret.extend(iterator);
579 impl<A> Extendable<A> for DList<A> {
580 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
581 for elt in iterator { self.push_back(elt); }
585 impl<A: PartialEq> PartialEq for DList<A> {
586 fn eq(&self, other: &DList<A>) -> bool {
587 self.len() == other.len() &&
588 iter::order::eq(self.iter(), other.iter())
591 fn ne(&self, other: &DList<A>) -> bool {
592 self.len() != other.len() ||
593 iter::order::ne(self.iter(), other.iter())
597 impl<A: PartialOrd> PartialOrd for DList<A> {
598 fn lt(&self, other: &DList<A>) -> bool {
599 iter::order::lt(self.iter(), other.iter())
601 fn le(&self, other: &DList<A>) -> bool {
602 iter::order::le(self.iter(), other.iter())
604 fn gt(&self, other: &DList<A>) -> bool {
605 iter::order::gt(self.iter(), other.iter())
607 fn ge(&self, other: &DList<A>) -> bool {
608 iter::order::ge(self.iter(), other.iter())
612 impl<A: Clone> Clone for DList<A> {
613 fn clone(&self) -> DList<A> {
614 self.iter().map(|x| x.clone()).collect()
618 impl<A: fmt::Show> fmt::Show for DList<A> {
619 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
620 try!(write!(f, "["));
622 for (i, e) in self.iter().enumerate() {
623 if i != 0 { try!(write!(f, ", ")); }
624 try!(write!(f, "{}", *e));
639 use super::{DList, Node, ListInsertion};
642 pub fn check_links<T>(list: &DList<T>) {
644 let mut last_ptr: Option<&Node<T>> = None;
645 let mut node_ptr: &Node<T>;
646 match list.list_head {
647 None => { assert_eq!(0u, list.length); return }
648 Some(ref node) => node_ptr = &**node,
651 match (last_ptr, node_ptr.prev.resolve_immut()) {
653 (None , _ ) => fail!("prev link for list_head"),
654 (Some(p), Some(pptr)) => {
655 assert_eq!(p as *Node<T>, pptr as *Node<T>);
657 _ => fail!("prev link is none, not good"),
659 match node_ptr.next {
661 last_ptr = Some(node_ptr);
671 assert_eq!(len, list.length);
676 let mut m: DList<Box<int>> = DList::new();
677 assert_eq!(m.pop_front(), None);
678 assert_eq!(m.pop_back(), None);
679 assert_eq!(m.pop_front(), None);
681 assert_eq!(m.pop_front(), Some(box 1));
684 assert_eq!(m.len(), 2);
685 assert_eq!(m.pop_front(), Some(box 2));
686 assert_eq!(m.pop_front(), Some(box 3));
687 assert_eq!(m.len(), 0);
688 assert_eq!(m.pop_front(), None);
693 assert_eq!(m.pop_front(), Some(box 1));
695 let mut n = DList::new();
699 assert_eq!(n.front().unwrap(), &3);
700 let x = n.front_mut().unwrap();
705 assert_eq!(n.back().unwrap(), &2);
706 let y = n.back_mut().unwrap();
710 assert_eq!(n.pop_front(), Some(0));
711 assert_eq!(n.pop_front(), Some(1));
715 fn generate_test() -> DList<int> {
716 list_from(&[0i,1,2,3,4,5,6])
720 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
721 v.iter().map(|x| (*x).clone()).collect()
727 let mut m = DList::new();
728 let mut n = DList::new();
731 assert_eq!(m.len(), 1);
732 assert_eq!(m.pop_back(), Some(2));
736 let mut m = DList::new();
737 let n = DList::new();
740 assert_eq!(m.len(), 1);
741 assert_eq!(m.pop_back(), Some(2));
745 let v = vec![1i,2,3,4,5];
746 let u = vec![9i,8,1,2,3,4,5];
747 let mut m = list_from(v.as_slice());
748 m.append(list_from(u.as_slice()));
750 let sum = v.append(u.as_slice());
751 assert_eq!(sum.len(), m.len());
752 for elt in sum.move_iter() {
753 assert_eq!(m.pop_front(), Some(elt))
760 let mut m = DList::new();
761 let mut n = DList::new();
764 assert_eq!(m.len(), 1);
765 assert_eq!(m.pop_back(), Some(2));
769 let v = vec![1i,2,3,4,5];
770 let u = vec![9i,8,1,2,3,4,5];
771 let mut m = list_from(v.as_slice());
772 m.prepend(list_from(u.as_slice()));
774 let sum = u.append(v.as_slice());
775 assert_eq!(sum.len(), m.len());
776 for elt in sum.move_iter() {
777 assert_eq!(m.pop_front(), Some(elt))
783 let mut n: DList<int> = DList::new();
784 n.rotate_backward(); check_links(&n);
785 assert_eq!(n.len(), 0);
786 n.rotate_forward(); check_links(&n);
787 assert_eq!(n.len(), 0);
789 let v = vec![1i,2,3,4,5];
790 let mut m = list_from(v.as_slice());
791 m.rotate_backward(); check_links(&m);
792 m.rotate_forward(); check_links(&m);
793 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
794 m.rotate_forward(); check_links(&m);
795 m.rotate_forward(); check_links(&m);
796 m.pop_front(); check_links(&m);
797 m.rotate_forward(); check_links(&m);
798 m.rotate_backward(); check_links(&m);
799 m.push_front(9); check_links(&m);
800 m.rotate_forward(); check_links(&m);
801 assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
806 let m = generate_test();
807 for (i, elt) in m.iter().enumerate() {
808 assert_eq!(i as int, *elt);
810 let mut n = DList::new();
811 assert_eq!(n.iter().next(), None);
813 let mut it = n.iter();
814 assert_eq!(it.size_hint(), (1, Some(1)));
815 assert_eq!(it.next().unwrap(), &4);
816 assert_eq!(it.size_hint(), (0, Some(0)));
817 assert_eq!(it.next(), None);
821 fn test_iterator_clone() {
822 let mut n = DList::new();
826 let mut it = n.iter();
828 let mut jt = it.clone();
829 assert_eq!(it.next(), jt.next());
830 assert_eq!(it.next_back(), jt.next_back());
831 assert_eq!(it.next(), jt.next());
835 fn test_iterator_double_end() {
836 let mut n = DList::new();
837 assert_eq!(n.iter().next(), None);
841 let mut it = n.iter();
842 assert_eq!(it.size_hint(), (3, Some(3)));
843 assert_eq!(it.next().unwrap(), &6);
844 assert_eq!(it.size_hint(), (2, Some(2)));
845 assert_eq!(it.next_back().unwrap(), &4);
846 assert_eq!(it.size_hint(), (1, Some(1)));
847 assert_eq!(it.next_back().unwrap(), &5);
848 assert_eq!(it.next_back(), None);
849 assert_eq!(it.next(), None);
854 let m = generate_test();
855 for (i, elt) in m.iter().rev().enumerate() {
856 assert_eq!((6 - i) as int, *elt);
858 let mut n = DList::new();
859 assert_eq!(n.iter().rev().next(), None);
861 let mut it = n.iter().rev();
862 assert_eq!(it.size_hint(), (1, Some(1)));
863 assert_eq!(it.next().unwrap(), &4);
864 assert_eq!(it.size_hint(), (0, Some(0)));
865 assert_eq!(it.next(), None);
870 let mut m = generate_test();
871 let mut len = m.len();
872 for (i, elt) in m.mut_iter().enumerate() {
873 assert_eq!(i as int, *elt);
877 let mut n = DList::new();
878 assert!(n.mut_iter().next().is_none());
881 let mut it = n.mut_iter();
882 assert_eq!(it.size_hint(), (2, Some(2)));
883 assert!(it.next().is_some());
884 assert!(it.next().is_some());
885 assert_eq!(it.size_hint(), (0, Some(0)));
886 assert!(it.next().is_none());
890 fn test_iterator_mut_double_end() {
891 let mut n = DList::new();
892 assert!(n.mut_iter().next_back().is_none());
896 let mut it = n.mut_iter();
897 assert_eq!(it.size_hint(), (3, Some(3)));
898 assert_eq!(*it.next().unwrap(), 6);
899 assert_eq!(it.size_hint(), (2, Some(2)));
900 assert_eq!(*it.next_back().unwrap(), 4);
901 assert_eq!(it.size_hint(), (1, Some(1)));
902 assert_eq!(*it.next_back().unwrap(), 5);
903 assert!(it.next_back().is_none());
904 assert!(it.next().is_none());
908 fn test_insert_prev() {
909 let mut m = list_from(&[0i,2,4,6,8]);
912 let mut it = m.mut_iter();
918 it.insert_next(*elt + 1);
919 match it.peek_next() {
920 Some(x) => assert_eq!(*x, *elt + 2),
921 None => assert_eq!(8, *elt),
930 assert_eq!(m.len(), 3 + len * 2);
931 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
936 let mut m = list_from([0i, 1, 3, 5, 6, 7, 2]);
937 let n = list_from([-1i, 0, 0, 7, 7, 9]);
938 let len = m.len() + n.len();
939 m.merge(n, |a, b| a <= b);
940 assert_eq!(m.len(), len);
942 let res = m.move_iter().collect::<Vec<int>>();
943 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
947 fn test_insert_ordered() {
948 let mut n = DList::new();
949 n.insert_ordered(1i);
950 assert_eq!(n.len(), 1);
951 assert_eq!(n.pop_front(), Some(1));
953 let mut m = DList::new();
958 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
962 fn test_mut_rev_iter() {
963 let mut m = generate_test();
964 for (i, elt) in m.mut_iter().rev().enumerate() {
965 assert_eq!((6-i) as int, *elt);
967 let mut n = DList::new();
968 assert!(n.mut_iter().rev().next().is_none());
970 let mut it = n.mut_iter().rev();
971 assert!(it.next().is_some());
972 assert!(it.next().is_none());
977 let n = list_from([1i,2,3]);
980 assert_eq!(&[&1,&2,&3], n.iter().collect::<Vec<&int>>().as_slice());
986 let mut n: DList<u8> = list_from([]);
987 let mut m = list_from([]);
994 let n = list_from([2i,3,4]);
995 let m = list_from([1i,2,3]);
1001 let n: DList<int> = list_from([]);
1002 let m = list_from([1i,2,3]);
1011 let nan = 0.0f64/0.0;
1012 let n = list_from([nan]);
1013 let m = list_from([nan]);
1019 let n = list_from([nan]);
1020 let one = list_from([1.0f64]);
1021 assert!(!(n < one));
1022 assert!(!(n > one));
1023 assert!(!(n <= one));
1024 assert!(!(n >= one));
1026 let u = list_from([1.0f64,2.0,nan]);
1027 let v = list_from([1.0f64,2.0,3.0]);
1033 let s = list_from([1.0f64,2.0,4.0,2.0]);
1034 let t = list_from([1.0f64,2.0,3.0,2.0]);
1037 assert!(!(s <= one));
1043 for _ in range(0u, 25) {
1052 let list: DList<int> = range(0i, 10).collect();
1053 assert!(list.to_str().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1055 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1058 assert!(list.to_str().as_slice() == "[just, one, test, more]");
1062 fn fuzz_test(sz: int) {
1063 let mut m: DList<int> = DList::new();
1065 for i in range(0, sz) {
1067 let r: u8 = rand::random();
1091 for (a, &b) in m.move_iter().zip(v.iter()) {
1095 assert_eq!(i, v.len());
1099 fn bench_collect_into(b: &mut test::Bencher) {
1100 let v = &[0i, ..64];
1102 let _: DList<int> = v.iter().map(|x| *x).collect();
1107 fn bench_push_front(b: &mut test::Bencher) {
1108 let mut m: DList<int> = DList::new();
1115 fn bench_push_back(b: &mut test::Bencher) {
1116 let mut m: DList<int> = DList::new();
1123 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1124 let mut m: DList<int> = DList::new();
1132 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1133 let mut m: DList<int> = DList::new();
1141 fn bench_rotate_forward(b: &mut test::Bencher) {
1142 let mut m: DList<int> = DList::new();
1151 fn bench_rotate_backward(b: &mut test::Bencher) {
1152 let mut m: DList<int> = DList::new();
1156 m.rotate_backward();
1161 fn bench_iter(b: &mut test::Bencher) {
1162 let v = &[0i, ..128];
1163 let m: DList<int> = v.iter().map(|&x|x).collect();
1165 assert!(m.iter().count() == 128);
1169 fn bench_iter_mut(b: &mut test::Bencher) {
1170 let v = &[0i, ..128];
1171 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1173 assert!(m.mut_iter().count() == 128);
1177 fn bench_iter_rev(b: &mut test::Bencher) {
1178 let v = &[0i, ..128];
1179 let m: DList<int> = v.iter().map(|&x|x).collect();
1181 assert!(m.iter().rev().count() == 128);
1185 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1186 let v = &[0i, ..128];
1187 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1189 assert!(m.mut_iter().rev().count() == 128);