2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-20 21:12:35 -05:00
|
|
|
export t;
|
|
|
|
export create;
|
|
|
|
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;
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2010-10-20 19:04:15 -05:00
|
|
|
// FIXME: 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
|
2010-10-21 09:36:13 -05:00
|
|
|
// for the case where nbits <= 32.
|
|
|
|
|
2011-08-04 19:24:54 -05:00
|
|
|
type t = @{storage: [mutable uint], nbits: uint};
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2010-10-21 00:04:19 -05:00
|
|
|
// FIXME: this should be a constant once they work
|
2011-06-15 13:19:50 -05:00
|
|
|
fn uint_bits() -> uint { ret 32u + (1u << 32u >> 27u); }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn create(nbits: uint, init: bool) -> t {
|
|
|
|
let elt = if init { !0u } else { 0u };
|
2011-08-12 12:56:57 -05:00
|
|
|
let storage = vec::init_elt_mut::<uint>(elt, nbits / uint_bits() + 1u);
|
2011-07-27 07:19:39 -05:00
|
|
|
ret @{storage: storage, nbits: nbits};
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn process(op: block(uint, uint) -> uint, v0: t, v1: t) -> 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);
|
2011-07-27 07:19:39 -05:00
|
|
|
let changed = false;
|
2011-10-21 05:31:48 -05:00
|
|
|
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-07-27 07:19:39 -05:00
|
|
|
fn lor(w0: uint, w1: uint) -> uint { ret w0 | w1; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn union(v0: t, v1: t) -> bool { let sub = lor; ret process(sub, v0, v1); }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn land(w0: uint, w1: uint) -> uint { ret w0 & w1; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn intersect(v0: t, v1: t) -> bool {
|
2011-07-27 07:19:39 -05:00
|
|
|
let sub = land;
|
2010-10-20 19:04:15 -05:00
|
|
|
ret process(sub, v0, v1);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn assign(v0: t, v1: t) -> bool { let sub = right; ret process(sub, v0, v1); }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn clone(v: t) -> t {
|
2011-08-12 12:56:57 -05:00
|
|
|
let storage = vec::init_elt_mut::<uint>(0u, v.nbits / uint_bits() + 1u);
|
2011-08-15 18:38:23 -05:00
|
|
|
let len = vec::len(v.storage);
|
2011-10-21 05:31:48 -05:00
|
|
|
uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
|
2011-07-27 07:19:39 -05:00
|
|
|
ret @{storage: storage, nbits: v.nbits};
|
2011-04-12 14:16:21 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn get(v: t, i: uint) -> bool {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (i < v.nbits);
|
2011-07-27 07:19:39 -05:00
|
|
|
let bits = uint_bits();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn equal(v0: t, v1: t) -> bool {
|
2010-10-20 19:04:15 -05:00
|
|
|
// FIXME: when we can break or return from inside an iterator loop,
|
|
|
|
// we can eliminate this painful while-loop
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-08-15 18:38:23 -05:00
|
|
|
let len = vec::len(v1.storage);
|
2011-07-27 07:19:39 -05:00
|
|
|
let i = 0u;
|
|
|
|
while i < len {
|
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
|
|
|
i = i + 1u;
|
|
|
|
}
|
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn clear(v: t) {
|
2011-10-21 05:31:48 -05:00
|
|
|
uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn set_all(v: t) {
|
2011-10-21 05:31:48 -05:00
|
|
|
uint::range(0u, v.nbits) {|i| set(v, i, true); };
|
2011-05-18 17:38:11 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn invert(v: t) {
|
2011-10-21 05:31:48 -05:00
|
|
|
uint::range(0u, vec::len(v.storage)) {|i|
|
2011-08-19 17:16:48 -05:00
|
|
|
v.storage[i] = !v.storage[i];
|
2011-10-21 05:31:48 -05:00
|
|
|
};
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2010-10-20 19:04:15 -05:00
|
|
|
/* v0 = v0 - v1 */
|
2011-09-12 04:27:30 -05:00
|
|
|
fn difference(v0: t, v1: t) -> 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;
|
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn set(v: t, i: uint, x: bool) {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (i < v.nbits);
|
2011-07-27 07:19:39 -05:00
|
|
|
let bits = uint_bits();
|
|
|
|
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
|
|
|
|
2011-04-06 19:56:44 -05:00
|
|
|
/* true if all bits are 1 */
|
2011-09-12 04:27:30 -05:00
|
|
|
fn is_true(v: t) -> bool {
|
2011-08-15 18:09:17 -05:00
|
|
|
for i: uint in to_vec(v) { if i != 1u { ret false; } }
|
2011-04-06 19:56:44 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-04-06 19:56:44 -05:00
|
|
|
/* true if all bits are non-1 */
|
2011-09-12 04:27:30 -05:00
|
|
|
fn is_false(v: t) -> bool {
|
2011-08-15 18:09:17 -05:00
|
|
|
for i: uint in to_vec(v) { if i == 1u { ret false; } }
|
2011-04-06 19:56:44 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn init_to_vec(v: t, i: uint) -> uint { ret if get(v, i) { 1u } else { 0u }; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn to_vec(v: t) -> [uint] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let sub = bind init_to_vec(v, _);
|
2011-08-12 12:56:57 -05:00
|
|
|
ret vec::init_fn::<uint>(sub, v.nbits);
|
2011-07-12 16:07:00 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn to_str(v: t) -> str {
|
2011-09-02 17:34:58 -05:00
|
|
|
let rs = "";
|
|
|
|
for i: uint in to_vec(v) { if i == 1u { rs += "1"; } else { rs += "0"; } }
|
2011-06-24 10:55:02 -05:00
|
|
|
ret rs;
|
2011-04-07 20:15:56 -05:00
|
|
|
}
|
|
|
|
|
2011-09-12 04:27:30 -05:00
|
|
|
fn eq_vec(v0: t, 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;
|
|
|
|
let i = 0u;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
2011-05-16 20:21:22 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-10-20 19:04:15 -05:00
|
|
|
// End:
|
|
|
|
//
|