2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-20 21:12:35 -05:00
|
|
|
export t;
|
|
|
|
export create;
|
|
|
|
export union;
|
|
|
|
export intersect;
|
|
|
|
export copy;
|
|
|
|
export clone;
|
|
|
|
export get;
|
|
|
|
export equal;
|
|
|
|
export clear;
|
|
|
|
export set_all;
|
|
|
|
export invert;
|
|
|
|
export difference;
|
|
|
|
export set;
|
|
|
|
export is_true;
|
|
|
|
export is_false;
|
|
|
|
export to_vec;
|
|
|
|
export to_str;
|
|
|
|
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.
|
|
|
|
|
|
|
|
// FIXME: Almost all the functions in this module should be state fns, but the
|
|
|
|
// effect system isn't currently working correctly.
|
2011-06-15 13:19:50 -05:00
|
|
|
type t = rec(vec[mutable uint] storage, uint nbits);
|
|
|
|
|
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
|
|
|
|
|
|
|
fn create(uint nbits, bool init) -> t {
|
2011-06-10 12:46:59 -05:00
|
|
|
auto elt = if (init) { !0u } else { 0u };
|
2011-05-17 13:41:41 -05:00
|
|
|
auto storage = vec::init_elt_mut[uint](elt, nbits / uint_bits() + 1u);
|
2011-06-15 13:19:50 -05:00
|
|
|
ret rec(storage=storage, nbits=nbits);
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn process(&fn(uint, uint) -> uint op, &t v0, &t v1) -> bool {
|
2011-05-17 13:41:41 -05:00
|
|
|
auto len = vec::len(v1.storage);
|
|
|
|
assert (vec::len(v0.storage) == len);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (v0.nbits == v1.nbits);
|
2010-10-20 19:04:15 -05:00
|
|
|
auto changed = false;
|
2011-05-17 13:41:41 -05:00
|
|
|
for each (uint i in uint::range(0u, len)) {
|
2010-10-20 19:04:15 -05:00
|
|
|
auto w0 = v0.storage.(i);
|
|
|
|
auto w1 = v1.storage.(i);
|
|
|
|
auto w = op(w0, w1);
|
2011-06-15 13:19:50 -05:00
|
|
|
if (w0 != w) { changed = true; v0.storage.(i) = w; }
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
ret changed;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn lor(uint w0, uint w1) -> uint { ret w0 | w1; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn union(&t v0, &t v1) -> bool { auto sub = lor; ret process(sub, v0, v1); }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn land(uint w0, uint w1) -> uint { ret w0 & w1; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn intersect(&t v0, &t v1) -> bool {
|
2010-10-20 19:04:15 -05:00
|
|
|
auto sub = land;
|
|
|
|
ret process(sub, v0, v1);
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn right(uint w0, uint w1) -> uint { ret w1; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
fn copy(&t v0, t v1) -> bool { auto sub = right; ret process(sub, v0, v1); }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2011-04-12 14:16:21 -05:00
|
|
|
fn clone(t v) -> t {
|
2011-05-17 13:41:41 -05:00
|
|
|
auto storage = vec::init_elt_mut[uint](0u, v.nbits / uint_bits() + 1u);
|
|
|
|
auto len = vec::len(v.storage);
|
2011-06-15 13:19:50 -05:00
|
|
|
for each (uint i in uint::range(0u, len)) { storage.(i) = v.storage.(i); }
|
|
|
|
ret rec(storage=storage, nbits=v.nbits);
|
2011-04-12 14:16:21 -05:00
|
|
|
}
|
|
|
|
|
2010-10-21 09:36:13 -05:00
|
|
|
fn get(&t v, uint i) -> bool {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (i < v.nbits);
|
2010-10-20 19:04:15 -05:00
|
|
|
auto bits = uint_bits();
|
|
|
|
auto w = i / bits;
|
|
|
|
auto b = i % bits;
|
2011-06-15 13:19:50 -05:00
|
|
|
auto x = 1u & v.storage.(w) >> b;
|
2010-10-20 19:04:15 -05:00
|
|
|
ret x == 1u;
|
|
|
|
}
|
|
|
|
|
2010-10-21 09:36:13 -05:00
|
|
|
fn equal(&t v0, &t v1) -> 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-05-17 13:41:41 -05:00
|
|
|
auto len = vec::len(v1.storage);
|
2010-10-20 19:04:15 -05:00
|
|
|
auto i = 0u;
|
|
|
|
while (i < len) {
|
2011-06-15 13:19:50 -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-04-19 15:35:49 -05:00
|
|
|
fn clear(&t v) {
|
2011-05-17 13:41:41 -05:00
|
|
|
for each (uint i in uint::range(0u, vec::len(v.storage))) {
|
2010-10-20 19:04:15 -05:00
|
|
|
v.storage.(i) = 0u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-18 17:38:11 -05:00
|
|
|
fn set_all(&t v) {
|
2011-06-15 13:19:50 -05:00
|
|
|
for each (uint i in uint::range(0u, v.nbits)) { set(v, i, true); }
|
2011-05-18 17:38:11 -05:00
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn invert(&t v) {
|
2011-05-17 13:41:41 -05:00
|
|
|
for each (uint i in uint::range(0u, vec::len(v.storage))) {
|
2011-06-10 12:46:59 -05:00
|
|
|
v.storage.(i) = !v.storage.(i);
|
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-04-19 15:35:49 -05:00
|
|
|
fn difference(&t v0, &t v1) -> bool {
|
2010-10-20 19:04:15 -05:00
|
|
|
invert(v1);
|
|
|
|
auto b = intersect(v0, v1);
|
|
|
|
invert(v1);
|
|
|
|
ret b;
|
|
|
|
}
|
|
|
|
|
2011-04-19 15:35:49 -05:00
|
|
|
fn set(&t v, uint i, bool x) {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (i < v.nbits);
|
2010-10-20 19:04:15 -05:00
|
|
|
auto bits = uint_bits();
|
|
|
|
auto w = i / bits;
|
|
|
|
auto b = i % bits;
|
|
|
|
auto w0 = v.storage.(w);
|
|
|
|
auto flag = 1u << b;
|
2011-06-15 13:19:50 -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 */
|
|
|
|
fn is_true(&t v) -> bool {
|
2011-06-15 13:19:50 -05:00
|
|
|
for (uint i 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 */
|
|
|
|
fn is_false(&t v) -> bool {
|
2011-06-15 13:19:50 -05:00
|
|
|
for (uint i 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
|
|
|
fn init_to_vec(t v, uint i) -> uint { ret if (get(v, i)) { 1u } else { 0u }; }
|
2010-10-20 19:04:15 -05:00
|
|
|
|
2010-10-21 09:36:13 -05:00
|
|
|
fn to_vec(&t v) -> vec[uint] {
|
2010-10-20 19:04:15 -05:00
|
|
|
auto sub = bind init_to_vec(v, _);
|
2011-05-17 13:41:41 -05:00
|
|
|
ret vec::init_fn[uint](sub, v.nbits);
|
2010-10-20 19:04:15 -05:00
|
|
|
}
|
|
|
|
|
2011-04-07 20:15:56 -05:00
|
|
|
fn to_str(&t v) -> str {
|
|
|
|
auto res = "";
|
2011-05-12 10:24:54 -05:00
|
|
|
for (uint i in bitv::to_vec(v)) {
|
2011-06-15 13:19:50 -05:00
|
|
|
if (i == 1u) { res += "1"; } else { res += "0"; }
|
2011-04-07 20:15:56 -05:00
|
|
|
}
|
|
|
|
ret res;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2010-10-21 09:36:13 -05:00
|
|
|
// FIXME: can we just use structural equality on to_vec?
|
|
|
|
fn eq_vec(&t v0, &vec[uint] v1) -> bool {
|
2011-05-17 13:41:41 -05:00
|
|
|
assert (v0.nbits == vec::len[uint](v1));
|
2010-10-21 09:36:13 -05:00
|
|
|
auto len = v0.nbits;
|
|
|
|
auto i = 0u;
|
|
|
|
while (i < len) {
|
|
|
|
auto w0 = get(v0, i);
|
|
|
|
auto w1 = v1.(i);
|
2011-06-15 13:19:50 -05:00
|
|
|
if (!w0 && w1 != 0u || w0 && w1 == 0u) { ret false; }
|
2010-10-21 09:36:13 -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:
|
|
|
|
//
|