// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // // Licensed under the Apache License, Version 2.0 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. //! A doubly-linked list with owned nodes. //! //! The DList allows pushing and popping elements at either end. //! //! DList implements the trait Deque. It should be imported with `use //! extra::container::Deque`. // DList is constructed like a singly-linked list over the field `next`. // including the last link being None; each Node owns its `next` field. // // Backlinks over DList::prev are raw pointers that form a full chain in // the reverse direction. use std::cast; use std::cmp; use std::ptr; use std::util; use std::iterator::{FromIterator, InvertIterator}; use container::Deque; /// A doubly-linked list. pub struct DList { priv length: uint, priv list_head: Link, priv list_tail: Rawlink>, } type Link = Option<~Node>; struct Rawlink { priv p: *mut T } struct Node { priv next: Link, priv prev: Rawlink>, priv value: T, } /// Double-ended DList iterator #[deriving(Clone)] pub struct DListIterator<'self, T> { priv head: &'self Link, priv tail: Rawlink>, priv nelem: uint, } /// Double-ended mutable DList iterator pub struct MutDListIterator<'self, T> { priv list: &'self mut DList, priv head: Rawlink>, priv tail: Rawlink>, priv nelem: uint, } /// DList consuming iterator #[deriving(Clone)] pub struct ConsumeIterator { priv list: DList } /// Rawlink is a type like Option but for holding a raw pointer impl Rawlink { /// Like Option::None for Rawlink fn none() -> Rawlink { Rawlink{p: ptr::mut_null()} } /// Like Option::Some for Rawlink fn some(n: &mut T) -> Rawlink { Rawlink{p: ptr::to_mut_unsafe_ptr(n)} } /// Convert the `Rawlink` into an Option value fn resolve_immut(&self) -> Option<&T> { unsafe { self.p.to_option() } } /// Convert the `Rawlink` into an Option value fn resolve(&mut self) -> Option<&mut T> { if self.p.is_null() { None } else { Some(unsafe { cast::transmute(self.p) }) } } } impl Clone for Rawlink { #[inline] fn clone(&self) -> Rawlink { Rawlink{p: self.p} } } impl Node { fn new(v: T) -> Node { Node{value: v, next: None, prev: Rawlink::none()} } } /// Set the .prev field on `next`, then return `Some(next)` fn link_with_prev(mut next: ~Node, prev: Rawlink>) -> Link { next.prev = prev; Some(next) } impl Container for DList { /// O(1) #[inline] fn is_empty(&self) -> bool { self.list_head.is_none() } /// O(1) #[inline] fn len(&self) -> uint { self.length } } impl Mutable for DList { /// Remove all elements from the DList /// /// O(N) #[inline] fn clear(&mut self) { *self = DList::new() } } // private methods impl DList { /// Add a Node first in the list #[inline] fn push_front_node(&mut self, mut new_head: ~Node) { match self.list_head { None => { self.list_tail = Rawlink::some(new_head); self.list_head = link_with_prev(new_head, Rawlink::none()); } Some(ref mut head) => { new_head.prev = Rawlink::none(); head.prev = Rawlink::some(new_head); util::swap(head, &mut new_head); head.next = Some(new_head); } } self.length += 1; } /// Remove the first Node and return it, or None if the list is empty #[inline] fn pop_front_node(&mut self) -> Option<~Node> { do self.list_head.take().map_consume |mut front_node| { self.length -= 1; match front_node.next.take() { Some(node) => self.list_head = link_with_prev(node, Rawlink::none()), None => self.list_tail = Rawlink::none() } front_node } } /// Add a Node last in the list #[inline] fn push_back_node(&mut self, mut new_tail: ~Node) { match self.list_tail.resolve() { None => return self.push_front_node(new_tail), Some(tail) => { self.list_tail = Rawlink::some(new_tail); tail.next = link_with_prev(new_tail, Rawlink::some(tail)); } } self.length += 1; } /// Remove the last Node and return it, or None if the list is empty #[inline] fn pop_back_node(&mut self) -> Option<~Node> { do self.list_tail.resolve().map_consume |tail| { self.length -= 1; self.list_tail = tail.prev; match tail.prev.resolve() { None => self.list_head.take_unwrap(), Some(tail_prev) => tail_prev.next.take_unwrap() } } } } impl Deque for DList { /// Provide a reference to the front element, or None if the list is empty #[inline] fn front<'a>(&'a self) -> Option<&'a T> { self.list_head.map(|head| &head.value) } /// Provide a mutable reference to the front element, or None if the list is empty #[inline] fn front_mut<'a>(&'a mut self) -> Option<&'a mut T> { self.list_head.map_mut(|head| &mut head.value) } /// Provide a reference to the back element, or None if the list is empty #[inline] fn back<'a>(&'a self) -> Option<&'a T> { self.list_tail.resolve_immut().map(|tail| &tail.value) } /// Provide a mutable reference to the back element, or None if the list is empty #[inline] fn back_mut<'a>(&'a mut self) -> Option<&'a mut T> { self.list_tail.resolve().map_mut(|tail| &mut tail.value) } /// Add an element first in the list /// /// O(1) fn push_front(&mut self, elt: T) { self.push_front_node(~Node::new(elt)) } /// Remove the first element and return it, or None if the list is empty /// /// O(1) fn pop_front(&mut self) -> Option { self.pop_front_node().map_consume(|~Node{value, _}| value) } /// Add an element last in the list /// /// O(1) fn push_back(&mut self, elt: T) { self.push_back_node(~Node::new(elt)) } /// Remove the last element and return it, or None if the list is empty /// /// O(1) fn pop_back(&mut self) -> Option { self.pop_back_node().map_consume(|~Node{value, _}| value) } } impl DList { /// Create an empty DList #[inline] pub fn new() -> DList { DList{list_head: None, list_tail: Rawlink::none(), length: 0} } /// Move the last element to the front of the list. /// /// If the list is empty, do nothing. #[inline] pub fn rotate_to_front(&mut self) { do self.pop_back_node().map_consume |tail| { self.push_front_node(tail) }; } /// Move the first element to the back of the list. /// /// If the list is empty, do nothing. #[inline] pub fn rotate_to_back(&mut self) { do self.pop_front_node().map_consume |head| { self.push_back_node(head) }; } /// Add all elements from `other` to the end of the list /// /// O(1) pub fn append(&mut self, other: DList) { match self.list_tail.resolve() { None => *self = other, Some(tail) => { match other { DList{list_head: None, _} => return, DList{list_head: Some(node), list_tail: o_tail, length: o_length} => { tail.next = link_with_prev(node, self.list_tail); self.list_tail = o_tail; self.length += o_length; } } } } } /// Add all elements from `other` to the beginning of the list /// /// O(1) #[inline] pub fn prepend(&mut self, mut other: DList) { util::swap(self, &mut other); self.append(other); } /// Insert `elt` before the first `x` in the list where `f(x, elt)` is true, /// or at the end. /// /// O(N) pub fn insert_when(&mut self, elt: T, f: &fn(&T, &T) -> bool) { { let mut it = self.mut_iter(); loop { match it.peek_next() { None => break, Some(x) => if f(x, &elt) { break } } it.next(); } it.insert_next(elt); } } /// Merge DList `other` into this DList, using the function `f`. /// Iterate the both DList with `a` from self and `b` from `other`, and /// put `a` in the result if `f(a, b)` is true, else `b`. /// /// O(max(N, M)) pub fn merge(&mut self, mut other: DList, f: &fn(&T, &T) -> bool) { { let mut it = self.mut_iter(); loop { let take_a = match (it.peek_next(), other.front()) { (_ , None) => return, (None, _ ) => break, (Some(ref mut x), Some(y)) => f(*x, y), }; if take_a { it.next(); } else { it.insert_next_node(other.pop_front_node().unwrap()); } } } self.append(other); } /// Provide a forward iterator #[inline] pub fn iter<'a>(&'a self) -> DListIterator<'a, T> { DListIterator{nelem: self.len(), head: &self.list_head, tail: self.list_tail} } /// Provide a reverse iterator #[inline] pub fn rev_iter<'a>(&'a self) -> InvertIterator<&'a T, DListIterator<'a, T>> { self.iter().invert() } /// Provide a forward iterator with mutable references #[inline] pub fn mut_iter<'a>(&'a mut self) -> MutDListIterator<'a, T> { let head_raw = match self.list_head { Some(ref mut h) => Rawlink::some(*h), None => Rawlink::none(), }; MutDListIterator{ nelem: self.len(), head: head_raw, tail: self.list_tail, list: self } } /// Provide a reverse iterator with mutable references #[inline] pub fn mut_rev_iter<'a>(&'a mut self) -> InvertIterator<&'a mut T, MutDListIterator<'a, T>> { self.mut_iter().invert() } /// Consume the list into an iterator yielding elements by value #[inline] pub fn consume_iter(self) -> ConsumeIterator { ConsumeIterator{list: self} } /// Consume the list into an iterator yielding elements by value, in reverse #[inline] pub fn consume_rev_iter(self) -> InvertIterator> { self.consume_iter().invert() } } impl DList { /// Insert `elt` sorted in ascending order /// /// O(N) #[inline] pub fn insert_ordered(&mut self, elt: T) { self.insert_when(elt, |a, b| a.cmp(b) != cmp::Less); } } impl<'self, A> Iterator<&'self A> for DListIterator<'self, A> { #[inline] fn next(&mut self) -> Option<&'self A> { if self.nelem == 0 { return None; } do self.head.map |head| { self.nelem -= 1; self.head = &head.next; &head.value } } #[inline] fn size_hint(&self) -> (uint, Option) { (self.nelem, Some(self.nelem)) } } impl<'self, A> DoubleEndedIterator<&'self A> for DListIterator<'self, A> { #[inline] fn next_back(&mut self) -> Option<&'self A> { if self.nelem == 0 { return None; } do self.tail.resolve().map_consume |prev| { self.nelem -= 1; self.tail = prev.prev; &prev.value } } } impl<'self, A> Iterator<&'self mut A> for MutDListIterator<'self, A> { #[inline] fn next(&mut self) -> Option<&'self mut A> { if self.nelem == 0 { return None; } do self.head.resolve().map_consume |next| { self.nelem -= 1; self.head = match next.next { Some(ref mut node) => Rawlink::some(&mut **node), None => Rawlink::none(), }; &mut next.value } } #[inline] fn size_hint(&self) -> (uint, Option) { (self.nelem, Some(self.nelem)) } } impl<'self, A> DoubleEndedIterator<&'self mut A> for MutDListIterator<'self, A> { #[inline] fn next_back(&mut self) -> Option<&'self mut A> { if self.nelem == 0 { return None; } do self.tail.resolve().map_consume |prev| { self.nelem -= 1; self.tail = prev.prev; &mut prev.value } } } /// Allow mutating the DList while iterating pub trait ListInsertion { /// Insert `elt` just after to the most recently yielded element fn insert_next(&mut self, elt: A); /// Provide a reference to the next element, without changing the iterator fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>; } // private methods for MutDListIterator impl<'self, A> MutDListIterator<'self, A> { fn insert_next_node(&mut self, mut ins_node: ~Node) { // Insert before `self.head` so that it is between the // previously yielded element and self.head. // // The inserted node will not appear in further iteration. match self.head.resolve() { None => { self.list.push_back_node(ins_node); } Some(node) => { let prev_node = match node.prev.resolve() { None => return self.list.push_front_node(ins_node), Some(prev) => prev, }; let node_own = prev_node.next.take_unwrap(); ins_node.next = link_with_prev(node_own, Rawlink::some(ins_node)); prev_node.next = link_with_prev(ins_node, Rawlink::some(prev_node)); self.list.length += 1; } } } } impl<'self, A> ListInsertion for MutDListIterator<'self, A> { #[inline] fn insert_next(&mut self, elt: A) { self.insert_next_node(~Node::new(elt)) } #[inline] fn peek_next<'a>(&'a mut self) -> Option<&'a mut A> { self.head.resolve().map_consume(|head| &mut head.value) } } impl Iterator for ConsumeIterator { #[inline] fn next(&mut self) -> Option { self.list.pop_front() } #[inline] fn size_hint(&self) -> (uint, Option) { (self.list.length, Some(self.list.length)) } } impl DoubleEndedIterator for ConsumeIterator { #[inline] fn next_back(&mut self) -> Option { self.list.pop_back() } } impl> FromIterator for DList { fn from_iterator(iterator: &mut T) -> DList { let mut ret = DList::new(); for iterator.advance |elt| { ret.push_back(elt); } ret } } impl Eq for DList { fn eq(&self, other: &DList) -> bool { self.len() == other.len() && self.iter().zip(other.iter()).all(|(a, b)| a.eq(b)) } #[inline] fn ne(&self, other: &DList) -> bool { !self.eq(other) } } impl Clone for DList { fn clone(&self) -> DList { self.iter().transform(|x| x.clone()).collect() } } #[cfg(test)] pub fn check_links(list: &DList) { let mut len = 0u; let mut last_ptr: Option<&Node> = None; let mut node_ptr: &Node; match list.list_head { None => { assert_eq!(0u, list.length); return } Some(ref node) => node_ptr = &**node, } loop { match (last_ptr, node_ptr.prev.resolve_immut()) { (None , None ) => {} (None , _ ) => fail!("prev link for list_head"), (Some(p), Some(pptr)) => { assert_eq!(p as *Node, pptr as *Node); } _ => fail!("prev link is none, not good"), } match node_ptr.next { Some(ref next) => { last_ptr = Some(node_ptr); node_ptr = &**next; len += 1; } None => { len += 1; break; } } } assert_eq!(len, list.length); } #[cfg(test)] mod tests { use super::*; use std::rand; use std::int; use extra::test; #[test] fn test_basic() { let mut m = DList::new::<~int>(); assert_eq!(m.pop_front(), None); assert_eq!(m.pop_back(), None); assert_eq!(m.pop_front(), None); m.push_front(~1); assert_eq!(m.pop_front(), Some(~1)); m.push_back(~2); m.push_back(~3); assert_eq!(m.len(), 2); assert_eq!(m.pop_front(), Some(~2)); assert_eq!(m.pop_front(), Some(~3)); assert_eq!(m.len(), 0); assert_eq!(m.pop_front(), None); m.push_back(~1); m.push_back(~3); m.push_back(~5); m.push_back(~7); assert_eq!(m.pop_front(), Some(~1)); let mut n = DList::new(); n.push_front(2); n.push_front(3); { assert_eq!(n.front().unwrap(), &3); let x = n.front_mut().unwrap(); assert_eq!(*x, 3); *x = 0; } { assert_eq!(n.back().unwrap(), &2); let y = n.back_mut().unwrap(); assert_eq!(*y, 2); *y = 1; } assert_eq!(n.pop_front(), Some(0)); assert_eq!(n.pop_front(), Some(1)); } #[cfg(test)] fn generate_test() -> DList { list_from(&[0,1,2,3,4,5,6]) } #[cfg(test)] fn list_from(v: &[T]) -> DList { v.iter().transform(|x| (*x).clone()).collect() } #[test] fn test_append() { { let mut m = DList::new(); let mut n = DList::new(); n.push_back(2); m.append(n); assert_eq!(m.len(), 1); assert_eq!(m.pop_back(), Some(2)); check_links(&m); } { let mut m = DList::new(); let n = DList::new(); m.push_back(2); m.append(n); assert_eq!(m.len(), 1); assert_eq!(m.pop_back(), Some(2)); check_links(&m); } let v = ~[1,2,3,4,5]; let u = ~[9,8,1,2,3,4,5]; let mut m = list_from(v); m.append(list_from(u)); check_links(&m); let sum = v + u; assert_eq!(sum.len(), m.len()); for sum.consume_iter().advance |elt| { assert_eq!(m.pop_front(), Some(elt)) } } #[test] fn test_prepend() { { let mut m = DList::new(); let mut n = DList::new(); n.push_back(2); m.prepend(n); assert_eq!(m.len(), 1); assert_eq!(m.pop_back(), Some(2)); check_links(&m); } let v = ~[1,2,3,4,5]; let u = ~[9,8,1,2,3,4,5]; let mut m = list_from(v); m.prepend(list_from(u)); check_links(&m); let sum = u + v; assert_eq!(sum.len(), m.len()); for sum.consume_iter().advance |elt| { assert_eq!(m.pop_front(), Some(elt)) } } #[test] fn test_rotate() { let mut n = DList::new::(); n.rotate_to_back(); check_links(&n); assert_eq!(n.len(), 0); n.rotate_to_front(); check_links(&n); assert_eq!(n.len(), 0); let v = ~[1,2,3,4,5]; let mut m = list_from(v); m.rotate_to_back(); check_links(&m); m.rotate_to_front(); check_links(&m); assert_eq!(v.iter().collect::<~[&int]>(), m.iter().collect()); m.rotate_to_front(); check_links(&m); m.rotate_to_front(); check_links(&m); m.pop_front(); check_links(&m); m.rotate_to_front(); check_links(&m); m.rotate_to_back(); check_links(&m); m.push_front(9); check_links(&m); m.rotate_to_front(); check_links(&m); assert_eq!(~[3,9,5,1,2], m.consume_iter().collect()); } #[test] fn test_iterator() { let m = generate_test(); for m.iter().enumerate().advance |(i, elt)| { assert_eq!(i as int, *elt); } let mut n = DList::new(); assert_eq!(n.iter().next(), None); n.push_front(4); let mut it = n.iter(); assert_eq!(it.size_hint(), (1, Some(1))); assert_eq!(it.next().unwrap(), &4); assert_eq!(it.size_hint(), (0, Some(0))); assert_eq!(it.next(), None); } #[test] fn test_iterator_clone() { let mut n = DList::new(); n.push_back(2); n.push_back(3); n.push_back(4); let mut it = n.iter(); it.next(); let mut jt = it.clone(); assert_eq!(it.next(), jt.next()); assert_eq!(it.next_back(), jt.next_back()); assert_eq!(it.next(), jt.next()); } #[test] fn test_iterator_double_end() { let mut n = DList::new(); assert_eq!(n.iter().next(), None); n.push_front(4); n.push_front(5); n.push_front(6); let mut it = n.iter(); assert_eq!(it.size_hint(), (3, Some(3))); assert_eq!(it.next().unwrap(), &6); assert_eq!(it.size_hint(), (2, Some(2))); assert_eq!(it.next_back().unwrap(), &4); assert_eq!(it.size_hint(), (1, Some(1))); assert_eq!(it.next_back().unwrap(), &5); assert_eq!(it.next_back(), None); assert_eq!(it.next(), None); } #[test] fn test_rev_iter() { let m = generate_test(); for m.rev_iter().enumerate().advance |(i, elt)| { assert_eq!((6 - i) as int, *elt); } let mut n = DList::new(); assert_eq!(n.rev_iter().next(), None); n.push_front(4); let mut it = n.rev_iter(); assert_eq!(it.size_hint(), (1, Some(1))); assert_eq!(it.next().unwrap(), &4); assert_eq!(it.size_hint(), (0, Some(0))); assert_eq!(it.next(), None); } #[test] fn test_mut_iter() { let mut m = generate_test(); let mut len = m.len(); for m.mut_iter().enumerate().advance |(i, elt)| { assert_eq!(i as int, *elt); len -= 1; } assert_eq!(len, 0); let mut n = DList::new(); assert!(n.mut_iter().next().is_none()); n.push_front(4); n.push_back(5); let mut it = n.mut_iter(); assert_eq!(it.size_hint(), (2, Some(2))); assert!(it.next().is_some()); assert!(it.next().is_some()); assert_eq!(it.size_hint(), (0, Some(0))); assert!(it.next().is_none()); } #[test] fn test_iterator_mut_double_end() { let mut n = DList::new(); assert!(n.mut_iter().next_back().is_none()); n.push_front(4); n.push_front(5); n.push_front(6); let mut it = n.mut_iter(); assert_eq!(it.size_hint(), (3, Some(3))); assert_eq!(*it.next().unwrap(), 6); assert_eq!(it.size_hint(), (2, Some(2))); assert_eq!(*it.next_back().unwrap(), 4); assert_eq!(it.size_hint(), (1, Some(1))); assert_eq!(*it.next_back().unwrap(), 5); assert!(it.next_back().is_none()); assert!(it.next().is_none()); } #[test] fn test_insert_prev() { let mut m = list_from(&[0,2,4,6,8]); let len = m.len(); { let mut it = m.mut_iter(); it.insert_next(-2); loop { match it.next() { None => break, Some(elt) => { it.insert_next(*elt + 1); match it.peek_next() { Some(x) => assert_eq!(*x, *elt + 2), None => assert_eq!(8, *elt), } } } } it.insert_next(0); it.insert_next(1); } check_links(&m); assert_eq!(m.len(), 3 + len * 2); assert_eq!(m.consume_iter().collect::<~[int]>(), ~[-2,0,1,2,3,4,5,6,7,8,9,0,1]); } #[test] fn test_merge() { let mut m = list_from([0, 1, 3, 5, 6, 7, 2]); let n = list_from([-1, 0, 0, 7, 7, 9]); let len = m.len() + n.len(); m.merge(n, |a, b| a <= b); assert_eq!(m.len(), len); check_links(&m); let res = m.consume_iter().collect::<~[int]>(); assert_eq!(res, ~[-1, 0, 0, 0, 1, 3, 5, 6, 7, 2, 7, 7, 9]); } #[test] fn test_insert_ordered() { let mut n = DList::new(); n.insert_ordered(1); assert_eq!(n.len(), 1); assert_eq!(n.pop_front(), Some(1)); let mut m = DList::new(); m.push_back(2); m.push_back(4); m.insert_ordered(3); check_links(&m); assert_eq!(~[2,3,4], m.consume_iter().collect::<~[int]>()); } #[test] fn test_mut_rev_iter() { let mut m = generate_test(); for m.mut_rev_iter().enumerate().advance |(i, elt)| { assert_eq!((6-i) as int, *elt); } let mut n = DList::new(); assert!(n.mut_rev_iter().next().is_none()); n.push_front(4); let mut it = n.mut_rev_iter(); assert!(it.next().is_some()); assert!(it.next().is_none()); } #[test] fn test_send() { let n = list_from([1,2,3]); do spawn { check_links(&n); assert_eq!(~[&1,&2,&3], n.iter().collect::<~[&int]>()); } } #[test] fn test_eq() { let mut n: DList = list_from([]); let mut m = list_from([]); assert_eq!(&n, &m); n.push_front(1); assert!(n != m); m.push_back(1); assert_eq!(&n, &m); } #[test] fn test_fuzz() { for 25.times { fuzz_test(3); fuzz_test(16); fuzz_test(189); } } #[cfg(test)] fn fuzz_test(sz: int) { let mut m = DList::new::(); let mut v = ~[]; for int::range(0i, sz) |i| { check_links(&m); let r: u8 = rand::random(); match r % 6 { 0 => { m.pop_back(); if v.len() > 0 { v.pop(); } } 1 => { m.pop_front(); if v.len() > 0 { v.shift(); } } 2 | 4 => { m.push_front(-i); v.unshift(-i); } 3 | 5 | _ => { m.push_back(i); v.push(i); } } } check_links(&m); let mut i = 0u; for m.consume_iter().zip(v.iter()).advance |(a, &b)| { i += 1; assert_eq!(a, b); } assert_eq!(i, v.len()); } #[bench] fn bench_collect_into(b: &mut test::BenchHarness) { let v = &[0, ..64]; do b.iter { let _: DList = v.iter().transform(|x| *x).collect(); } } #[bench] fn bench_push_front(b: &mut test::BenchHarness) { let mut m = DList::new::(); do b.iter { m.push_front(0); } } #[bench] fn bench_push_back(b: &mut test::BenchHarness) { let mut m = DList::new::(); do b.iter { m.push_back(0); } } #[bench] fn bench_push_back_pop_back(b: &mut test::BenchHarness) { let mut m = DList::new::(); do b.iter { m.push_back(0); m.pop_back(); } } #[bench] fn bench_push_front_pop_front(b: &mut test::BenchHarness) { let mut m = DList::new::(); do b.iter { m.push_front(0); m.pop_front(); } } #[bench] fn bench_rotate_to_front(b: &mut test::BenchHarness) { let mut m = DList::new::(); m.push_front(0); m.push_front(1); do b.iter { m.rotate_to_front(); } } #[bench] fn bench_rotate_to_back(b: &mut test::BenchHarness) { let mut m = DList::new::(); m.push_front(0); m.push_front(1); do b.iter { m.rotate_to_back(); } } #[bench] fn bench_iter(b: &mut test::BenchHarness) { let v = &[0, ..128]; let m: DList = v.iter().transform(|&x|x).collect(); do b.iter { assert!(m.iter().len_() == 128); } } #[bench] fn bench_iter_mut(b: &mut test::BenchHarness) { let v = &[0, ..128]; let mut m: DList = v.iter().transform(|&x|x).collect(); do b.iter { assert!(m.mut_iter().len_() == 128); } } #[bench] fn bench_iter_rev(b: &mut test::BenchHarness) { let v = &[0, ..128]; let m: DList = v.iter().transform(|&x|x).collect(); do b.iter { assert!(m.rev_iter().len_() == 128); } } #[bench] fn bench_iter_mut_rev(b: &mut test::BenchHarness) { let v = &[0, ..128]; let mut m: DList = v.iter().transform(|&x|x).collect(); do b.iter { assert!(m.mut_rev_iter().len_() == 128); } } }