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

add dedicated fold and rfold impls for iterators #280

Merged
merged 1 commit into from
Jun 4, 2024
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/bitmap/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,30 @@ impl Iterator for Iter<'_> {
(usize::MAX, None)
}
}

#[inline]
fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, f)
}
}

impl DoubleEndedIterator for Iter<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.size_hint = self.size_hint.saturating_sub(1);
self.inner.next_back()
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.inner.rfold(init, fold)
}
}

#[cfg(target_pointer_width = "64")]
Expand All @@ -77,13 +94,30 @@ impl Iterator for IntoIter {
(usize::MAX, None)
}
}

#[inline]
fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, f)
}
}

impl DoubleEndedIterator for IntoIter {
fn next_back(&mut self) -> Option<Self::Item> {
self.size_hint = self.size_hint.saturating_sub(1);
self.inner.next_back()
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.inner.rfold(init, fold)
}
}

#[cfg(target_pointer_width = "64")]
Expand Down
70 changes: 70 additions & 0 deletions src/treemap/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,30 @@ impl<'a> Iterator for To64Iter<'a> {
fn next(&mut self) -> Option<u64> {
self.inner.next().map(|n| util::join(self.hi, n))
}

#[inline]
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, move |b, lo| f(b, ((self.hi as u64) << 32) + (lo as u64)))
}
}

impl DoubleEndedIterator for To64Iter<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().map(|n| util::join(self.hi, n))
}

#[inline]
fn rfold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.rfold(init, move |b, lo| f(b, ((self.hi as u64) << 32) + (lo as u64)))
}
}

fn to64iter<'a>(t: (&'a u32, &'a RoaringBitmap)) -> To64Iter<'a> {
Expand All @@ -38,12 +56,30 @@ impl Iterator for To64IntoIter {
fn next(&mut self) -> Option<u64> {
self.inner.next().map(|n| util::join(self.hi, n))
}

#[inline]
fn fold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, move |b, lo| f(b, ((self.hi as u64) << 32) + (lo as u64)))
}
}

impl DoubleEndedIterator for To64IntoIter {
fn next_back(&mut self) -> Option<Self::Item> {
self.inner.next_back().map(|n| util::join(self.hi, n))
}

#[inline]
fn rfold<B, F>(self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.rfold(init, move |b, lo| f(b, ((self.hi as u64) << 32) + (lo as u64)))
}
}

fn to64intoiter(t: (u32, RoaringBitmap)) -> To64IntoIter {
Expand Down Expand Up @@ -104,13 +140,30 @@ impl<'a> Iterator for Iter<'a> {
(usize::MAX, None)
}
}

#[inline]
fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, f)
}
}

impl DoubleEndedIterator for Iter<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.size_hint = self.size_hint.saturating_sub(1);
self.inner.next_back()
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.inner.rfold(init, fold)
}
}

#[cfg(target_pointer_width = "64")]
Expand All @@ -135,13 +188,30 @@ impl Iterator for IntoIter {
(usize::MAX, None)
}
}

#[inline]
fn fold<B, F>(self, init: B, f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
{
self.inner.fold(init, f)
}
}

impl DoubleEndedIterator for IntoIter {
fn next_back(&mut self) -> Option<Self::Item> {
self.size_hint = self.size_hint.saturating_sub(1);
self.inner.next_back()
}

#[inline]
fn rfold<Acc, Fold>(self, init: Acc, fold: Fold) -> Acc
where
Fold: FnMut(Acc, Self::Item) -> Acc,
{
self.inner.rfold(init, fold)
}
}

#[cfg(target_pointer_width = "64")]
Expand Down
14 changes: 14 additions & 0 deletions tests/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,20 @@ proptest! {
}
}

proptest! {
#[test]
fn fold(values in btree_set(any::<u32>(), ..=10_000)) {
let bitmap = RoaringBitmap::from_sorted_iter(values.iter().cloned()).unwrap();
let mut val_iter = values.into_iter();
// `Iterator::all` uses currently unimplementable `try_fold`, we test `fold`
#[allow(clippy::unnecessary_fold)]
let r = bitmap.into_iter().fold(true, |b, i| {
b && i == val_iter.next().unwrap()
});
assert!(r)
}
}

#[test]
fn rev_array() {
let values = 0..100;
Expand Down
14 changes: 14 additions & 0 deletions tests/treemap_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ proptest! {
}
}

proptest! {
#[test]
fn fold(values in btree_set(any::<u64>(), ..=10_000)) {
let bitmap = RoaringTreemap::from_sorted_iter(values.iter().cloned()).unwrap();
let mut val_iter = values.into_iter();
// `Iterator::all` uses currently unimplementable `try_fold`, we test `fold`
#[allow(clippy::unnecessary_fold)]
let r = bitmap.into_iter().fold(true, |b, i| {
b && i == val_iter.next().unwrap()
});
assert!(r)
}
}

#[test]
fn rev() {
let values = (1..3)
Expand Down
Loading