Skip to content

Commit

Permalink
Improve the Pairs implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kerollmops committed Jun 1, 2021
1 parent ef5801a commit c04ac3d
Showing 1 changed file with 11 additions and 23 deletions.
34 changes: 11 additions & 23 deletions src/bitmap/cmp.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::Ordering;
use std::iter::Peekable;
use std::slice;

Expand Down Expand Up @@ -39,8 +40,8 @@ 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()))
.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 Down Expand Up @@ -113,28 +114,15 @@ impl<'a> Iterator for Pairs<'a> {
type Item = (Option<&'a Container>, Option<&'a Container>);

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.0.peek(), self.1.peek()) {
(None, None) => None,
(Some(_), None) => Some((self.0.next(), None)),
(None, Some(_)) => Some((None, self.1.next())),
(Some(c1), Some(c2)) => match c1.key.cmp(&c2.key) {
Ordering::Equal => Some((self.0.next(), self.1.next())),
Ordering::Less => Some((self.0.next(), None)),
Ordering::Greater => Some((None, self.1.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,
}
}
}

0 comments on commit c04ac3d

Please sign in to comment.