Fixed bug with is_subset.
This commit is contained in:
28
src/lib.rs
28
src/lib.rs
@@ -96,14 +96,14 @@ impl BitSet {
|
||||
}
|
||||
|
||||
pub fn is_subset(&self, other: &BitSet) -> bool {
|
||||
if self.len() > other.len() {
|
||||
if self.nbits > other.nbits {
|
||||
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,
|
||||
None => *block_a == 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -145,8 +145,7 @@ impl BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn extend_from_bitset(&mut self, other: &Self) {
|
||||
pub fn union_with(&mut self, other: &Self) {
|
||||
for (key, value) in &other.blocks {
|
||||
*self.blocks.entry(*key).or_insert(0) |= value;
|
||||
}
|
||||
@@ -470,6 +469,23 @@ mod tests {
|
||||
assert!(!a.is_superset(&b));
|
||||
assert!(!b.is_subset(&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]
|
||||
@@ -612,11 +628,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_extend_from_bitset() {
|
||||
fn test_union_with() {
|
||||
let mut set = [1, 2, 3].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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user