Skip to content

Fix combinations(0) and combinations_with_replacement(0). #383

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

Merged
merged 1 commit into from
Jan 14, 2020
Merged
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
8 changes: 3 additions & 5 deletions src/combinations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,11 @@ impl<I> Iterator for Combinations<I>
type Item = Vec<I::Item>;
fn next(&mut self) -> Option<Self::Item> {
let mut pool_len = self.pool.len();
if self.pool.is_done() {
if pool_len == 0 || self.k > pool_len {
return None;
}
}

if self.first {
if self.pool.is_done() {
return None;
}
self.first = false;
} else if self.k == 0 {
return None;
Expand Down
5 changes: 2 additions & 3 deletions src/combinations_with_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ where
// 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() {
return if self.k != 0 && !self.pool.get_next() {
None
// Otherwise, yield the initial state
} else {
Expand All @@ -77,8 +77,7 @@ where

// Check if we need to consume more from the iterator
// This will run while we increment our first index digit
if !self.pool.is_done() {
self.pool.get_next();
if self.pool.get_next() {
self.max_index = self.pool.len() - 1;
}

Expand Down
13 changes: 2 additions & 11 deletions src/lazy_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,10 @@ where
I: Iterator,
{
pub fn new(it: I) -> LazyBuffer<I> {
let mut it = it;
let mut buffer = Vec::new();
let done;
if let Some(first) = it.next() {
buffer.push(first);
done = false;
} else {
done = true;
}
LazyBuffer {
it: it,
done: done,
buffer: buffer,
done: false,
buffer: Vec::new(),
}
}

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