Skip to content

Commit 14e0964

Browse files
bors[bot]phimuemue
andauthored
Merge #487
487: Simplify CombinationsWithReplacement r=jswrenn a=phimuemue * Eliminate `k`: It can be obtained via `indices.len`. * Eliminate `max_index`: It can be obtained via `pool.len()-1`. Co-authored-by: philipp <[email protected]>
2 parents 4114081 + 7d9d5b4 commit 14e0964

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

src/combinations_with_replacement.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ where
1212
I: Iterator,
1313
I::Item: Clone,
1414
{
15-
k: usize,
1615
indices: Vec<usize>,
17-
// The current known max index value. This increases as pool grows.
18-
max_index: usize,
1916
pool: LazyBuffer<I>,
2017
first: bool,
2118
}
@@ -25,7 +22,7 @@ where
2522
I: Iterator + fmt::Debug,
2623
I::Item: fmt::Debug + Clone,
2724
{
28-
debug_fmt_fields!(Combinations, k, indices, max_index, pool, first);
25+
debug_fmt_fields!(Combinations, indices, pool, first);
2926
}
3027

3128
impl<I> CombinationsWithReplacement<I>
@@ -49,9 +46,7 @@ where
4946
let pool: LazyBuffer<I> = LazyBuffer::new(iter);
5047

5148
CombinationsWithReplacement {
52-
k,
5349
indices,
54-
max_index: 0,
5550
pool,
5651
first: true,
5752
}
@@ -67,7 +62,7 @@ where
6762
// If this is the first iteration, return early
6863
if self.first {
6964
// In empty edge cases, stop iterating immediately
70-
return if self.k != 0 && !self.pool.get_next() {
65+
return if self.indices.len() != 0 && !self.pool.get_next() {
7166
None
7267
// Otherwise, yield the initial state
7368
} else {
@@ -78,14 +73,12 @@ where
7873

7974
// Check if we need to consume more from the iterator
8075
// This will run while we increment our first index digit
81-
if self.pool.get_next() {
82-
self.max_index = self.pool.len() - 1;
83-
}
76+
self.pool.get_next();
8477

8578
// Work out where we need to update our indices
8679
let mut increment: Option<(usize, usize)> = None;
8780
for (i, indices_int) in self.indices.iter().enumerate().rev() {
88-
if indices_int < &self.max_index {
81+
if *indices_int < self.pool.len()-1 {
8982
increment = Some((i, indices_int + 1));
9083
break;
9184
}

0 commit comments

Comments
 (0)