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
30 changes: 3 additions & 27 deletions src/powerset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::usize;
use alloc::vec::Vec;

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 All @@ -13,22 +12,20 @@ use super::size_hint;
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Powerset<I: Iterator> {
combs: Combinations<I>,
// Iterator `position` (equal to count of yielded elements).
pos: usize,
}

impl<I> Clone for Powerset<I>
where I: Clone + Iterator,
I::Item: Clone,
{
clone_fields!(combs, pos);
clone_fields!(combs);
}

impl<I> fmt::Debug for Powerset<I>
where I: Iterator + fmt::Debug,
I::Item: fmt::Debug,
{
debug_fmt_fields!(Powerset, combs, pos);
debug_fmt_fields!(Powerset, combs);
}

/// Create a new `Powerset` from a clonable iterator.
Expand All @@ -38,7 +35,6 @@ pub fn powerset<I>(src: I) -> Powerset<I>
{
Powerset {
combs: combinations(src, 0),
pos: 0,
}
}

Expand All @@ -51,37 +47,17 @@ impl<I> Iterator for Powerset<I>

fn next(&mut self) -> Option<Self::Item> {
if let Some(elt) = self.combs.next() {
self.pos = self.pos.saturating_add(1);
Some(elt)
} else if self.combs.k() < self.combs.n()
|| self.combs.k() == 0
{
self.combs.reset(self.combs.k() + 1);
self.combs.next().map(|elt| {
self.pos = self.pos.saturating_add(1);
elt
})
self.combs.next()
} else {
None
}
}

fn size_hint(&self) -> (usize, Option<usize>) {
// Total bounds for source iterator.
let src_total = self.combs.src().size_hint();

// Total bounds for self ( length(powerset(set) == 2 ^ length(set) )
let self_total = size_hint::pow_scalar_base(2, src_total);

if self.pos < usize::MAX {
// Subtract count of elements already yielded from total.
size_hint::sub_scalar(self_total, self.pos)
} else {
// Fallback: self.pos is saturated and no longer reliable.
(0, self_total.1)
}
}

fn count(self) -> usize {
let k = self.combs.k();
let (n, combs_count) = self.combs.n_and_count();
Expand Down
1 change: 1 addition & 0 deletions src/size_hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub fn mul_scalar(sh: SizeHint, x: usize) -> SizeHint {

/// Raise `base` correctly by a `SizeHint` exponent.
#[inline]
#[allow(dead_code)]
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
pub fn pow_scalar_base(base: usize, exp: SizeHint) -> SizeHint {
let exp_low = cmp::min(exp.0, u32::MAX as usize) as u32;
let low = base.saturating_pow(exp_low);
Expand Down