-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An iterator adapter that produces vectors containing all subsets of input iterator elements.
- Loading branch information
Showing
7 changed files
with
297 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
extern crate itertools; | ||
|
||
use test::{black_box, Bencher}; | ||
use itertools::Itertools; | ||
|
||
// Keep aggregate generated elements the same, regardless of powerset length. | ||
const TOTAL_ELEMENTS: usize = 1 << 16; | ||
const fn calc_iters(n: usize) -> usize { | ||
TOTAL_ELEMENTS / (1 << n) | ||
} | ||
|
||
fn pset_n(b: &mut Bencher, n: usize) { | ||
b.iter(|| { | ||
for _ in 0..calc_iters(n) { | ||
for elt in (0..n).powerset() { | ||
black_box(elt); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
#[bench] | ||
fn pset_00(b: &mut Bencher) { pset_n(b, 0); } | ||
|
||
#[bench] | ||
fn pset_01(b: &mut Bencher) { pset_n(b, 1); } | ||
|
||
#[bench] | ||
fn pset_02(b: &mut Bencher) { pset_n(b, 2); } | ||
|
||
#[bench] | ||
fn pset_03(b: &mut Bencher) { pset_n(b, 3); } | ||
|
||
#[bench] | ||
fn pset_04(b: &mut Bencher) { pset_n(b, 4); } | ||
|
||
#[bench] | ||
fn pset_05(b: &mut Bencher) { pset_n(b, 5); } | ||
|
||
#[bench] | ||
fn pset_06(b: &mut Bencher) { pset_n(b, 6); } | ||
|
||
#[bench] | ||
fn pset_07(b: &mut Bencher) { pset_n(b, 7); } | ||
|
||
#[bench] | ||
fn pset_08(b: &mut Bencher) { pset_n(b, 8); } | ||
|
||
#[bench] | ||
fn pset_09(b: &mut Bencher) { pset_n(b, 9); } | ||
|
||
#[bench] | ||
fn pset_10(b: &mut Bencher) { pset_n(b, 10); } | ||
|
||
#[bench] | ||
fn pset_11(b: &mut Bencher) { pset_n(b, 11); } | ||
|
||
#[bench] | ||
fn pset_12(b: &mut Bencher) { pset_n(b, 12); } | ||
|
||
#[bench] | ||
fn pset_13(b: &mut Bencher) { pset_n(b, 13); } | ||
|
||
#[bench] | ||
fn pset_14(b: &mut Bencher) { pset_n(b, 14); } | ||
|
||
#[bench] | ||
fn pset_15(b: &mut Bencher) { pset_n(b, 15); } | ||
|
||
#[bench] | ||
fn pset_16(b: &mut Bencher) { pset_n(b, 16); } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use std::vec::IntoIter; | ||
use std::fmt; | ||
use std::usize; | ||
use super::combinations::{self, 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<IntoIter<<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, | ||
I::Item: Clone | ||
{ | ||
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 iter = self.buf.clone().into_iter(); | ||
let mut combs = combinations::combinations(iter, 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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters