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-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2341): With recursive object types, we could implement binary
|
|
|
|
// methods like union, intersection, and difference. At that point, we could
|
|
|
|
// write an optimizing version of this module that produces a different obj
|
|
|
|
// for the case where nbits <= 32.
|
2010-10-21 09:36:13 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// The bitvector type
|
2012-07-17 17:16:09 -05:00
|
|
|
type bitv = {storage: ~[mut uint], nbits: uint};
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-17 17:16:09 -05:00
|
|
|
#[cfg(target_arch="x86")]
|
|
|
|
const uint_bits: uint = 32;
|
|
|
|
#[cfg(target_arch="x86_64")]
|
|
|
|
const uint_bits: uint = 64;
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Constructs a bitvector
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * nbits - The number of bits in the bitvector
|
|
|
|
* * init - If true then the bits are initialized to 1, otherwise 0
|
|
|
|
*/
|
2012-03-15 16:06:33 -05:00
|
|
|
fn bitv(nbits: uint, init: bool) -> bitv {
|
2011-07-27 07:19:39 -05:00
|
|
|
let elt = if init { !0u } else { 0u };
|
2012-03-12 17:52:30 -05:00
|
|
|
let storage = vec::to_mut(vec::from_elem(nbits / uint_bits + 1u, elt));
|
2012-07-17 17:16:09 -05:00
|
|
|
ret {storage: storage, nbits: nbits};
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2012-03-15 16:06:33 -05:00
|
|
|
fn process(v0: bitv, v1: bitv, op: fn(uint, uint) -> uint) -> bool {
|
2011-08-15 18:38:23 -05:00
|
|
|
let len = vec::len(v1.storage);
|
|
|
|
assert (vec::len(v0.storage) == len);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (v0.nbits == v1.nbits);
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut changed = false;
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, len) |i| {
|
2011-08-19 17:16:48 -05:00
|
|
|
let w0 = v0.storage[i];
|
|
|
|
let w1 = v1.storage[i];
|
2011-07-27 07:19:39 -05:00
|
|
|
let w = op(w0, w1);
|
2011-08-19 17:16:48 -05:00
|
|
|
if w0 != w { changed = true; v0.storage[i] = w; }
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
2010-10-20 19:04:15 -05:00
|
|
|
ret changed;
|
|
|
|
}
|
|
|
|
|
2011-10-27 17:35:56 -05:00
|
|
|
|
2012-07-17 17:16:09 -05:00
|
|
|
/**
|
|
|
|
* Calculates the union of two bitvectors
|
|
|
|
*
|
|
|
|
* Sets `v0` to the union of `v0` and `v1`. Both bitvectors must be the
|
|
|
|
* same length. Returns 'true' if `v0` was changed.
|
|
|
|
*/
|
2012-03-15 16:06:33 -05:00
|
|
|
fn union(v0: bitv, v1: bitv) -> bool {
|
2012-07-17 17:16:09 -05:00
|
|
|
process(v0, v1, |a, b| a | b)
|
2012-03-15 16:06:33 -05:00
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Calculates the intersection of two bitvectors
|
|
|
|
*
|
|
|
|
* Sets `v0` to the intersection of `v0` and `v1`. Both bitvectors must be the
|
|
|
|
* same length. Returns 'true' if `v0` was changed.
|
|
|
|
*/
|
2012-03-15 16:06:33 -05:00
|
|
|
fn intersect(v0: bitv, v1: bitv) -> bool {
|
2012-07-17 17:16:09 -05:00
|
|
|
process(v0, v1, |a, b| a & b)
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2011-08-18 03:01:39 -05:00
|
|
|
fn right(_w0: uint, w1: uint) -> uint { ret w1; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Assigns the value of `v1` to `v0`
|
|
|
|
*
|
|
|
|
* Both bitvectors must be the same length. Returns `true` if `v0` was changed
|
|
|
|
*/
|
2012-03-15 16:06:33 -05:00
|
|
|
fn assign(v0: bitv, v1: bitv) -> bool {
|
|
|
|
let sub = right; ret process(v0, v1, sub);
|
|
|
|
}
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Makes a copy of a bitvector
|
2012-03-15 16:06:33 -05:00
|
|
|
fn clone(v: bitv) -> bitv {
|
2012-07-17 17:16:09 -05:00
|
|
|
copy v
|
2011-04-12 14:16:21 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Retrieve the value at index `i`
|
2012-05-31 13:37:39 -05:00
|
|
|
#[inline(always)]
|
2012-03-15 16:06:33 -05:00
|
|
|
pure fn get(v: bitv, i: uint) -> bool {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (i < v.nbits);
|
2011-11-15 19:10:43 -06:00
|
|
|
let bits = uint_bits;
|
2011-07-27 07:19:39 -05:00
|
|
|
let w = i / bits;
|
|
|
|
let b = i % bits;
|
2011-08-19 17:16:48 -05:00
|
|
|
let x = 1u & v.storage[w] >> b;
|
2010-10-20 19:04:15 -05:00
|
|
|
ret x == 1u;
|
|
|
|
}
|
|
|
|
|
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-03-15 16:06:33 -05:00
|
|
|
fn equal(v0: bitv, v1: bitv) -> bool {
|
2012-05-31 13:37:39 -05:00
|
|
|
if v0.nbits != v1.nbits { ret false; }
|
2011-08-15 18:38:23 -05:00
|
|
|
let len = vec::len(v1.storage);
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::iterate(0u, len) |i| {
|
2011-08-19 17:16:48 -05:00
|
|
|
if v0.storage[i] != v1.storage[i] { ret false; }
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Set all bits to 0
|
2012-05-31 13:37:39 -05:00
|
|
|
#[inline(always)]
|
2012-06-30 18:19:07 -05:00
|
|
|
fn clear(v: bitv) { for each_storage(v) |w| { w = 0u } }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Set all bits to 1
|
2012-05-31 13:37:39 -05:00
|
|
|
#[inline(always)]
|
2012-06-30 18:19:07 -05:00
|
|
|
fn set_all(v: bitv) { for each_storage(v) |w| { w = !0u } }
|
2011-05-18 17:38:11 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Invert all bits
|
2012-05-31 13:37:39 -05:00
|
|
|
#[inline(always)]
|
2012-06-30 18:19:07 -05:00
|
|
|
fn invert(v: bitv) { for each_storage(v) |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-03-15 16:06:33 -05:00
|
|
|
fn difference(v0: bitv, v1: bitv) -> bool {
|
2010-10-20 19:04:15 -05:00
|
|
|
invert(v1);
|
2011-07-27 07:19:39 -05:00
|
|
|
let b = intersect(v0, v1);
|
2010-10-20 19:04:15 -05:00
|
|
|
invert(v1);
|
|
|
|
ret b;
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Set the value of a bit at a given index
|
|
|
|
*
|
|
|
|
* `i` must be less than the length of the bitvector.
|
|
|
|
*/
|
2012-05-31 13:37:39 -05:00
|
|
|
#[inline(always)]
|
2012-03-15 16:06:33 -05:00
|
|
|
fn set(v: bitv, i: uint, x: bool) {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (i < v.nbits);
|
2011-11-15 19:10:43 -06:00
|
|
|
let bits = uint_bits;
|
2011-07-27 07:19:39 -05:00
|
|
|
let w = i / bits;
|
|
|
|
let b = i % bits;
|
|
|
|
let flag = 1u << b;
|
2011-08-19 17:16:48 -05:00
|
|
|
v.storage[w] = if x { v.storage[w] | flag } else { v.storage[w] & !flag };
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if all bits are 1
|
2012-03-15 16:06:33 -05:00
|
|
|
fn is_true(v: bitv) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for each(v) |i| { if !i { ret false; } }
|
2011-04-06 19:56:44 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if all bits are 0
|
2012-03-15 16:06:33 -05:00
|
|
|
fn is_false(v: bitv) -> bool {
|
2012-06-30 18:19:07 -05:00
|
|
|
for each(v) |i| { if i { ret false; } }
|
2011-04-06 19:56:44 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Converts the bitvector to a vector of uint with the same length.
|
|
|
|
*
|
|
|
|
* Each uint in the resulting vector has either value 0u or 1u.
|
|
|
|
*/
|
2012-06-29 18:26:56 -05:00
|
|
|
fn to_vec(v: bitv) -> ~[uint] {
|
2012-07-17 17:16:09 -05:00
|
|
|
vec::from_fn::<uint>(v.nbits, |i| if get(v, i) { 1 } else { 0 })
|
2011-07-12 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
2012-05-31 13:37:39 -05:00
|
|
|
#[inline(always)]
|
2012-03-27 08:14:12 -05:00
|
|
|
fn each(v: bitv, f: fn(bool) -> bool) {
|
|
|
|
let mut i = 0u;
|
|
|
|
while i < v.nbits {
|
|
|
|
if !f(get(v, i)) { break; }
|
2012-05-27 15:08:48 -05:00
|
|
|
i = i + 1u;
|
2012-03-27 08:14:12 -05:00
|
|
|
}
|
|
|
|
}
|
2011-10-27 17:35:56 -05:00
|
|
|
|
2012-05-31 13:37:39 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn each_storage(v: bitv, op: fn(&uint) -> bool) {
|
2012-06-30 18:19:07 -05:00
|
|
|
for uint::range(0u, vec::len(v.storage)) |i| {
|
2012-05-31 13:37:39 -05:00
|
|
|
let mut w = v.storage[i];
|
|
|
|
let b = !op(w);
|
|
|
|
v.storage[i] = w;
|
|
|
|
if !b { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Converts the bitvector to a string.
|
|
|
|
*
|
|
|
|
* The resulting string has the same length as the bitvector, and each
|
|
|
|
* character is either '0' or '1'.
|
|
|
|
*/
|
2012-07-14 00:57:48 -05:00
|
|
|
fn to_str(v: bitv) -> ~str {
|
|
|
|
let mut rs = ~"";
|
|
|
|
for each(v) |i| { if i { rs += ~"1"; } else { rs += ~"0"; } }
|
2011-06-24 10:55:02 -05:00
|
|
|
ret 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-06-29 18:26:56 -05:00
|
|
|
fn eq_vec(v0: bitv, v1: ~[uint]) -> bool {
|
2011-08-12 12:56:57 -05:00
|
|
|
assert (v0.nbits == vec::len::<uint>(v1));
|
2011-07-27 07:19:39 -05:00
|
|
|
let len = v0.nbits;
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut i = 0u;
|
2011-07-27 07:19:39 -05:00
|
|
|
while i < len {
|
|
|
|
let w0 = get(v0, i);
|
2011-08-19 17:16:48 -05:00
|
|
|
let w1 = v1[i];
|
2011-07-27 07:19:39 -05:00
|
|
|
if !w0 && w1 != 0u || w0 && w1 == 0u { ret false; }
|
2011-07-12 16:07:00 -05:00
|
|
|
i = i + 1u;
|
|
|
|
}
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2012-07-17 19:45:24 -05:00
|
|
|
trait methods {
|
|
|
|
fn union(rhs: bitv) -> bool;
|
|
|
|
fn intersect(rhs: bitv) -> bool;
|
|
|
|
fn assign(rhs: bitv) -> bool;
|
|
|
|
fn get(i: uint) -> bool;
|
|
|
|
fn [](i: uint) -> bool;
|
|
|
|
fn eq(rhs: bitv) -> bool;
|
|
|
|
fn clear();
|
|
|
|
fn set_all();
|
|
|
|
fn invert();
|
|
|
|
fn difference(rhs: bitv) -> bool;
|
|
|
|
fn set(i: uint, x: bool);
|
|
|
|
fn is_true() -> bool;
|
|
|
|
fn is_false() -> bool;
|
|
|
|
fn to_vec() -> ~[uint];
|
|
|
|
fn each(f: fn(bool) -> bool);
|
|
|
|
fn each_storage(f: fn(&uint) -> bool);
|
|
|
|
fn eq_vec(v: ~[uint]) -> bool;
|
|
|
|
|
|
|
|
fn ones(f: fn(uint) -> bool);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl of methods for bitv {
|
2012-07-17 17:16:09 -05:00
|
|
|
fn union(rhs: bitv) -> bool { union(self, rhs) }
|
|
|
|
fn intersect(rhs: bitv) -> bool { intersect(self, rhs) }
|
|
|
|
fn assign(rhs: bitv) -> bool { assign(self, rhs) }
|
|
|
|
fn get(i: uint) -> bool { get(self, i) }
|
|
|
|
fn [](i: uint) -> bool { self.get(i) }
|
|
|
|
fn eq(rhs: bitv) -> bool { equal(self, rhs) }
|
|
|
|
fn clear() { clear(self) }
|
|
|
|
fn set_all() { set_all(self) }
|
|
|
|
fn invert() { invert(self) }
|
|
|
|
fn difference(rhs: bitv) -> bool { difference(self, rhs) }
|
|
|
|
fn set(i: uint, x: bool) { set(self, i, x) }
|
|
|
|
fn is_true() -> bool { is_true(self) }
|
|
|
|
fn is_false() -> bool { is_false(self) }
|
|
|
|
fn to_vec() -> ~[uint] { to_vec(self) }
|
|
|
|
fn each(f: fn(bool) -> bool) { each(self, f) }
|
|
|
|
fn each_storage(f: fn(&uint) -> bool) { each_storage(self, f) }
|
|
|
|
fn eq_vec(v: ~[uint]) -> bool { eq_vec(self, v) }
|
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-17 17:16:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl of to_str::to_str for bitv {
|
|
|
|
fn to_str() -> ~str { to_str(self) }
|
|
|
|
}
|
|
|
|
|
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-14 00:57:48 -05:00
|
|
|
assert to_str(zerolen) == ~"";
|
2012-05-27 15:08:48 -05:00
|
|
|
|
|
|
|
let eightbits = bitv(8u, false);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(eightbits) == ~"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-01-17 21:05:07 -06:00
|
|
|
assert (eq_vec(act, exp));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-06-29 18:26:56 -05:00
|
|
|
assert (eq_vec(act, ~[0u]));
|
2012-03-15 16:06:33 -05:00
|
|
|
act = bitv(1u, true);
|
2012-06-29 18:26:56 -05:00
|
|
|
assert (eq_vec(act, ~[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-06-29 18:26:56 -05:00
|
|
|
assert (eq_vec(act, ~[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-06-29 18:26:56 -05:00
|
|
|
assert (eq_vec(act, ~[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-01-17 21:05:07 -06:00
|
|
|
set(act, 0u, true);
|
|
|
|
set(act, 1u, true);
|
|
|
|
set(act, 2u, true);
|
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 4u, true);
|
2012-06-29 18:26:56 -05:00
|
|
|
assert (eq_vec(act, ~[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-01-17 21:05:07 -06:00
|
|
|
set(act, 5u, true);
|
|
|
|
set(act, 6u, true);
|
|
|
|
set(act, 7u, true);
|
|
|
|
set(act, 8u, true);
|
|
|
|
set(act, 9u, true);
|
2012-06-29 18:26:56 -05:00
|
|
|
assert (eq_vec(act, ~[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-01-17 21:05:07 -06:00
|
|
|
set(act, 0u, true);
|
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 6u, true);
|
|
|
|
set(act, 9u, true);
|
2012-06-29 18:26:56 -05:00
|
|
|
assert (eq_vec(act, ~[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-01-17 21:05:07 -06:00
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 0u, true);
|
|
|
|
set(act, 1u, true);
|
|
|
|
set(act, 2u, true);
|
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 4u, true);
|
|
|
|
set(act, 5u, true);
|
|
|
|
set(act, 6u, true);
|
|
|
|
set(act, 7u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 16u, true);
|
|
|
|
set(act, 17u, true);
|
|
|
|
set(act, 18u, true);
|
|
|
|
set(act, 19u, true);
|
|
|
|
set(act, 20u, true);
|
|
|
|
set(act, 21u, true);
|
|
|
|
set(act, 22u, true);
|
|
|
|
set(act, 23u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 24u, true);
|
|
|
|
set(act, 25u, true);
|
|
|
|
set(act, 26u, true);
|
|
|
|
set(act, 27u, true);
|
|
|
|
set(act, 28u, true);
|
|
|
|
set(act, 29u, true);
|
|
|
|
set(act, 30u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 17u, true);
|
|
|
|
set(act, 30u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 0u, true);
|
|
|
|
set(act, 1u, true);
|
|
|
|
set(act, 2u, true);
|
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 4u, true);
|
|
|
|
set(act, 5u, true);
|
|
|
|
set(act, 6u, true);
|
|
|
|
set(act, 7u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 16u, true);
|
|
|
|
set(act, 17u, true);
|
|
|
|
set(act, 18u, true);
|
|
|
|
set(act, 19u, true);
|
|
|
|
set(act, 20u, true);
|
|
|
|
set(act, 21u, true);
|
|
|
|
set(act, 22u, true);
|
|
|
|
set(act, 23u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 24u, true);
|
|
|
|
set(act, 25u, true);
|
|
|
|
set(act, 26u, true);
|
|
|
|
set(act, 27u, true);
|
|
|
|
set(act, 28u, true);
|
|
|
|
set(act, 29u, true);
|
|
|
|
set(act, 30u, true);
|
|
|
|
set(act, 31u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 17u, true);
|
|
|
|
set(act, 30u, true);
|
|
|
|
set(act, 31u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 0u, true);
|
|
|
|
set(act, 1u, true);
|
|
|
|
set(act, 2u, true);
|
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 4u, true);
|
|
|
|
set(act, 5u, true);
|
|
|
|
set(act, 6u, true);
|
|
|
|
set(act, 7u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 16u, true);
|
|
|
|
set(act, 17u, true);
|
|
|
|
set(act, 18u, true);
|
|
|
|
set(act, 19u, true);
|
|
|
|
set(act, 20u, true);
|
|
|
|
set(act, 21u, true);
|
|
|
|
set(act, 22u, true);
|
|
|
|
set(act, 23u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 24u, true);
|
|
|
|
set(act, 25u, true);
|
|
|
|
set(act, 26u, true);
|
|
|
|
set(act, 27u, true);
|
|
|
|
set(act, 28u, true);
|
|
|
|
set(act, 29u, true);
|
|
|
|
set(act, 30u, true);
|
|
|
|
set(act, 31u, true);
|
|
|
|
assert (eq_vec(act,
|
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-01-17 21:05:07 -06:00
|
|
|
set(act, 3u, true);
|
|
|
|
set(act, 17u, true);
|
|
|
|
set(act, 30u, true);
|
|
|
|
set(act, 31u, true);
|
|
|
|
set(act, 32u, true);
|
|
|
|
assert (eq_vec(act,
|
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);
|
|
|
|
assert !equal(v0, v1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_equal_greatly_differing_sizes() {
|
|
|
|
let v0 = bitv(10u, false);
|
|
|
|
let v1 = bitv(110u, false);
|
|
|
|
assert !equal(v0, v1);
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
//
|