1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use alloc::vec::Vec;
use std::fmt;
use std::iter::FusedIterator;
use std::usize;

use super::combinations::{combinations, Combinations};
use crate::adaptors::checked_binomial;
use crate::size_hint::{self, SizeHint};

/// An iterator to iterate through the powerset of the elements from an iterator.
///
/// See [`.powerset()`](crate::Itertools::powerset) for more
/// information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Powerset<I: Iterator> {
    combs: Combinations<I>,
}

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

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

/// Create a new `Powerset` from a clonable iterator.
pub fn powerset<I>(src: I) -> Powerset<I>
where
    I: Iterator,
    I::Item: Clone,
{
    Powerset {
        combs: combinations(src, 0),
    }
}

impl<I> Iterator for Powerset<I>
where
    I: Iterator,
    I::Item: Clone,
{
    type Item = Vec<I::Item>;

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

    fn size_hint(&self) -> SizeHint {
        let k = self.combs.k();
        // Total bounds for source iterator.
        let (n_min, n_max) = self.combs.src().size_hint();
        let low = remaining_for(n_min, k).unwrap_or(usize::MAX);
        let upp = n_max.and_then(|n| remaining_for(n, k));
        size_hint::add(self.combs.size_hint(), (low, upp))
    }

    fn count(self) -> usize {
        let k = self.combs.k();
        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>
where
    I: Iterator,
    I::Item: Clone,
{
}

fn remaining_for(n: usize, k: usize) -> Option<usize> {
    (k + 1..=n).try_fold(0usize, |sum, i| sum.checked_add(checked_binomial(n, i)?))
}