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

Improve the Pairs Iterator type #112

Merged
merged 2 commits into from
Jun 2, 2021
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
92 changes: 55 additions & 37 deletions src/bitmap/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use std::borrow::Borrow;
use std::cmp::Ordering;
use std::iter::Peekable;
use std::slice;

use super::container::Container;
use crate::RoaringBitmap;

struct Pairs<'a>(Peekable<slice::Iter<'a, Container>>, Peekable<slice::Iter<'a, Container>>);

impl RoaringBitmap {
fn pairs<'a>(&'a self, other: &'a RoaringBitmap) -> Pairs<'a> {
Pairs(self.containers.iter().peekable(), other.containers.iter().peekable())
}

/// Returns true if the set has no elements in common with other. This is equivalent to
/// checking for an empty intersection.
///
Expand All @@ -32,9 +27,9 @@ impl RoaringBitmap {
///
/// ```
pub fn is_disjoint(&self, other: &Self) -> bool {
self.pairs(other)
.filter(|&(c1, c2)| c1.is_some() && c2.is_some())
.all(|(c1, c2)| c1.unwrap().is_disjoint(c2.unwrap()))
Pairs::new(&self.containers, &other.containers)
.filter_map(|(c1, c2)| c1.zip(c2))
.all(|(c1, c2)| c1.is_disjoint(c2))
}

/// Returns `true` if this set is a subset of `other`.
Expand All @@ -60,12 +55,10 @@ impl RoaringBitmap {
/// assert_eq!(rb1.is_subset(&rb2), false);
/// ```
pub fn is_subset(&self, other: &Self) -> bool {
for pair in self.pairs(other) {
for pair in Pairs::new(&self.containers, &other.containers) {
match pair {
(None, _) => (),
(_, None) => {
return false;
}
(_, None) => return false,
(Some(c1), Some(c2)) => {
if !c1.is_subset(c2) {
return false;
Expand Down Expand Up @@ -103,32 +96,57 @@ impl RoaringBitmap {
}
}

impl<'a> Iterator for Pairs<'a> {
type Item = (Option<&'a Container>, Option<&'a Container>);
/// An helping Iterator over pairs of containers.
///
/// Returns the smallest container according to its key
/// or both if the key is the same. It is useful when you need
/// to iterate over two containers to do operations on them.
pub struct Pairs<I, J, L, R>
where
I: Iterator<Item = L>,
J: Iterator<Item = R>,
L: Borrow<Container>,
R: Borrow<Container>,
{
left: Peekable<I>,
right: Peekable<J>,
}

impl<I, J, L, R> Pairs<I, J, L, R>
where
I: Iterator<Item = L>,
J: Iterator<Item = R>,
L: Borrow<Container>,
R: Borrow<Container>,
{
pub fn new<A, B>(left: A, right: B) -> Pairs<I, J, L, R>
where
A: IntoIterator<Item = L, IntoIter = I>,
B: IntoIterator<Item = R, IntoIter = J>,
{
Pairs { left: left.into_iter().peekable(), right: right.into_iter().peekable() }
}
}

impl<I, J, L, R> Iterator for Pairs<I, J, L, R>
where
I: Iterator<Item = L>,
J: Iterator<Item = R>,
L: Borrow<Container>,
R: Borrow<Container>,
{
type Item = (Option<L>, Option<R>);

fn next(&mut self) -> Option<Self::Item> {
enum Which {
Left,
Right,
Both,
None,
}
let which = match (self.0.peek(), self.1.peek()) {
(None, None) => Which::None,
(Some(_), None) => Which::Left,
(None, Some(_)) => Which::Right,
(Some(c1), Some(c2)) => match (c1.key, c2.key) {
(key1, key2) if key1 == key2 => Which::Both,
(key1, key2) if key1 < key2 => Which::Left,
(key1, key2) if key1 > key2 => Which::Right,
(_, _) => unreachable!(),
match (self.left.peek(), self.right.peek()) {
(None, None) => None,
(Some(_), None) => Some((self.left.next(), None)),
(None, Some(_)) => Some((None, self.right.next())),
(Some(c1), Some(c2)) => match c1.borrow().key.cmp(&c2.borrow().key) {
Ordering::Equal => Some((self.left.next(), self.right.next())),
Ordering::Less => Some((self.left.next(), None)),
Ordering::Greater => Some((None, self.right.next())),
},
};
match which {
Which::Left => Some((self.0.next(), None)),
Which::Right => Some((None, self.1.next())),
Which::Both => Some((self.0.next(), self.1.next())),
Which::None => None,
}
}
}
1 change: 1 addition & 0 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod iter;
mod ops;
mod serialization;

use self::cmp::Pairs;
pub use self::iter::IntoIter;
pub use self::iter::Iter;

Expand Down
80 changes: 19 additions & 61 deletions src/bitmap/ops.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::cmp::Ordering;
use std::mem;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};

use retain_mut::RetainMut;

use crate::bitmap::container::Container;
use crate::bitmap::Pairs;
use crate::RoaringBitmap;

impl RoaringBitmap {
Expand Down Expand Up @@ -416,38 +416,17 @@ impl BitXor<&RoaringBitmap> for &RoaringBitmap {
impl BitXorAssign<RoaringBitmap> for RoaringBitmap {
/// A `symmetric difference` between two sets.
fn bitxor_assign(&mut self, rhs: RoaringBitmap) {
let mut left = mem::take(&mut self.containers).into_iter().peekable();
let mut right = rhs.containers.into_iter().peekable();

loop {
match (left.peek(), right.peek()) {
(None, None) => break,
(Some(_), None) => {
self.containers.extend(left);
break;
}
(None, Some(_)) => {
self.containers.extend(right);
break;
}
(Some(l), Some(r)) => match l.key.cmp(&r.key) {
Ordering::Equal => {
let mut lhs = left.next().unwrap();
let rhs = right.next().unwrap();
BitXorAssign::bitxor_assign(&mut lhs, rhs);
if lhs.len != 0 {
self.containers.push(lhs);
}
}
Ordering::Less => {
let lhs = left.next().unwrap();
for pair in Pairs::new(mem::take(&mut self.containers), rhs.containers) {
match pair {
(Some(mut lhs), Some(rhs)) => {
BitXorAssign::bitxor_assign(&mut lhs, rhs);
if lhs.len != 0 {
self.containers.push(lhs);
}
Ordering::Greater => {
let rhs = right.next().unwrap();
self.containers.push(rhs);
}
},
}
(Some(lhs), None) => self.containers.push(lhs),
(None, Some(rhs)) => self.containers.push(rhs),
(None, None) => break,
}
}
}
Expand All @@ -456,38 +435,17 @@ impl BitXorAssign<RoaringBitmap> for RoaringBitmap {
impl BitXorAssign<&RoaringBitmap> for RoaringBitmap {
/// A `symmetric difference` between two sets.
fn bitxor_assign(&mut self, rhs: &RoaringBitmap) {
let mut left = mem::take(&mut self.containers).into_iter().peekable();
let mut right = rhs.containers.iter().peekable();

loop {
match (left.peek(), right.peek()) {
(None, None) => break,
(Some(_), None) => {
self.containers.extend(left);
break;
}
(None, Some(_)) => {
self.containers.extend(right.cloned());
break;
}
(Some(l), Some(r)) => match l.key.cmp(&r.key) {
Ordering::Equal => {
let mut lhs = left.next().unwrap();
let rhs = right.next().unwrap();
BitXorAssign::bitxor_assign(&mut lhs, rhs);
if lhs.len != 0 {
self.containers.push(lhs);
}
}
Ordering::Less => {
let lhs = left.next().unwrap();
for pair in Pairs::new(mem::take(&mut self.containers), &rhs.containers) {
match pair {
(Some(mut lhs), Some(rhs)) => {
BitXorAssign::bitxor_assign(&mut lhs, rhs);
if lhs.len != 0 {
self.containers.push(lhs);
}
Ordering::Greater => {
let rhs = right.next().unwrap();
self.containers.push(rhs.clone());
}
},
}
(Some(lhs), None) => self.containers.push(lhs),
(None, Some(rhs)) => self.containers.push(rhs.clone()),
(None, None) => break,
}
}
}
Expand Down