Skip to content

Commit c4b8fd4

Browse files
kinto-bPhilippe-Cholet
authored andcommitted
Return directly from nth()
Do not go via `next()`
1 parent 3c198b7 commit c4b8fd4

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

src/combinations.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ impl<I: Iterator> Combinations<I> {
9696
}
9797

9898
/// Initialises the iterator by filling a buffer with elements from the
99-
/// iterator, return a boolean indicating whether or not we've run out of
100-
/// combinations.
99+
/// iterator. Returns true if there are no combinations, false otherwise.
101100
fn init(&mut self) -> bool {
102101
self.pool.prefill(self.k());
103102
let done = self.k() > self.n();
@@ -110,9 +109,9 @@ impl<I: Iterator> Combinations<I> {
110109

111110
/// Increments indices representing the combination to advance to the next
112111
/// (in lexicographic order by increasing sequence) combination. For example
113-
/// if we have n=3 & k=2 then [0, 1] -> [0, 2] -> [0, 3] -> [1, 2] -> ...
112+
/// if we have n=4 & k=2 then `[0, 1] -> [0, 2] -> [0, 3] -> [1, 2] -> ...`
114113
///
115-
/// Returns a boolean indicating whether or not we've run out of combinations.
114+
/// Returns true if we've run out of combinations, false otherwise.
116115
fn increment_indices(&mut self) -> bool {
117116
if self.indices.is_empty() {
118117
return true; // Done
@@ -171,7 +170,7 @@ where
171170
return self.next();
172171
}
173172

174-
let mut done = if self.first {
173+
let done = if self.first {
175174
self.init()
176175
} else {
177176
self.increment_indices()
@@ -181,14 +180,13 @@ where
181180
return None;
182181
}
183182

184-
for _ in 0..(n - 1) {
185-
done = self.increment_indices();
186-
if done {
183+
for _ in 0..n {
184+
if self.increment_indices() {
187185
return None;
188186
}
189187
}
190188

191-
self.next()
189+
Some(self.indices.iter().map(|i| self.pool[*i].clone()).collect())
192190
}
193191

194192
fn size_hint(&self) -> (usize, Option<usize>) {

0 commit comments

Comments
 (0)