Fix cloning from a BitSet with a different domain size
The previous implementation incorrectly assumed that the number of words in a bit set is equal to the domain size. The new implementation delegates to `Vec::clone_from` which is specialized for `Copy` elements.
This commit is contained in:
parent
eba361ae36
commit
9139a63b25
2 changed files with 19 additions and 6 deletions
|
@ -1061,12 +1061,8 @@ impl<T> Clone for BitSet<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn clone_from(&mut self, from: &Self) {
|
fn clone_from(&mut self, from: &Self) {
|
||||||
if self.domain_size != from.domain_size {
|
|
||||||
self.words.resize(from.domain_size, 0);
|
|
||||||
self.domain_size = from.domain_size;
|
self.domain_size = from.domain_size;
|
||||||
}
|
self.words.clone_from(&from.words);
|
||||||
|
|
||||||
self.words.copy_from_slice(&from.words);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,23 @@ fn bitset_iter_works_2() {
|
||||||
assert_eq!(bitset.iter().collect::<Vec<_>>(), [0, 127, 191, 255, 319]);
|
assert_eq!(bitset.iter().collect::<Vec<_>>(), [0, 127, 191, 255, 319]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bitset_clone_from() {
|
||||||
|
let mut a: BitSet<usize> = BitSet::new_empty(10);
|
||||||
|
a.insert(4);
|
||||||
|
a.insert(7);
|
||||||
|
a.insert(9);
|
||||||
|
|
||||||
|
let mut b = BitSet::new_empty(2);
|
||||||
|
b.clone_from(&a);
|
||||||
|
assert_eq!(b.domain_size(), 10);
|
||||||
|
assert_eq!(b.iter().collect::<Vec<_>>(), [4, 7, 9]);
|
||||||
|
|
||||||
|
b.clone_from(&BitSet::new_empty(40));
|
||||||
|
assert_eq!(b.domain_size(), 40);
|
||||||
|
assert_eq!(b.iter().collect::<Vec<_>>(), []);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn union_two_sets() {
|
fn union_two_sets() {
|
||||||
let mut set1: BitSet<usize> = BitSet::new_empty(65);
|
let mut set1: BitSet<usize> = BitSet::new_empty(65);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue