@@ -7,7 +7,6 @@ use super::lazy_buffer::LazyBuffer;
7
7
/// See [`.combinations()`](../trait.Itertools.html#method.combinations) for more information.
8
8
#[ must_use = "iterator adaptors are lazy and do nothing unless consumed" ]
9
9
pub struct Combinations < I : Iterator > {
10
- k : usize ,
11
10
indices : Vec < usize > ,
12
11
pool : LazyBuffer < I > ,
13
12
first : bool ,
@@ -17,14 +16,14 @@ impl<I> Clone for Combinations<I>
17
16
where I : Clone + Iterator ,
18
17
I :: Item : Clone ,
19
18
{
20
- clone_fields ! ( k , indices, pool, first) ;
19
+ clone_fields ! ( indices, pool, first) ;
21
20
}
22
21
23
22
impl < I > fmt:: Debug for Combinations < I >
24
23
where I : Iterator + fmt:: Debug ,
25
24
I :: Item : fmt:: Debug ,
26
25
{
27
- debug_fmt_fields ! ( Combinations , k , indices, pool, first) ;
26
+ debug_fmt_fields ! ( Combinations , indices, pool, first) ;
28
27
}
29
28
30
29
/// Create a new `Combinations` from a clonable iterator.
@@ -40,7 +39,6 @@ pub fn combinations<I>(iter: I, k: usize) -> Combinations<I>
40
39
}
41
40
42
41
Combinations {
43
- k : k,
44
42
indices : ( 0 ..k) . collect ( ) ,
45
43
pool : pool,
46
44
first : true ,
@@ -60,11 +58,11 @@ impl<I> Iterator for Combinations<I>
60
58
return None ;
61
59
}
62
60
self . first = false ;
63
- } else if self . k == 0 {
61
+ } else if self . indices . len ( ) == 0 {
64
62
return None ;
65
63
} else {
66
64
// Scan from the end, looking for an index to increment
67
- let mut i: usize = self . k - 1 ;
65
+ let mut i: usize = self . indices . len ( ) - 1 ;
68
66
69
67
// Check if we need to consume more from the iterator
70
68
if self . indices [ i] == pool_len - 1 && !self . pool . is_done ( ) {
@@ -73,7 +71,7 @@ impl<I> Iterator for Combinations<I>
73
71
}
74
72
}
75
73
76
- while self . indices [ i] == i + pool_len - self . k {
74
+ while self . indices [ i] == i + pool_len - self . indices . len ( ) {
77
75
if i > 0 {
78
76
i -= 1 ;
79
77
} else {
@@ -85,14 +83,14 @@ impl<I> Iterator for Combinations<I>
85
83
// Increment index, and reset the ones to its right
86
84
self . indices [ i] += 1 ;
87
85
let mut j = i + 1 ;
88
- while j < self . k {
86
+ while j < self . indices . len ( ) {
89
87
self . indices [ j] = self . indices [ j - 1 ] + 1 ;
90
88
j += 1 ;
91
89
}
92
90
}
93
91
94
92
// Create result vector based on the indices
95
- let mut result = Vec :: with_capacity ( self . k ) ;
93
+ let mut result = Vec :: with_capacity ( self . indices . len ( ) ) ;
96
94
for i in self . indices . iter ( ) {
97
95
result. push ( self . pool [ * i] . clone ( ) ) ;
98
96
}
0 commit comments