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.
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(&mut **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(&mut *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 partial_cmp(&self, other: &DList<A>) -> Option<Ordering> {
599 iter::order::partial_cmp(self.iter(), other.iter())
603 impl<A: Clone> Clone for DList<A> {
604 fn clone(&self) -> DList<A> {
605 self.iter().map(|x| x.clone()).collect()
609 impl<A: fmt::Show> fmt::Show for DList<A> {
610 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
611 try!(write!(f, "["));
613 for (i, e) in self.iter().enumerate() {
614 if i != 0 { try!(write!(f, ", ")); }
615 try!(write!(f, "{}", *e));
630 use super::{DList, Node, ListInsertion};
633 pub fn check_links<T>(list: &DList<T>) {
635 let mut last_ptr: Option<&Node<T>> = None;
636 let mut node_ptr: &Node<T>;
637 match list.list_head {
638 None => { assert_eq!(0u, list.length); return }
639 Some(ref node) => node_ptr = &**node,
642 match (last_ptr, node_ptr.prev.resolve_immut()) {
644 (None , _ ) => fail!("prev link for list_head"),
645 (Some(p), Some(pptr)) => {
646 assert_eq!(p as *const Node<T>, pptr as *const Node<T>);
648 _ => fail!("prev link is none, not good"),
650 match node_ptr.next {
652 last_ptr = Some(node_ptr);
662 assert_eq!(len, list.length);
667 let mut m: DList<Box<int>> = DList::new();
668 assert_eq!(m.pop_front(), None);
669 assert_eq!(m.pop_back(), None);
670 assert_eq!(m.pop_front(), None);
672 assert_eq!(m.pop_front(), Some(box 1));
675 assert_eq!(m.len(), 2);
676 assert_eq!(m.pop_front(), Some(box 2));
677 assert_eq!(m.pop_front(), Some(box 3));
678 assert_eq!(m.len(), 0);
679 assert_eq!(m.pop_front(), None);
684 assert_eq!(m.pop_front(), Some(box 1));
686 let mut n = DList::new();
690 assert_eq!(n.front().unwrap(), &3);
691 let x = n.front_mut().unwrap();
696 assert_eq!(n.back().unwrap(), &2);
697 let y = n.back_mut().unwrap();
701 assert_eq!(n.pop_front(), Some(0));
702 assert_eq!(n.pop_front(), Some(1));
706 fn generate_test() -> DList<int> {
707 list_from(&[0i,1,2,3,4,5,6])
711 fn list_from<T: Clone>(v: &[T]) -> DList<T> {
712 v.iter().map(|x| (*x).clone()).collect()
718 let mut m = DList::new();
719 let mut n = DList::new();
722 assert_eq!(m.len(), 1);
723 assert_eq!(m.pop_back(), Some(2));
727 let mut m = DList::new();
728 let n = DList::new();
731 assert_eq!(m.len(), 1);
732 assert_eq!(m.pop_back(), Some(2));
736 let v = vec![1i,2,3,4,5];
737 let u = vec![9i,8,1,2,3,4,5];
738 let mut m = list_from(v.as_slice());
739 m.append(list_from(u.as_slice()));
741 let sum = v.append(u.as_slice());
742 assert_eq!(sum.len(), m.len());
743 for elt in sum.move_iter() {
744 assert_eq!(m.pop_front(), Some(elt))
751 let mut m = DList::new();
752 let mut n = DList::new();
755 assert_eq!(m.len(), 1);
756 assert_eq!(m.pop_back(), Some(2));
760 let v = vec![1i,2,3,4,5];
761 let u = vec![9i,8,1,2,3,4,5];
762 let mut m = list_from(v.as_slice());
763 m.prepend(list_from(u.as_slice()));
765 let sum = u.append(v.as_slice());
766 assert_eq!(sum.len(), m.len());
767 for elt in sum.move_iter() {
768 assert_eq!(m.pop_front(), Some(elt))
774 let mut n: DList<int> = DList::new();
775 n.rotate_backward(); check_links(&n);
776 assert_eq!(n.len(), 0);
777 n.rotate_forward(); check_links(&n);
778 assert_eq!(n.len(), 0);
780 let v = vec![1i,2,3,4,5];
781 let mut m = list_from(v.as_slice());
782 m.rotate_backward(); check_links(&m);
783 m.rotate_forward(); check_links(&m);
784 assert_eq!(v.iter().collect::<Vec<&int>>(), m.iter().collect());
785 m.rotate_forward(); check_links(&m);
786 m.rotate_forward(); check_links(&m);
787 m.pop_front(); check_links(&m);
788 m.rotate_forward(); check_links(&m);
789 m.rotate_backward(); check_links(&m);
790 m.push_front(9); check_links(&m);
791 m.rotate_forward(); check_links(&m);
792 assert_eq!(vec![3i,9,5,1,2], m.move_iter().collect());
797 let m = generate_test();
798 for (i, elt) in m.iter().enumerate() {
799 assert_eq!(i as int, *elt);
801 let mut n = DList::new();
802 assert_eq!(n.iter().next(), None);
804 let mut it = n.iter();
805 assert_eq!(it.size_hint(), (1, Some(1)));
806 assert_eq!(it.next().unwrap(), &4);
807 assert_eq!(it.size_hint(), (0, Some(0)));
808 assert_eq!(it.next(), None);
812 fn test_iterator_clone() {
813 let mut n = DList::new();
817 let mut it = n.iter();
819 let mut jt = it.clone();
820 assert_eq!(it.next(), jt.next());
821 assert_eq!(it.next_back(), jt.next_back());
822 assert_eq!(it.next(), jt.next());
826 fn test_iterator_double_end() {
827 let mut n = DList::new();
828 assert_eq!(n.iter().next(), None);
832 let mut it = n.iter();
833 assert_eq!(it.size_hint(), (3, Some(3)));
834 assert_eq!(it.next().unwrap(), &6);
835 assert_eq!(it.size_hint(), (2, Some(2)));
836 assert_eq!(it.next_back().unwrap(), &4);
837 assert_eq!(it.size_hint(), (1, Some(1)));
838 assert_eq!(it.next_back().unwrap(), &5);
839 assert_eq!(it.next_back(), None);
840 assert_eq!(it.next(), None);
845 let m = generate_test();
846 for (i, elt) in m.iter().rev().enumerate() {
847 assert_eq!((6 - i) as int, *elt);
849 let mut n = DList::new();
850 assert_eq!(n.iter().rev().next(), None);
852 let mut it = n.iter().rev();
853 assert_eq!(it.size_hint(), (1, Some(1)));
854 assert_eq!(it.next().unwrap(), &4);
855 assert_eq!(it.size_hint(), (0, Some(0)));
856 assert_eq!(it.next(), None);
861 let mut m = generate_test();
862 let mut len = m.len();
863 for (i, elt) in m.mut_iter().enumerate() {
864 assert_eq!(i as int, *elt);
868 let mut n = DList::new();
869 assert!(n.mut_iter().next().is_none());
872 let mut it = n.mut_iter();
873 assert_eq!(it.size_hint(), (2, Some(2)));
874 assert!(it.next().is_some());
875 assert!(it.next().is_some());
876 assert_eq!(it.size_hint(), (0, Some(0)));
877 assert!(it.next().is_none());
881 fn test_iterator_mut_double_end() {
882 let mut n = DList::new();
883 assert!(n.mut_iter().next_back().is_none());
887 let mut it = n.mut_iter();
888 assert_eq!(it.size_hint(), (3, Some(3)));
889 assert_eq!(*it.next().unwrap(), 6);
890 assert_eq!(it.size_hint(), (2, Some(2)));
891 assert_eq!(*it.next_back().unwrap(), 4);
892 assert_eq!(it.size_hint(), (1, Some(1)));
893 assert_eq!(*it.next_back().unwrap(), 5);
894 assert!(it.next_back().is_none());
895 assert!(it.next().is_none());
899 fn test_insert_prev() {
900 let mut m = list_from(&[0i,2,4,6,8]);
903 let mut it = m.mut_iter();
909 it.insert_next(*elt + 1);
910 match it.peek_next() {
911 Some(x) => assert_eq!(*x, *elt + 2),
912 None => assert_eq!(8, *elt),
921 assert_eq!(m.len(), 3 + len * 2);
922 assert_eq!(m.move_iter().collect::<Vec<int>>(), vec![-2,0,1,2,3,4,5,6,7,8,9,0,1]);
927 let mut m = list_from([0i, 1, 3, 5, 6, 7, 2]);
928 let n = list_from([-1i, 0, 0, 7, 7, 9]);
929 let len = m.len() + n.len();
930 m.merge(n, |a, b| a <= b);
931 assert_eq!(m.len(), len);
933 let res = m.move_iter().collect::<Vec<int>>();
934 assert_eq!(res, vec![-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]);
938 fn test_insert_ordered() {
939 let mut n = DList::new();
940 n.insert_ordered(1i);
941 assert_eq!(n.len(), 1);
942 assert_eq!(n.pop_front(), Some(1));
944 let mut m = DList::new();
949 assert_eq!(vec![2,3,4], m.move_iter().collect::<Vec<int>>());
953 fn test_mut_rev_iter() {
954 let mut m = generate_test();
955 for (i, elt) in m.mut_iter().rev().enumerate() {
956 assert_eq!((6-i) as int, *elt);
958 let mut n = DList::new();
959 assert!(n.mut_iter().rev().next().is_none());
961 let mut it = n.mut_iter().rev();
962 assert!(it.next().is_some());
963 assert!(it.next().is_none());
968 let n = list_from([1i,2,3]);
971 assert_eq!(&[&1,&2,&3], n.iter().collect::<Vec<&int>>().as_slice());
977 let mut n: DList<u8> = list_from([]);
978 let mut m = list_from([]);
985 let n = list_from([2i,3,4]);
986 let m = list_from([1i,2,3]);
992 let n: DList<int> = list_from([]);
993 let m = list_from([1i,2,3]);
1002 let nan = 0.0f64/0.0;
1003 let n = list_from([nan]);
1004 let m = list_from([nan]);
1010 let n = list_from([nan]);
1011 let one = list_from([1.0f64]);
1012 assert!(!(n < one));
1013 assert!(!(n > one));
1014 assert!(!(n <= one));
1015 assert!(!(n >= one));
1017 let u = list_from([1.0f64,2.0,nan]);
1018 let v = list_from([1.0f64,2.0,3.0]);
1024 let s = list_from([1.0f64,2.0,4.0,2.0]);
1025 let t = list_from([1.0f64,2.0,3.0,2.0]);
1028 assert!(!(s <= one));
1034 for _ in range(0u, 25) {
1043 let list: DList<int> = range(0i, 10).collect();
1044 assert!(list.to_string().as_slice() == "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
1046 let list: DList<&str> = vec!["just", "one", "test", "more"].iter()
1049 assert!(list.to_string().as_slice() == "[just, one, test, more]");
1053 fn fuzz_test(sz: int) {
1054 let mut m: DList<int> = DList::new();
1056 for i in range(0, sz) {
1058 let r: u8 = rand::random();
1082 for (a, &b) in m.move_iter().zip(v.iter()) {
1086 assert_eq!(i, v.len());
1090 fn bench_collect_into(b: &mut test::Bencher) {
1091 let v = &[0i, ..64];
1093 let _: DList<int> = v.iter().map(|x| *x).collect();
1098 fn bench_push_front(b: &mut test::Bencher) {
1099 let mut m: DList<int> = DList::new();
1106 fn bench_push_back(b: &mut test::Bencher) {
1107 let mut m: DList<int> = DList::new();
1114 fn bench_push_back_pop_back(b: &mut test::Bencher) {
1115 let mut m: DList<int> = DList::new();
1123 fn bench_push_front_pop_front(b: &mut test::Bencher) {
1124 let mut m: DList<int> = DList::new();
1132 fn bench_rotate_forward(b: &mut test::Bencher) {
1133 let mut m: DList<int> = DList::new();
1142 fn bench_rotate_backward(b: &mut test::Bencher) {
1143 let mut m: DList<int> = DList::new();
1147 m.rotate_backward();
1152 fn bench_iter(b: &mut test::Bencher) {
1153 let v = &[0i, ..128];
1154 let m: DList<int> = v.iter().map(|&x|x).collect();
1156 assert!(m.iter().count() == 128);
1160 fn bench_iter_mut(b: &mut test::Bencher) {
1161 let v = &[0i, ..128];
1162 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1164 assert!(m.mut_iter().count() == 128);
1168 fn bench_iter_rev(b: &mut test::Bencher) {
1169 let v = &[0i, ..128];
1170 let m: DList<int> = v.iter().map(|&x|x).collect();
1172 assert!(m.iter().rev().count() == 128);
1176 fn bench_iter_mut_rev(b: &mut test::Bencher) {
1177 let v = &[0i, ..128];
1178 let mut m: DList<int> = v.iter().map(|&x|x).collect();
1180 assert!(m.mut_iter().rev().count() == 128);