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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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);
39 changes: 36 additions & 3 deletions 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,19 +731,19 @@ 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)),*);

fn next(&mut self) -> Option<Self::Item> {
if let Some(($($X),*,)) = self.c.next() {
if let Some(($($X,)*)) = self.c.next() {
let z = self.item.clone().unwrap();
Some((z, $($X),*))
} else {
self.item = self.iter.next();
self.item.clone().and_then(|z| {
self.c = self.iter.clone().into();
self.c.next().map(|($($X),*,)| (z, $($X),*))
self.c.next().map(|($($X,)*)| (z, $($X),*))
})
}
}
Expand All @@ -747,6 +761,25 @@ 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;
if let Some(z) = item.as_ref() {
init = c
.map(|($($X,)*)| (z.clone(), $($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