Skip to content

Commit f152f5e

Browse files
bors[bot]phimuemue
andauthored
Merge #404
404: `Combinations::k` is redundant r=jswrenn a=phimuemue If I am not mistaken, we can get rid of `Combinations::k` by simply referring to `indices.len` instead. Also, we could simplify some things in `Combinations::next`. Co-authored-by: philipp <[email protected]>
2 parents d45663e + c763dfa commit f152f5e

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

src/combinations.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::lazy_buffer::LazyBuffer;
77
/// See [`.combinations()`](../trait.Itertools.html#method.combinations) for more information.
88
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
99
pub struct Combinations<I: Iterator> {
10-
k: usize,
1110
indices: Vec<usize>,
1211
pool: LazyBuffer<I>,
1312
first: bool,
@@ -17,24 +16,20 @@ impl<I> Clone for Combinations<I>
1716
where I: Clone + Iterator,
1817
I::Item: Clone,
1918
{
20-
clone_fields!(k, indices, pool, first);
19+
clone_fields!(indices, pool, first);
2120
}
2221

2322
impl<I> fmt::Debug for Combinations<I>
2423
where I: Iterator + fmt::Debug,
2524
I::Item: fmt::Debug,
2625
{
27-
debug_fmt_fields!(Combinations, k, indices, pool, first);
26+
debug_fmt_fields!(Combinations, indices, pool, first);
2827
}
2928

3029
/// Create a new `Combinations` from a clonable iterator.
3130
pub fn combinations<I>(iter: I, k: usize) -> Combinations<I>
3231
where I: Iterator
3332
{
34-
let mut indices: Vec<usize> = Vec::with_capacity(k);
35-
for i in 0..k {
36-
indices.push(i);
37-
}
3833
let mut pool: LazyBuffer<I> = LazyBuffer::new(iter);
3934

4035
for _ in 0..k {
@@ -44,8 +39,7 @@ pub fn combinations<I>(iter: I, k: usize) -> Combinations<I>
4439
}
4540

4641
Combinations {
47-
k: k,
48-
indices: indices,
42+
indices: (0..k).collect(),
4943
pool: pool,
5044
first: true,
5145
}
@@ -64,11 +58,11 @@ impl<I> Iterator for Combinations<I>
6458
return None;
6559
}
6660
self.first = false;
67-
} else if self.k == 0 {
61+
} else if self.indices.len() == 0 {
6862
return None;
6963
} else {
7064
// Scan from the end, looking for an index to increment
71-
let mut i: usize = self.k - 1;
65+
let mut i: usize = self.indices.len() - 1;
7266

7367
// Check if we need to consume more from the iterator
7468
if self.indices[i] == pool_len - 1 && !self.pool.is_done() {
@@ -77,7 +71,7 @@ impl<I> Iterator for Combinations<I>
7771
}
7872
}
7973

80-
while self.indices[i] == i + pool_len - self.k {
74+
while self.indices[i] == i + pool_len - self.indices.len() {
8175
if i > 0 {
8276
i -= 1;
8377
} else {
@@ -88,18 +82,12 @@ impl<I> Iterator for Combinations<I>
8882

8983
// Increment index, and reset the ones to its right
9084
self.indices[i] += 1;
91-
let mut j = i + 1;
92-
while j < self.k {
85+
for j in i+1..self.indices.len() {
9386
self.indices[j] = self.indices[j - 1] + 1;
94-
j += 1;
9587
}
9688
}
9789

9890
// Create result vector based on the indices
99-
let mut result = Vec::with_capacity(self.k);
100-
for i in self.indices.iter() {
101-
result.push(self.pool[*i].clone());
102-
}
103-
Some(result)
91+
Some(self.indices.iter().map(|i| self.pool[*i].clone()).collect())
10492
}
10593
}

0 commit comments

Comments
 (0)