Skip to content

Commit cf26241

Browse files
committed
Document std::bitv
1 parent 33f2f22 commit cf26241

File tree

1 file changed

+125
-3
lines changed

1 file changed

+125
-3
lines changed

src/lib/bitv.rs

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,67 @@ fn process(op: block(uint, uint) -> uint, v0: t, v1: t) -> bool {
6868
ret changed;
6969
}
7070

71+
7172
fn lor(w0: uint, w1: uint) -> uint { ret w0 | w1; }
7273

7374
fn union(v0: t, v1: t) -> bool { let sub = lor; ret process(sub, v0, v1); }
7475

7576
fn land(w0: uint, w1: uint) -> uint { ret w0 & w1; }
7677

78+
/*
79+
Function: intersect
80+
81+
Calculates the intersection of two bitvectors
82+
83+
Sets `v0` to the intersection of `v0` and `v1`
84+
85+
Preconditions:
86+
87+
Both bitvectors must be the same length
88+
89+
Returns:
90+
91+
True if `v0` was changed
92+
*/
7793
fn intersect(v0: t, v1: t) -> bool {
7894
let sub = land;
7995
ret process(sub, v0, v1);
8096
}
8197

8298
fn right(_w0: uint, w1: uint) -> uint { ret w1; }
8399

100+
/*
101+
Function: assign
102+
103+
Assigns the value of `v1` to `v0`
104+
105+
Preconditions:
106+
107+
Both bitvectors must be the same length
108+
109+
Returns:
110+
111+
True if `v0` was changed
112+
*/
84113
fn assign(v0: t, v1: t) -> bool { let sub = right; ret process(sub, v0, v1); }
85114

115+
/*
116+
Function: clone
117+
118+
Makes a copy of a bitvector
119+
*/
86120
fn clone(v: t) -> t {
87121
let storage = vec::init_elt_mut::<uint>(0u, v.nbits / uint_bits() + 1u);
88122
let len = vec::len(v.storage);
89123
uint::range(0u, len) {|i| storage[i] = v.storage[i]; };
90124
ret @{storage: storage, nbits: v.nbits};
91125
}
92126

127+
/*
128+
Function: get
129+
130+
Retreive the value at index `i`
131+
*/
93132
fn get(v: t, i: uint) -> bool {
94133
assert (i < v.nbits);
95134
let bits = uint_bits();
@@ -99,6 +138,21 @@ fn get(v: t, i: uint) -> bool {
99138
ret x == 1u;
100139
}
101140

141+
// FIXME: This doesn't account for the actual size of the vectors,
142+
// so it could end up comparing garbage bits
143+
/*
144+
Function: equal
145+
146+
Compares two bitvectors
147+
148+
Preconditions:
149+
150+
Both bitvectors must be the same length
151+
152+
Returns:
153+
154+
True if both bitvectors contain identical elements
155+
*/
102156
fn equal(v0: t, v1: t) -> bool {
103157
// FIXME: when we can break or return from inside an iterator loop,
104158
// we can eliminate this painful while-loop
@@ -112,29 +166,67 @@ fn equal(v0: t, v1: t) -> bool {
112166
ret true;
113167
}
114168

169+
/*
170+
Function: clear
171+
172+
Set all bits to 0
173+
*/
115174
fn clear(v: t) {
116175
uint::range(0u, vec::len(v.storage)) {|i| v.storage[i] = 0u; };
117176
}
118177

178+
/*
179+
Function: set_all
180+
181+
Set all bits to 1
182+
*/
119183
fn set_all(v: t) {
120184
uint::range(0u, v.nbits) {|i| set(v, i, true); };
121185
}
122186

187+
/*
188+
Function: invert
189+
190+
Invert all bits
191+
*/
123192
fn invert(v: t) {
124193
uint::range(0u, vec::len(v.storage)) {|i|
125194
v.storage[i] = !v.storage[i];
126195
};
127196
}
128197

198+
/*
199+
Function: difference
200+
201+
Calculate the difference between two bitvectors
202+
203+
Sets each element of `v0` to the value of that element minus the element
204+
of `v1` at the same index.
205+
206+
Preconditions:
207+
208+
Both bitvectors must be the same length
129209
130-
/* v0 = v0 - v1 */
210+
Returns:
211+
212+
True if `v0` was changed
213+
*/
131214
fn difference(v0: t, v1: t) -> bool {
132215
invert(v1);
133216
let b = intersect(v0, v1);
134217
invert(v1);
135218
ret b;
136219
}
137220

221+
/*
222+
Function: set
223+
224+
Set the value of a bit at a given index
225+
226+
Preconditions:
227+
228+
`i` must be less than the length of the bitvector
229+
*/
138230
fn set(v: t, i: uint, x: bool) {
139231
assert (i < v.nbits);
140232
let bits = uint_bits();
@@ -145,32 +237,62 @@ fn set(v: t, i: uint, x: bool) {
145237
}
146238

147239

148-
/* true if all bits are 1 */
240+
/*
241+
Function: is_true
242+
243+
Returns true if all bits are 1
244+
*/
149245
fn is_true(v: t) -> bool {
150246
for i: uint in to_vec(v) { if i != 1u { ret false; } }
151247
ret true;
152248
}
153249

154250

155-
/* true if all bits are non-1 */
251+
/*
252+
Function: is_false
253+
254+
Returns true if all bits are 0
255+
*/
156256
fn is_false(v: t) -> bool {
157257
for i: uint in to_vec(v) { if i == 1u { ret false; } }
158258
ret true;
159259
}
160260

161261
fn init_to_vec(v: t, i: uint) -> uint { ret if get(v, i) { 1u } else { 0u }; }
162262

263+
/*
264+
Function: to_vec
265+
266+
Converts the bitvector to a vector of uint with the same length. Each uint
267+
in the resulting vector has either value 0u or 1u.
268+
*/
163269
fn to_vec(v: t) -> [uint] {
164270
let sub = bind init_to_vec(v, _);
165271
ret vec::init_fn::<uint>(sub, v.nbits);
166272
}
167273

274+
/*
275+
Function: to_str
276+
277+
Converts the bitvector to a string. The resulting string has the same
278+
length as the bitvector, and each character is either '0' or '1'.
279+
*/
168280
fn to_str(v: t) -> str {
169281
let rs = "";
170282
for i: uint in to_vec(v) { if i == 1u { rs += "1"; } else { rs += "0"; } }
171283
ret rs;
172284
}
173285

286+
/*
287+
Function: eq_vec
288+
289+
Compare a bitvector to a vector of uint. The uint vector is expected to
290+
only contain the values 0u and 1u.
291+
292+
Preconditions:
293+
294+
Both the bitvector and vector must have the same length
295+
*/
174296
fn eq_vec(v0: t, v1: [uint]) -> bool {
175297
assert (v0.nbits == vec::len::<uint>(v1));
176298
let len = v0.nbits;

0 commit comments

Comments
 (0)