Skip to content

Commit

Permalink
Make hi calculation clearer in DuplicatesBy size_hint
Browse files Browse the repository at this point in the history
  • Loading branch information
arpankapoor committed Aug 16, 2021
1 parent 9e2531c commit 4a14bf7
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/duplicates_impl.rs
Expand Up @@ -84,13 +84,18 @@ mod private {
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let (_, hi) = self.iter.size_hint();
// There are `hi` number of items left in the base iterator. In the best case scenario,
// these items are exactly the same as the ones pending (i.e items seen exactly once so
// far), plus (hi - pending) / 2 pairs of never seen before items.
let hi = hi.map(|hi| {
let max_pending = std::cmp::min(self.meta.pending, hi);
let max_new = hi.saturating_sub(self.meta.pending) / 2;
max_pending + max_new
if hi <= self.meta.pending {
// fewer or equally many iter-remaining elements than pending elements
// => at most, each iter-remaining element is matched
hi
} else {
// fewer pending elements than iter-remaining elements
// => at most:
// * each pending element is matched
// * the other iter-remaining elements come in pairs
self.meta.pending + (hi - self.meta.pending) / 2
}
});
// The lower bound is always 0 since we might only get unique items from now on
(0, hi)
Expand Down

0 comments on commit 4a14bf7

Please sign in to comment.