Skip to content

Commit

Permalink
MultiProduct::count
Browse files Browse the repository at this point in the history
`count` is generally not a cheap operation so I avoid it when the result would be zero anyway.
  • Loading branch information
Philippe-Cholet committed Jan 3, 2024
1 parent cf2e73a commit ab2d72a
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/adaptors/multi_product.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,34 @@ where
}
}

fn count(self) -> usize {
match self.0 {
None => 0,
Some(MultiProductInner { iters, cur }) => {
if cur.is_none() {
iters
.into_iter()
.map(|iter| iter.iter_orig.count())
.try_fold(1, |product, count| {
if count == 0 {
None
} else {
Some(product * count)
}
})
.unwrap_or_default()
} else {
iters.into_iter().fold(0, |mut acc, iter| {
if acc != 0 {
acc *= iter.iter_orig.count();
}
acc + iter.iter.count()
})
}
}
}
}

fn last(self) -> Option<Self::Item> {
let MultiProductInner { iters, cur } = self.0?;
if let Some(values) = cur {
Expand Down

0 comments on commit ab2d72a

Please sign in to comment.