|
| 1 | +import std._uint; |
| 2 | +import std._int; |
| 3 | +import std._vec; |
| 4 | + |
| 5 | +// FIXME: With recursive object types, we could implement binary methods like |
| 6 | +// union, intersection, and difference. At that point, we could write |
| 7 | +// an optimizing version of this module that produces a different obj |
| 8 | +// for the case where nbits < 32. |
| 9 | + |
| 10 | +state type t = rec(vec[mutable uint] storage, uint nbits); |
| 11 | + |
| 12 | +// FIXME: we should bind std.max_int |
| 13 | +fn is32bit() -> bool { |
| 14 | + let uint n = 0xffffffffu; |
| 15 | + ret (n + 1u) == 0u; |
| 16 | +} |
| 17 | + |
| 18 | +fn uint_bits() -> uint { |
| 19 | + if (is32bit()) { |
| 20 | + ret 31u; |
| 21 | + } else { |
| 22 | + ret 63u; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// FIXME: this should be state |
| 27 | +fn create(uint nbits, bool init) -> t { |
| 28 | + auto elt; |
| 29 | + if (init) { |
| 30 | + elt = 1u; |
| 31 | + } else { |
| 32 | + elt = 0u; |
| 33 | + } |
| 34 | + |
| 35 | + ret rec(storage = _vec.init_elt[mutable uint](nbits / uint_bits() + 1u, elt), |
| 36 | + nbits = nbits); |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +// FIXME: this should be state |
| 42 | +fn process(fn(uint, uint) -> uint op, t v0, t v1) -> bool { |
| 43 | + auto len = _vec.len[mutable uint](v1.storage); |
| 44 | + |
| 45 | + check (_vec.len[mutable uint](v0.storage) == len); |
| 46 | + check (v0.nbits == v1.nbits); |
| 47 | + |
| 48 | + auto changed = false; |
| 49 | + |
| 50 | + for each (uint i in _uint.range(0u, len)) { |
| 51 | + auto w0 = v0.storage.(i); |
| 52 | + auto w1 = v1.storage.(i); |
| 53 | + |
| 54 | + auto w = op(w0, w1); |
| 55 | + if (w0 != w) { |
| 56 | + changed = true; |
| 57 | + v0.storage.(i) = w; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + ret changed; |
| 62 | +} |
| 63 | + |
| 64 | +fn lor(uint w0, uint w1) -> uint { |
| 65 | + ret w0 | w1; |
| 66 | +} |
| 67 | + |
| 68 | +// FIXME: this should be state |
| 69 | +fn union(t v0, t v1) -> bool { |
| 70 | + auto sub = lor; |
| 71 | + ret process(sub, v0, v1); |
| 72 | +} |
| 73 | + |
| 74 | +fn land(uint w0, uint w1) -> uint { |
| 75 | + ret w0 & w1; |
| 76 | +} |
| 77 | + |
| 78 | +// FIXME: this should be state |
| 79 | +fn intersect(t v0, t v1) -> bool { |
| 80 | + auto sub = land; |
| 81 | + ret process(sub, v0, v1); |
| 82 | +} |
| 83 | + |
| 84 | +fn right(uint w0, uint w1) -> uint { |
| 85 | + ret w1; |
| 86 | +} |
| 87 | + |
| 88 | +// FIXME: this should be state |
| 89 | +fn copy(t v0, t v1) -> bool { |
| 90 | + auto sub = right; |
| 91 | + ret process(sub, v0, v1); |
| 92 | +} |
| 93 | + |
| 94 | +// FIXME: this should be state |
| 95 | +fn get(t v, uint i) -> bool { |
| 96 | + check (i < v.nbits); |
| 97 | + |
| 98 | + auto bits = uint_bits(); |
| 99 | + |
| 100 | + auto w = i / bits; |
| 101 | + auto b = i % bits; |
| 102 | + auto x = 1u & (v.storage.(w) >> b); |
| 103 | + ret x == 1u; |
| 104 | +} |
| 105 | + |
| 106 | +// FIXME: this should be state |
| 107 | +fn equal(t v0, t v1) -> bool { |
| 108 | + // FIXME: when we can break or return from inside an iterator loop, |
| 109 | + // we can eliminate this painful while-loop |
| 110 | + auto len = _vec.len[mutable uint](v1.storage); |
| 111 | + auto i = 0u; |
| 112 | + while (i < len) { |
| 113 | + if (v0.storage.(i) != v1.storage.(i)) { |
| 114 | + ret false; |
| 115 | + } |
| 116 | + i = i + 1u; |
| 117 | + } |
| 118 | + ret true; |
| 119 | +} |
| 120 | + |
| 121 | +// FIXME: this should be state |
| 122 | +fn clear(t v) { |
| 123 | + for each (uint i in _uint.range(0u, _vec.len[mutable uint](v.storage))) { |
| 124 | + v.storage.(i) = 0u; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// FIXME: this should be state |
| 129 | +fn invert(t v) { |
| 130 | + for each (uint i in _uint.range(0u, _vec.len[mutable uint](v.storage))) { |
| 131 | + v.storage.(i) = ~v.storage.(i); |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// FIXME: this should be state |
| 136 | +/* v0 = v0 - v1 */ |
| 137 | +fn difference(t v0, t v1) -> bool { |
| 138 | + invert(v1); |
| 139 | + auto b = intersect(v0, v1); |
| 140 | + invert(v1); |
| 141 | + ret b; |
| 142 | +} |
| 143 | + |
| 144 | +// FIXME: this should be state |
| 145 | +fn set(t v, uint i, bool x) { |
| 146 | + check (i < v.nbits); |
| 147 | + |
| 148 | + auto bits = uint_bits(); |
| 149 | + |
| 150 | + auto w = i / bits; |
| 151 | + auto b = i % bits; |
| 152 | + auto w0 = v.storage.(w); |
| 153 | + auto flag = 1u << b; |
| 154 | + if (x) { |
| 155 | + v.storage.(w) = v.storage.(w) | flag; |
| 156 | + } else { |
| 157 | + v.storage.(w) = v.storage.(w) & ~flag; |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// FIXME: this should be state |
| 162 | +fn init_to_vec(t v, uint i) -> uint { |
| 163 | + if (get(v, i)) { |
| 164 | + ret 1u; |
| 165 | + } else { |
| 166 | + ret 0u; |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +// FIXME: this should be state |
| 171 | +fn to_vec(t v) -> vec[uint] { |
| 172 | + auto sub = bind init_to_vec(v, _); |
| 173 | + ret _vec.init_fn[uint](sub, v.nbits); |
| 174 | +} |
| 175 | + |
| 176 | +// |
| 177 | +// Local Variables: |
| 178 | +// mode: rust |
| 179 | +// fill-column: 78; |
| 180 | +// indent-tabs-mode: nil |
| 181 | +// c-basic-offset: 4 |
| 182 | +// buffer-file-coding-system: utf-8-unix |
| 183 | +// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; |
| 184 | +// End: |
| 185 | +// |
0 commit comments