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 count #735

Merged
merged 11 commits into from
Aug 26, 2023
8 changes: 7 additions & 1 deletion src/powerset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::iter::FusedIterator;
use std::usize;
use alloc::vec::Vec;

use super::combinations::{Combinations, combinations};
use super::combinations::{Combinations, checked_binomial, combinations};
use super::size_hint;

/// An iterator to iterate through the powerset of the elements from an iterator.
Expand Down Expand Up @@ -81,6 +81,12 @@ impl<I> Iterator for Powerset<I>
(0, self_total.1)
}
}

fn count(self) -> usize {
let k = self.combs.k();
let (n, combs_count) = self.combs.n_and_count();
combs_count + (k + 1..=n).map(|i| checked_binomial(n, i).unwrap()).sum::<usize>()
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl<I> FusedIterator for Powerset<I>
Expand Down