rust/src/libstd/bitv.rs

317 lines
6.0 KiB
Rust
Raw Normal View History

/*
Module: bitv
Bitvectors.
*/
export t;
export create;
export union;
export intersect;
export assign;
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 16:09:17 -07:00
export to_vec;
export to_str;
2011-08-18 14:32:25 -07:00
export eq_vec;
// 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
// for the case where nbits <= 32.
/*
Type: t
The bitvector type.
*/
type t = @{storage: [mutable uint], nbits: uint};
const uint_bits: uint = 32u + (1u << 32u >> 27u);
/*
Function: create
Constructs a bitvector.
Parameters:
nbits - The number of bits in the bitvector
init - If true then the bits are initialized to 1, otherwise 0
*/
2011-07-27 14:19:39 +02:00
fn create(nbits: uint, init: bool) -> t {
let elt = if init { !0u } else { 0u };
let storage = vec::init_elt_mut::<uint>(elt, nbits / uint_bits + 1u);
2011-07-27 14:19:39 +02:00
ret @{storage: storage, nbits: nbits};
}
fn process(op: block(uint, uint) -> uint, v0: t, v1: t) -> bool {
2011-08-15 16:38:23 -07:00
let len = vec::len(v1.storage);
assert (vec::len(v0.storage) == len);
assert (v0.nbits == v1.nbits);
2011-07-27 14:19:39 +02:00
let changed = false;
uint::range(0u, len) {|i|
let w0 = v0.storage[i];
let w1 = v1.storage[i];
2011-07-27 14:19:39 +02:00
let w = op(w0, w1);
if w0 != w { changed = true; v0.storage[i] = w; }
};
ret changed;
}
2011-10-27 15:35:56 -07:00
2011-07-27 14:19:39 +02:00
fn lor(w0: uint, w1: uint) -> uint { ret w0 | w1; }
fn union(v0: t, v1: t) -> bool { let sub = lor; ret process(sub, v0, v1); }
2011-07-27 14:19:39 +02:00
fn land(w0: uint, w1: uint) -> uint { ret w0 & w1; }
2011-10-27 15:35:56 -07:00
/*
Function: intersect
Calculates the intersection of two bitvectors
Sets `v0` to the intersection of `v0` and `v1`
Preconditions:
Both bitvectors must be the same length
Returns:
True if `v0` was changed
*/
fn intersect(v0: t, v1: t) -> bool {
2011-07-27 14:19:39 +02:00
let sub = land;
ret process(sub, v0, v1);
}
fn right(_w0: uint, w1: uint) -> uint { ret w1; }
2011-10-27 15:35:56 -07:00
/*
Function: assign
Assigns the value of `v1` to `v0`
Preconditions:
Both bitvectors must be the same length
Returns:
True if `v0` was changed
*/
fn assign(v0: t, v1: t) -> bool { let sub = right; ret process(sub, v0, v1); }
2011-10-27 15:35:56 -07:00
/*
Function: clone
Makes a copy of a bitvector
*/
2011-07-27 14:19:39 +02:00
fn clone(v: t) -> t {
let storage = vec::init_elt_mut::<uint>(0u, v.nbits / uint_bits + 1u);
2011-08-15 16:38:23 -07:00
let len = vec::len(v.storage);
uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
2011-07-27 14:19:39 +02:00
ret @{storage: storage, nbits: v.nbits};
}
2011-10-27 15:35:56 -07:00
/*
Function: get
Retreive the value at index `i`
*/
fn get(v: t, i: uint) -> bool {
assert (i < v.nbits);
let bits = uint_bits;
2011-07-27 14:19:39 +02:00
let w = i / bits;
let b = i % bits;
let x = 1u & v.storage[w] >> b;
ret x == 1u;
}
2011-10-27 15:35:56 -07:00
// FIXME: This doesn't account for the actual size of the vectors,
// so it could end up comparing garbage bits
/*
Function: equal
Compares two bitvectors
Preconditions:
Both bitvectors must be the same length
Returns:
True if both bitvectors contain identical elements
*/
fn equal(v0: t, v1: t) -> bool {
// FIXME: when we can break or return from inside an iterator loop,
// we can eliminate this painful while-loop
2011-08-15 16:38:23 -07:00
let len = vec::len(v1.storage);
2011-07-27 14:19:39 +02:00
let i = 0u;
while i < len {
if v0.storage[i] != v1.storage[i] { ret false; }
i = i + 1u;
}
ret true;
}
2011-10-27 15:35:56 -07:00
/*
Function: clear
Set all bits to 0
*/
fn clear(v: t) {
uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
}
2011-10-27 15:35:56 -07:00
/*
Function: set_all
Set all bits to 1
*/
fn set_all(v: t) {
uint::range(0u, v.nbits) {|i| set(v, i, true); };
}
2011-10-27 15:35:56 -07:00
/*
Function: invert
Invert all bits
*/
fn invert(v: t) {
uint::range(0u, vec::len(v.storage)) {|i|
v.storage[i] = !v.storage[i];
};
}
2011-10-27 15:35:56 -07:00
/*
Function: difference
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.
Preconditions:
Both bitvectors must be the same length
2011-10-27 15:35:56 -07:00
Returns:
True if `v0` was changed
*/
fn difference(v0: t, v1: t) -> bool {
invert(v1);
2011-07-27 14:19:39 +02:00
let b = intersect(v0, v1);
invert(v1);
ret b;
}
2011-10-27 15:35:56 -07:00
/*
Function: set
Set the value of a bit at a given index
Preconditions:
`i` must be less than the length of the bitvector
*/
fn set(v: t, i: uint, x: bool) {
assert (i < v.nbits);
let bits = uint_bits;
2011-07-27 14:19:39 +02:00
let w = i / bits;
let b = i % bits;
let flag = 1u << b;
v.storage[w] = if x { v.storage[w] | flag } else { v.storage[w] & !flag };
}
2011-10-27 15:35:56 -07:00
/*
Function: is_true
Returns true if all bits are 1
*/
fn is_true(v: t) -> bool {
2011-08-15 16:09:17 -07:00
for i: uint in to_vec(v) { if i != 1u { ret false; } }
ret true;
}
2011-10-27 15:35:56 -07:00
/*
Function: is_false
Returns true if all bits are 0
*/
fn is_false(v: t) -> bool {
2011-08-15 16:09:17 -07:00
for i: uint in to_vec(v) { if i == 1u { ret false; } }
ret true;
}
2011-07-27 14:19:39 +02:00
fn init_to_vec(v: t, i: uint) -> uint { ret if get(v, i) { 1u } else { 0u }; }
2011-10-27 15:35:56 -07:00
/*
Function: to_vec
Converts the bitvector to a vector of uint with the same length. Each uint
in the resulting vector has either value 0u or 1u.
*/
fn to_vec(v: t) -> [uint] {
2011-07-27 14:19:39 +02:00
let sub = bind init_to_vec(v, _);
ret vec::init_fn::<uint>(sub, v.nbits);
}
2011-10-27 15:35:56 -07:00
/*
Function: to_str
Converts the bitvector to a string. The resulting string has the same
length as the bitvector, and each character is either '0' or '1'.
*/
fn to_str(v: t) -> str {
2011-09-02 15:34:58 -07:00
let rs = "";
for i: uint in to_vec(v) { if i == 1u { rs += "1"; } else { rs += "0"; } }
2011-06-24 17:55:02 +02:00
ret rs;
}
2011-10-27 15:35:56 -07:00
/*
Function: eq_vec
Compare a bitvector to a vector of uint. The uint vector is expected to
only contain the values 0u and 1u.
Preconditions:
Both the bitvector and vector must have the same length
*/
fn eq_vec(v0: t, v1: [uint]) -> bool {
assert (v0.nbits == vec::len::<uint>(v1));
2011-07-27 14:19:39 +02:00
let len = v0.nbits;
let i = 0u;
while i < len {
let w0 = get(v0, i);
let w1 = v1[i];
2011-07-27 14:19:39 +02:00
if !w0 && w1 != 0u || w0 && w1 == 0u { ret false; }
i = i + 1u;
}
ret true;
}
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//