Skip to content

Commit 4a14bf7

Browse files
committed
Make hi calculation clearer in DuplicatesBy size_hint
1 parent 9e2531c commit 4a14bf7

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

src/duplicates_impl.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,18 @@ mod private {
8484
#[inline]
8585
fn size_hint(&self) -> (usize, Option<usize>) {
8686
let (_, hi) = self.iter.size_hint();
87-
// There are `hi` number of items left in the base iterator. In the best case scenario,
88-
// these items are exactly the same as the ones pending (i.e items seen exactly once so
89-
// far), plus (hi - pending) / 2 pairs of never seen before items.
9087
let hi = hi.map(|hi| {
91-
let max_pending = std::cmp::min(self.meta.pending, hi);
92-
let max_new = hi.saturating_sub(self.meta.pending) / 2;
93-
max_pending + max_new
88+
if hi <= self.meta.pending {
89+
// fewer or equally many iter-remaining elements than pending elements
90+
// => at most, each iter-remaining element is matched
91+
hi
92+
} else {
93+
// fewer pending elements than iter-remaining elements
94+
// => at most:
95+
// * each pending element is matched
96+
// * the other iter-remaining elements come in pairs
97+
self.meta.pending + (hi - self.meta.pending) / 2
98+
}
9499
});
95100
// The lower bound is always 0 since we might only get unique items from now on
96101
(0, hi)

0 commit comments

Comments
 (0)