Implemented is_subset.

This commit is contained in:
2018-01-05 19:27:00 +01:00
parent 9ce71307d9
commit 2ab88456ca

View File

@@ -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);
}
}