Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ExactSizeIterator for Tuples #761

Merged
merged 6 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/tuple_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ where
fn next(&mut self) -> Option<Self::Item> {
T::collect_from_iter(&mut self.iter, &mut self.buf)
}

fn size_hint(&self) -> (usize, Option<usize>) {
// The number of elts we've drawn from the underlying iterator, but have
// not yet produced as a tuple.
let buffered = T::buffer_len(&self.buf);
// To that, we must add the size estimates of the underlying iterator.
let (mut unbuffered_lo, mut unbuffered_hi) = self.iter.size_hint();
// The total low estimate is the sum of the already-buffered elements,
// plus the low estimate of remaining unbuffered elements, divided by
// the tuple size.
let total_lo = add_then_div(unbuffered_lo, buffered, T::num_items()).unwrap_or(usize::MAX);
// And likewise for the total high estimate, but using the high estimate
// of the remaining unbuffered elements.
let total_hi = unbuffered_hi.and_then(|hi| add_then_div(hi, buffered, T::num_items()));
(total_lo, total_hi)
}
}

/// `(n + a) / d` avoiding overflow when possible, returns `None` if it overflows.
fn add_then_div(n: usize, a: usize, d: usize) -> Option<usize> {
debug_assert_ne!(d, 0);
(n / d).checked_add(a / d)?.checked_add((n % d + a % d) / d)
}

impl<I, T> ExactSizeIterator for Tuples<I, T>
where
I: ExactSizeIterator<Item = T::Item>,
T: HomogeneousTuple,
{
}

impl<I, T> Tuples<I, T>
Expand Down Expand Up @@ -284,6 +313,11 @@ pub trait TupleCollect: Sized {
type Item;
type Buffer: Default + AsRef<[Option<Self::Item>]> + AsMut<[Option<Self::Item>]>;

fn buffer_len(buf: &Self::Buffer) -> usize {
let s = buf.as_ref();
s.iter().position(Option::is_none).unwrap_or(s.len())
}

fn collect_from_iter<I>(iter: I, buf: &mut Self::Buffer) -> Option<Self>
where
I: IntoIterator<Item = Self::Item>;
Expand Down
12 changes: 12 additions & 0 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,18 @@ quickcheck! {
assert_eq!(buffer.len(), a.len() % 4);
exact_size(buffer)
}

fn tuples_size_hint_inexact(a: Iter<u8>) -> bool {
correct_size_hint(a.clone().tuples::<(_,)>())
&& correct_size_hint(a.clone().tuples::<(_, _)>())
&& correct_size_hint(a.tuples::<(_, _, _, _)>())
}

fn tuples_size_hint_exact(a: Iter<u8, Exact>) -> bool {
exact_size(a.clone().tuples::<(_,)>())
&& exact_size(a.clone().tuples::<(_, _)>())
&& exact_size(a.tuples::<(_, _, _, _)>())
}
}

// with_position
Expand Down