/
powerset.rs
130 lines (113 loc) · 3.76 KB
/
powerset.rs
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::fmt;
use std::usize;
use std::iter::{self, Empty};
use super::combinations::Combinations;
use super::size_hint;
/// An iterator over the powerset of all elements from its source iterator.
///
/// See [`.powerset()`](../trait.Itertools.html#method.powerset) for more information.
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Powerset<I: Iterator> {
src: I,
buf: Vec<I::Item>,
// Only used when once a source containing two or more items is exhausted.
combs: Option<SetCombinations<I>>,
// Used for detecting special cases and size_hint() calculation
pos: usize,
}
type SetCombinations<I> = Combinations<iter::Empty<<I as Iterator>::Item>>;
impl<I> fmt::Debug for Powerset<I>
where I: Iterator + fmt::Debug,
I::Item: fmt::Debug,
{
debug_fmt_fields!(Powerset, src, buf, combs, pos);
}
/// Create a new `Powerset` from a cloneable iterator.
pub fn powerset<I>(src: I) -> Powerset<I>
where I: Iterator
{
let (src_low, _) = src.size_hint();
Powerset {
src,
buf: Vec::with_capacity(src_low),
combs: None,
pos: 0,
}
}
impl<I> Powerset<I>
where
I: Iterator,
I::Item: Clone
{
#[inline]
/// Creates and stores the next Combination, returning the first element or None if the
/// iterator is done.
fn next_from_new_combs(&mut self) -> Option<Vec<I::Item>> {
debug_assert!(self.buf.len() > 1);
let new_len = match &self.combs {
Some(combs) => combs.n() + 1,
None => 2
};
let mut combs = Combinations::<Empty<I::Item>>::from_slice(&self.buf[..], new_len);
let result = combs.next();
self.combs = Some(combs);
result
}
}
impl<I> Iterator for Powerset<I>
where
I: Iterator,
I::Item: Clone
{
type Item = Vec<I::Item>;
fn next(&mut self) -> Option<Vec<I::Item>> {
let mut new_combs = false;
let mut result = match &mut self.combs {
None if self.pos == 0 => {
// First item, return the empty set
Some(Vec::new())
}
None => {
// Still draining from source iterator
if let Some(elt) = self.src.next() {
self.buf.push(elt.clone());
Some(vec!(elt))
} else {
new_combs = self.buf.len() >= 2;
None
}
}
Some(combs) if combs.n() < self.buf.len() => {
// Generating elements from Combinations
match combs.next() {
Some(elt) => Some(elt),
None => {
new_combs = self.buf.len() >= 2;
None
}
}
}
Some(_) => {
// Iteration is done
None
}
};
// Merge these lines into the relevant match arms above when NLLs are available
if new_combs { result = self.next_from_new_combs(); }
if result.is_some() { self.pos = self.pos.saturating_add(1); }
result
}
fn size_hint(&self) -> (usize, Option<usize>) {
// Aggregate bounds for 'unwalked' source iterator
let src_total = size_hint::add_scalar(self.src.size_hint(), self.buf.len());
// Aggregate bounds for self ( length(powerset(set) == 2 ^ length(set) )
let self_total = size_hint::two_raised_pow(src_total);
if self.pos < usize::MAX {
// Subtract count of walked elements from total
size_hint::sub_scalar(self_total, self.pos)
} else {
// self.pos is saturated, no longer reliable
(0, self_total.1)
}
}
}