@@ -95,6 +95,7 @@ fn clone(v: bitv) -> bitv {
95
95
}
96
96
97
97
#[ doc = "Retreive the value at index `i`" ]
98
+ #[ inline( always) ]
98
99
pure fn get ( v : bitv , i : uint ) -> bool {
99
100
assert ( i < v. nbits ) ;
100
101
let bits = uint_bits;
@@ -104,37 +105,31 @@ pure fn get(v: bitv, i: uint) -> bool {
104
105
ret x == 1 u;
105
106
}
106
107
107
- // FIXME: This doesn't account for the actual size of the vectors,
108
- // so it could end up comparing garbage bits (#2342)
109
108
#[ doc = "
110
109
Compares two bitvectors
111
110
112
111
Both bitvectors must be the same length. Returns `true` if both bitvectors
113
112
contain identical elements.
114
113
" ]
115
114
fn equal ( v0 : bitv , v1 : bitv ) -> bool {
115
+ if v0. nbits != v1. nbits { ret false ; }
116
116
let len = vec:: len ( v1. storage ) ;
117
117
for uint:: iterate( 0 u, len) { |i|
118
118
if v0. storage [ i] != v1. storage [ i] { ret false ; }
119
119
}
120
120
}
121
121
122
122
#[ doc = "Set all bits to 0" ]
123
- fn clear ( v : bitv ) {
124
- for uint:: range( 0 u, vec:: len( v. storage) ) { |i| v. storage [ i] = 0 u; } ;
125
- }
123
+ #[ inline( always) ]
124
+ fn clear ( v : bitv ) { for each_storage( v) { |w| w = 0 u } }
126
125
127
126
#[ doc = "Set all bits to 1" ]
128
- fn set_all ( v : bitv ) {
129
- for uint:: range( 0 u, v. nbits) { |i| set( v, i, true ) ; } ;
130
- }
127
+ #[ inline( always) ]
128
+ fn set_all ( v : bitv ) { for each_storage( v) { |w| w = !0 u } }
131
129
132
130
#[ doc = "Invert all bits" ]
133
- fn invert ( v : bitv ) {
134
- for uint:: range( 0 u, vec:: len( v. storage) ) { |i|
135
- v. storage [ i] = !v. storage [ i] ;
136
- } ;
137
- }
131
+ #[ inline( always) ]
132
+ fn invert ( v : bitv ) { for each_storage( v) { |w| w = !w } }
138
133
139
134
#[ doc = "
140
135
Calculate the difference between two bitvectors
@@ -156,6 +151,7 @@ Set the value of a bit at a given index
156
151
157
152
`i` must be less than the length of the bitvector.
158
153
" ]
154
+ #[ inline( always) ]
159
155
fn set ( v : bitv , i : uint , x : bool ) {
160
156
assert ( i < v. nbits ) ;
161
157
let bits = uint_bits;
@@ -193,6 +189,7 @@ fn to_vec(v: bitv) -> [uint] {
193
189
ret vec:: from_fn :: < uint > ( v. nbits , sub) ;
194
190
}
195
191
192
+ #[ inline( always) ]
196
193
fn each ( v : bitv , f : fn ( bool ) -> bool ) {
197
194
let mut i = 0 u;
198
195
while i < v. nbits {
@@ -201,6 +198,16 @@ fn each(v: bitv, f: fn(bool) -> bool) {
201
198
}
202
199
}
203
200
201
+ #[ inline( always) ]
202
+ fn each_storage( v : bitv , op : fn ( & uint ) -> bool ) {
203
+ for uint:: range( 0 u, vec:: len( v. storage) ) { |i|
204
+ let mut w = v. storage[ i] ;
205
+ let b = !op( w) ;
206
+ v. storage[ i] = w;
207
+ if !b { break; }
208
+ }
209
+ }
210
+
204
211
#[ doc = "
205
212
Converts the bitvector to a string.
206
213
@@ -524,6 +531,20 @@ mod tests {
524
531
0 u, 0 u, 0 u, 0 u, 1 u, 1 u, 1 u] ) ) ;
525
532
}
526
533
534
+ #[ test]
535
+ fn test_equal_differing_sizes ( ) {
536
+ let v0 = bitv ( 10 u, false ) ;
537
+ let v1 = bitv ( 11 u, false ) ;
538
+ assert ! equal( v0, v1) ;
539
+ }
540
+
541
+ #[ test]
542
+ fn test_equal_greatly_differing_sizes ( ) {
543
+ let v0 = bitv ( 10 u, false ) ;
544
+ let v1 = bitv ( 110 u, false ) ;
545
+ assert ! equal( v0, v1) ;
546
+ }
547
+
527
548
}
528
549
529
550
//
0 commit comments