Small performance boost.

This commit is contained in:
2018-01-12 18:17:59 +01:00
parent f37cc5efa6
commit ae6e292c9d
3 changed files with 49 additions and 25 deletions

View File

@@ -76,10 +76,12 @@ impl BitSet {
}
}
#[inline]
pub fn len(&self) -> usize {
self.nbits
}
#[inline]
pub fn is_empty(&self) -> bool {
self.nbits == 0
}
@@ -89,6 +91,7 @@ impl BitSet {
self.nbits = 0;
}
#[inline]
pub fn contains(&self, value: &u64) -> bool {
let (block, bit) = block_bit(value, &BITS);
@@ -98,6 +101,7 @@ impl BitSet {
}
}
#[inline]
pub fn is_subset(&self, other: &BitSet) -> bool {
if self.nbits > other.nbits {
false
@@ -116,6 +120,7 @@ impl BitSet {
other.is_subset(self)
}
#[inline]
pub fn insert(&mut self, value: u64) -> bool {
let (block, bit) = block_bit(&value, &BITS);
let block = self.blocks.entry(block).or_insert(0);
@@ -132,6 +137,7 @@ impl BitSet {
}
}
#[inline]
pub fn remove(&mut self, value: &u64) -> bool {
let (block, bit) = block_bit(value, &BITS);
let block = self.blocks.entry(block).or_insert(0);
@@ -148,15 +154,17 @@ impl BitSet {
}
}
#[inline]
pub fn union_with(&mut self, other: &Self) {
for (key, value) in &other.blocks {
*self.blocks.entry(*key).or_insert(0) |= value;
}
let block = self.blocks.entry(*key).or_insert(0);
self.nbits = self.blocks
.values()
.map(|block| block.count_ones() as usize)
.sum();
let n = block.count_ones();
*block |= value;
self.nbits += (block.count_ones() - n) as usize;
}
}
}
@@ -206,13 +214,14 @@ impl ops::BitOr for BitSet {
#[inline]
fn bitor(mut self, rhs: Self) -> Self {
for (key, value) in &rhs.blocks {
*self.blocks.entry(*key).or_insert(0) |= value;
}
let block = self.blocks.entry(*key).or_insert(0);
self.nbits = self.blocks
.values()
.map(|block| block.count_ones() as usize)
.sum();
let n = block.count_ones();
*block |= value;
self.nbits += (block.count_ones() - n) as usize;
}
self
}
@@ -224,13 +233,14 @@ impl<'a> ops::BitOr<&'a Self> for BitSet {
#[inline]
fn bitor(mut self, rhs: &'a Self) -> Self {
for (key, value) in &rhs.blocks {
*self.blocks.entry(*key).or_insert(0) |= value;
}
let block = self.blocks.entry(*key).or_insert(0);
self.nbits = self.blocks
.values()
.map(|block| block.count_ones() as usize)
.sum();
let n = block.count_ones();
*block |= value;
self.nbits += (block.count_ones() - n) as usize;
}
self
}