-
Notifications
You must be signed in to change notification settings - Fork 340
Infinite Sequence of All Combinations #311
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
base: master
Are you sure you want to change the base?
Changes from all commits
ca3b061
4df912c
a397b3d
c65a4e1
2378381
801657c
e19aed5
2755375
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| /target/ | ||
| Cargo.lock | ||
| Cargo.lock | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /// An iterator to iterate through all product combinations of an iterator. | ||
| /// | ||
| /// See [`.product_combination()`](../trait.Itertools.html#method.product_combination) for more information. | ||
| pub struct ProductCombination<I: Iterator + Clone> { | ||
| data: I, | ||
| //Internal state is a vec of tuples containing an iterator and the last retrieved item from this iterator | ||
| internal_state: Vec<(I, I::Item)> | ||
| } | ||
|
|
||
| /// Create a new `ProductionCombination` from a clonable iterator. | ||
| pub fn product_combination<I>(iter: I) -> ProductCombination<I> | ||
| where I: Iterator + Clone | ||
| { | ||
| ProductCombination { | ||
| data: iter, | ||
| internal_state: Vec::new(), | ||
| } | ||
| } | ||
|
|
||
| impl<I: Iterator + Clone> ProductCombination<I> { | ||
| fn increment(&mut self) { | ||
| let mut pointer: usize = 0; | ||
| let mut tack_on = false; | ||
| loop { | ||
| match self.internal_state.get_mut(pointer) { | ||
| None => { | ||
| tack_on = true; | ||
| break; | ||
| }, | ||
| Some(ref mut pair) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can't use default match binding in Itertools since it's still on Rust 1.12, but the tuple deref/item re-ref can still be done within the pattern match: Some(&mut (ref mut iter, ref mut item)) => { |
||
| let iter = &mut pair.0; | ||
| let item = &mut pair.1; | ||
|
|
||
| let new_item = iter.next(); | ||
| match new_item { | ||
| //Reached the end of the iterator ... restart the iterator and advance the pointer | ||
| None => { | ||
| *iter = self.data.clone(); | ||
| match iter.next() { | ||
| None => { | ||
| panic!("0 sized iterator"); | ||
| }, | ||
| Some(i) => { | ||
| *item = i; | ||
| } | ||
| } | ||
| pointer += 1; | ||
| }, | ||
| Some(i) => { | ||
| *item = i; | ||
| break; | ||
| } | ||
| } | ||
| }, | ||
| } | ||
| } | ||
| if tack_on { | ||
| let mut new_iter = self.data.clone(); | ||
| match new_iter.next() { | ||
| None => { | ||
| panic!("0 sized iterator"); | ||
| }, | ||
| Some(new_item) => { | ||
| self.internal_state.push((new_iter, new_item)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<I> Iterator for ProductCombination<I> | ||
| where I: Iterator + Clone, I::Item: Clone | ||
| { | ||
| type Item = Vec<I::Item>; | ||
|
|
||
| fn next(&mut self) -> Option<Self::Item> { | ||
| self.increment(); | ||
| Some(self.internal_state.iter().map(|ref pair| pair.1.clone()).collect()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The item yielded by .map(|&(_, ref item)| item.clone()) |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you re-add the final line break, you can remove this file from the diff entirely (or just
git checkout master -- .gitignore)