Skip to content

Commit

Permalink
Specialize CombinationsWithReplacement::nth
Browse files Browse the repository at this point in the history
Similar to `next` except we increment indices n times before generating the vector item.
  • Loading branch information
Philippe-Cholet committed Apr 25, 2024
1 parent a04e0bc commit a250651
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/combinations_with_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ where
Some(self.pool.get_at(&self.indices))
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
if self.first {
// In empty edge cases, stop iterating immediately
if !(self.indices.is_empty() || self.pool.get_next()) {
return None;
}
self.first = false;
} else if self.increment_indices() {
return None;
}
for _ in 0..n {
if self.increment_indices() {
return None;
}
}
Some(self.pool.get_at(&self.indices))
}

fn size_hint(&self) -> (usize, Option<usize>) {
let (mut low, mut upp) = self.pool.size_hint();
low = remaining_for(low, self.first, &self.indices).unwrap_or(usize::MAX);
Expand Down

0 comments on commit a250651

Please sign in to comment.