Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize TupleCombinations::fold #775

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions benches/bench1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,17 @@ fn with_position_fold(c: &mut Criterion) {
});
}

fn tuple_combinations_fold(c: &mut Criterion) {
let v = black_box((0..64).collect_vec());
c.bench_function("tuple_combinations fold", move |b| {
b.iter(|| {
v.iter()
.tuple_combinations()
.fold(0, |acc, (a, b, c, d)| acc + *a * *c - *b * *d)
})
});
}

criterion_group!(
benches,
slice_iter,
Expand Down Expand Up @@ -903,6 +914,7 @@ criterion_group!(
permutations_range,
permutations_slice,
with_position_fold,
tuple_combinations_fold,
while_some,
);
criterion_main!(benches);
36 changes: 35 additions & 1 deletion src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,13 @@ where
fn count(self) -> usize {
self.iter.count()
}

fn fold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
self.iter.fold(init, f)
}
}

impl<I, T> FusedIterator for TupleCombinations<I, T>
Expand Down Expand Up @@ -684,6 +691,13 @@ impl<I: Iterator> Iterator for Tuple1Combination<I> {
fn count(self) -> usize {
self.iter.count()
}

fn fold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
self.iter.map(|x| (x,)).fold(init, f)
}
}

impl<I: Iterator> HasCombination<I> for (I::Item,) {
Expand Down Expand Up @@ -717,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 @@ -747,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),*,)| {
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
let z = item.clone().unwrap();
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
(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),*))
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
.fold(init, &mut f);
}
init
}
}

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