Skip to content

Commit

Permalink
Update CombinationsWithReplacement::next (2)
Browse files Browse the repository at this point in the history
Use `pool.get_at` once! That way, `next` and `nth` will ressemble each other.
  • Loading branch information
Philippe-Cholet committed Apr 26, 2024
1 parent cd1ed15 commit cb06691
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions src/combinations_with_replacement.rs
Expand Up @@ -94,22 +94,15 @@ where
type Item = Vec<I::Item>;

fn next(&mut self) -> Option<Self::Item> {
// If this is the first iteration, return early
if self.first {
// In empty edge cases, stop iterating immediately
return if !(self.indices.is_empty() || self.pool.get_next()) {
None
// Otherwise, yield the initial state
} else {
self.first = false;
Some(self.pool.get_at(&self.indices))
};
}

if self.increment_indices() {
if !(self.indices.is_empty() || self.pool.get_next()) {
return None;
}
self.first = false;
} else if self.increment_indices() {
return None;
}

Some(self.pool.get_at(&self.indices))
}

Expand Down

0 comments on commit cb06691

Please sign in to comment.