@@ -68,28 +68,67 @@ fn process(op: block(uint, uint) -> uint, v0: t, v1: t) -> bool {
68
68
ret changed;
69
69
}
70
70
71
+
71
72
fn lor ( w0 : uint , w1 : uint ) -> uint { ret w0 | w1; }
72
73
73
74
fn union ( v0 : t , v1 : t ) -> bool { let sub = lor; ret process( sub, v0, v1) ; }
74
75
75
76
fn land ( w0 : uint , w1 : uint ) -> uint { ret w0 & w1; }
76
77
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
+ */
77
93
fn intersect ( v0 : t , v1 : t ) -> bool {
78
94
let sub = land;
79
95
ret process( sub, v0, v1) ;
80
96
}
81
97
82
98
fn right ( _w0 : uint , w1 : uint ) -> uint { ret w1; }
83
99
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
+ */
84
113
fn assign ( v0 : t , v1 : t ) -> bool { let sub = right; ret process( sub, v0, v1) ; }
85
114
115
+ /*
116
+ Function: clone
117
+
118
+ Makes a copy of a bitvector
119
+ */
86
120
fn clone ( v : t ) -> t {
87
121
let storage = vec:: init_elt_mut :: < uint > ( 0 u, v. nbits / uint_bits ( ) + 1 u) ;
88
122
let len = vec:: len ( v. storage ) ;
89
123
uint:: range ( 0 u, len) { |i| storage[ i] = v. storage [ i] ; } ;
90
124
ret @{ storage : storage, nbits : v. nbits } ;
91
125
}
92
126
127
+ /*
128
+ Function: get
129
+
130
+ Retreive the value at index `i`
131
+ */
93
132
fn get ( v : t , i : uint ) -> bool {
94
133
assert ( i < v. nbits ) ;
95
134
let bits = uint_bits ( ) ;
@@ -99,6 +138,21 @@ fn get(v: t, i: uint) -> bool {
99
138
ret x == 1 u;
100
139
}
101
140
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
+ */
102
156
fn equal ( v0 : t , v1 : t ) -> bool {
103
157
// FIXME: when we can break or return from inside an iterator loop,
104
158
// we can eliminate this painful while-loop
@@ -112,29 +166,67 @@ fn equal(v0: t, v1: t) -> bool {
112
166
ret true;
113
167
}
114
168
169
+ /*
170
+ Function: clear
171
+
172
+ Set all bits to 0
173
+ */
115
174
fn clear ( v : t ) {
116
175
uint:: range ( 0 u, vec:: len ( v. storage ) ) { |i| v. storage [ i] = 0 u; } ;
117
176
}
118
177
178
+ /*
179
+ Function: set_all
180
+
181
+ Set all bits to 1
182
+ */
119
183
fn set_all ( v : t ) {
120
184
uint:: range ( 0 u, v. nbits ) { |i| set ( v, i, true ) ; } ;
121
185
}
122
186
187
+ /*
188
+ Function: invert
189
+
190
+ Invert all bits
191
+ */
123
192
fn invert ( v : t ) {
124
193
uint:: range ( 0 u, vec:: len ( v. storage ) ) { |i|
125
194
v. storage [ i] = !v. storage [ i] ;
126
195
} ;
127
196
}
128
197
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
129
209
130
- /* v0 = v0 - v1 */
210
+ Returns:
211
+
212
+ True if `v0` was changed
213
+ */
131
214
fn difference ( v0 : t , v1 : t ) -> bool {
132
215
invert ( v1) ;
133
216
let b = intersect ( v0, v1) ;
134
217
invert ( v1) ;
135
218
ret b;
136
219
}
137
220
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
+ */
138
230
fn set ( v : t , i : uint , x : bool ) {
139
231
assert ( i < v. nbits ) ;
140
232
let bits = uint_bits ( ) ;
@@ -145,32 +237,62 @@ fn set(v: t, i: uint, x: bool) {
145
237
}
146
238
147
239
148
- /* true if all bits are 1 */
240
+ /*
241
+ Function: is_true
242
+
243
+ Returns true if all bits are 1
244
+ */
149
245
fn is_true ( v : t ) -> bool {
150
246
for i: uint in to_vec ( v) { if i != 1 u { ret false ; } }
151
247
ret true;
152
248
}
153
249
154
250
155
- /* true if all bits are non-1 */
251
+ /*
252
+ Function: is_false
253
+
254
+ Returns true if all bits are 0
255
+ */
156
256
fn is_false ( v : t ) -> bool {
157
257
for i: uint in to_vec ( v) { if i == 1 u { ret false ; } }
158
258
ret true;
159
259
}
160
260
161
261
fn init_to_vec ( v : t , i : uint ) -> uint { ret if get ( v, i) { 1 u } else { 0 u } ; }
162
262
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
+ */
163
269
fn to_vec ( v : t ) -> [ uint ] {
164
270
let sub = bind init_to_vec ( v, _) ;
165
271
ret vec:: init_fn :: < uint > ( sub, v. nbits ) ;
166
272
}
167
273
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
+ */
168
280
fn to_str ( v : t ) -> str {
169
281
let rs = "" ;
170
282
for i: uint in to_vec ( v) { if i == 1 u { rs += "1" ; } else { rs += "0" ; } }
171
283
ret rs;
172
284
}
173
285
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
+ */
174
296
fn eq_vec ( v0 : t , v1 : [ uint ] ) -> bool {
175
297
assert ( v0. nbits == vec:: len :: < uint > ( v1) ) ;
176
298
let len = v0. nbits ;
0 commit comments