Fixed bug with is_subset.

This commit is contained in:
2018-01-06 17:44:14 +01:00
parent c887e8197d
commit 9dd8d400b3

View File

@@ -96,14 +96,14 @@ impl BitSet {
} }
pub fn is_subset(&self, other: &BitSet) -> bool { pub fn is_subset(&self, other: &BitSet) -> bool {
if self.len() > other.len() { if self.nbits > other.nbits {
false false
} else { } else {
self.blocks self.blocks
.iter() .iter()
.all(|(key, block_a)| match other.blocks.get(key) { .all(|(key, block_a)| match other.blocks.get(key) {
Some(block_b) => block_a | block_b == *block_b, Some(block_b) => block_a | block_b == *block_b,
None => false, None => *block_a == 0,
}) })
} }
} }
@@ -145,8 +145,7 @@ impl BitSet {
} }
} }
#[inline] pub fn union_with(&mut self, other: &Self) {
pub fn extend_from_bitset(&mut self, other: &Self) {
for (key, value) in &other.blocks { for (key, value) in &other.blocks {
*self.blocks.entry(*key).or_insert(0) |= value; *self.blocks.entry(*key).or_insert(0) |= value;
} }
@@ -470,6 +469,23 @@ mod tests {
assert!(!a.is_superset(&b)); assert!(!a.is_superset(&b));
assert!(!b.is_subset(&a)); assert!(!b.is_subset(&a));
assert!(b.is_superset(&a)); assert!(b.is_superset(&a));
let mut a = BitSet::new();
assert!(a.insert(0));
assert!(a.insert(5));
assert!(a.insert(11));
assert!(a.insert(7));
let mut b = BitSet::new();
assert!(b.insert(0));
assert!(b.insert(7));
assert!(b.insert(250));
assert!(!b.is_subset(&a));
b.remove(&250);
assert!(b.is_subset(&a));
} }
#[test] #[test]
@@ -612,11 +628,11 @@ mod tests {
} }
#[test] #[test]
fn test_extend_from_bitset() { fn test_union_with() {
let mut set = [1, 2, 3].iter().cloned().collect::<BitSet>(); let mut set = [1, 2, 3].iter().cloned().collect::<BitSet>();
let other = [3, 4, 5].iter().cloned().collect::<BitSet>(); let other = [3, 4, 5].iter().cloned().collect::<BitSet>();
set.extend_from_bitset(&other); set.union_with(&other);
assert_eq!(set.len(), 5); assert_eq!(set.len(), 5);