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

Powerset::fold specialization #765

Merged
merged 3 commits into from
Sep 28, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions benches/powerset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ fn powerset_n(c: &mut Criterion, n: usize) {
});
}

fn powerset_n_fold(c: &mut Criterion, n: usize) {
let id = format!("powerset {} fold", n);
c.bench_function(id.as_str(), move |b| {
b.iter(|| {
for _ in 0..calc_iters(n) {
(0..n).powerset().fold(0, |s, elt| s + black_box(elt).len());
}
})
});
}

fn powerset_0(c: &mut Criterion) {
powerset_n(c, 0);
}
Expand All @@ -44,6 +55,30 @@ fn powerset_12(c: &mut Criterion) {
powerset_n(c, 12);
}

fn powerset_0_fold(c: &mut Criterion) {
powerset_n_fold(c, 0);
}

fn powerset_1_fold(c: &mut Criterion) {
powerset_n_fold(c, 1);
}

fn powerset_2_fold(c: &mut Criterion) {
powerset_n_fold(c, 2);
}

fn powerset_4_fold(c: &mut Criterion) {
powerset_n_fold(c, 4);
}

fn powerset_8_fold(c: &mut Criterion) {
powerset_n_fold(c, 8);
}

fn powerset_12_fold(c: &mut Criterion) {
powerset_n_fold(c, 12);
}

criterion_group!(
benches,
powerset_0,
Expand All @@ -52,5 +87,11 @@ criterion_group!(
powerset_4,
powerset_8,
powerset_12,
powerset_0_fold,
powerset_1_fold,
powerset_2_fold,
powerset_4_fold,
powerset_8_fold,
powerset_12_fold,
);
criterion_main!(benches);
18 changes: 18 additions & 0 deletions src/powerset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,24 @@ where
let (n, combs_count) = self.combs.n_and_count();
combs_count + remaining_for(n, k).unwrap()
}

fn fold<B, F>(self, mut init: B, mut f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
let mut it = self.combs;
if it.k() == 0 {
init = it.by_ref().fold(init, &mut f);
it.reset(1);
}
init = it.by_ref().fold(init, &mut f);
// n is now known for sure because k >= 1 and all k-combinations have been generated.
for k in it.k() + 1..=it.n() {
it.reset(k);
init = it.by_ref().fold(init, &mut f);
}
init
}
}

impl<I> FusedIterator for Powerset<I>
Expand Down
6 changes: 6 additions & 0 deletions tests/specializations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ quickcheck! {
fn intersperse(v: Vec<u8>) -> () {
test_specializations(&v.into_iter().intersperse(0));
}

fn powerset(a: Vec<u8>) -> () {
let mut a = a;
a.truncate(6);
test_specializations(&a.iter().powerset())
}
}

quickcheck! {
Expand Down