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 6, 2023
1 parent ded12e8 commit 0e15566
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 @@ -715,7 +715,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 @@ -745,6 +745,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 0e15566

Please sign in to comment.