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

Modify the len method to be constant-time #105

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ fn union_with(c: &mut Criterion) {
});
}

fn sub(c: &mut Criterion) {
c.bench_function("sub", |b| {
let bitmap1: RoaringBitmap = (1..100_000).collect();
let bitmap2: RoaringBitmap = (10..2_000_000).collect();

b.iter(|| &bitmap1 - &bitmap2);
});
}

fn xor(c: &mut Criterion) {
c.bench_function("xor", |b| {
let bitmap1: RoaringBitmap = (1..100).collect();
Expand Down Expand Up @@ -319,6 +328,7 @@ criterion_group!(
intersect_with,
or,
union_with,
sub,
xor,
symmetric_deference_with,
is_subset,
Expand Down
62 changes: 61 additions & 1 deletion src/bitmap/container.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{BitAndAssign, BitOrAssign, BitXorAssign, SubAssign};
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
use std::{fmt, ops::Range};

use super::store::{self, Store};
Expand Down Expand Up @@ -118,6 +118,21 @@ impl Container {
}
}

impl BitOr<&Container> for &Container {
type Output = Container;

fn bitor(self, rhs: &Container) -> Container {
let store = BitOr::bitor(&self.store, &rhs.store);
let mut container = Container {
key: self.key,
len: store.len(),
store,
};
container.ensure_correct_store();
container
}
}

impl BitOrAssign<Container> for Container {
fn bitor_assign(&mut self, rhs: Container) {
BitOrAssign::bitor_assign(&mut self.store, rhs.store);
Expand All @@ -134,6 +149,21 @@ impl BitOrAssign<&Container> for Container {
}
}

impl BitAnd<&Container> for &Container {
type Output = Container;

fn bitand(self, rhs: &Container) -> Container {
let store = BitAnd::bitand(&self.store, &rhs.store);
let mut container = Container {
key: self.key,
len: store.len(),
store,
};
container.ensure_correct_store();
container
}
}

impl BitAndAssign<Container> for Container {
fn bitand_assign(&mut self, rhs: Container) {
BitAndAssign::bitand_assign(&mut self.store, rhs.store);
Expand All @@ -150,6 +180,21 @@ impl BitAndAssign<&Container> for Container {
}
}

impl Sub<&Container> for &Container {
type Output = Container;

fn sub(self, rhs: &Container) -> Container {
let store = Sub::sub(&self.store, &rhs.store);
let mut container = Container {
key: self.key,
len: store.len(),
store,
};
container.ensure_correct_store();
container
}
}

impl SubAssign<&Container> for Container {
fn sub_assign(&mut self, rhs: &Container) {
SubAssign::sub_assign(&mut self.store, &rhs.store);
Expand All @@ -158,6 +203,21 @@ impl SubAssign<&Container> for Container {
}
}

impl BitXor<&Container> for &Container {
type Output = Container;

fn bitxor(self, rhs: &Container) -> Container {
let store = BitXor::bitxor(&self.store, &rhs.store);
let mut container = Container {
key: self.key,
len: store.len(),
store,
};
container.ensure_correct_store();
container
}
}

impl BitXorAssign<Container> for Container {
fn bitxor_assign(&mut self, rhs: Container) {
BitXorAssign::bitxor_assign(&mut self.store, rhs.store);
Expand Down
44 changes: 34 additions & 10 deletions src/bitmap/inherent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl RoaringBitmap {
pub fn new() -> RoaringBitmap {
RoaringBitmap {
containers: Vec::new(),
len: 0,
}
}

Expand All @@ -42,7 +43,13 @@ impl RoaringBitmap {
&mut self.containers[loc]
}
};
container.insert(index)

if container.insert(index) {
self.len += 1;
true
} else {
false
}
}

/// Inserts a range of values from the set specific as [start..end). Returns
Expand Down Expand Up @@ -94,7 +101,9 @@ impl RoaringBitmap {
// If the end range value is in the same container, just call into
// the one container.
if start_container_key == end_container_key {
return self.containers[start_i].insert_range(start_index..end_index);
let inserted = self.containers[start_i].insert_range(start_index..end_index);
self.len += inserted;
return inserted;
}

// For the first container, insert start_index..u16::MAX, with
Expand Down Expand Up @@ -138,6 +147,8 @@ impl RoaringBitmap {
};
c.insert_range(0..end_index);

self.len += inserted;

inserted
}

Expand All @@ -162,12 +173,20 @@ impl RoaringBitmap {
let (key, index) = util::split(value);

match self.containers.last_mut() {
Some(container) if container.key == key => container.push(index),
Some(container) if container.key == key => {
if container.push(index) {
self.len += 1;
true
} else {
false
}
}
Some(container) if container.key > key => false,
_otherwise => {
let mut container = Container::new(key);
container.push(index);
self.containers.push(container);
self.len += 1;
true
}
}
Expand All @@ -194,12 +213,13 @@ impl RoaringBitmap {
if self.containers[loc].len == 0 {
self.containers.remove(loc);
}
self.len -= 1;
true
} else {
false
}
}
_ => false,
Err(_) => false,
}
}
/// Removes a range of values from the set specific as [start..end).
Expand Down Expand Up @@ -230,7 +250,7 @@ impl RoaringBitmap {
let (start_hi, start_lo) = util::split(range.start as u32);
let (end_hi, end_lo) = util::split((range.end - 1) as u32);
let mut index = 0;
let mut result = 0;
let mut removed = 0;
while index < self.containers.len() {
let key = self.containers[index].key;
if key >= start_hi && key <= end_hi {
Expand All @@ -246,11 +266,11 @@ impl RoaringBitmap {
};
// remove container?
if a == 0 && b == u32::from(u16::max_value()) + 1 {
result += self.containers[index].len;
removed += self.containers[index].len;
self.containers.remove(index);
continue;
} else {
result += self.containers[index].remove_range(a, b);
removed += self.containers[index].remove_range(a, b);
if self.containers[index].len == 0 {
self.containers.remove(index);
continue;
Expand All @@ -259,7 +279,10 @@ impl RoaringBitmap {
}
index += 1;
}
result

self.len -= removed;

removed
}

/// Returns `true` if this set contains the specified integer.
Expand Down Expand Up @@ -298,6 +321,7 @@ impl RoaringBitmap {
/// ```
pub fn clear(&mut self) {
self.containers.clear();
self.len = 0;
}

/// Returns `true` if there are no integers in this set.
Expand All @@ -314,7 +338,7 @@ impl RoaringBitmap {
/// assert_eq!(rb.is_empty(), false);
/// ```
pub fn is_empty(&self) -> bool {
self.containers.is_empty()
self.len == 0
}

/// Returns the number of distinct integers added to the set.
Expand All @@ -335,7 +359,7 @@ impl RoaringBitmap {
/// assert_eq!(rb.len(), 2);
/// ```
pub fn len(&self) -> u64 {
self.containers.iter().map(|container| container.len).sum()
self.len
}

/// Returns the minimum value in the set (if the set is non-empty).
Expand Down
1 change: 1 addition & 0 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ pub use self::iter::Iter;
#[derive(PartialEq, Clone)]
pub struct RoaringBitmap {
containers: Vec<container::Container>,
len: u64,
}
Loading