binius_m3/builder/
multi_iter.rs

1// Copyright 2025 Irreducible Inc.
2
3/// An iterator that iterates multiple inner iterators in parallel.
4///
5/// The iterator yields vectors of items from the inner iterators.
6#[derive(Debug)]
7pub struct MultiIterator<T> {
8	entries: Vec<T>,
9}
10
11impl<I: Iterator> MultiIterator<I> {
12	pub fn new(entries: Vec<I>) -> Self {
13		Self { entries }
14	}
15}
16
17impl<I: ExactSizeIterator> Iterator for MultiIterator<I> {
18	type Item = Vec<I::Item>;
19
20	fn next(&mut self) -> Option<Vec<I::Item>> {
21		self.entries.iter_mut().map(Iterator::next).collect()
22	}
23
24	fn size_hint(&self) -> (usize, Option<usize>) {
25		let len = self.len();
26		(len, Some(len))
27	}
28}
29
30impl<I: ExactSizeIterator> ExactSizeIterator for MultiIterator<I> {
31	fn len(&self) -> usize {
32		self.entries
33			.iter()
34			.map(ExactSizeIterator::len)
35			.min()
36			.unwrap_or_default()
37	}
38}
39
40impl<I: ExactSizeIterator + DoubleEndedIterator> DoubleEndedIterator for MultiIterator<I> {
41	fn next_back(&mut self) -> Option<Vec<I::Item>> {
42		self.entries
43			.iter_mut()
44			.map(DoubleEndedIterator::next_back)
45			.collect()
46	}
47}