Skip to content

Commit 7d9d5b4

Browse files
committed
Eliminate redundant CombinationsWithReplacement::max_index
Can be inferred from `pool`: * For `self.indices.len()==0`, the first iteration yields `Some(self.current())`, and afterwards we just go through, unnecessarily call `self.pool.get_next()`, `increment` stays `None` as the loop is not iterated, and we return `None`. * For `self.indices.len()>0`: * If `pool` is empty from the start, `pool.get_next` is always `false` and we will always return `None` (without even setting `first=false`). * Otherwise (i.e. if `pool` is non-empty), we return `Some(self.current())` on first it eration, and afterwards always call `pool.get_next`, possibly updating `self.max_index`. In the first iteration `max_index` is 0, thus equals `pool.len()-1` when we return `Some(self.current())`. Thus, we know that `self.max_index` is equal to `self.pool.len()-1` when we come to the line `let mut increment...`.
1 parent 1ee5b2a commit 7d9d5b4

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

src/combinations_with_replacement.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ where
1313
I::Item: Clone,
1414
{
1515
indices: Vec<usize>,
16-
// The current known max index value. This increases as pool grows.
17-
max_index: usize,
1816
pool: LazyBuffer<I>,
1917
first: bool,
2018
}
@@ -24,7 +22,7 @@ where
2422
I: Iterator + fmt::Debug,
2523
I::Item: fmt::Debug + Clone,
2624
{
27-
debug_fmt_fields!(Combinations, indices, max_index, pool, first);
25+
debug_fmt_fields!(Combinations, indices, pool, first);
2826
}
2927

3028
impl<I> CombinationsWithReplacement<I>
@@ -49,7 +47,6 @@ where
4947

5048
CombinationsWithReplacement {
5149
indices,
52-
max_index: 0,
5350
pool,
5451
first: true,
5552
}
@@ -76,14 +73,12 @@ where
7673

7774
// Check if we need to consume more from the iterator
7875
// This will run while we increment our first index digit
79-
if self.pool.get_next() {
80-
self.max_index = self.pool.len() - 1;
81-
}
76+
self.pool.get_next();
8277

8378
// Work out where we need to update our indices
8479
let mut increment: Option<(usize, usize)> = None;
8580
for (i, indices_int) in self.indices.iter().enumerate().rev() {
86-
if indices_int < &self.max_index {
81+
if *indices_int < self.pool.len()-1 {
8782
increment = Some((i, indices_int + 1));
8883
break;
8984
}

0 commit comments

Comments
 (0)