Skip to content

Fixed bug when initialising bitv from init=true #11615

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 19, 2014
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: 30 additions & 7 deletions src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,23 @@ impl Bitv {

impl Bitv {
pub fn new(nbits: uint, init: bool) -> Bitv {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add documentation for new (ie, what the parameters do)

let rep = if nbits <= uint::bits {
let rep = if nbits < uint::bits {
Small(SmallBitv::new(if init {(1<<nbits)-1} else {0}))
} else if nbits == uint::bits {
Small(SmallBitv::new(if init {!0} else {0}))
}
else {
let nelems = nbits/uint::bits +
if nbits % uint::bits == 0 {0} else {1};
let elem = if init {!0u} else {0u};
let s = vec::from_elem(nelems, elem);
} else {
let exact = nbits % uint::bits == 0;
let nelems = nbits/uint::bits + if exact {0} else {1};
let s =
if init {
if exact {
vec::from_elem(nelems, !0u)
} else {
let mut v = vec::from_elem(nelems-1, !0u);
v.push((1<<nbits % uint::bits)-1);
v
}
} else { vec::from_elem(nelems, 0u)};
Big(BigBitv::new(s))
};
Bitv {rep: rep, nbits: nbits}
Expand Down Expand Up @@ -1329,6 +1338,20 @@ mod tests {
assert_eq!(idxs, ~[0, 2, 3]);
}

#[test]
fn test_bitv_set_frombitv_init() {
let bools = [true, false];
let lengths = [10, 64, 100];
for &b in bools.iter() {
for &l in lengths.iter() {
let bitset = BitvSet::from_bitv(Bitv::new(l, b));
assert_eq!(bitset.contains(&1u), b)
assert_eq!(bitset.contains(&(l-1u)), b)
assert!(!bitset.contains(&l))
}
}
}

#[test]
fn test_small_difference() {
let mut b1 = Bitv::new(3, false);
Expand Down