|
| 1 | +use std::cmp; |
| 2 | +use self::EitherOrBoth::{Right, Left, Both}; |
| 3 | + |
| 4 | +// ZipLongest originally written by SimonSapin, |
| 5 | +// and dedicated to itertools https://github.com/rust-lang/rust/pull/19283 |
| 6 | + |
| 7 | +/// An iterator which iterates two other iterators simultaneously |
| 8 | +#[deriving(Clone)] |
| 9 | +#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] |
| 10 | +pub struct ZipLongest<T, U> { |
| 11 | + a: T, |
| 12 | + b: U |
| 13 | +} |
| 14 | + |
| 15 | +impl<T, U> ZipLongest<T, U> |
| 16 | +{ |
| 17 | + /// Create a new ZipLongest iterator. |
| 18 | + pub fn new(a: T, b: U) -> ZipLongest<T, U> |
| 19 | + { |
| 20 | + ZipLongest{a: a, b: b} |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +impl<A, B, T: Iterator<A>, U: Iterator<B>> Iterator<EitherOrBoth<A, B>> for ZipLongest<T, U> { |
| 25 | + #[inline] |
| 26 | + fn next(&mut self) -> Option<EitherOrBoth<A, B>> { |
| 27 | + match (self.a.next(), self.b.next()) { |
| 28 | + (None, None) => None, |
| 29 | + (Some(a), None) => Some(Left(a)), |
| 30 | + (None, Some(b)) => Some(Right(b)), |
| 31 | + (Some(a), Some(b)) => Some(Both(a, b)), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + #[inline] |
| 36 | + fn size_hint(&self) -> (uint, Option<uint>) { |
| 37 | + let (a_lower, a_upper) = self.a.size_hint(); |
| 38 | + let (b_lower, b_upper) = self.b.size_hint(); |
| 39 | + |
| 40 | + let lower = cmp::max(a_lower, b_lower); |
| 41 | + |
| 42 | + let upper = match (a_upper, b_upper) { |
| 43 | + (Some(x), Some(y)) => Some(cmp::max(x,y)), |
| 44 | + _ => None |
| 45 | + }; |
| 46 | + |
| 47 | + (lower, upper) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl<A, B, T: ExactSizeIterator<A>, U: ExactSizeIterator<B>> DoubleEndedIterator<EitherOrBoth<A, B>> |
| 52 | +for ZipLongest<T, U> { |
| 53 | + #[inline] |
| 54 | + fn next_back(&mut self) -> Option<EitherOrBoth<A, B>> { |
| 55 | + use std::cmp::{Equal, Greater, Less}; |
| 56 | + match self.a.len().cmp(&self.b.len()) { |
| 57 | + Equal => match (self.a.next_back(), self.b.next_back()) { |
| 58 | + (None, None) => None, |
| 59 | + (Some(a), Some(b)) => Some(Both(a, b)), |
| 60 | + // These can only happen if .len() is inconsistent with .next_back() |
| 61 | + (Some(a), None) => Some(Left(a)), |
| 62 | + (None, Some(b)) => Some(Right(b)), |
| 63 | + }, |
| 64 | + Greater => self.a.next_back().map(Left), |
| 65 | + Less => self.b.next_back().map(Right), |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<A, B, T: RandomAccessIterator<A>, U: RandomAccessIterator<B>> |
| 71 | +RandomAccessIterator<EitherOrBoth<A, B>> for ZipLongest<T, U> { |
| 72 | + #[inline] |
| 73 | + fn indexable(&self) -> uint { |
| 74 | + cmp::max(self.a.indexable(), self.b.indexable()) |
| 75 | + } |
| 76 | + |
| 77 | + #[inline] |
| 78 | + fn idx(&mut self, index: uint) -> Option<EitherOrBoth<A, B>> { |
| 79 | + match (self.a.idx(index), self.b.idx(index)) { |
| 80 | + (None, None) => None, |
| 81 | + (Some(a), None) => Some(Left(a)), |
| 82 | + (None, Some(b)) => Some(Right(b)), |
| 83 | + (Some(a), Some(b)) => Some(Both(a, b)), |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +#[unstable = "trait is unstable"] |
| 89 | +impl<A, B, T, U> ExactSizeIterator<EitherOrBoth<A, B>> for ZipLongest<T, U> |
| 90 | + where T: ExactSizeIterator<A>, U: ExactSizeIterator<B> {} |
| 91 | + |
| 92 | + |
| 93 | +/// A value yielded by `ZipLongest`. |
| 94 | +/// Contains one or two values, |
| 95 | +/// depending on which of the input iterators are exhausted. |
| 96 | +#[deriving(Clone, PartialEq, Eq, Show)] |
| 97 | +pub enum EitherOrBoth<A, B> { |
| 98 | + /// Neither input iterator is exhausted yet, yielding two values. |
| 99 | + Both(A, B), |
| 100 | + /// The parameter iterator of `.zip_longest()` is exhausted, |
| 101 | + /// only yielding a value from the `self` iterator. |
| 102 | + Left(A), |
| 103 | + /// The `self` iterator of `.zip_longest()` is exhausted, |
| 104 | + /// only yielding a value from the parameter iterator. |
| 105 | + Right(B), |
| 106 | +} |
0 commit comments