Modernize bitv mut fields and explicit self

This commit is contained in:
Alex Crichton 2013-02-17 17:07:51 -05:00
parent a6945f2a45
commit 91fae27912
4 changed files with 144 additions and 142 deletions

View file

@ -16,11 +16,7 @@ use core::vec;
struct SmallBitv {
/// only the lowest nbits of this value are used. the rest is undefined.
mut bits: u32
}
fn SmallBitv(bits: u32) -> SmallBitv {
SmallBitv {bits: bits}
bits: u32
}
/// a mask that has a 1 for each defined bit in a small_bitv, assuming n bits
@ -30,9 +26,13 @@ fn small_mask(nbits: uint) -> u32 {
}
impl SmallBitv {
static fn new(bits: u32) -> SmallBitv {
SmallBitv {bits: bits}
}
#[inline(always)]
fn bits_op(right_bits: u32, nbits: uint, f: fn(u32, u32) -> u32) -> bool {
fn bits_op(&mut self, right_bits: u32, nbits: uint,
f: fn(u32, u32) -> u32) -> bool {
let mask = small_mask(nbits);
let old_b: u32 = self.bits;
let new_b = f(old_b, right_bits);
@ -41,32 +41,32 @@ impl SmallBitv {
}
#[inline(always)]
fn union(s: &SmallBitv, nbits: uint) -> bool {
fn union(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |u1, u2| u1 | u2)
}
#[inline(always)]
fn intersect(s: &SmallBitv, nbits: uint) -> bool {
fn intersect(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |u1, u2| u1 & u2)
}
#[inline(always)]
fn become(s: &SmallBitv, nbits: uint) -> bool {
fn become(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |_u1, u2| u2)
}
#[inline(always)]
fn difference(s: &SmallBitv, nbits: uint) -> bool {
fn difference(&mut self, s: &SmallBitv, nbits: uint) -> bool {
self.bits_op(s.bits, nbits, |u1, u2| u1 & !u2)
}
#[inline(always)]
pure fn get(i: uint) -> bool {
pure fn get(&self, i: uint) -> bool {
(self.bits & (1 << i)) != 0
}
#[inline(always)]
fn set(i: uint, x: bool) {
fn set(&mut self, i: uint, x: bool) {
if x {
self.bits |= 1<<i;
}
@ -76,39 +76,34 @@ impl SmallBitv {
}
#[inline(always)]
fn equals(b: &SmallBitv, nbits: uint) -> bool {
fn equals(&self, b: &SmallBitv, nbits: uint) -> bool {
let mask = small_mask(nbits);
mask & self.bits == mask & b.bits
}
#[inline(always)]
fn clear() { self.bits = 0; }
fn clear(&mut self) { self.bits = 0; }
#[inline(always)]
fn set_all() { self.bits = !0; }
fn set_all(&mut self) { self.bits = !0; }
#[inline(always)]
fn is_true(nbits: uint) -> bool {
fn is_true(&self, nbits: uint) -> bool {
small_mask(nbits) & !self.bits == 0
}
#[inline(always)]
fn is_false(nbits: uint) -> bool {
fn is_false(&self, nbits: uint) -> bool {
small_mask(nbits) & self.bits == 0
}
#[inline(always)]
fn invert() { self.bits = !self.bits; }
fn invert(&mut self) { self.bits = !self.bits; }
}
struct BigBitv {
// only mut b/c of clone and lack of other constructor
mut storage: ~[uint]
}
fn BigBitv(storage: ~[uint]) -> BigBitv {
BigBitv {storage: storage}
storage: ~[uint]
}
/**
@ -128,9 +123,13 @@ fn big_mask(nbits: uint, elem: uint) -> uint {
}
impl BigBitv {
static fn new(storage: ~[uint]) -> BigBitv {
BigBitv {storage: storage}
}
#[inline(always)]
fn process(b: &BigBitv, nbits: uint, op: fn(uint, uint) -> uint) -> bool {
fn process(&mut self, b: &BigBitv, nbits: uint,
op: fn(uint, uint) -> uint) -> bool {
let len = b.storage.len();
assert (self.storage.len() == len);
let mut changed = false;
@ -151,7 +150,7 @@ impl BigBitv {
}
#[inline(always)]
fn each_storage(op: fn(v: &mut uint) -> bool) {
fn each_storage(&mut self, op: fn(v: &mut uint) -> bool) {
for uint::range(0, self.storage.len()) |i| {
let mut w = self.storage[i];
let b = op(&mut w);
@ -161,30 +160,30 @@ impl BigBitv {
}
#[inline(always)]
fn invert() { for self.each_storage() |w| { *w = !*w } }
fn invert(&mut self) { for self.each_storage |w| { *w = !*w } }
#[inline(always)]
fn union(b: &BigBitv, nbits: uint) -> bool {
fn union(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, lor)
}
#[inline(always)]
fn intersect(b: &BigBitv, nbits: uint) -> bool {
fn intersect(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, land)
}
#[inline(always)]
fn become(b: &BigBitv, nbits: uint) -> bool {
fn become(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, right)
}
#[inline(always)]
fn difference(b: &BigBitv, nbits: uint) -> bool {
fn difference(&mut self, b: &BigBitv, nbits: uint) -> bool {
self.process(b, nbits, difference)
}
#[inline(always)]
pure fn get(i: uint) -> bool {
pure fn get(&self, i: uint) -> bool {
let w = i / uint_bits;
let b = i % uint_bits;
let x = 1 & self.storage[w] >> b;
@ -192,16 +191,16 @@ impl BigBitv {
}
#[inline(always)]
fn set(i: uint, x: bool) {
fn set(&mut self, i: uint, x: bool) {
let w = i / uint_bits;
let b = i % uint_bits;
let flag = 1 << b;
self.storage[w] = if x { self.storage[w] | flag }
else { self.storage[w] & !flag };
else { self.storage[w] & !flag };
}
#[inline(always)]
fn equals(b: &BigBitv, nbits: uint) -> bool {
fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
let len = b.storage.len();
for uint::iterate(0, len) |i| {
let mask = big_mask(nbits, i);
@ -223,33 +222,19 @@ pub struct Bitv {
nbits: uint
}
pub fn Bitv (nbits: uint, init: bool) -> Bitv {
let rep = if nbits <= 32 {
Small(~SmallBitv(if init {!0} else {0}))
}
else {
let nelems = nbits/uint_bits +
if nbits % uint_bits == 0 {0} else {1};
let elem = if init {!0} else {0};
let s = from_elem(nelems, elem);
Big(~BigBitv(s))
};
Bitv {rep: rep, nbits: nbits}
}
priv impl Bitv {
fn die() -> ! {
fn die(&self) -> ! {
fail!(~"Tried to do operation on bit vectors with different sizes");
}
#[inline(always)]
fn do_op(op: Op, other: &Bitv) -> bool {
fn do_op(&mut self, op: Op, other: &Bitv) -> bool {
if self.nbits != other.nbits {
self.die();
}
match self.rep {
Small(ref s) => match other.rep {
Small(ref mut s) => match other.rep {
Small(ref s1) => match op {
Union => s.union(*s1, self.nbits),
Intersect => s.intersect(*s1, self.nbits),
@ -258,7 +243,7 @@ priv impl Bitv {
},
Big(_) => self.die()
},
Big(ref s) => match other.rep {
Big(ref mut s) => match other.rep {
Small(_) => self.die(),
Big(ref s1) => match op {
Union => s.union(*s1, self.nbits),
@ -273,6 +258,19 @@ priv impl Bitv {
}
impl Bitv {
static fn new(nbits: uint, init: bool) -> Bitv {
let rep = if nbits <= 32 {
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 {!0} else {0};
let s = from_elem(nelems, elem);
Big(~BigBitv::new(s))
};
Bitv {rep: rep, nbits: nbits}
}
/**
* Calculates the union of two bitvectors
@ -281,7 +279,7 @@ impl Bitv {
* the same length. Returns 'true' if `self` changed.
*/
#[inline(always)]
fn union(v1: &Bitv) -> bool { self.do_op(Union, v1) }
fn union(&mut self, v1: &Bitv) -> bool { self.do_op(Union, v1) }
/**
* Calculates the intersection of two bitvectors
@ -290,7 +288,7 @@ impl Bitv {
* must be the same length. Returns 'true' if `self` changed.
*/
#[inline(always)]
fn intersect(v1: &Bitv) -> bool { self.do_op(Intersect, v1) }
fn intersect(&mut self, v1: &Bitv) -> bool { self.do_op(Intersect, v1) }
/**
* Assigns the value of `v1` to `self`
@ -299,11 +297,11 @@ impl Bitv {
* changed
*/
#[inline(always)]
fn assign(v: &Bitv) -> bool { self.do_op(Assign, v) }
fn assign(&mut self, v: &Bitv) -> bool { self.do_op(Assign, v) }
/// Retrieve the value at index `i`
#[inline(always)]
pure fn get(i: uint) -> bool {
pure fn get(&self, i: uint) -> bool {
assert (i < self.nbits);
match self.rep {
Big(ref b) => b.get(i),
@ -317,11 +315,11 @@ impl Bitv {
* `i` must be less than the length of the bitvector.
*/
#[inline(always)]
fn set(i: uint, x: bool) {
fn set(&mut self, i: uint, x: bool) {
assert (i < self.nbits);
match self.rep {
Big(ref b) => b.set(i, x),
Small(ref s) => s.set(i, x)
Big(ref mut b) => b.set(i, x),
Small(ref mut s) => s.set(i, x)
}
}
@ -332,7 +330,7 @@ impl Bitv {
* bitvectors contain identical elements.
*/
#[inline(always)]
fn equal(v1: &Bitv) -> bool {
fn equal(&self, v1: &Bitv) -> bool {
if self.nbits != v1.nbits { return false; }
match self.rep {
Small(ref b) => match v1.rep {
@ -348,27 +346,27 @@ impl Bitv {
/// Set all bits to 0
#[inline(always)]
fn clear() {
fn clear(&mut self) {
match self.rep {
Small(ref b) => b.clear(),
Big(ref s) => for s.each_storage() |w| { *w = 0u }
Small(ref mut b) => b.clear(),
Big(ref mut s) => for s.each_storage() |w| { *w = 0u }
}
}
/// Set all bits to 1
#[inline(always)]
fn set_all() {
fn set_all(&mut self) {
match self.rep {
Small(ref b) => b.set_all(),
Big(ref s) => for s.each_storage() |w| { *w = !0u } }
Small(ref mut b) => b.set_all(),
Big(ref mut s) => for s.each_storage() |w| { *w = !0u } }
}
/// Invert all bits
#[inline(always)]
fn invert() {
fn invert(&mut self) {
match self.rep {
Small(ref b) => b.invert(),
Big(ref s) => for s.each_storage() |w| { *w = !*w } }
Small(ref mut b) => b.invert(),
Big(ref mut s) => for s.each_storage() |w| { *w = !*w } }
}
/**
@ -381,11 +379,11 @@ impl Bitv {
* Returns `true` if `v0` was changed.
*/
#[inline(always)]
fn difference(v: &Bitv) -> bool { self.do_op(Difference, v) }
fn difference(&mut self, v: &Bitv) -> bool { self.do_op(Difference, v) }
/// Returns true if all bits are 1
#[inline(always)]
fn is_true() -> bool {
fn is_true(&self) -> bool {
match self.rep {
Small(ref b) => b.is_true(self.nbits),
_ => {
@ -396,7 +394,7 @@ impl Bitv {
}
#[inline(always)]
fn each(f: fn(bool) -> bool) {
fn each(&self, f: fn(bool) -> bool) {
let mut i = 0;
while i < self.nbits {
if !f(self.get(i)) { break; }
@ -405,7 +403,7 @@ impl Bitv {
}
/// Returns true if all bits are 0
fn is_false() -> bool {
fn is_false(&self) -> bool {
match self.rep {
Small(ref b) => b.is_false(self.nbits),
Big(_) => {
@ -415,7 +413,7 @@ impl Bitv {
}
}
fn init_to_vec(i: uint) -> uint {
fn init_to_vec(&self, i: uint) -> uint {
return if self.get(i) { 1 } else { 0 };
}
@ -424,7 +422,7 @@ impl Bitv {
*
* Each uint in the resulting vector has either value 0u or 1u.
*/
fn to_vec() -> ~[uint] {
fn to_vec(&self) -> ~[uint] {
vec::from_fn(self.nbits, |x| self.init_to_vec(x))
}
@ -434,7 +432,7 @@ impl Bitv {
* size of the bitv is not a multiple of 8 then trailing bits
* will be filled-in with false/0
*/
fn to_bytes() -> ~[u8] {
fn to_bytes(&self) -> ~[u8] {
fn bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 {
let offset = byte * 8 + bit;
@ -448,21 +446,21 @@ impl Bitv {
let len = self.nbits/8 +
if self.nbits % 8 == 0 { 0 } else { 1 };
vec::from_fn(len, |i|
bit(&self, i, 0) |
bit(&self, i, 1) |
bit(&self, i, 2) |
bit(&self, i, 3) |
bit(&self, i, 4) |
bit(&self, i, 5) |
bit(&self, i, 6) |
bit(&self, i, 7)
bit(self, i, 0) |
bit(self, i, 1) |
bit(self, i, 2) |
bit(self, i, 3) |
bit(self, i, 4) |
bit(self, i, 5) |
bit(self, i, 6) |
bit(self, i, 7)
)
}
/**
* Transform self into a [bool] by turning each bit into a bool
*/
fn to_bools() -> ~[bool] {
fn to_bools(&self) -> ~[bool] {
vec::from_fn(self.nbits, |i| self[i])
}
@ -485,7 +483,7 @@ impl Bitv {
* The uint vector is expected to only contain the values 0u and 1u. Both
* the bitvector and vector must have the same length
*/
fn eq_vec(v: ~[uint]) -> bool {
fn eq_vec(&self, v: ~[uint]) -> bool {
assert self.nbits == v.len();
let mut i = 0;
while i < self.nbits {
@ -497,7 +495,7 @@ impl Bitv {
true
}
fn ones(f: fn(uint) -> bool) {
fn ones(&self, f: fn(uint) -> bool) {
for uint::range(0, self.nbits) |i| {
if self.get(i) {
if !f(i) { break }
@ -551,7 +549,7 @@ pub fn from_bools(bools: &[bool]) -> Bitv {
* index is f(index).
*/
pub fn from_fn(len: uint, f: fn(index: uint) -> bool) -> Bitv {
let bitv = Bitv(len, false);
let mut bitv = Bitv::new(len, false);
for uint::range(0, len) |i| {
bitv.set(i, f(i));
}
@ -586,10 +584,10 @@ mod tests {
#[test]
pub fn test_to_str() {
let zerolen = Bitv(0u, false);
let zerolen = Bitv::new(0u, false);
assert zerolen.to_str() == ~"";
let eightbits = Bitv(8u, false);
let eightbits = Bitv::new(8u, false);
assert eightbits.to_str() == ~"00000000";
}
@ -597,7 +595,7 @@ mod tests {
pub fn test_0_elements() {
let mut act;
let mut exp;
act = Bitv(0u, false);
act = Bitv::new(0u, false);
exp = vec::from_elem::<uint>(0u, 0u);
assert act.eq_vec(exp);
}
@ -605,15 +603,15 @@ mod tests {
#[test]
pub fn test_1_element() {
let mut act;
act = Bitv(1u, false);
act = Bitv::new(1u, false);
assert act.eq_vec(~[0u]);
act = Bitv(1u, true);
act = Bitv::new(1u, true);
assert act.eq_vec(~[1u]);
}
#[test]
pub fn test_2_elements() {
let b = bitv::Bitv(2, false);
let mut b = bitv::Bitv::new(2, false);
b.set(0, true);
b.set(1, false);
assert b.to_str() == ~"10";
@ -624,15 +622,15 @@ mod tests {
let mut act;
// all 0
act = Bitv(10u, false);
act = Bitv::new(10u, false);
assert (act.eq_vec(~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = Bitv(10u, true);
act = Bitv::new(10u, true);
assert (act.eq_vec(~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = Bitv(10u, false);
act = Bitv::new(10u, false);
act.set(0u, true);
act.set(1u, true);
act.set(2u, true);
@ -641,7 +639,7 @@ mod tests {
assert (act.eq_vec(~[1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = Bitv(10u, false);
act = Bitv::new(10u, false);
act.set(5u, true);
act.set(6u, true);
act.set(7u, true);
@ -650,7 +648,7 @@ mod tests {
assert (act.eq_vec(~[0u, 0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = Bitv(10u, false);
act = Bitv::new(10u, false);
act.set(0u, true);
act.set(3u, true);
act.set(6u, true);
@ -663,21 +661,21 @@ mod tests {
let mut act;
// all 0
act = Bitv(31u, false);
act = Bitv::new(31u, false);
assert (act.eq_vec(
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u]));
// all 1
act = Bitv(31u, true);
act = Bitv::new(31u, true);
assert (act.eq_vec(
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u]));
// mixed
act = Bitv(31u, false);
act = Bitv::new(31u, false);
act.set(0u, true);
act.set(1u, true);
act.set(2u, true);
@ -692,7 +690,7 @@ mod tests {
0u, 0u, 0u, 0u, 0u]));
// mixed
act = Bitv(31u, false);
act = Bitv::new(31u, false);
act.set(16u, true);
act.set(17u, true);
act.set(18u, true);
@ -707,7 +705,7 @@ mod tests {
0u, 0u, 0u, 0u, 0u]));
// mixed
act = Bitv(31u, false);
act = Bitv::new(31u, false);
act.set(24u, true);
act.set(25u, true);
act.set(26u, true);
@ -721,7 +719,7 @@ mod tests {
1u, 1u, 1u, 1u, 1u]));
// mixed
act = Bitv(31u, false);
act = Bitv::new(31u, false);
act.set(3u, true);
act.set(17u, true);
act.set(30u, true);
@ -736,21 +734,21 @@ mod tests {
let mut act;
// all 0
act = Bitv(32u, false);
act = Bitv::new(32u, false);
assert (act.eq_vec(
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = Bitv(32u, true);
act = Bitv::new(32u, true);
assert (act.eq_vec(
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = Bitv(32u, false);
act = Bitv::new(32u, false);
act.set(0u, true);
act.set(1u, true);
act.set(2u, true);
@ -765,7 +763,7 @@ mod tests {
0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = Bitv(32u, false);
act = Bitv::new(32u, false);
act.set(16u, true);
act.set(17u, true);
act.set(18u, true);
@ -780,7 +778,7 @@ mod tests {
0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = Bitv(32u, false);
act = Bitv::new(32u, false);
act.set(24u, true);
act.set(25u, true);
act.set(26u, true);
@ -795,7 +793,7 @@ mod tests {
1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = Bitv(32u, false);
act = Bitv::new(32u, false);
act.set(3u, true);
act.set(17u, true);
act.set(30u, true);
@ -811,21 +809,21 @@ mod tests {
let mut act;
// all 0
act = Bitv(33u, false);
act = Bitv::new(33u, false);
assert (act.eq_vec(
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// all 1
act = Bitv(33u, true);
act = Bitv::new(33u, true);
assert (act.eq_vec(
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
1u, 1u, 1u, 1u, 1u, 1u, 1u]));
// mixed
act = Bitv(33u, false);
act = Bitv::new(33u, false);
act.set(0u, true);
act.set(1u, true);
act.set(2u, true);
@ -840,7 +838,7 @@ mod tests {
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = Bitv(33u, false);
act = Bitv::new(33u, false);
act.set(16u, true);
act.set(17u, true);
act.set(18u, true);
@ -855,7 +853,7 @@ mod tests {
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
// mixed
act = Bitv(33u, false);
act = Bitv::new(33u, false);
act.set(24u, true);
act.set(25u, true);
act.set(26u, true);
@ -870,7 +868,7 @@ mod tests {
1u, 1u, 1u, 1u, 1u, 1u, 0u]));
// mixed
act = Bitv(33u, false);
act = Bitv::new(33u, false);
act.set(3u, true);
act.set(17u, true);
act.set(30u, true);
@ -884,24 +882,24 @@ mod tests {
#[test]
pub fn test_equal_differing_sizes() {
let v0 = Bitv(10u, false);
let v1 = Bitv(11u, false);
let v0 = Bitv::new(10u, false);
let v1 = Bitv::new(11u, false);
assert !v0.equal(&v1);
}
#[test]
pub fn test_equal_greatly_differing_sizes() {
let v0 = Bitv(10u, false);
let v1 = Bitv(110u, false);
let v0 = Bitv::new(10u, false);
let v1 = Bitv::new(110u, false);
assert !v0.equal(&v1);
}
#[test]
pub fn test_equal_sneaky_small() {
let a = bitv::Bitv(1, false);
let mut a = bitv::Bitv::new(1, false);
a.set(0, true);
let b = bitv::Bitv(1, true);
let mut b = bitv::Bitv::new(1, true);
b.set(0, true);
assert a.equal(&b);
@ -909,12 +907,12 @@ mod tests {
#[test]
pub fn test_equal_sneaky_big() {
let a = bitv::Bitv(100, false);
let mut a = bitv::Bitv::new(100, false);
for uint::range(0, 100) |i| {
a.set(i, true);
}
let b = bitv::Bitv(100, true);
let mut b = bitv::Bitv::new(100, true);
for uint::range(0, 100) |i| {
b.set(i, true);
}
@ -931,11 +929,11 @@ mod tests {
#[test]
pub fn test_to_bytes() {
let bv = Bitv(3, true);
let mut bv = Bitv::new(3, true);
bv.set(1, false);
assert bv.to_bytes() == ~[0b10100000];
let bv = Bitv(9, false);
let mut bv = Bitv::new(9, false);
bv.set(2, true);
bv.set(8, true);
assert bv.to_bytes() == ~[0b00100000, 0b10000000];
@ -954,8 +952,8 @@ mod tests {
#[test]
pub fn test_small_difference() {
let b1 = Bitv(3, false);
let b2 = Bitv(3, false);
let mut b1 = Bitv::new(3, false);
let mut b2 = Bitv::new(3, false);
b1.set(0, true);
b1.set(1, true);
b2.set(1, true);
@ -968,8 +966,8 @@ mod tests {
#[test]
pub fn test_big_difference() {
let b1 = Bitv(100, false);
let b2 = Bitv(100, false);
let mut b1 = Bitv::new(100, false);
let mut b2 = Bitv::new(100, false);
b1.set(0, true);
b1.set(40, true);
b2.set(40, true);
@ -982,7 +980,7 @@ mod tests {
#[test]
pub fn test_small_clear() {
let b = Bitv(14, true);
let mut b = Bitv::new(14, true);
b.clear();
for b.ones |i| {
fail!(fmt!("found 1 at %?", i));
@ -991,7 +989,7 @@ mod tests {
#[test]
pub fn test_big_clear() {
let b = Bitv(140, true);
let mut b = Bitv::new(140, true);
b.clear();
for b.ones |i| {
fail!(fmt!("found 1 at %?", i));

View file

@ -43,13 +43,13 @@ use ext::base::ext_ctxt;
use ext::pipes::proto::protocol;
use core::str;
use std::bitv::{Bitv};
use std::bitv::Bitv;
pub fn analyze(proto: protocol, _cx: ext_ctxt) {
debug!("initializing colive analysis");
let num_states = proto.num_states();
let colive = do (copy proto.states).map_to_vec |state| {
let bv = ~Bitv(num_states, false);
let mut colive = do (copy proto.states).map_to_vec |state| {
let mut bv = ~Bitv::new(num_states, false);
for state.reachable |s| {
bv.set(s.id, true);
}
@ -61,15 +61,19 @@ pub fn analyze(proto: protocol, _cx: ext_ctxt) {
while changed {
changed = false;
debug!("colive iteration %?", i);
let mut new_colive = ~[];
for colive.eachi |i, this_colive| {
let mut result = ~this_colive.clone();
let this = proto.get_state_by_id(i);
for this_colive.ones |j| {
let next = proto.get_state_by_id(j);
if this.dir == next.dir {
changed = changed || this_colive.union(colive[j]);
changed = result.union(colive[j]) || changed;
}
}
new_colive.push(result)
}
colive = new_colive;
i += 1;
}

View file

@ -58,7 +58,7 @@ pub fn solve_grid(g: grid_t) {
fn next_color(mut g: grid, row: u8, col: u8, start_color: u8) -> bool {
if start_color < 10u8 {
// colors not yet used
let avail = bitv::Bitv(10u, false);
let mut avail = bitv::Bitv::new(10u, false);
for u8::range(start_color, 10u8) |color| {
avail.set(color as uint, true);
}
@ -80,7 +80,7 @@ pub fn solve_grid(g: grid_t) {
// find colors available in neighbourhood of (row, col)
fn drop_colors(g: grid, avail: bitv::Bitv, row: u8, col: u8) {
fn drop_color(g: grid, colors: bitv::Bitv, row: u8, col: u8) {
fn drop_color(g: grid, mut colors: bitv::Bitv, row: u8, col: u8) {
let color = g[row][col];
if color != 0u8 { colors.set(color as uint, false); }
}

View file

@ -14,8 +14,8 @@ extern mod std;
use std::bitv::*;
fn bitv_test() -> bool {
let v1 = ~Bitv(31, false);
let v2 = ~Bitv(31, true);
let mut v1 = ~Bitv::new(31, false);
let v2 = ~Bitv::new(31, true);
v1.union(v2);
true
}