2012-09-19 21:07:58 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-09-04 13:23:53 -05:00
|
|
|
use vec::{to_mut, from_elem};
|
2012-07-29 18:00:55 -05:00
|
|
|
|
2012-09-02 10:10:14 -05:00
|
|
|
export Bitv, from_bytes, from_bools, from_fn;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
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}
|
|
|
|
}
|
|
|
|
|
2012-08-21 13:42:16 -05:00
|
|
|
/// a mask that has a 1 for each defined bit in a small_bitv, assuming n bits
|
|
|
|
#[inline(always)]
|
|
|
|
fn small_mask(nbits: uint) -> u32 {
|
|
|
|
(1 << nbits) - 1
|
|
|
|
}
|
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
impl SmallBitv {
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn bits_op(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);
|
|
|
|
self.bits = new_b;
|
|
|
|
mask & old_b != mask & new_b
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn union(s: &SmallBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
self.bits_op(s.bits, nbits, |u1, u2| u1 | u2)
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn intersect(s: &SmallBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
self.bits_op(s.bits, nbits, |u1, u2| u1 & u2)
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn become(s: &SmallBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
self.bits_op(s.bits, nbits, |_u1, u2| u2)
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn difference(s: &SmallBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
self.bits_op(s.bits, nbits, |u1, u2| u1 ^ u2)
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn get(i: uint) -> bool {
|
|
|
|
(self.bits & (1 << i)) != 0
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(i: uint, x: bool) {
|
|
|
|
if x {
|
|
|
|
self.bits |= 1<<i;
|
|
|
|
}
|
|
|
|
else {
|
2012-08-21 13:41:29 -05:00
|
|
|
self.bits &= !(1<<i as u32);
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn equals(b: &SmallBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
let mask = small_mask(nbits);
|
|
|
|
mask & self.bits == mask & b.bits
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn clear() { self.bits = 0; }
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn set_all() { self.bits = !0; }
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-21 13:42:16 -05:00
|
|
|
fn is_true(nbits: uint) -> bool {
|
|
|
|
small_mask(nbits) & !self.bits == 0
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-21 13:42:16 -05:00
|
|
|
fn is_false(nbits: uint) -> bool {
|
|
|
|
small_mask(nbits) & self.bits == 0
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn invert() { self.bits = !self.bits; }
|
2012-09-02 12:04:38 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
struct BigBitv {
|
|
|
|
// only mut b/c of clone and lack of other constructor
|
|
|
|
mut storage: ~[mut uint]
|
|
|
|
}
|
|
|
|
|
2012-09-19 21:07:58 -05:00
|
|
|
fn BigBitv(+storage: ~[mut uint]) -> BigBitv {
|
2012-09-02 12:04:38 -05:00
|
|
|
BigBitv {storage: storage}
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2010-10-21 09:36:13 -05:00
|
|
|
|
2012-08-21 13:42:16 -05:00
|
|
|
/**
|
|
|
|
* a mask that has a 1 for each defined bit in the nth element of a big_bitv,
|
|
|
|
* assuming n bits.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
|
|
|
fn big_mask(nbits: uint, elem: uint) -> uint {
|
|
|
|
let rmd = nbits % uint_bits;
|
|
|
|
let nelems = nbits/uint_bits + if rmd == 0 {0} else {1};
|
|
|
|
|
|
|
|
if elem < nelems - 1 || rmd == 0 {
|
|
|
|
!0
|
|
|
|
} else {
|
|
|
|
(1 << rmd) - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
impl BigBitv {
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn process(b: &BigBitv, nbits: uint, op: fn(uint, uint) -> uint) -> bool {
|
|
|
|
let len = b.storage.len();
|
|
|
|
assert (self.storage.len() == len);
|
|
|
|
let mut changed = false;
|
|
|
|
do uint::range(0, len) |i| {
|
|
|
|
let mask = big_mask(nbits, i);
|
|
|
|
let w0 = self.storage[i] & mask;
|
|
|
|
let w1 = b.storage[i] & mask;
|
|
|
|
let w = op(w0, w1) & mask;
|
2012-09-18 13:17:40 -05:00
|
|
|
if w0 != w unsafe {
|
2012-09-02 12:04:38 -05:00
|
|
|
changed = true;
|
|
|
|
self.storage[i] = w;
|
2012-08-21 13:42:16 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
true
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
changed
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-09-23 06:39:27 -05:00
|
|
|
fn each_storage(op: fn(&v: uint) -> bool) {
|
2012-07-29 18:00:55 -05:00
|
|
|
for uint::range(0, self.storage.len()) |i| {
|
|
|
|
let mut w = self.storage[i];
|
|
|
|
let b = !op(w);
|
|
|
|
self.storage[i] = w;
|
|
|
|
if !b { break; }
|
|
|
|
}
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn invert() { for self.each_storage() |w| { w = !w } }
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn union(b: &BigBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
self.process(b, nbits, lor)
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn intersect(b: &BigBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
self.process(b, nbits, land)
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn become(b: &BigBitv, nbits: uint) -> bool {
|
2012-08-21 13:42:16 -05:00
|
|
|
self.process(b, nbits, right)
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn difference(b: &BigBitv, nbits: uint) -> bool {
|
2012-07-29 18:00:55 -05:00
|
|
|
self.invert();
|
2012-08-21 13:42:16 -05:00
|
|
|
let b = self.intersect(b, nbits);
|
2012-07-29 18:00:55 -05:00
|
|
|
self.invert();
|
|
|
|
b
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
pure fn get(i: uint) -> bool {
|
|
|
|
let w = i / uint_bits;
|
|
|
|
let b = i % uint_bits;
|
|
|
|
let x = 1 & self.storage[w] >> b;
|
|
|
|
x == 1
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(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 };
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn equals(b: &BigBitv, nbits: uint) -> bool {
|
2012-07-29 18:00:55 -05:00
|
|
|
let len = b.storage.len();
|
|
|
|
for uint::iterate(0, len) |i| {
|
2012-08-21 13:42:16 -05:00
|
|
|
let mask = big_mask(nbits, i);
|
|
|
|
if mask & self.storage[i] != mask & b.storage[i] {
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
enum BitvVariant { Big(~BigBitv), Small(~SmallBitv) }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
enum Op {Union, Intersect, Assign, Difference}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
// The bitvector type
|
2012-08-11 09:08:42 -05:00
|
|
|
struct Bitv {
|
2012-09-02 12:04:38 -05:00
|
|
|
rep: BitvVariant,
|
|
|
|
nbits: uint
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
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 = to_mut(from_elem(nelems, elem));
|
2012-09-11 19:17:54 -05:00
|
|
|
Big(~BigBitv(move s))
|
2012-09-02 12:04:38 -05:00
|
|
|
};
|
|
|
|
Bitv {rep: rep, nbits: nbits}
|
|
|
|
}
|
|
|
|
|
|
|
|
priv impl Bitv {
|
|
|
|
|
|
|
|
fn die() -> ! {
|
|
|
|
fail ~"Tried to do operation on bit vectors with different sizes";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn do_op(op: Op, other: &Bitv) -> bool {
|
|
|
|
if self.nbits != other.nbits {
|
|
|
|
self.die();
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
match self.rep {
|
|
|
|
Small(s) => match other.rep {
|
|
|
|
Small(s1) => match op {
|
|
|
|
Union => s.union(s1, self.nbits),
|
|
|
|
Intersect => s.intersect(s1, self.nbits),
|
|
|
|
Assign => s.become(s1, self.nbits),
|
|
|
|
Difference => s.difference(s1, self.nbits)
|
|
|
|
},
|
|
|
|
Big(_) => self.die()
|
|
|
|
},
|
|
|
|
Big(s) => match other.rep {
|
|
|
|
Small(_) => self.die(),
|
|
|
|
Big(s1) => match op {
|
|
|
|
Union => s.union(s1, self.nbits),
|
|
|
|
Intersect => s.intersect(s1, self.nbits),
|
|
|
|
Assign => s.become(s1, self.nbits),
|
|
|
|
Difference => s.difference(s1, self.nbits)
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-09-02 12:04:38 -05:00
|
|
|
}
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2011-10-27 17:35:56 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Bitv {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the union of two bitvectors
|
|
|
|
*
|
|
|
|
* Sets `self` to the union of `self` and `v1`. Both bitvectors must be
|
|
|
|
* the same length. Returns 'true' if `self` changed.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn union(v1: &Bitv) -> bool { self.do_op(Union, v1) }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Calculates the intersection of two bitvectors
|
|
|
|
*
|
|
|
|
* Sets `self` to the intersection of `self` and `v1`. Both bitvectors
|
|
|
|
* must be the same length. Returns 'true' if `self` changed.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn intersect(v1: &Bitv) -> bool { self.do_op(Intersect, v1) }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Assigns the value of `v1` to `self`
|
|
|
|
*
|
|
|
|
* Both bitvectors must be the same length. Returns `true` if `self` was
|
|
|
|
* changed
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn assign(v: &Bitv) -> bool { self.do_op(Assign, v) }
|
2012-07-29 18:00:55 -05:00
|
|
|
|
|
|
|
/// Makes a copy of a bitvector
|
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn clone() -> ~Bitv {
|
2012-08-06 14:34:08 -05:00
|
|
|
~match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Small(b) => {
|
|
|
|
Bitv{nbits: self.nbits, rep: Small(~SmallBitv{bits: b.bits})}
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2012-08-11 09:08:42 -05:00
|
|
|
Big(b) => {
|
2012-07-29 18:00:55 -05:00
|
|
|
let st = to_mut(from_elem(self.nbits / uint_bits + 1, 0));
|
|
|
|
let len = st.len();
|
|
|
|
for uint::range(0, len) |i| { st[i] = b.storage[i]; };
|
2012-08-11 09:08:42 -05:00
|
|
|
Bitv{nbits: self.nbits, rep: Big(~BigBitv{storage: st})}
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
/// Retrieve the value at index `i`
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn get(i: uint) -> bool {
|
|
|
|
assert (i < self.nbits);
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Big(b) => b.get(i),
|
|
|
|
Small(s) => s.get(i)
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2011-04-12 14:16:21 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Set the value of a bit at a given index
|
|
|
|
*
|
|
|
|
* `i` must be less than the length of the bitvector.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(i: uint, x: bool) {
|
|
|
|
assert (i < self.nbits);
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Big(b) => b.set(i, x),
|
|
|
|
Small(s) => s.set(i, x)
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Compares two bitvectors
|
|
|
|
*
|
|
|
|
* Both bitvectors must be the same length. Returns `true` if both
|
|
|
|
* bitvectors contain identical elements.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn equal(v1: Bitv) -> bool {
|
2012-08-01 19:30:05 -05:00
|
|
|
if self.nbits != v1.nbits { return false; }
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Small(b) => match v1.rep {
|
|
|
|
Small(b1) => b.equals(b1, self.nbits),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => false
|
2012-08-06 19:14:32 -05:00
|
|
|
},
|
2012-08-11 09:08:42 -05:00
|
|
|
Big(s) => match v1.rep {
|
|
|
|
Big(s1) => s.equals(s1, self.nbits),
|
|
|
|
Small(_) => return false
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
/// Set all bits to 0
|
|
|
|
#[inline(always)]
|
|
|
|
fn clear() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Small(b) => b.clear(),
|
|
|
|
Big(s) => for s.each_storage() |w| { w = 0u }
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
/// Set all bits to 1
|
|
|
|
#[inline(always)]
|
|
|
|
fn set_all() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Small(b) => b.set_all(),
|
|
|
|
Big(s) => for s.each_storage() |w| { w = !0u } }
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2011-05-18 17:38:11 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
/// Invert all bits
|
|
|
|
#[inline(always)]
|
|
|
|
fn invert() {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Small(b) => b.invert(),
|
|
|
|
Big(s) => for s.each_storage() |w| { w = !w } }
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Calculate the difference between two bitvectors
|
|
|
|
*
|
|
|
|
* Sets each element of `v0` to the value of that element minus the
|
|
|
|
* element of `v1` at the same index. Both bitvectors must be the same
|
|
|
|
* length.
|
|
|
|
*
|
|
|
|
* Returns `true` if `v0` was changed.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2012-08-11 09:08:42 -05:00
|
|
|
fn difference(v: ~Bitv) -> bool { self.do_op(Difference, v) }
|
2012-07-29 18:00:55 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/// Returns true if all bits are 1
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn is_true() -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Small(b) => b.is_true(self.nbits),
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => {
|
2012-08-01 19:30:05 -05:00
|
|
|
for self.each() |i| { if !i { return false; } }
|
2012-07-29 18:00:55 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn each(f: fn(bool) -> bool) {
|
|
|
|
let mut i = 0;
|
|
|
|
while i < self.nbits {
|
|
|
|
if !f(self.get(i)) { break; }
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
/// Returns true if all bits are 0
|
|
|
|
fn is_false() -> bool {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.rep {
|
2012-08-11 09:08:42 -05:00
|
|
|
Small(b) => b.is_false(self.nbits),
|
|
|
|
Big(_) => {
|
2012-08-01 19:30:05 -05:00
|
|
|
for self.each() |i| { if i { return false; } }
|
2012-07-29 18:00:55 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
fn init_to_vec(i: uint) -> uint {
|
2012-08-01 19:30:05 -05:00
|
|
|
return if self.get(i) { 1 } else { 0 };
|
2012-07-29 18:00:55 -05:00
|
|
|
}
|
2011-04-06 19:56:44 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Converts `self` to a vector of uint with the same length.
|
|
|
|
*
|
|
|
|
* Each uint in the resulting vector has either value 0u or 1u.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
fn to_vec() -> ~[uint] {
|
2012-08-02 17:42:56 -05:00
|
|
|
vec::from_fn(self.nbits, |x| self.init_to_vec(x))
|
2012-03-27 08:14:12 -05:00
|
|
|
}
|
2012-05-31 13:37:39 -05:00
|
|
|
|
2012-09-01 12:13:37 -05:00
|
|
|
/**
|
|
|
|
* Organise the bits into bytes, such that the first bit in the
|
|
|
|
* bitv becomes the high-order bit of the first byte. If the
|
|
|
|
* 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 bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 {
|
|
|
|
let offset = byte * 8 + bit;
|
|
|
|
if offset >= bitv.nbits {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
bitv[offset] as u8 << (7 - bit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform self into a [bool] by turning each bit into a bool
|
|
|
|
*/
|
|
|
|
fn to_bools() -> ~[bool] {
|
|
|
|
vec::from_fn(self.nbits, |i| self[i])
|
|
|
|
}
|
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Converts `self` to a string.
|
|
|
|
*
|
|
|
|
* The resulting string has the same length as `self`, and each
|
|
|
|
* character is either '0' or '1'.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
fn to_str() -> ~str {
|
|
|
|
let mut rs = ~"";
|
2012-08-03 13:22:35 -05:00
|
|
|
for self.each() |i| { if i { rs += ~"1"; } else { rs += ~"0"; } };
|
2012-07-29 18:00:55 -05:00
|
|
|
rs
|
|
|
|
}
|
|
|
|
|
2011-04-07 20:15:56 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
/**
|
|
|
|
* Compare a bitvector to a vector of uint
|
|
|
|
*
|
|
|
|
* 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 {
|
|
|
|
assert self.nbits == v.len();
|
|
|
|
let mut i = 0;
|
|
|
|
while i < self.nbits {
|
|
|
|
let w0 = self.get(i);
|
|
|
|
let w1 = v[i];
|
|
|
|
if !w0 && w1 != 0u || w0 && w1 == 0u { return false; }
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
2012-07-17 19:03:27 -05:00
|
|
|
|
|
|
|
fn ones(f: fn(uint) -> bool) {
|
|
|
|
for uint::range(0, self.nbits) |i| {
|
|
|
|
if self.get(i) {
|
|
|
|
if !f(i) { break }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-29 18:00:55 -05:00
|
|
|
|
2012-09-02 12:04:38 -05:00
|
|
|
}
|
2012-07-29 18:00:55 -05:00
|
|
|
|
2012-09-01 12:13:37 -05:00
|
|
|
/**
|
|
|
|
* Transform a byte-vector into a bitv. Each byte becomes 8 bits,
|
|
|
|
* with the most significant bits of each byte coming first. Each
|
|
|
|
* bit becomes true if equal to 1 or false if equal to 0.
|
|
|
|
*/
|
|
|
|
fn from_bytes(bytes: &[u8]) -> Bitv {
|
|
|
|
from_fn(bytes.len() * 8, |i| {
|
|
|
|
let b = bytes[i / 8] as uint;
|
|
|
|
let offset = i % 8;
|
|
|
|
b >> (7 - offset) & 1 == 1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform a [bool] into a bitv by converting each bool into a bit.
|
|
|
|
*/
|
|
|
|
fn from_bools(bools: &[bool]) -> Bitv {
|
|
|
|
from_fn(bools.len(), |i| bools[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a bitv of the specified length where the value at each
|
|
|
|
* index is f(index).
|
|
|
|
*/
|
|
|
|
fn from_fn(len: uint, f: fn(index: uint) -> bool) -> Bitv {
|
|
|
|
let bitv = Bitv(len, false);
|
|
|
|
for uint::range(0, len) |i| {
|
|
|
|
bitv.set(i, f(i));
|
|
|
|
}
|
2012-09-10 19:50:48 -05:00
|
|
|
move bitv
|
2012-09-01 12:13:37 -05:00
|
|
|
}
|
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
const uint_bits: uint = 32u + (1u << 32u >> 27u);
|
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn lor(w0: uint, w1: uint) -> uint { return w0 | w1; }
|
2012-07-29 18:00:55 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn land(w0: uint, w1: uint) -> uint { return w0 & w1; }
|
2012-07-29 18:00:55 -05:00
|
|
|
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn right(_w0: uint, w1: uint) -> uint { return w1; }
|
2012-07-17 17:16:09 -05:00
|
|
|
|
2012-09-02 20:13:48 -05:00
|
|
|
impl Bitv: ops::Index<uint,bool> {
|
2012-09-25 20:27:45 -05:00
|
|
|
pure fn index(+i: uint) -> bool {
|
2012-07-27 16:51:19 -05:00
|
|
|
self.get(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-09-21 20:10:45 -05:00
|
|
|
#[legacy_exports];
|
2012-05-27 15:08:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_to_str() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let zerolen = Bitv(0u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert zerolen.to_str() == ~"";
|
2012-05-27 15:08:48 -05:00
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
let eightbits = Bitv(8u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert eightbits.to_str() == ~"00000000";
|
2012-05-27 15:08:48 -05:00
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_0_elements() {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut act;
|
|
|
|
let mut exp;
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(0u, false);
|
2012-03-12 17:52:30 -05:00
|
|
|
exp = vec::from_elem::<uint>(0u, 0u);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert act.eq_vec(exp);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_1_element() {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut act;
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(1u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert act.eq_vec(~[0u]);
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(1u, true);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert act.eq_vec(~[1u]);
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-08-21 13:41:29 -05:00
|
|
|
#[test]
|
|
|
|
fn test_2_elements() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let b = bitv::Bitv(2, false);
|
2012-08-21 13:41:29 -05:00
|
|
|
b.set(0, true);
|
|
|
|
b.set(1, false);
|
|
|
|
assert b.to_str() == ~"10";
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[test]
|
|
|
|
fn test_10_elements() {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut act;
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 0
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(10u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 1
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(10u, true);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(10u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(0u, true);
|
|
|
|
act.set(1u, true);
|
|
|
|
act.set(2u, true);
|
|
|
|
act.set(3u, true);
|
|
|
|
act.set(4u, true);
|
|
|
|
assert (act.eq_vec(~[1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(10u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(5u, true);
|
|
|
|
act.set(6u, true);
|
|
|
|
act.set(7u, true);
|
|
|
|
act.set(8u, true);
|
|
|
|
act.set(9u, true);
|
|
|
|
assert (act.eq_vec(~[0u, 0u, 0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(10u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(0u, true);
|
|
|
|
act.set(3u, true);
|
|
|
|
act.set(6u, true);
|
|
|
|
act.set(9u, true);
|
|
|
|
assert (act.eq_vec(~[1u, 0u, 0u, 1u, 0u, 0u, 1u, 0u, 0u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_31_elements() {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut act;
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 0
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(31u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 1
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(31u, true);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
|
2012-01-17 21:05:07 -06:00
|
|
|
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
|
2012-06-29 18:26:56 -05:00
|
|
|
1u, 1u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(31u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(0u, true);
|
|
|
|
act.set(1u, true);
|
|
|
|
act.set(2u, true);
|
|
|
|
act.set(3u, true);
|
|
|
|
act.set(4u, true);
|
|
|
|
act.set(5u, true);
|
|
|
|
act.set(6u, true);
|
|
|
|
act.set(7u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(31u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(16u, true);
|
|
|
|
act.set(17u, true);
|
|
|
|
act.set(18u, true);
|
|
|
|
act.set(19u, true);
|
|
|
|
act.set(20u, true);
|
|
|
|
act.set(21u, true);
|
|
|
|
act.set(22u, true);
|
|
|
|
act.set(23u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(31u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(24u, true);
|
|
|
|
act.set(25u, true);
|
|
|
|
act.set(26u, true);
|
|
|
|
act.set(27u, true);
|
|
|
|
act.set(28u, true);
|
|
|
|
act.set(29u, true);
|
|
|
|
act.set(30u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
|
2012-06-29 18:26:56 -05:00
|
|
|
1u, 1u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(31u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(3u, true);
|
|
|
|
act.set(17u, true);
|
|
|
|
act.set(30u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_32_elements() {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut act;
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 0
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(32u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 1
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(32u, true);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
|
2012-01-17 21:05:07 -06:00
|
|
|
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
|
2012-06-29 18:26:56 -05:00
|
|
|
1u, 1u, 1u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(32u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(0u, true);
|
|
|
|
act.set(1u, true);
|
|
|
|
act.set(2u, true);
|
|
|
|
act.set(3u, true);
|
|
|
|
act.set(4u, true);
|
|
|
|
act.set(5u, true);
|
|
|
|
act.set(6u, true);
|
|
|
|
act.set(7u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(32u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(16u, true);
|
|
|
|
act.set(17u, true);
|
|
|
|
act.set(18u, true);
|
|
|
|
act.set(19u, true);
|
|
|
|
act.set(20u, true);
|
|
|
|
act.set(21u, true);
|
|
|
|
act.set(22u, true);
|
|
|
|
act.set(23u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(32u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(24u, true);
|
|
|
|
act.set(25u, true);
|
|
|
|
act.set(26u, true);
|
|
|
|
act.set(27u, true);
|
|
|
|
act.set(28u, true);
|
|
|
|
act.set(29u, true);
|
|
|
|
act.set(30u, true);
|
|
|
|
act.set(31u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
|
2012-06-29 18:26:56 -05:00
|
|
|
1u, 1u, 1u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(32u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(3u, true);
|
|
|
|
act.set(17u, true);
|
|
|
|
act.set(30u, true);
|
|
|
|
act.set(31u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_33_elements() {
|
2012-03-22 10:39:41 -05:00
|
|
|
let mut act;
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 0
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(33u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// all 1
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(33u, true);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
|
2012-01-17 21:05:07 -06:00
|
|
|
1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u,
|
2012-06-29 18:26:56 -05:00
|
|
|
1u, 1u, 1u, 1u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(33u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(0u, true);
|
|
|
|
act.set(1u, true);
|
|
|
|
act.set(2u, true);
|
|
|
|
act.set(3u, true);
|
|
|
|
act.set(4u, true);
|
|
|
|
act.set(5u, true);
|
|
|
|
act.set(6u, true);
|
|
|
|
act.set(7u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(33u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(16u, true);
|
|
|
|
act.set(17u, true);
|
|
|
|
act.set(18u, true);
|
|
|
|
act.set(19u, true);
|
|
|
|
act.set(20u, true);
|
|
|
|
act.set(21u, true);
|
|
|
|
act.set(22u, true);
|
|
|
|
act.set(23u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 1u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(33u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(24u, true);
|
|
|
|
act.set(25u, true);
|
|
|
|
act.set(26u, true);
|
|
|
|
act.set(27u, true);
|
|
|
|
act.set(28u, true);
|
|
|
|
act.set(29u, true);
|
|
|
|
act.set(30u, true);
|
|
|
|
act.set(31u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 1u, 1u,
|
2012-06-29 18:26:56 -05:00
|
|
|
1u, 1u, 1u, 1u, 1u, 1u, 0u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
// mixed
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
act = Bitv(33u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
act.set(3u, true);
|
|
|
|
act.set(17u, true);
|
|
|
|
act.set(30u, true);
|
|
|
|
act.set(31u, true);
|
|
|
|
act.set(32u, true);
|
|
|
|
assert (act.eq_vec(
|
2012-06-29 18:26:56 -05:00
|
|
|
~[0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-01-17 21:05:07 -06:00
|
|
|
0u, 0u, 0u, 0u, 1u, 0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u,
|
2012-06-29 18:26:56 -05:00
|
|
|
0u, 0u, 0u, 0u, 1u, 1u, 1u]));
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2012-05-31 13:37:39 -05:00
|
|
|
#[test]
|
|
|
|
fn test_equal_differing_sizes() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let v0 = Bitv(10u, false);
|
|
|
|
let v1 = Bitv(11u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert !v0.equal(v1);
|
2012-05-31 13:37:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_equal_greatly_differing_sizes() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let v0 = Bitv(10u, false);
|
|
|
|
let v1 = Bitv(110u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert !v0.equal(v1);
|
2012-05-31 13:37:39 -05:00
|
|
|
}
|
2012-08-21 13:42:16 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_equal_sneaky_small() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let a = bitv::Bitv(1, false);
|
2012-08-21 13:42:16 -05:00
|
|
|
a.set(0, true);
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
let b = bitv::Bitv(1, true);
|
2012-08-21 13:42:16 -05:00
|
|
|
b.set(0, true);
|
|
|
|
|
|
|
|
assert a.equal(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_equal_sneaky_big() {
|
2012-08-11 09:08:42 -05:00
|
|
|
let a = bitv::Bitv(100, false);
|
2012-08-21 13:42:16 -05:00
|
|
|
for uint::range(0, 100) |i| {
|
|
|
|
a.set(i, true);
|
|
|
|
}
|
|
|
|
|
2012-08-11 09:08:42 -05:00
|
|
|
let b = bitv::Bitv(100, true);
|
2012-08-21 13:42:16 -05:00
|
|
|
for uint::range(0, 100) |i| {
|
|
|
|
b.set(i, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert a.equal(b);
|
|
|
|
}
|
|
|
|
|
2012-09-01 12:13:37 -05:00
|
|
|
#[test]
|
|
|
|
fn test_from_bytes() {
|
|
|
|
let bitv = from_bytes([0b10110110, 0b00000000, 0b11111111]);
|
|
|
|
let str = ~"10110110" + ~"00000000" + ~"11111111";
|
|
|
|
assert bitv.to_str() == str;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_bytes() {
|
|
|
|
let bv = Bitv(3, true);
|
|
|
|
bv.set(1, false);
|
|
|
|
assert bv.to_bytes() == ~[0b10100000];
|
|
|
|
|
|
|
|
let bv = Bitv(9, false);
|
|
|
|
bv.set(2, true);
|
|
|
|
bv.set(8, true);
|
|
|
|
assert bv.to_bytes() == ~[0b00100000, 0b10000000];
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_bools() {
|
|
|
|
assert from_bools([true, false, true, true]).to_str() == ~"1011";
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_to_bools() {
|
|
|
|
let bools = ~[false, false, true, false, false, true, true, false];
|
|
|
|
assert from_bytes([0b00100110]).to_bools() == bools;
|
|
|
|
}
|
2012-01-17 21:05:07 -06:00
|
|
|
}
|
|
|
|
|
2010-10-20 19:04:15 -05:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|