Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ impl<I> Iterator for Combinations<I>
{
type Item = Vec<I::Item>;
fn next(&mut self) -> Option<Self::Item> {
if self.first && self.k == 0 {
self.first = false;
return Some(Vec::new());
}

let mut pool_len = self.pool.len();
if self.pool.is_done() {
if pool_len == 0 || self.k > pool_len {
Expand Down
13 changes: 6 additions & 7 deletions src/combinations_with_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ where
fn next(&mut self) -> Option<Self::Item> {
// If this is the first iteration, return early
if self.first {
// In empty edge cases, stop iterating immediately
return if self.k == 0 || self.pool.is_done() {
None
// Otherwise, yield the initial state
} else {
self.first = false;
Some(self.current())
self.first = false;
// Handle corner cases of ((N,0)), ((0,N)), and ((0,0))
return match (self.pool.is_done(), self.k == 0) {
(_, true) => Some(Vec::new()), // ((0/N, 0)) = 1
(true, false) => None, // ((0, N)) = 0
_ => Some(self.current()) // Otherwise, yield the initial state
};
}

Expand Down
14 changes: 13 additions & 1 deletion tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,13 @@ fn combinations_of_too_short() {
#[test]
fn combinations_zero() {
it::assert_equal((1..3).combinations(0), vec![vec![]]);
it::assert_equal((0..0).combinations(0), vec![vec![]]);
}

#[test]
fn permutations_zero() {
it::assert_equal((1..3).permutations(0), vec![vec![]]);
it::assert_equal((0..0).permutations(0), vec![vec![]]);
}

#[test]
Expand All @@ -620,7 +627,12 @@ fn combinations_with_replacement() {
// Zero size
it::assert_equal(
(0..3).combinations_with_replacement(0),
<Vec<Vec<_>>>::new(),
vec![vec![]],
);
// Zero size on empty pool
it::assert_equal(
(0..0).combinations_with_replacement(0),
vec![vec![]],
);
// Empty pool
it::assert_equal(
Expand Down