Skip to content

Commit

Permalink
add dedicated fold and rfold impls
Browse files Browse the repository at this point in the history
add test for `fold`

also add test for treemap

use closures directly
  • Loading branch information
oskgo committed May 30, 2024
1 parent 3fa2e73 commit 73714f8
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 0 deletions.
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

0 comments on commit 73714f8

Please sign in to comment.