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

Combination for zero sized array #380

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
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
5 changes: 5 additions & 0 deletions src/combinations.rs
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
12 changes: 12 additions & 0 deletions tests/test_std.rs
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 @@ -622,6 +629,11 @@ fn combinations_with_replacement() {
(0..3).combinations_with_replacement(0),
<Vec<Vec<_>>>::new(),
);
// Zero size on empty pool
it::assert_equal(
(0..0).combinations_with_replacement(0),
<Vec<Vec<_>>>::new(),
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. The empty vector is a valid combination of zero elements in 0..0, so (0..0).combinations_with_replacement(0) should be vec![vec![]] (like (0..0).combinations(0) and (0..0).permutations(0)), not <Vec<Vec<_>>>::new() == vec![].

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andersk

It seems that the original test cases of combinations_with_replacement has the same test cases, and I just made a copy of it.
Should fixing combinations_with_replacement and its test cases be included in this PR or should I make a separate PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that python itertools implementation shows value 1 (single empty set) for following inputs.

>>> len(list(itertools.combinations_with_replacement([], 0)))
1
>>> len(list(itertools.combinations([], 0)))
1
>>>

// Empty pool
it::assert_equal(
(0..0).combinations_with_replacement(2),
Expand Down