Skip to content

Commit

Permalink
lazily compute iterator len, add interator overrides
Browse files Browse the repository at this point in the history
This means the price of visting every container is only paid when needed: e.g.
when calling `.collect()`

This adds more efficent overrides for `count`, `nth` and `nth_back`, and
implements `FusedIterator`
  • Loading branch information
Dr-Emann committed Oct 18, 2024
1 parent c4e3b34 commit 820b270
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 46 deletions.
17 changes: 17 additions & 0 deletions roaring/src/bitmap/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,21 @@ impl Iterator for Iter<'_> {
fn next(&mut self) -> Option<u32> {
self.inner.next().map(|i| util::join(self.key, i))
}

fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}

fn count(self) -> usize
where
Self: Sized,
{
self.inner.count()
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.inner.nth(n).map(|i| util::join(self.key, i))
}
}

impl DoubleEndedIterator for Iter<'_> {
Expand All @@ -308,6 +323,8 @@ impl DoubleEndedIterator for Iter<'_> {
}
}

impl ExactSizeIterator for Iter<'_> {}

impl fmt::Debug for Container {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
format!("Container<{:?} @ {:?}>", self.len(), self.key).fmt(formatter)
Expand Down
Loading

0 comments on commit 820b270

Please sign in to comment.