summaryrefslogtreecommitdiffstats
path: root/third_party/rust/itertools/src/zip_longest.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/itertools/src/zip_longest.rs
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/itertools/src/zip_longest.rs')
-rw-r--r--third_party/rust/itertools/src/zip_longest.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/third_party/rust/itertools/src/zip_longest.rs b/third_party/rust/itertools/src/zip_longest.rs
new file mode 100644
index 0000000000..cb9a7bacb2
--- /dev/null
+++ b/third_party/rust/itertools/src/zip_longest.rs
@@ -0,0 +1,83 @@
+use std::cmp::Ordering::{Equal, Greater, Less};
+use super::size_hint;
+use std::iter::{Fuse, FusedIterator};
+
+use crate::either_or_both::EitherOrBoth;
+
+// ZipLongest originally written by SimonSapin,
+// and dedicated to itertools https://github.com/rust-lang/rust/pull/19283
+
+/// An iterator which iterates two other iterators simultaneously
+///
+/// This iterator is *fused*.
+///
+/// See [`.zip_longest()`](crate::Itertools::zip_longest) for more information.
+#[derive(Clone, Debug)]
+#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
+pub struct ZipLongest<T, U> {
+ a: Fuse<T>,
+ b: Fuse<U>,
+}
+
+/// Create a new `ZipLongest` iterator.
+pub fn zip_longest<T, U>(a: T, b: U) -> ZipLongest<T, U>
+ where T: Iterator,
+ U: Iterator
+{
+ ZipLongest {
+ a: a.fuse(),
+ b: b.fuse(),
+ }
+}
+
+impl<T, U> Iterator for ZipLongest<T, U>
+ where T: Iterator,
+ U: Iterator
+{
+ type Item = EitherOrBoth<T::Item, U::Item>;
+
+ #[inline]
+ fn next(&mut self) -> Option<Self::Item> {
+ match (self.a.next(), self.b.next()) {
+ (None, None) => None,
+ (Some(a), None) => Some(EitherOrBoth::Left(a)),
+ (None, Some(b)) => Some(EitherOrBoth::Right(b)),
+ (Some(a), Some(b)) => Some(EitherOrBoth::Both(a, b)),
+ }
+ }
+
+ #[inline]
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ size_hint::max(self.a.size_hint(), self.b.size_hint())
+ }
+}
+
+impl<T, U> DoubleEndedIterator for ZipLongest<T, U>
+ where T: DoubleEndedIterator + ExactSizeIterator,
+ U: DoubleEndedIterator + ExactSizeIterator
+{
+ #[inline]
+ fn next_back(&mut self) -> Option<Self::Item> {
+ match self.a.len().cmp(&self.b.len()) {
+ Equal => match (self.a.next_back(), self.b.next_back()) {
+ (None, None) => None,
+ (Some(a), Some(b)) => Some(EitherOrBoth::Both(a, b)),
+ // These can only happen if .len() is inconsistent with .next_back()
+ (Some(a), None) => Some(EitherOrBoth::Left(a)),
+ (None, Some(b)) => Some(EitherOrBoth::Right(b)),
+ },
+ Greater => self.a.next_back().map(EitherOrBoth::Left),
+ Less => self.b.next_back().map(EitherOrBoth::Right),
+ }
+ }
+}
+
+impl<T, U> ExactSizeIterator for ZipLongest<T, U>
+ where T: ExactSizeIterator,
+ U: ExactSizeIterator
+{}
+
+impl<T, U> FusedIterator for ZipLongest<T, U>
+ where T: Iterator,
+ U: Iterator
+{}