Skip to content

Commit

Permalink
MultiProduct values: comment with code (1)
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed Jan 30, 2024
1 parent 2cd6f44 commit 515a17b
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/adaptors/multi_product.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![cfg(feature = "use_alloc")]
use Option::{self as State, None as ProductEnded, Some as ProductInProgress};
use Option::{self as CurrentItems, None as NotYetPopulated, Some as Populated};

use alloc::vec::Vec;

Expand Down Expand Up @@ -28,8 +29,8 @@ where
{
/// Holds the iterators.
iters: Vec<MultiProductIter<I>>,
/// It is `None` at the beginning then it holds the current item of each iterator.
cur: Option<Vec<I::Item>>,
/// Not populated at the beginning then it holds the current item of each iterator.
cur: CurrentItems<Vec<I::Item>>,
}

impl<I> std::fmt::Debug for MultiProduct<I>
Expand Down Expand Up @@ -63,7 +64,7 @@ where
iters: iters
.map(|i| MultiProductIter::new(i.into_iter()))
.collect(),
cur: None,
cur: NotYetPopulated,
};
MultiProduct(ProductInProgress(inner))
}
Expand Down Expand Up @@ -103,7 +104,7 @@ where
// This fuses the iterator.
let inner = self.0.as_mut()?;
match &mut inner.cur {
Some(values) => {
Populated(values) => {
debug_assert!(!inner.iters.is_empty());
// Find (from the right) a non-finished iterator and
// reset the finished ones encountered.
Expand All @@ -113,15 +114,15 @@ where
return Some(values.clone());
} else {
iter.iter = iter.iter_orig.clone();
// `cur` is not none so the untouched `iter_orig` can not be empty.
// `cur` is populated so the untouched `iter_orig` can not be empty.
*item = iter.iter.next().unwrap();
}
}
self.0 = ProductEnded;
None
}
// Only the first time.
None => {
NotYetPopulated => {
let next: Option<Vec<_>> = inner.iters.iter_mut().map(|i| i.iter.next()).collect();
if next.is_none() || inner.iters.is_empty() {
// This cartesian product had at most one item to generate and now ends.
Expand Down Expand Up @@ -181,7 +182,7 @@ where
size_hint::add(sh, iter.iter.size_hint())
})
} else {
// Since `cur` is some, this cartesian product has started so `iters` is not empty.
// Since it is populated, this cartesian product has started so `iters` is not empty.
unreachable!()
}
}
Expand All @@ -191,7 +192,7 @@ where
fn last(self) -> Option<Self::Item> {
let MultiProductInner { iters, cur } = self.0?;
// Collect the last item of each iterator of the product.
if let Some(values) = cur {
if let Populated(values) = cur {
let mut count = iters.len();
let last = iters
.into_iter()
Expand Down

0 comments on commit 515a17b

Please sign in to comment.