2012-07-29 18:00:55 -05:00
|
|
|
import vec::{to_mut, from_elem};
|
|
|
|
|
2012-03-15 16:06:33 -05:00
|
|
|
export bitv;
|
2011-05-20 21:12:35 -05:00
|
|
|
export union;
|
|
|
|
export intersect;
|
2011-08-15 17:35:40 -05:00
|
|
|
export assign;
|
2011-05-20 21:12:35 -05:00
|
|
|
export clone;
|
|
|
|
export get;
|
|
|
|
export equal;
|
|
|
|
export clear;
|
|
|
|
export set_all;
|
|
|
|
export invert;
|
|
|
|
export difference;
|
|
|
|
export set;
|
|
|
|
export is_true;
|
|
|
|
export is_false;
|
2011-08-15 18:09:17 -05:00
|
|
|
export to_vec;
|
2011-05-20 21:12:35 -05:00
|
|
|
export to_str;
|
2011-08-18 16:32:25 -05:00
|
|
|
export eq_vec;
|
2012-07-17 17:16:09 -05:00
|
|
|
export methods;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
class small_bitv {
|
|
|
|
let mut bits: u32;
|
|
|
|
new(bits: u32) { self.bits = bits; }
|
|
|
|
priv {
|
|
|
|
#[inline(always)]
|
|
|
|
fn bits_op(right_bits: u32, f: fn(u32, u32) -> u32) -> bool {
|
|
|
|
let old_b: u32 = self.bits;
|
|
|
|
let new_b = f(old_b, right_bits);
|
|
|
|
self.bits = new_b;
|
|
|
|
old_b != new_b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn union(s: &small_bitv) -> bool {
|
|
|
|
self.bits_op(s.bits, |u1, u2| { u1 | u2 })
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn intersect(s: &small_bitv) -> bool {
|
|
|
|
self.bits_op(s.bits, |u1, u2| { u1 & u2 })
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn become(s: &small_bitv) -> bool {
|
|
|
|
let old = self.bits;
|
|
|
|
self.bits = s.bits;
|
|
|
|
old != self.bits
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn difference(s: &small_bitv) -> bool {
|
|
|
|
let old = self.bits;
|
|
|
|
self.bits &= !s.bits;
|
|
|
|
old != self.bits
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
pure fn get(i: uint) -> bool {
|
|
|
|
(self.bits & (1 << i)) != 0
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn set(i: uint, x: bool) {
|
|
|
|
if x {
|
|
|
|
self.bits |= 1<<i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
self.bits &= !(i as u32);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn equals(b: &small_bitv) -> bool { self.bits == b.bits }
|
|
|
|
#[inline(always)]
|
|
|
|
fn clear() { self.bits = 0; }
|
|
|
|
#[inline(always)]
|
|
|
|
fn set_all() { self.bits = !0; }
|
|
|
|
#[inline(always)]
|
|
|
|
fn is_true() -> bool { self.bits == !0 }
|
|
|
|
#[inline(always)]
|
|
|
|
fn is_false() -> bool { self.bits == 0 }
|
|
|
|
#[inline(always)]
|
|
|
|
fn invert() { self.bits = !self.bits; }
|
|
|
|
}
|
2010-10-21 09:36:13 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
class big_bitv {
|
|
|
|
// only mut b/c of clone and lack of other constructor
|
|
|
|
let mut storage: ~[mut uint];
|
|
|
|
new(-storage: ~[mut uint]) {
|
|
|
|
self.storage <- storage;
|
|
|
|
}
|
|
|
|
priv {
|
|
|
|
#[inline(always)]
|
|
|
|
fn process(b: &big_bitv, 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 w0 = self.storage[i];
|
|
|
|
let w1 = b.storage[i];
|
|
|
|
let w = op(w0, w1);
|
|
|
|
if w0 != w unchecked { changed = true; self.storage[i] = w; };
|
|
|
|
true
|
|
|
|
};
|
|
|
|
changed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn each_storage(op: fn(&uint) -> bool) {
|
|
|
|
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; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn invert() { for self.each_storage() |w| { w = !w } }
|
|
|
|
#[inline(always)]
|
|
|
|
fn union(b: &big_bitv) -> bool { self.process(b, lor) }
|
|
|
|
#[inline(always)]
|
|
|
|
fn intersect(b: &big_bitv) -> bool { self.process(b, land) }
|
|
|
|
#[inline(always)]
|
|
|
|
fn become(b: &big_bitv) -> bool { self.process(b, right) }
|
|
|
|
#[inline(always)]
|
|
|
|
fn difference(b: &big_bitv) -> bool {
|
|
|
|
self.invert();
|
|
|
|
let b = self.intersect(b);
|
|
|
|
self.invert();
|
|
|
|
b
|
|
|
|
}
|
|
|
|
#[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
|
|
|
|
}
|
|
|
|
#[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 };
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
|
|
|
fn equals(b: &big_bitv) -> bool {
|
|
|
|
let len = b.storage.len();
|
|
|
|
for uint::iterate(0, len) |i| {
|
|
|
|
if self.storage[i] != b.storage[i] { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
enum a_bitv { big(~big_bitv), small(~small_bitv) }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-29 18:00:55 -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
|
|
|
|
class bitv {
|
|
|
|
let rep: a_bitv;
|
|
|
|
let nbits: uint;
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
new(nbits: uint, init: bool) {
|
|
|
|
self.nbits = nbits;
|
|
|
|
if nbits <= 32 {
|
|
|
|
self.rep = small(~small_bitv(if init {!0} else {0}));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let s = to_mut(from_elem(nbits / uint_bits + 1,
|
|
|
|
if init {!0} else {0}));
|
|
|
|
self.rep = big(~big_bitv(s));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
priv {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
alt self.rep {
|
|
|
|
small(s) {
|
|
|
|
alt other.rep {
|
|
|
|
small(s1) {
|
|
|
|
alt op {
|
|
|
|
union { s.union(s1) }
|
|
|
|
intersect { s.intersect(s1) }
|
|
|
|
assign { s.become(s1) }
|
|
|
|
difference { s.difference(s1) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
big(s1) {
|
|
|
|
self.die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
big(s) {
|
|
|
|
alt other.rep {
|
|
|
|
small(_) { self.die(); }
|
|
|
|
big(s1) {
|
|
|
|
alt op {
|
|
|
|
union { s.union(s1) }
|
|
|
|
intersect { s.intersect(s1) }
|
|
|
|
assign { s.become(s1) }
|
|
|
|
difference { s.difference(s1) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-10-27 17:35:56 -05:00
|
|
|
|
2012-07-17 17:16:09 -05:00
|
|
|
/**
|
|
|
|
* Calculates the union of two bitvectors
|
|
|
|
*
|
2012-07-29 18:00:55 -05:00
|
|
|
* Sets `self` to the union of `self` and `v1`. Both bitvectors must be
|
|
|
|
* the same length. Returns 'true' if `self` changed.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
|
|
|
fn union(v1: &bitv) -> bool { self.do_op(union, v1) }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Calculates the intersection of two bitvectors
|
|
|
|
*
|
2012-07-29 18:00:55 -05:00
|
|
|
* Sets `self` to the intersection of `self` and `v1`. Both bitvectors must be
|
|
|
|
* the same length. Returns 'true' if `self` changed.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
|
|
|
fn intersect(v1: &bitv) -> bool { self.do_op(intersect, v1) }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2012-07-29 18:00:55 -05:00
|
|
|
* Assigns the value of `v1` to `self`
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
2012-07-29 18:00:55 -05:00
|
|
|
* Both bitvectors must be the same length. Returns `true` if `self` was
|
|
|
|
* changed
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn assign(v: &bitv) -> bool { self.do_op(assign, v) }
|
|
|
|
|
|
|
|
/// Makes a copy of a bitvector
|
|
|
|
#[inline(always)]
|
|
|
|
fn clone() -> ~bitv {
|
|
|
|
~alt self.rep {
|
|
|
|
small(b) {
|
|
|
|
bitv{nbits: self.nbits, rep: small(~small_bitv{bits: b.bits})}
|
|
|
|
}
|
|
|
|
big(b) {
|
|
|
|
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]; };
|
|
|
|
bitv{nbits: self.nbits, rep: big(~big_bitv{storage: st})}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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);
|
|
|
|
alt self.rep {
|
|
|
|
big(b) { b.get(i) }
|
|
|
|
small(s) { s.get(i) }
|
|
|
|
}
|
|
|
|
}
|
2011-04-12 14:16:21 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
/**
|
|
|
|
* Set the value of a bit at a given index
|
|
|
|
*
|
|
|
|
* `i` must be less than the length of the bitvector.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
|
|
|
fn set(i: uint, x: bool) {
|
|
|
|
assert (i < self.nbits);
|
|
|
|
alt self.rep {
|
|
|
|
big(b) { b.set(i, x); }
|
|
|
|
small(s) { s.set(i, x); }
|
|
|
|
}
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -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)]
|
|
|
|
fn equal(v1: bitv) -> bool {
|
|
|
|
if self.nbits != v1.nbits { ret false; }
|
|
|
|
alt self.rep {
|
|
|
|
small(b) {
|
|
|
|
alt v1.rep {
|
|
|
|
small(b1) { b.equals(b1) }
|
|
|
|
_ { false }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
big(s) {
|
|
|
|
alt v1.rep {
|
|
|
|
big(s1) {
|
|
|
|
s.equals(s1)
|
|
|
|
}
|
|
|
|
small(_) { ret false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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() {
|
|
|
|
alt self.rep {
|
|
|
|
small(b) { b.clear(); }
|
|
|
|
big(s) {
|
|
|
|
for s.each_storage() |w| { w = 0u }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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() {
|
|
|
|
alt self.rep {
|
|
|
|
small(b) { b.set_all(); }
|
|
|
|
big(s) {
|
|
|
|
for s.each_storage() |w| { w = !0u } }
|
|
|
|
}
|
|
|
|
}
|
2011-05-18 17:38:11 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
/// Invert all bits
|
|
|
|
#[inline(always)]
|
|
|
|
fn invert() {
|
|
|
|
alt self.rep {
|
|
|
|
small(b) { b.invert(); }
|
|
|
|
big(s) {
|
|
|
|
for s.each_storage() |w| { w = !w } }
|
|
|
|
}
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -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.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn difference(v: ~bitv) -> bool { self.do_op(difference, v) }
|
|
|
|
|
|
|
|
/// Returns true if all bits are 1
|
|
|
|
#[inline(always)]
|
|
|
|
fn is_true() -> bool {
|
|
|
|
alt self.rep {
|
|
|
|
small(b) { b.is_true() }
|
|
|
|
_ {
|
|
|
|
for self.each() |i| { if !i { ret false; } }
|
|
|
|
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
|
2011-04-06 19:56:44 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
fn is_false() -> bool {
|
|
|
|
alt self.rep {
|
|
|
|
small(b) { b.is_false() }
|
|
|
|
big(_) {
|
|
|
|
for self.each() |i| { if i { ret false; } }
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-29 18:00:55 -05:00
|
|
|
fn init_to_vec(i: uint) -> uint {
|
|
|
|
ret if self.get(i) { 1 } else { 0 };
|
|
|
|
}
|
2011-04-06 19:56:44 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2012-07-29 18:00:55 -05:00
|
|
|
* Converts `self` to a vector of uint with the same length.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* Each uint in the resulting vector has either value 0u or 1u.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
fn to_vec() -> ~[uint] {
|
|
|
|
let sub = |x| self.init_to_vec(x);
|
|
|
|
ret vec::from_fn::<uint>(self.nbits, sub);
|
2012-03-27 08:14:12 -05:00
|
|
|
}
|
2012-05-31 13:37:39 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2012-07-29 18:00:55 -05:00
|
|
|
* Converts `self` to a string.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
2012-07-29 18:00:55 -05:00
|
|
|
* The resulting string has the same length as `self`, and each
|
2012-07-04 16:53:12 -05:00
|
|
|
* character is either '0' or '1'.
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
fn to_str() -> ~str {
|
|
|
|
let mut rs = ~"";
|
|
|
|
for self.each() |i| { if i { rs += "1"; } else { rs += "0"; } };
|
|
|
|
rs
|
|
|
|
}
|
|
|
|
|
2011-04-07 20:15:56 -05:00
|
|
|
|
2012-07-04 16:53:12 -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
|
|
|
|
*/
|
2012-07-29 18:00:55 -05:00
|
|
|
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 { ret 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
|
|
|
|
|
|
|
} // end of bitv class
|
|
|
|
|
|
|
|
const uint_bits: uint = 32u + (1u << 32u >> 27u);
|
|
|
|
|
|
|
|
pure fn lor(w0: uint, w1: uint) -> uint { ret w0 | w1; }
|
|
|
|
|
|
|
|
pure fn land(w0: uint, w1: uint) -> uint { ret w0 & w1; }
|
|
|
|
|
|
|
|
pure fn right(_w0: uint, w1: uint) -> uint { ret w1; }
|
2012-07-17 17:16:09 -05:00
|
|
|
|
2012-07-27 16:51:19 -05:00
|
|
|
impl extensions of ops::index<uint,bool> for bitv {
|
|
|
|
pure fn index(&&i: uint) -> bool {
|
|
|
|
self.get(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-17 21:05:07 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2012-05-27 15:08:48 -05:00
|
|
|
#[test]
|
|
|
|
fn test_to_str() {
|
|
|
|
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
|
|
|
|
|
|
|
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-03-15 16:06:33 -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-03-15 16:06:33 -05:00
|
|
|
act = bitv(1u, false);
|
2012-07-29 18:00:55 -05:00
|
|
|
assert act.eq_vec(~[0u]);
|
2012-03-15 16:06:33 -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
|
|
|
}
|
|
|
|
|
|
|
|
#[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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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-03-15 16:06:33 -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() {
|
|
|
|
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() {
|
|
|
|
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-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:
|
|
|
|
//
|