Skip to content

Commit

Permalink
MultiProduct: ends after the nullary product
Browse files Browse the repository at this point in the history
While adding comments, I realized I could set the iterator as finished in the case of the nullary cartesian product.
It adds a simple invariant. I thought of making a comment but a debug check is better.
  • Loading branch information
Philippe-Cholet committed Jan 8, 2024
1 parent cfa8a51 commit 0c23808
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/adaptors/multi_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ where
let inner = self.0.as_mut()?;
match &mut inner.cur {
Some(values) => {
debug_assert!(!inner.iters.is_empty());
for (iter, item) in inner.iters.iter_mut().zip(values.iter_mut()).rev() {
if let Some(new) = iter.iter.next() {
*item = new;
Expand All @@ -122,10 +123,11 @@ where
// Only the first time.
None => {
let next: Option<Vec<_>> = inner.iters.iter_mut().map(|i| i.iter.next()).collect();
if next.is_some() {
inner.cur = next.clone();
} else {
if next.is_none() || inner.iters.is_empty() {
// This cartesian product had at most one item to generate and now ends.
self.0 = None;
} else {
inner.cur = next.clone();
}
next
}
Expand Down Expand Up @@ -175,7 +177,8 @@ where
size_hint::add(sh, iter.iter.size_hint())
})
} else {
(0, Some(0))
// Since `cur` is some, this cartesian product has started so `iters` is not empty.
unreachable!()
}
}
}
Expand Down

0 comments on commit 0c23808

Please sign in to comment.