Skip to content

Fix the difference method on bit vectors #4546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions src/libstd/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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> {
Expand Down Expand Up @@ -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];
}
}

//
Expand Down