Skip to content

Commit 1cfa01d

Browse files
committed
Merge pull request #4546 from alexcrichton/bitv-difference-bug
Fix the difference method on bit vectors
2 parents ef0f71d + 95d25ca commit 1cfa01d

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

src/libstd/bitv.rs

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl SmallBitv {
5959

6060
#[inline(always)]
6161
fn difference(s: &SmallBitv, nbits: uint) -> bool {
62-
self.bits_op(s.bits, nbits, |u1, u2| u1 ^ u2)
62+
self.bits_op(s.bits, nbits, |u1, u2| u1 & !u2)
6363
}
6464

6565
#[inline(always)]
@@ -180,10 +180,7 @@ impl BigBitv {
180180

181181
#[inline(always)]
182182
fn difference(b: &BigBitv, nbits: uint) -> bool {
183-
self.invert();
184-
let b = self.intersect(b, nbits);
185-
self.invert();
186-
b
183+
self.process(b, nbits, difference)
187184
}
188185

189186
#[inline(always)]
@@ -567,6 +564,8 @@ pure fn lor(w0: uint, w1: uint) -> uint { return w0 | w1; }
567564

568565
pure fn land(w0: uint, w1: uint) -> uint { return w0 & w1; }
569566

567+
pure fn difference(w0: uint, w1: uint) -> uint { return w0 & !w1; }
568+
570569
pure fn right(_w0: uint, w1: uint) -> uint { return w1; }
571570

572571
impl Bitv: ops::Index<uint,bool> {
@@ -954,6 +953,34 @@ mod tests {
954953
let bools = ~[false, false, true, false, false, true, true, false];
955954
assert from_bytes([0b00100110]).to_bools() == bools;
956955
}
956+
957+
#[test]
958+
fn test_small_difference() {
959+
let b1 = Bitv(3, false);
960+
let b2 = Bitv(3, false);
961+
b1.set(0, true);
962+
b1.set(1, true);
963+
b2.set(1, true);
964+
b2.set(2, true);
965+
assert b1.difference(&b2);
966+
assert b1[0];
967+
assert !b1[1];
968+
assert !b1[2];
969+
}
970+
971+
#[test]
972+
fn test_big_difference() {
973+
let b1 = Bitv(100, false);
974+
let b2 = Bitv(100, false);
975+
b1.set(0, true);
976+
b1.set(40, true);
977+
b2.set(40, true);
978+
b2.set(80, true);
979+
assert b1.difference(&b2);
980+
assert b1[0];
981+
assert !b1[40];
982+
assert !b1[80];
983+
}
957984
}
958985

959986
//

0 commit comments

Comments
 (0)