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

Specialize CombinationsWithReplacement::nth #923

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 4 additions & 24 deletions src/combinations_with_replacement.rs
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() {
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved
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