From 4f7a742a067dcd6e8dbcf1da2bbcf203147562cf Mon Sep 17 00:00:00 2001 From: Jed Estep Date: Tue, 25 Jun 2013 15:08:47 -0400 Subject: [PATCH] deque iterator --- src/libextra/deque.rs | 95 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/src/libextra/deque.rs b/src/libextra/deque.rs index c8bb984736a..2aacf908982 100644 --- a/src/libextra/deque.rs +++ b/src/libextra/deque.rs @@ -153,6 +153,101 @@ pub fn reserve(&mut self, n: uint) { pub fn reserve_at_least(&mut self, n: uint) { vec::reserve_at_least(&mut self.elts, n); } + + /// Front-to-back iterator. + pub fn iter<'a>(&'a self) -> DequeIterator<'a, T> { + DequeIterator { iter: self.elts.iter() } + } + + /// Front-to-back iterator which returns mutable values. + pub fn mut_iter<'a>(&'a mut self) -> DequeMutIterator<'a, T> { + DequeMutIterator { iter: self.elts.mut_iter() } + } + + /// Back-to-front iterator. + pub fn rev_iter<'a>(&'a self) -> DequeRevIterator<'a, T> { + DequeRevIterator { iter: self.elts.rev_iter() } + } + + /// Back-to-front iterator which returns mutable values. + pub fn mut_rev_iter<'a>(&'a mut self) -> DequeMutRevIterator<'a, T> { + DequeMutRevIterator { iter: self.elts.mut_rev_iter() } + } +} + +/// Deque iterator +pub struct DequeIterator<'self, T> { + priv iter: vec::VecIterator<'self, Option> +} + +/// Deque reverse iterator +pub struct DequeRevIterator<'self, T> { + priv iter: vec::VecRevIterator<'self, Option> +} +/// Deque mutable iterator +pub struct DequeMutIterator<'self, T> { + priv iter: vec::VecMutIterator<'self, Option> +} + +/// Deque mutable reverse iterator +pub struct DequeMutRevIterator<'self, T> { + priv iter: vec::VecMutRevIterator<'self, Option> +} + +/// Iterator visiting elements of the deque from front to back +impl<'self, T> Iterator<&'self T> for DequeIterator<'self, T> { + #[inline] + fn next(&mut self) -> Option<&'self T> { + for self.iter.advance |elt| { + match elt { + &Some(ref e) => return Some(e), + &None => {}, + } + } + None + } +} + +/// Iterator visiting elements of the deque mutably from front to back +impl<'self, T> Iterator<&'self mut T> for DequeMutIterator<'self, T> { + #[inline] + fn next(&mut self) -> Option<&'self mut T> { + for self.iter.advance |elt| { + match elt { + &Some(ref mut e) => return Some(e), + &None => {}, + } + } + None + } +} + +/// Iterator visiting elements of the deque from back to front +impl<'self, T> Iterator<&'self T> for DequeRevIterator<'self, T> { + #[inline] + fn next(&mut self) -> Option<&'self T> { + for self.iter.advance |elt| { + match elt { + &Some(ref e) => return Some(e), + &None => {}, + } + } + None + } +} + +/// Iterator visiting elements of the deque mutably from back to front +impl<'self, T> Iterator<&'self mut T> for DequeMutRevIterator<'self, T> { + #[inline] + fn next(&mut self) -> Option<&'self mut T> { + for self.iter.advance |elt| { + match elt { + &Some(ref mut e) => return Some(e), + &None => {}, + } + } + None + } } /// Grow is only called on full elts, so nelts is also len(elts), unlike