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::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.
27 use std::mem::{replace, swap};
32 /// A doubly-linked list.
36 list_tail: Rawlink<Node<T>>,
39 type Link<T> = Option<Box<Node<T>>>;
40 struct Rawlink<T> { p: *mut T }
44 prev: Rawlink<Node<T>>,
48 /// Double-ended DList iterator
49 pub struct Items<'a, T> {
51 tail: Rawlink<Node<T>>,
55 // FIXME #11820: the &'a Option<> of the Link stops clone working.
56 impl<'a, T> Clone for Items<'a, T> {
57 fn clone(&self) -> Items<'a, T> { *self }
60 /// Double-ended mutable DList iterator
61 pub struct MutItems<'a, T> {
62 list: &'a mut DList<T>,
63 head: Rawlink<Node<T>>,
64 tail: Rawlink<Node<T>>,
68 /// DList consuming iterator
70 pub struct MoveItems<T> {
74 /// Rawlink is a type like Option<T> but for holding a raw pointer
76 /// Like Option::None for Rawlink
77 fn none() -> Rawlink<T> {
78 Rawlink{p: ptr::mut_null()}
81 /// Like Option::Some for Rawlink
82 fn some(n: &mut T) -> Rawlink<T> {
86 /// Convert the `Rawlink` into an Option value
87 fn resolve_immut(&self) -> Option<&T> {
88 unsafe { self.p.to_option() }
91 /// Convert the `Rawlink` into an Option value
92 fn resolve(&mut self) -> Option<&mut T> {
96 Some(unsafe { cast::transmute(self.p) })
100 /// Return the `Rawlink` and replace with `Rawlink::none()`
101 fn take(&mut self) -> Rawlink<T> {
102 replace(self, Rawlink::none())
106 impl<T> Clone for Rawlink<T> {
108 fn clone(&self) -> Rawlink<T> {
114 fn new(v: T) -> Node<T> {
115 Node{value: v, next: None, prev: Rawlink::none()}
119 /// Set the .prev field on `next`, then return `Some(next)`
120 fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
126 impl<T> Container for DList<T> {
129 fn is_empty(&self) -> bool {
130 self.list_head.is_none()
134 fn len(&self) -> uint {
139 impl<T> Mutable for DList<T> {
140 /// Remove all elements from the DList
144 fn clear(&mut self) {
151 /// Add a Node first in the list
153 fn push_front_node(&mut self, mut new_head: Box<Node<T>>) {
154 match self.list_head {
156 self.list_tail = Rawlink::some(new_head);
157 self.list_head = link_with_prev(new_head, Rawlink::none());
159 Some(ref mut head) => {
160 new_head.prev = Rawlink::none();
161 head.prev = Rawlink::some(new_head);
162 swap(head, &mut new_head);
163 head.next = Some(new_head);
169 /// Remove the first Node and return it, or None if the list is empty
171 fn pop_front_node(&mut self) -> Option<Box<Node<T>>> {
172 self.list_head.take().map(|mut front_node| {
174 match front_node.next.take() {
175 Some(node) => self.list_head = link_with_prev(node, Rawlink::none()),
176 None => self.list_tail = Rawlink::none()
182 /// Add a Node last in the list
184 fn push_back_node(&mut self, mut new_tail: Box<Node<T>>) {
185 match self.list_tail.resolve() {
186 None => return self.push_front_node(new_tail),
188 self.list_tail = Rawlink::some(new_tail);
189 tail.next = link_with_prev(new_tail, Rawlink::some(tail));
195 /// Remove the last Node and return it, or None if the list is empty
197 fn pop_back_node(&mut self) -> Option<Box<Node<T>>> {
198 self.list_tail.resolve().map_or(None, |tail| {
200 self.list_tail = tail.prev;
201 match tail.prev.resolve() {
202 None => self.list_head.take(),
203 Some(tail_prev) => tail_prev.next.take()
209 impl<T> Deque<T> for DList<T> {
210 /// Provide a reference to the front element, or None if the list is empty
212 fn front<'a>(&'a self) -> Option<&'a T> {
213 self.list_head.as_ref().map(|head| &head.value)
216 /// Provide a mutable reference to the front element, or None if the list is empty
218 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> {
219 self.list_head.as_mut().map(|head| &mut head.value)
222 /// Provide a reference to the back element, or None if the list is empty
224 fn back<'a>(&'a self) -> Option<&'a T> {
225 let tmp = self.list_tail.resolve_immut(); // FIXME: #3511: shouldn't need variable
226 tmp.as_ref().map(|tail| &tail.value)
229 /// Provide a mutable reference to the back element, or None if the list is empty
231 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> {
232 let tmp: Option<&'a mut Node<T>> =
233 self.list_tail.resolve(); // FIXME: #3511: shouldn't need variable
234 tmp.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)
267 /// Create an empty DList
269 pub fn new() -> DList<T> {
270 DList{list_head: None, list_tail: Rawlink::none(), length: 0}
273 /// Move the last element to the front of the list.
275 /// If the list is empty, do nothing.
277 pub fn rotate_forward(&mut self) {
278 self.pop_back_node().map(|tail| {
279 self.push_front_node(tail)
283 /// Move the first element to the back of the list.
285 /// If the list is empty, do nothing.
287 pub fn rotate_backward(&mut self) {
288 self.pop_front_node().map(|head| {
289 self.push_back_node(head)
293 /// Add all elements from `other` to the end of the list
296 pub fn append(&mut self, mut other: DList<T>) {
297 match self.list_tail.resolve() {
298 None => *self = other,
300 // Carefully empty `other`.
301 let o_tail = other.list_tail.take();
302 let o_length = other.length;
303 match other.list_head.take() {
306 tail.next = link_with_prev(node, self.list_tail);
307 self.list_tail = o_tail;
308 self.length += o_length;
315 /// Add all elements from `other` to the beginning of the list
319 pub fn prepend(&mut self, mut other: DList<T>) {
320 swap(self, &mut other);
324 /// Insert `elt` before the first `x` in the list where `f(x, elt)` is true,
328 pub fn insert_when(&mut self, elt: T, f: |&T, &T| -> bool) {
330 let mut it = self.mut_iter();
332 match it.peek_next() {
334 Some(x) => if f(x, &elt) { break }
342 /// Merge DList `other` into this DList, using the function `f`.
343 /// Iterate the both DList with `a` from self and `b` from `other`, and
344 /// put `a` in the result if `f(a, b)` is true, else `b`.
347 pub fn merge(&mut self, mut other: DList<T>, f: |&T, &T| -> bool) {
349 let mut it = self.mut_iter();
351 let take_a = match (it.peek_next(), other.front()) {
352 (_ , None) => return,
354 (Some(ref mut x), Some(y)) => f(*x, y),
359 it.insert_next_node(other.pop_front_node().unwrap());
367 /// Provide a forward iterator
369 pub fn iter<'a>(&'a self) -> Items<'a, T> {
370 Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
374 #[deprecated = "replaced by .iter().rev()"]
375 pub fn rev_iter<'a>(&'a self) -> Rev<Items<'a, T>> {
379 /// Provide a forward iterator with mutable references
381 pub fn mut_iter<'a>(&'a mut self) -> MutItems<'a, T> {
382 let head_raw = match self.list_head {
383 Some(ref mut h) => Rawlink::some(*h),
384 None => Rawlink::none(),
389 tail: self.list_tail,
395 #[deprecated = "replaced by .mut_iter().rev()"]
396 pub fn mut_rev_iter<'a>(&'a mut self) -> Rev<MutItems<'a, T>> {
397 self.mut_iter().rev()
401 /// Consume the list into an iterator yielding elements by value
403 pub fn move_iter(self) -> MoveItems<T> {
404 MoveItems{list: self}
408 #[deprecated = "replaced by .move_iter().rev()"]
409 pub fn move_rev_iter(self) -> Rev<MoveItems<T>> {
410 self.move_iter().rev()
414 impl<T: Ord> DList<T> {
415 /// Insert `elt` sorted in ascending order
419 pub fn insert_ordered(&mut self, elt: T) {
420 self.insert_when(elt, |a, b| a >= b)
425 impl<T> Drop for DList<T> {
427 // Dissolve the dlist in backwards direction
428 // Just dropping the list_head can lead to stack exhaustion
429 // when length is >> 1_000_000
430 let mut tail = self.list_tail;
432 match tail.resolve() {
435 prev.next.take(); // release Box<Node<T>>
441 self.list_head = None;
442 self.list_tail = Rawlink::none();
447 impl<'a, A> Iterator<&'a A> for Items<'a, A> {
449 fn next(&mut self) -> Option<&'a A> {
453 self.head.as_ref().map(|head| {
455 self.head = &head.next;
461 fn size_hint(&self) -> (uint, Option<uint>) {
462 (self.nelem, Some(self.nelem))
466 impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> {
468 fn next_back(&mut self) -> Option<&'a A> {
472 let tmp = self.tail.resolve_immut(); // FIXME: #3511: shouldn't need variable
473 tmp.as_ref().map(|prev| {
475 self.tail = prev.prev;
481 impl<'a, A> ExactSize<&'a A> for Items<'a, A> {}
483 impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> {
485 fn next(&mut self) -> Option<&'a mut A> {
489 self.head.resolve().map(|next| {
491 self.head = match next.next {
492 Some(ref mut node) => Rawlink::some(&mut **node),
493 None => Rawlink::none(),
500 fn size_hint(&self) -> (uint, Option<uint>) {
501 (self.nelem, Some(self.nelem))
505 impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> {
507 fn next_back(&mut self) -> Option<&'a mut A> {
511 self.tail.resolve().map(|prev| {
513 self.tail = prev.prev;
519 impl<'a, A> ExactSize<&'a mut A> for MutItems<'a, A> {}
521 /// Allow mutating the DList while iterating
522 pub trait ListInsertion<A> {
523 /// Insert `elt` just after to the element most recently returned by `.next()`
525 /// The inserted element does not appear in the iteration.
526 fn insert_next(&mut self, elt: A);
528 /// Provide a reference to the next element, without changing the iterator
529 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>;
532 // private methods for MutItems
533 impl<'a, A> MutItems<'a, A> {
534 fn insert_next_node(&mut self, mut ins_node: Box<Node<A>>) {
535 // Insert before `self.head` so that it is between the
536 // previously yielded element and self.head.
538 // The inserted node will not appear in further iteration.
539 match self.head.resolve() {
540 None => { self.list.push_back_node(ins_node); }
542 let prev_node = match node.prev.resolve() {
543 None => return self.list.push_front_node(ins_node),
546 let node_own = prev_node.next.take_unwrap();
547 ins_node.next = link_with_prev(node_own, Rawlink::some(ins_node));
548 prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node));
549 self.list.length += 1;
555 impl<'a, A> ListInsertion<A> for MutItems<'a, A> {
557 fn insert_next(&mut self, elt: A) {
558 self.insert_next_node(box Node::new(elt))
562 fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> {
566 self.head.resolve().map(|head| &mut head.value)
570 impl<A> Iterator<A> for MoveItems<A> {
572 fn next(&mut self) -> Option<A> { self.list.pop_front() }
575 fn size_hint(&self) -> (uint, Option<uint>) {
576 (self.list.length, Some(self.list.length))
580 impl<A> DoubleEndedIterator<A> for MoveItems<A> {
582 fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
585 impl<A> FromIterator<A> for DList<A> {
586 fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
587 let mut ret = DList::new();
588 ret.extend(iterator);
593 impl<A> Extendable<A> for DList<A> {
594 fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
595 for elt in iterator { self.push_back(elt); }
599 impl<A: Eq> Eq for DList<A> {
600 fn eq(&self, other: &DList<A>) -> bool {
601 self.len() == other.len() &&
602 iter::order::eq(self.iter(), other.iter())
605 fn ne(&self, other: &DList<A>) -> bool {
606 self.len() != other.len() ||
607 iter::order::ne(self.iter(), other.iter())
611 impl<A: Ord> Ord for DList<A> {
612 fn lt(&self, other: &DList<A>) -> bool {
613 iter::order::lt(self.iter(), other.iter())
615 fn le(&self, other: &DList<A>) -> bool {
616 iter::order::le(self.iter(), other.iter())
618 fn gt(&self, other: &DList<A>) -> bool {
619 iter::order::gt(self.iter(), other.iter())
621 fn ge(&self, other: &DList<A>) -> bool {
622 iter::order::ge(self.iter(), other.iter())
626 impl<A: Clone> Clone for DList<A> {
627 fn clone(&self) -> DList<A> {
628 self.iter().map(|x| x.clone()).collect()
635 use self::test::Bencher;
638 use super::{DList, Node, ListInsertion};
640 pub fn check_links<T>(list: &DList<T>) {
642 let mut last_ptr: Option<&Node<T>> = None;
643 let mut node_ptr: &Node<T>;
644 match list.list_head {
645 None => { assert_eq!(0u, list.length); return }
646 Some(ref node) => node_ptr = &**node,
649 match (last_ptr, node_ptr.prev.resolve_immut()) {
651 (None , _ ) => fail!("prev link for list_head"),
652 (Some(p), Some(pptr)) => {
653 assert_eq!(p as *Node<T>, pptr as *Node<T>);
655 _ => fail!("prev link is none, not good"),
657 match node_ptr.next {
659 last_ptr = Some(node_ptr);
669 assert_eq!(len, list.length);
674 let mut m: DList<Box<int>> = DList::new();
675 assert_eq!(m.pop_front(), None);
676 assert_eq!(m.pop_back(), None);
677 assert_eq!(m.pop_front(), None);
679 assert_eq!(m.pop_front(), Some(box 1));
682 assert_eq!(m.len(), 2);
683 assert_eq!(m.pop_front(), Some(box 2));
684 assert_eq!(m.pop_front(), Some(box 3));
685 assert_eq!(m.len(), 0);
686 assert_eq!(m.pop_front(), None);
691 assert_eq!(m.pop_front(), Some(box 1));
693 let mut n = DList::new();
697 assert_eq!(n.front().unwrap(), &3);
698 let x = n.front_mut().unwrap();
703 assert_eq!(n.back().unwrap(), &2);
704 let y = n.back_mut().unwrap();
708 assert_eq!(n.pop_front(), Some(0));
709 assert_eq!(n.pop_front(), Some(1));
713 fn generate_test() -> DList<int> {
714 list_from(&[0,1,2,3,4,5,6])
718 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
719 v.iter().map(|x| (*x).clone()).collect()
725 let mut m = DList::new();
726 let mut n = DList::new();
729 assert_eq!(m.len(), 1);
730 assert_eq!(m.pop_back(), Some(2));
734 let mut m = DList::new();
735 let n = DList::new();
738 assert_eq!(m.len(), 1);
739 assert_eq!(m.pop_back(), Some(2));
743 let v = vec![1,2,3,4,5];
744 let u = vec![9,8,1,2,3,4,5];
745 let mut m = list_from(v.as_slice());
746 m.append(list_from(u.as_slice()));
748 let sum = v.append(u.as_slice());
749 assert_eq!(sum.len(), m.len());
750 for elt in sum.move_iter() {
751 assert_eq!(m.pop_front(), Some(elt))
758 let mut m = DList::new();
759 let mut n = DList::new();
762 assert_eq!(m.len(), 1);
763 assert_eq!(m.pop_back(), Some(2));
767 let v = vec![1,2,3,4,5];
768 let u = vec![9,8,1,2,3,4,5];
769 let mut m = list_from(v.as_slice());
770 m.prepend(list_from(u.as_slice()));
772 let sum = u.append(v.as_slice());
773 assert_eq!(sum.len(), m.len());
774 for elt in sum.move_iter() {
775 assert_eq!(m.pop_front(), Some(elt))
781 let mut n: DList<int> = DList::new();
782 n.rotate_backward(); check_links(&n);
783 assert_eq!(n.len(), 0);
784 n.rotate_forward(); check_links(&n);
785 assert_eq!(n.len(), 0);
787 let v = vec![1,2,3,4,5];
788 let mut m = list_from(v.as_slice());
789 m.rotate_backward(); check_links(&m);
790 m.rotate_forward(); check_links(&m);
791 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
792 m.rotate_forward(); check_links(&m);
793 m.rotate_forward(); check_links(&m);
794 m.pop_front(); check_links(&m);
795 m.rotate_forward(); check_links(&m);
796 m.rotate_backward(); check_links(&m);
797 m.push_front(9); check_links(&m);
798 m.rotate_forward(); check_links(&m);
799 assert_eq!(vec![3,9,5,1,2], m.move_iter().collect());
804 let m = generate_test();
805 for (i, elt) in m.iter().enumerate() {
806 assert_eq!(i as int, *elt);
808 let mut n = DList::new();
809 assert_eq!(n.iter().next(), None);
811 let mut it = n.iter();
812 assert_eq!(it.size_hint(), (1, Some(1)));
813 assert_eq!(it.next().unwrap(), &4);
814 assert_eq!(it.size_hint(), (0, Some(0)));
815 assert_eq!(it.next(), None);
819 fn test_iterator_clone() {
820 let mut n = DList::new();
824 let mut it = n.iter();
826 let mut jt = it.clone();
827 assert_eq!(it.next(), jt.next());
828 assert_eq!(it.next_back(), jt.next_back());
829 assert_eq!(it.next(), jt.next());
833 fn test_iterator_double_end() {
834 let mut n = DList::new();
835 assert_eq!(n.iter().next(), None);
839 let mut it = n.iter();
840 assert_eq!(it.size_hint(), (3, Some(3)));
841 assert_eq!(it.next().unwrap(), &6);
842 assert_eq!(it.size_hint(), (2, Some(2)));
843 assert_eq!(it.next_back().unwrap(), &4);
844 assert_eq!(it.size_hint(), (1, Some(1)));
845 assert_eq!(it.next_back().unwrap(), &5);
846 assert_eq!(it.next_back(), None);
847 assert_eq!(it.next(), None);
852 let m = generate_test();
853 for (i, elt) in m.iter().rev().enumerate() {
854 assert_eq!((6 - i) as int, *elt);
856 let mut n = DList::new();
857 assert_eq!(n.iter().rev().next(), None);
859 let mut it = n.iter().rev();
860 assert_eq!(it.size_hint(), (1, Some(1)));
861 assert_eq!(it.next().unwrap(), &4);
862 assert_eq!(it.size_hint(), (0, Some(0)));
863 assert_eq!(it.next(), None);
868 let mut m = generate_test();
869 let mut len = m.len();
870 for (i, elt) in m.mut_iter().enumerate() {
871 assert_eq!(i as int, *elt);
875 let mut n = DList::new();
876 assert!(n.mut_iter().next().is_none());
879 let mut it = n.mut_iter();
880 assert_eq!(it.size_hint(), (2, Some(2)));
881 assert!(it.next().is_some());
882 assert!(it.next().is_some());
883 assert_eq!(it.size_hint(), (0, Some(0)));
884 assert!(it.next().is_none());
888 fn test_iterator_mut_double_end() {
889 let mut n = DList::new();
890 assert!(n.mut_iter().next_back().is_none());
894 let mut it = n.mut_iter();
895 assert_eq!(it.size_hint(), (3, Some(3)));
896 assert_eq!(*it.next().unwrap(), 6);
897 assert_eq!(it.size_hint(), (2, Some(2)));
898 assert_eq!(*it.next_back().unwrap(), 4);
899 assert_eq!(it.size_hint(), (1, Some(1)));
900 assert_eq!(*it.next_back().unwrap(), 5);
901 assert!(it.next_back().is_none());
902 assert!(it.next().is_none());
906 fn test_insert_prev() {
907 let mut m = list_from(&[0,2,4,6,8]);
910 let mut it = m.mut_iter();
916 it.insert_next(*elt + 1);
917 match it.peek_next() {
918 Some(x) => assert_eq!(*x, *elt + 2),
919 None => assert_eq!(8, *elt),
928 assert_eq!(m.len(), 3 + len * 2);
929 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
934 let mut m = list_from([0, 1, 3, 5, 6, 7, 2]);
935 let n = list_from([-1, 0, 0, 7, 7, 9]);
936 let len = m.len() + n.len();
937 m.merge(n, |a, b| a <= b);
938 assert_eq!(m.len(), len);
940 let res = m.move_iter().collect::<Vec<int>>();
941 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
945 fn test_insert_ordered() {
946 let mut n = DList::new();
948 assert_eq!(n.len(), 1);
949 assert_eq!(n.pop_front(), Some(1));
951 let mut m = DList::new();
956 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
960 fn test_mut_rev_iter() {
961 let mut m = generate_test();
962 for (i, elt) in m.mut_iter().rev().enumerate() {
963 assert_eq!((6-i) as int, *elt);
965 let mut n = DList::new();
966 assert!(n.mut_iter().rev().next().is_none());
968 let mut it = n.mut_iter().rev();
969 assert!(it.next().is_some());
970 assert!(it.next().is_none());
975 let n = list_from([1,2,3]);
978 assert_eq!(&[&1,&2,&3], n.iter().collect::<Vec<&int>>().as_slice());
984 let mut n: DList<u8> = list_from([]);
985 let mut m = list_from([]);
992 let n = list_from([2,3,4]);
993 let m = list_from([1,2,3]);
999 let n: DList<int> = list_from([]);
1000 let m = list_from([1,2,3]);
1010 let n = list_from([nan]);
1011 let m = list_from([nan]);
1017 let n = list_from([nan]);
1018 let one = list_from([1.0]);
1019 assert!(!(n < one));
1020 assert!(!(n > one));
1021 assert!(!(n <= one));
1022 assert!(!(n >= one));
1024 let u = list_from([1.0,2.0,nan]);
1025 let v = list_from([1.0,2.0,3.0]);
1031 let s = list_from([1.0,2.0,4.0,2.0]);
1032 let t = list_from([1.0,2.0,3.0,2.0]);
1035 assert!(!(s <= one));
1041 for _ in range(0, 25) {
1049 fn fuzz_test(sz: int) {
1050 let mut m: DList<int> = DList::new();
1052 for i in range(0, sz) {
1054 let r: u8 = rand::random();
1078 for (a, &b) in m.move_iter().zip(v.iter()) {
1082 assert_eq!(i, v.len());
1086 fn bench_collect_into(b: &mut test::Bencher) {
1089 let _: DList<int> = v.iter().map(|x| *x).collect();
1094 fn bench_push_front(b: &mut test::Bencher) {
1095 let mut m: DList<int> = DList::new();
1102 fn bench_push_back(b: &mut test::Bencher) {
1103 let mut m: DList<int> = DList::new();
1110 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1111 let mut m: DList<int> = DList::new();
1119 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1120 let mut m: DList<int> = DList::new();
1128 fn bench_rotate_forward(b: &mut test::Bencher) {
1129 let mut m: DList<int> = DList::new();
1138 fn bench_rotate_backward(b: &mut test::Bencher) {
1139 let mut m: DList<int> = DList::new();
1143 m.rotate_backward();
1148 fn bench_iter(b: &mut test::Bencher) {
1149 let v = &[0, ..128];
1150 let m: DList<int> = v.iter().map(|&x|x).collect();
1152 assert!(m.iter().len() == 128);
1156 fn bench_iter_mut(b: &mut test::Bencher) {
1157 let v = &[0, ..128];
1158 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1160 assert!(m.mut_iter().len() == 128);
1164 fn bench_iter_rev(b: &mut test::Bencher) {
1165 let v = &[0, ..128];
1166 let m: DList<int> = v.iter().map(|&x|x).collect();
1168 assert!(m.iter().rev().len() == 128);
1172 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1173 let v = &[0, ..128];
1174 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1176 assert!(m.mut_iter().rev().len() == 128);