Implemented is_subset.
This commit is contained in:
31
src/lib.rs
31
src/lib.rs
@@ -114,6 +114,25 @@ impl BitSet {
|
||||
.map(|block| block.count_ones() as usize)
|
||||
.sum();
|
||||
}
|
||||
|
||||
pub fn is_subset(&self, other: &BitSet) -> bool {
|
||||
if self.len() > other.len() {
|
||||
false
|
||||
} else {
|
||||
self.blocks.iter()
|
||||
.all(|(key, block_a)| {
|
||||
match other.blocks.get(key) {
|
||||
Some(block_b) => block_a | block_b == *block_b,
|
||||
None => false
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn is_superset(&self, other: &BitSet) -> bool {
|
||||
other.is_subset(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for BitSet {
|
||||
@@ -254,4 +273,16 @@ mod tests {
|
||||
assert_eq!(set.contains(4), true);
|
||||
assert_eq!(set.contains(5), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_subset() {
|
||||
let sup: BitSet = [1, 2, 3].iter().cloned().collect();
|
||||
let mut set = BitSet::new();
|
||||
|
||||
assert_eq!(set.is_subset(&sup), true);
|
||||
set.insert(2);
|
||||
assert_eq!(set.is_subset(&sup), true);
|
||||
set.insert(4);
|
||||
assert_eq!(set.is_subset(&sup), false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user