Skip to content

Commit

Permalink
Tuple*Combinations::fold (macro)
Browse files Browse the repository at this point in the history
`I::Item` and `A` are the same so the "where condition" is not really changed, but it is apparently needed here.
We fold `c` then for each item of `iter`, we fold the cloned `iter`.
The `while let` loop should probably be converted to `fold` somehow later (if possible) but the core logic is done and it is already really faster.
  • Loading branch information
Philippe-Cholet committed Oct 24, 2023
1 parent b743e2a commit 21004ed
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ macro_rules! impl_tuple_combination {

impl<I, A> Iterator for $C<I>
where I: Iterator<Item = A> + Clone,
I::Item: Clone
A: Clone,
{
type Item = (A, $(ignore_ident!($X, A)),*);

Expand Down Expand Up @@ -761,6 +761,26 @@ macro_rules! impl_tuple_combination {
let n = self.iter.count();
checked_binomial(n, K).unwrap() + self.c.count()
}

fn fold<B, F>(self, mut init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
let Self { c, item, mut iter } = self;
init = c
.map(|($($X),*,)| {
let z = item.clone().unwrap();
(z, $($X),*)
})
.fold(init, &mut f);
while let Some(z) = iter.next() {
let c: $P<I> = iter.clone().into();
init = c
.map(|($($X),*,)| (z.clone(), $($X),*))
.fold(init, &mut f);
}
init
}
}

impl<I, A> HasCombination<I> for (A, $(ignore_ident!($X, A)),*)
Expand Down

0 comments on commit 21004ed

Please sign in to comment.