@@ -14,23 +14,22 @@ pub struct Combinations<I: Iterator> {
14
14
indices : Vec < usize > ,
15
15
pool : LazyBuffer < I > ,
16
16
first : bool ,
17
- done : bool ,
18
17
}
19
18
20
19
impl < I > Clone for Combinations < I >
21
20
where
22
21
I : Clone + Iterator ,
23
22
I :: Item : Clone ,
24
23
{
25
- clone_fields ! ( indices, pool, first, done ) ;
24
+ clone_fields ! ( indices, pool, first) ;
26
25
}
27
26
28
27
impl < I > fmt:: Debug for Combinations < I >
29
28
where
30
29
I : Iterator + fmt:: Debug ,
31
30
I :: Item : fmt:: Debug ,
32
31
{
33
- debug_fmt_fields ! ( Combinations , indices, pool, first, done ) ;
32
+ debug_fmt_fields ! ( Combinations , indices, pool, first) ;
34
33
}
35
34
36
35
/// Create a new `Combinations` from a clonable iterator.
42
41
indices : ( 0 ..k) . collect ( ) ,
43
42
pool : LazyBuffer :: new ( iter) ,
44
43
first : true ,
45
- done : false ,
46
44
}
47
45
}
48
46
@@ -72,7 +70,6 @@ impl<I: Iterator> Combinations<I> {
72
70
/// elements.
73
71
pub ( crate ) fn reset ( & mut self , k : usize ) {
74
72
self . first = true ;
75
- self . done = false ;
76
73
77
74
if k < self . indices . len ( ) {
78
75
self . indices . truncate ( k) ;
@@ -93,30 +90,32 @@ impl<I: Iterator> Combinations<I> {
93
90
indices,
94
91
pool,
95
92
first,
96
- done : _,
97
93
} = self ;
98
94
let n = pool. count ( ) ;
99
95
( n, remaining_for ( n, first, & indices) . unwrap ( ) )
100
96
}
101
97
102
98
/// Initialises the iterator by filling a buffer with elements from the
103
- /// iterator.
104
- fn init ( & mut self ) {
99
+ /// iterator, return a boolean indicating whether or not we've run out of
100
+ /// combinations.
101
+ fn init ( & mut self ) -> bool {
105
102
self . pool . prefill ( self . k ( ) ) ;
106
- if self . k ( ) > self . n ( ) {
107
- self . done = true ;
108
- } else {
103
+ let done = self . k ( ) > self . n ( ) ;
104
+ if !done {
109
105
self . first = false ;
110
106
}
107
+
108
+ done
111
109
}
112
110
113
111
/// Increments indices representing the combination to advance to the next
114
112
/// (in lexicographic order by increasing sequence) combination. For example
115
113
/// if we have n=3 & k=2 then [0, 1] -> [0, 2] -> [0, 3] -> [1, 2] -> ...
116
- fn increment_indices ( & mut self ) {
114
+ ///
115
+ /// Returns a boolean indicating whether or not we've run out of combinations.
116
+ fn increment_indices ( & mut self ) -> bool {
117
117
if self . indices . is_empty ( ) {
118
- self . done = true ;
119
- return ;
118
+ return true ; // Done
120
119
}
121
120
122
121
// Scan from the end, looking for an index to increment
@@ -132,8 +131,7 @@ impl<I: Iterator> Combinations<I> {
132
131
i -= 1 ;
133
132
} else {
134
133
// Reached the last combination
135
- self . done = true ;
136
- return ;
134
+ return true ;
137
135
}
138
136
}
139
137
@@ -142,6 +140,9 @@ impl<I: Iterator> Combinations<I> {
142
140
for j in i + 1 ..self . indices . len ( ) {
143
141
self . indices [ j] = self . indices [ j - 1 ] + 1 ;
144
142
}
143
+
144
+ // If we've made it this far, we haven't run out of combos
145
+ false
145
146
}
146
147
}
147
148
@@ -152,30 +153,37 @@ where
152
153
{
153
154
type Item = Vec < I :: Item > ;
154
155
fn next ( & mut self ) -> Option < Self :: Item > {
155
- if self . first {
156
+ let done = if self . first {
156
157
self . init ( )
157
158
} else {
158
159
self . increment_indices ( )
159
- }
160
+ } ;
160
161
161
- if self . done {
162
+ if done {
162
163
return None ;
163
164
}
164
165
165
166
Some ( self . indices . iter ( ) . map ( |i| self . pool [ * i] . clone ( ) ) . collect ( ) )
166
167
}
167
168
168
169
fn nth ( & mut self , n : usize ) -> Option < Self :: Item > {
169
- // Delegate initialisation work to next()
170
- let first = self . next ( ) ;
171
-
172
170
if n == 0 {
173
- return first;
171
+ return self . next ( ) ;
172
+ }
173
+
174
+ let mut done = if self . first {
175
+ self . init ( )
176
+ } else {
177
+ self . increment_indices ( )
178
+ } ;
179
+
180
+ if done {
181
+ return None ;
174
182
}
175
183
176
184
for _ in 0 ..( n - 1 ) {
177
- self . increment_indices ( ) ;
178
- if self . done {
185
+ done = self . increment_indices ( ) ;
186
+ if done {
179
187
return None ;
180
188
}
181
189
}
0 commit comments