Skip to content

Commit

Permalink
Update CombinationsWithReplacement::next (1)
Browse files Browse the repository at this point in the history
Use the new `increment_indices`. This is done in a different commit because the git difference was difficult to read.
  • Loading branch information
Philippe-Cholet committed Apr 26, 2024
1 parent 21907bd commit cd1ed15
Showing 1 changed file with 4 additions and 24 deletions.
28 changes: 4 additions & 24 deletions src/combinations_with_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ where
I::Item: Clone,
{
type Item = Vec<I::Item>;

fn next(&mut self) -> Option<Self::Item> {
// If this is the first iteration, return early
if self.first {
Expand All @@ -105,32 +106,11 @@ where
};
}

// Check if we need to consume more from the iterator
// This will run while we increment our first index digit
self.pool.get_next();

// Work out where we need to update our indices
let mut increment: Option<(usize, usize)> = None;
for (i, indices_int) in self.indices.iter().enumerate().rev() {
if *indices_int < self.pool.len() - 1 {
increment = Some((i, indices_int + 1));
break;
}
if self.increment_indices() {
return None;
}

match increment {
// If we can update the indices further
Some((increment_from, increment_value)) => {
// We need to update the rightmost non-max value
// and all those to the right
for indices_index in increment_from..self.indices.len() {
self.indices[indices_index] = increment_value;
}
Some(self.pool.get_at(&self.indices))
}
// Otherwise, we're done
None => None,
}
Some(self.pool.get_at(&self.indices))
}

fn size_hint(&self) -> (usize, Option<usize>) {
Expand Down

0 comments on commit cd1ed15

Please sign in to comment.