1
- /*
2
- Module: bitv
3
-
4
- Bitvectors.
5
- */
6
-
7
1
export t;
8
2
export create;
9
3
export union;
@@ -28,25 +22,19 @@ export eq_vec;
28
22
// an optimizing version of this module that produces a different obj
29
23
// for the case where nbits <= 32.
30
24
31
- /*
32
- Type: t
33
-
34
- The bitvector type.
35
- */
25
+ #[ doc = "The bitvector type" ]
36
26
type t = @{ storage : [ mutable uint] , nbits : uint } ;
37
27
38
-
39
28
const uint_bits: uint = 32 u + ( 1 u << 32 u >> 27 u) ;
40
29
41
- /*
42
- Function: create
30
+ # [ doc = "
31
+ Constructs a bitvector
43
32
44
- Constructs a bitvector.
33
+ # Arguments
45
34
46
- Parameters:
47
- nbits - The number of bits in the bitvector
48
- init - If true then the bits are initialized to 1, otherwise 0
49
- */
35
+ * nbits - The number of bits in the bitvector
36
+ * init - If true then the bits are initialized to 1, otherwise 0
37
+ " ]
50
38
fn create ( nbits : uint , init : bool ) -> t {
51
39
let elt = if init { !0 u } else { 0 u } ;
52
40
let storage = vec:: to_mut ( vec:: init_elt ( nbits / uint_bits + 1 u, elt) ) ;
@@ -74,60 +62,35 @@ fn union(v0: t, v1: t) -> bool { let sub = lor; ret process(v0, v1, sub); }
74
62
75
63
fn land ( w0 : uint , w1 : uint ) -> uint { ret w0 & w1; }
76
64
77
- /*
78
- Function: intersect
79
-
65
+ #[ doc = "
80
66
Calculates the intersection of two bitvectors
81
67
82
- Sets `v0` to the intersection of `v0` and `v1`
83
-
84
- Preconditions:
85
-
86
- Both bitvectors must be the same length
87
-
88
- Returns:
89
-
90
- True if `v0` was changed
91
- */
68
+ Sets `v0` to the intersection of `v0` and `v1`. Both bitvectors must be the
69
+ same length. Returns 'true' if `v0` was changed.
70
+ " ]
92
71
fn intersect ( v0 : t , v1 : t ) -> bool {
93
72
let sub = land;
94
73
ret process( v0, v1, sub) ;
95
74
}
96
75
97
76
fn right ( _w0 : uint , w1 : uint ) -> uint { ret w1; }
98
77
99
- /*
100
- Function: assign
101
-
78
+ #[ doc = "
102
79
Assigns the value of `v1` to `v0`
103
80
104
- Preconditions:
105
-
106
- Both bitvectors must be the same length
107
-
108
- Returns:
109
-
110
- True if `v0` was changed
111
- */
81
+ Both bitvectors must be the same length. Returns `true` if `v0` was changed
82
+ " ]
112
83
fn assign ( v0 : t , v1 : t ) -> bool { let sub = right; ret process( v0, v1, sub) ; }
113
84
114
- /*
115
- Function: clone
116
-
117
- Makes a copy of a bitvector
118
- */
85
+ #[ doc = "Makes a copy of a bitvector" ]
119
86
fn clone ( v : t ) -> t {
120
87
let storage = vec:: to_mut ( vec:: init_elt ( v. nbits / uint_bits + 1 u, 0 u) ) ;
121
88
let len = vec:: len ( v. storage ) ;
122
89
uint:: range ( 0 u, len) { |i| storage[ i] = v. storage [ i] ; } ;
123
90
ret @{ storage : storage, nbits : v. nbits } ;
124
91
}
125
92
126
- /*
127
- Function: get
128
-
129
- Retreive the value at index `i`
130
- */
93
+ #[ doc = "Retreive the value at index `i`" ]
131
94
pure fn get ( v : t , i : uint ) -> bool {
132
95
assert ( i < v. nbits ) ;
133
96
let bits = uint_bits;
@@ -139,19 +102,12 @@ pure fn get(v: t, i: uint) -> bool {
139
102
140
103
// FIXME: This doesn't account for the actual size of the vectors,
141
104
// so it could end up comparing garbage bits
142
- /*
143
- Function: equal
144
-
105
+ #[ doc = "
145
106
Compares two bitvectors
146
107
147
- Preconditions:
148
-
149
- Both bitvectors must be the same length
150
-
151
- Returns:
152
-
153
- True if both bitvectors contain identical elements
154
- */
108
+ Both bitvectors must be the same length. Returns `true` if both bitvectors
109
+ contain identical elements.
110
+ " ]
155
111
fn equal ( v0 : t , v1 : t ) -> bool {
156
112
// FIXME: when we can break or return from inside an iterator loop,
157
113
// we can eliminate this painful while-loop
@@ -165,67 +121,43 @@ fn equal(v0: t, v1: t) -> bool {
165
121
ret true;
166
122
}
167
123
168
- /*
169
- Function: clear
170
-
171
- Set all bits to 0
172
- */
124
+ #[ doc = "Set all bits to 0" ]
173
125
fn clear ( v : t ) {
174
126
uint:: range ( 0 u, vec:: len ( v. storage ) ) { |i| v. storage [ i] = 0 u; } ;
175
127
}
176
128
177
- /*
178
- Function: set_all
179
-
180
- Set all bits to 1
181
- */
129
+ #[ doc = "Set all bits to 1" ]
182
130
fn set_all ( v : t ) {
183
131
uint:: range ( 0 u, v. nbits ) { |i| set ( v, i, true ) ; } ;
184
132
}
185
133
186
- /*
187
- Function: invert
188
-
189
- Invert all bits
190
- */
134
+ #[ doc = "Invert all bits" ]
191
135
fn invert ( v : t ) {
192
136
uint:: range ( 0 u, vec:: len ( v. storage ) ) { |i|
193
137
v. storage [ i] = !v. storage [ i] ;
194
138
} ;
195
139
}
196
140
197
- /*
198
- Function: difference
199
-
141
+ #[ doc = "
200
142
Calculate the difference between two bitvectors
201
143
202
144
Sets each element of `v0` to the value of that element minus the element
203
- of `v1` at the same index.
204
-
205
- Preconditions:
206
-
207
- Both bitvectors must be the same length
145
+ of `v1` at the same index. Both bitvectors must be the same length.
208
146
209
- Returns:
210
-
211
- True if `v0` was changed
212
- */
147
+ Returns `true` if `v0` was changed.
148
+ " ]
213
149
fn difference ( v0 : t , v1 : t ) -> bool {
214
150
invert ( v1) ;
215
151
let b = intersect ( v0, v1) ;
216
152
invert ( v1) ;
217
153
ret b;
218
154
}
219
155
220
- /*
221
- Function: set
222
-
156
+ #[ doc = "
223
157
Set the value of a bit at a given index
224
158
225
- Preconditions:
226
-
227
- `i` must be less than the length of the bitvector
228
- */
159
+ `i` must be less than the length of the bitvector.
160
+ " ]
229
161
fn set ( v : t , i : uint , x : bool ) {
230
162
assert ( i < v. nbits ) ;
231
163
let bits = uint_bits;
@@ -236,62 +168,50 @@ fn set(v: t, i: uint, x: bool) {
236
168
}
237
169
238
170
239
- /*
240
- Function: is_true
241
-
242
- Returns true if all bits are 1
243
- */
171
+ #[ doc = "Returns true if all bits are 1" ]
244
172
fn is_true ( v : t ) -> bool {
245
173
for i: uint in to_vec ( v) { if i != 1 u { ret false ; } }
246
174
ret true;
247
175
}
248
176
249
177
250
- /*
251
- Function: is_false
252
-
253
- Returns true if all bits are 0
254
- */
178
+ #[ doc = "Returns true if all bits are 0" ]
255
179
fn is_false ( v : t ) -> bool {
256
180
for i: uint in to_vec ( v) { if i == 1 u { ret false ; } }
257
181
ret true;
258
182
}
259
183
260
184
fn init_to_vec ( v : t , i : uint ) -> uint { ret if get ( v, i) { 1 u } else { 0 u } ; }
261
185
262
- /*
263
- Function: to_vec
186
+ # [ doc = "
187
+ Converts the bitvector to a vector of uint with the same length.
264
188
265
- Converts the bitvector to a vector of uint with the same length. Each uint
266
- in the resulting vector has either value 0u or 1u.
267
- */
189
+ Each uint in the resulting vector has either value 0u or 1u.
190
+ " ]
268
191
fn to_vec ( v : t ) -> [ uint ] {
269
192
let sub = bind init_to_vec ( v, _) ;
270
193
ret vec:: init_fn :: < uint > ( v. nbits , sub) ;
271
194
}
272
195
273
- /*
274
- Function: to_str
275
196
276
- Converts the bitvector to a string. The resulting string has the same
277
- length as the bitvector, and each character is either '0' or '1'.
278
- */
197
+ #[ doc = "
198
+ Converts the bitvector to a string.
199
+
200
+ The resulting string has the same length as the bitvector, and each character
201
+ is either '0' or '1'.
202
+ " ]
279
203
fn to_str ( v : t ) -> str {
280
204
let rs = "" ;
281
205
for i: uint in to_vec ( v) { if i == 1 u { rs += "1" ; } else { rs += "0" ; } }
282
206
ret rs;
283
207
}
284
208
285
- /*
286
- Function: eq_vec
287
-
288
- Compare a bitvector to a vector of uint. The uint vector is expected to
289
- only contain the values 0u and 1u.
290
-
291
- Preconditions:
209
+ #[ doc = "
210
+ Compare a bitvector to a vector of uint
292
211
293
- Both the bitvector and vector must have the same length
294
- */
212
+ The uint vector is expected to only contain the values 0u and 1u. Both the
213
+ bitvector and vector must have the same length
214
+ " ]
295
215
fn eq_vec ( v0 : t , v1 : [ uint ] ) -> bool {
296
216
assert ( v0. nbits == vec:: len :: < uint > ( v1) ) ;
297
217
let len = v0. nbits ;
0 commit comments