Skip to content

Commit dd6a569

Browse files
Specialize CombinationsWithReplacement::nth
Similar to `next` except we increment indices n times before generating the vector item.
1 parent cb06691 commit dd6a569

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/combinations_with_replacement.rs

+18
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,24 @@ where
106106
Some(self.pool.get_at(&self.indices))
107107
}
108108

109+
fn nth(&mut self, n: usize) -> Option<Self::Item> {
110+
if self.first {
111+
// In empty edge cases, stop iterating immediately
112+
if !(self.indices.is_empty() || self.pool.get_next()) {
113+
return None;
114+
}
115+
self.first = false;
116+
} else if self.increment_indices() {
117+
return None;
118+
}
119+
for _ in 0..n {
120+
if self.increment_indices() {
121+
return None;
122+
}
123+
}
124+
Some(self.pool.get_at(&self.indices))
125+
}
126+
109127
fn size_hint(&self) -> (usize, Option<usize>) {
110128
let (mut low, mut upp) = self.pool.size_hint();
111129
low = remaining_for(low, self.first, &self.indices).unwrap_or(usize::MAX);

0 commit comments

Comments
 (0)