Merge pull request #4546 from alexcrichton/bitv-difference-bug
Fix the difference method on bit vectors
This commit is contained in:
commit
1cfa01decf
@ -59,7 +59,7 @@ impl SmallBitv {
|
||||
|
||||
#[inline(always)]
|
||||
fn difference(s: &SmallBitv, nbits: uint) -> bool {
|
||||
self.bits_op(s.bits, nbits, |u1, u2| u1 ^ u2)
|
||||
self.bits_op(s.bits, nbits, |u1, u2| u1 & !u2)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@ -180,10 +180,7 @@ impl BigBitv {
|
||||
|
||||
#[inline(always)]
|
||||
fn difference(b: &BigBitv, nbits: uint) -> bool {
|
||||
self.invert();
|
||||
let b = self.intersect(b, nbits);
|
||||
self.invert();
|
||||
b
|
||||
self.process(b, nbits, difference)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
@ -567,6 +564,8 @@ pure fn lor(w0: uint, w1: uint) -> uint { return w0 | w1; }
|
||||
|
||||
pure fn land(w0: uint, w1: uint) -> uint { return w0 & w1; }
|
||||
|
||||
pure fn difference(w0: uint, w1: uint) -> uint { return w0 & !w1; }
|
||||
|
||||
pure fn right(_w0: uint, w1: uint) -> uint { return w1; }
|
||||
|
||||
impl Bitv: ops::Index<uint,bool> {
|
||||
@ -954,6 +953,34 @@ mod tests {
|
||||
let bools = ~[false, false, true, false, false, true, true, false];
|
||||
assert from_bytes([0b00100110]).to_bools() == bools;
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_small_difference() {
|
||||
let b1 = Bitv(3, false);
|
||||
let b2 = Bitv(3, false);
|
||||
b1.set(0, true);
|
||||
b1.set(1, true);
|
||||
b2.set(1, true);
|
||||
b2.set(2, true);
|
||||
assert b1.difference(&b2);
|
||||
assert b1[0];
|
||||
assert !b1[1];
|
||||
assert !b1[2];
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_big_difference() {
|
||||
let b1 = Bitv(100, false);
|
||||
let b2 = Bitv(100, false);
|
||||
b1.set(0, true);
|
||||
b1.set(40, true);
|
||||
b2.set(40, true);
|
||||
b2.set(80, true);
|
||||
assert b1.difference(&b2);
|
||||
assert b1[0];
|
||||
assert !b1[40];
|
||||
assert !b1[80];
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
Loading…
x
Reference in New Issue
Block a user