Skip to content
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

Simplify CombinationsWithReplacement #487

Merged
merged 2 commits into from
Oct 10, 2020

Conversation

phimuemue
Copy link
Member

@phimuemue phimuemue commented Oct 10, 2020

  • Eliminate k: It can be obtained via indices.len.
  • Eliminate max_index: It can be obtained via pool.len()-1.

It is given by indices.len. (Similar to what we do in Combinations.)
Can be inferred from `pool`:

* For `self.indices.len()==0`, the first iteration yields `Some(self.current())`, and afterwards we just go through, unnecessarily call `self.pool.get_next()`, `increment` stays `None` as the loop is not iterated, and we return `None`.
* For `self.indices.len()>0`:
  * If `pool` is empty from the start, `pool.get_next` is always `false` and we will always return `None` (without even setting `first=false`).
  * Otherwise (i.e. if `pool` is non-empty), we return `Some(self.current())` on first it eration, and afterwards always call `pool.get_next`, possibly updating `self.max_index`.
  In the first iteration `max_index` is 0, thus equals `pool.len()-1` when we return `Some(self.current())`.
  Thus, we know that `self.max_index` is equal to `self.pool.len()-1` when we come to the line `let mut increment...`.
@phimuemue phimuemue changed the title Eliminate CombinationsWithReplacement::k Simplify CombinationsWithReplacement Oct 10, 2020
Copy link
Member

@jswrenn jswrenn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 10, 2020

Build succeeded:

@bors bors bot merged commit 14e0964 into rust-itertools:master Oct 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants