Skip to content

Commit fb1bc1f

Browse files
Make Combinations lazy
To make the `Combinations` adaptor lazy, the operation "prefill the lazy buffer" is now done when handling the first element rather than at definition. However, the value returned by the `n` method is changed when called on a fresh `it.combinations(k)` since the "prefill" operation was delayed. I'm perplex on why the method `n` was made public in the first place as the value is subject to change (said in its documentation). But it is public and this change is therefore a breaking one.
1 parent 04baddb commit fb1bc1f

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

src/combinations.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,9 @@ pub fn combinations<I>(iter: I, k: usize) -> Combinations<I>
3737
where
3838
I: Iterator,
3939
{
40-
let mut pool = LazyBuffer::new(iter);
41-
pool.prefill(k);
42-
4340
Combinations {
4441
indices: (0..k).collect(),
45-
pool,
42+
pool: LazyBuffer::new(iter),
4643
first: true,
4744
}
4845
}
@@ -107,6 +104,7 @@ where
107104
type Item = Vec<I::Item>;
108105
fn next(&mut self) -> Option<Self::Item> {
109106
if self.first {
107+
self.pool.prefill(self.k());
110108
if self.k() > self.n() {
111109
return None;
112110
}

tests/test_std.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ fn combinations_inexact_size_hints() {
10441044
let len = binomial(real_n, k);
10451045
assert_eq!(len, it.clone().count());
10461046

1047-
let mut nb_loaded = numbers.by_ref().take(k).count(); // because of `LazyBuffer::prefill(k)`
1047+
let mut nb_loaded = 0;
10481048
let sh = numbers.size_hint();
10491049
assert_eq!(binomial(sh.0 + nb_loaded, k), it.size_hint().0);
10501050
assert_eq!(sh.1.map(|n| binomial(n + nb_loaded, k)), it.size_hint().1);
@@ -1053,8 +1053,10 @@ fn combinations_inexact_size_hints() {
10531053
let elem = it.next();
10541054
assert!(elem.is_some());
10551055
assert_eq!(len - next_count, it.clone().count());
1056-
// It does not load anything more the very first time (it's prefilled).
1057-
if next_count > 1 {
1056+
if next_count == 1 {
1057+
// The very first time, the lazy buffer is prefilled.
1058+
nb_loaded = numbers.by_ref().take(k).count();
1059+
} else {
10581060
// Then it loads one item each time until exhausted.
10591061
let nb = numbers.next();
10601062
if nb.is_some() {

0 commit comments

Comments
 (0)