Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An iterator to iterate through the powerset of the elements from an iterator.
- Loading branch information
Showing
6 changed files
with
179 additions
and
13 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
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,56 @@ | ||
use std::fmt; | ||
use alloc::vec::Vec; | ||
|
||
use super::combinations::Combinations; | ||
|
||
/// An iterator to iterate through the powerset of the elements from an 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> { | ||
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::new(src, 0) } | ||
} | ||
|
||
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>> { | ||
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 | ||
} | ||
} | ||
} |
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