Skip to content

Commit

Permalink
New private method Combinations::try_nth
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe-Cholet committed May 2, 2024
1 parent d045e9d commit 350b948
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,27 @@ impl<I: Iterator> Combinations<I> {
// If we've made it this far, we haven't run out of combos
false
}

/// Returns the n-th item or the remaining steps.
pub(crate) fn try_nth(&mut self, n: usize) -> Result<<Self as Iterator>::Item, usize>
where
I::Item: Clone,
{
let done = if self.first {
self.init()
} else {
self.increment_indices()
};
if done {
return Err(n);
}
for i in 0..n {
if self.increment_indices() {
return Err(n - 1 - i);
}
}
Ok(self.pool.get_at(&self.indices))
}
}

impl<I> Iterator for Combinations<I>
Expand All @@ -166,23 +187,7 @@ where
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
let done = if self.first {
self.init()
} else {
self.increment_indices()
};

if done {
return None;
}

for _ in 0..n {
if self.increment_indices() {
return None;
}
}

Some(self.pool.get_at(&self.indices))
self.try_nth(n).ok()
}

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

0 comments on commit 350b948

Please sign in to comment.