@@ -235,20 +235,11 @@ impl Bitv {
235
235
/// }
236
236
/// ```
237
237
pub fn with_capacity ( nbits : uint , init : bool ) -> Bitv {
238
- let mut bitv = Bitv {
238
+ Bitv {
239
239
storage : Vec :: from_elem ( ( nbits + uint:: BITS - 1 ) / uint:: BITS ,
240
240
if init { !0 u } else { 0 u } ) ,
241
241
nbits : nbits
242
- } ;
243
-
244
- // Zero out any unused bits in the highest word if necessary
245
- let used_bits = bitv. nbits % uint:: BITS ;
246
- if init && used_bits != 0 {
247
- let largest_used_word = ( bitv. nbits + uint:: BITS - 1 ) / uint:: BITS - 1 ;
248
- * bitv. storage . get_mut ( largest_used_word) &= ( 1 << used_bits) - 1 ;
249
242
}
250
-
251
- bitv
252
243
}
253
244
254
245
/// Retrieves the value at index `i`.
@@ -638,9 +629,9 @@ impl Bitv {
638
629
/// ```
639
630
pub fn reserve ( & mut self , size : uint ) {
640
631
let old_size = self . storage . len ( ) ;
641
- let new_size = ( size + uint:: BITS - 1 ) / uint:: BITS ;
642
- if old_size < new_size {
643
- self . storage . grow ( new_size - old_size, 0 ) ;
632
+ let size = ( size + uint:: BITS - 1 ) / uint:: BITS ;
633
+ if old_size < size {
634
+ self . storage . grow ( size - old_size, 0 ) ;
644
635
}
645
636
}
646
637
@@ -695,15 +686,8 @@ impl Bitv {
695
686
}
696
687
// Allocate new words, if needed
697
688
if new_nwords > self . storage . len ( ) {
698
- let to_add = new_nwords - self . storage . len ( ) ;
699
- self . storage . grow ( to_add, full_value) ;
700
-
701
- // Zero out and unused bits in the new tail word
702
- if value {
703
- let tail_word = new_nwords - 1 ;
704
- let used_bits = new_nbits % uint:: BITS ;
705
- * self . storage . get_mut ( tail_word) &= ( 1 << used_bits) - 1 ;
706
- }
689
+ let to_add = new_nwords - self . storage . len ( ) ;
690
+ self . storage . grow ( to_add, full_value) ;
707
691
}
708
692
// Adjust internal bit count
709
693
self . nbits = new_nbits;
@@ -986,8 +970,9 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
986
970
/// }
987
971
///
988
972
/// // Can convert back to a `Bitv`
989
- /// let bv: Bitv = s.into_bitv();
990
- /// assert!(bv.get(3));
973
+ /// let bv: Bitv = s.unwrap();
974
+ /// assert!(bv.eq_vec([true, true, false, true,
975
+ /// false, false, false, false]));
991
976
/// ```
992
977
#[ deriving( Clone ) ]
993
978
pub struct BitvSet ( Bitv ) ;
@@ -1008,8 +993,7 @@ impl FromIterator<bool> for BitvSet {
1008
993
impl Extendable < bool > for BitvSet {
1009
994
#[ inline]
1010
995
fn extend < I : Iterator < bool > > ( & mut self , iterator : I ) {
1011
- let & BitvSet ( ref mut self_bitv) = self ;
1012
- self_bitv. extend ( iterator) ;
996
+ self . get_mut_ref ( ) . extend ( iterator) ;
1013
997
}
1014
998
}
1015
999
@@ -1065,8 +1049,7 @@ impl BitvSet {
1065
1049
/// ```
1066
1050
#[ inline]
1067
1051
pub fn with_capacity ( nbits : uint ) -> BitvSet {
1068
- let bitv = Bitv :: with_capacity ( nbits, false ) ;
1069
- BitvSet :: from_bitv ( bitv)
1052
+ BitvSet ( Bitv :: with_capacity ( nbits, false ) )
1070
1053
}
1071
1054
1072
1055
/// Creates a new bit vector set from the given bit vector.
@@ -1085,9 +1068,7 @@ impl BitvSet {
1085
1068
/// }
1086
1069
/// ```
1087
1070
#[ inline]
1088
- pub fn from_bitv ( mut bitv : Bitv ) -> BitvSet {
1089
- // Mark every bit as valid
1090
- bitv. nbits = bitv. capacity ( ) ;
1071
+ pub fn from_bitv ( bitv : Bitv ) -> BitvSet {
1091
1072
BitvSet ( bitv)
1092
1073
}
1093
1074
@@ -1121,10 +1102,7 @@ impl BitvSet {
1121
1102
/// ```
1122
1103
pub fn reserve ( & mut self , size : uint ) {
1123
1104
let & BitvSet ( ref mut bitv) = self ;
1124
- bitv. reserve ( size) ;
1125
- if bitv. nbits < size {
1126
- bitv. nbits = bitv. capacity ( ) ;
1127
- }
1105
+ bitv. reserve ( size)
1128
1106
}
1129
1107
1130
1108
/// Consumes this set to return the underlying bit vector.
@@ -1138,12 +1116,11 @@ impl BitvSet {
1138
1116
/// s.insert(0);
1139
1117
/// s.insert(3);
1140
1118
///
1141
- /// let bv = s.into_bitv();
1142
- /// assert!(bv.get(0));
1143
- /// assert!(bv.get(3));
1119
+ /// let bv = s.unwrap();
1120
+ /// assert!(bv.eq_vec([true, false, false, true]));
1144
1121
/// ```
1145
1122
#[ inline]
1146
- pub fn into_bitv ( self ) -> Bitv {
1123
+ pub fn unwrap ( self ) -> Bitv {
1147
1124
let BitvSet ( bitv) = self ;
1148
1125
bitv
1149
1126
}
@@ -1167,15 +1144,38 @@ impl BitvSet {
1167
1144
bitv
1168
1145
}
1169
1146
1147
+ /// Returns a mutable reference to the underlying bit vector.
1148
+ ///
1149
+ /// # Example
1150
+ ///
1151
+ /// ```
1152
+ /// use std::collections::BitvSet;
1153
+ ///
1154
+ /// let mut s = BitvSet::new();
1155
+ /// s.insert(0);
1156
+ /// assert_eq!(s.contains(&0), true);
1157
+ /// {
1158
+ /// // Will free the set during bv's lifetime
1159
+ /// let bv = s.get_mut_ref();
1160
+ /// bv.set(0, false);
1161
+ /// }
1162
+ /// assert_eq!(s.contains(&0), false);
1163
+ /// ```
1170
1164
#[ inline]
1171
- fn other_op ( & mut self , other : & BitvSet , f : |uint , uint| -> uint) {
1172
- // Expand the vector if necessary
1173
- self . reserve ( other. capacity ( ) ) ;
1165
+ pub fn get_mut_ref < ' a > ( & ' a mut self ) -> & ' a mut Bitv {
1166
+ let & BitvSet ( ref mut bitv) = self ;
1167
+ bitv
1168
+ }
1174
1169
1170
+ #[ inline]
1171
+ fn other_op ( & mut self , other : & BitvSet , f : |uint , uint| -> uint) {
1175
1172
// Unwrap Bitvs
1176
1173
let & BitvSet ( ref mut self_bitv) = self ;
1177
1174
let & BitvSet ( ref other_bitv) = other;
1178
1175
1176
+ // Expand the vector if necessary
1177
+ self_bitv. reserve ( other_bitv. capacity ( ) ) ;
1178
+
1179
1179
// virtually pad other with 0's for equal lengths
1180
1180
let mut other_words = {
1181
1181
let ( _, result) = match_words ( self_bitv, other_bitv) ;
@@ -1376,10 +1376,9 @@ impl BitvSet {
1376
1376
///
1377
1377
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([a]));
1378
1378
/// let b = BitvSet::from_bitv(bitv::from_bytes([b]));
1379
- /// let res = BitvSet::from_bitv(bitv::from_bytes([res]));
1380
1379
///
1381
1380
/// a.union_with(&b);
1382
- /// assert_eq!(a, res);
1381
+ /// assert_eq!(a.unwrap(), bitv::from_bytes([ res]) );
1383
1382
/// ```
1384
1383
#[ inline]
1385
1384
pub fn union_with ( & mut self , other : & BitvSet ) {
@@ -1400,10 +1399,9 @@ impl BitvSet {
1400
1399
///
1401
1400
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([a]));
1402
1401
/// let b = BitvSet::from_bitv(bitv::from_bytes([b]));
1403
- /// let res = BitvSet::from_bitv(bitv::from_bytes([res]));
1404
1402
///
1405
1403
/// a.intersect_with(&b);
1406
- /// assert_eq!(a, res);
1404
+ /// assert_eq!(a.unwrap(), bitv::from_bytes([ res]) );
1407
1405
/// ```
1408
1406
#[ inline]
1409
1407
pub fn intersect_with ( & mut self , other : & BitvSet ) {
@@ -1426,17 +1424,15 @@ impl BitvSet {
1426
1424
///
1427
1425
/// let mut bva = BitvSet::from_bitv(bitv::from_bytes([a]));
1428
1426
/// let bvb = BitvSet::from_bitv(bitv::from_bytes([b]));
1429
- /// let bva_b = BitvSet::from_bitv(bitv::from_bytes([a_b]));
1430
- /// let bvb_a = BitvSet::from_bitv(bitv::from_bytes([b_a]));
1431
1427
///
1432
1428
/// bva.difference_with(&bvb);
1433
- /// assert_eq!(bva, bva_b );
1429
+ /// assert_eq!(bva.unwrap(), bitv::from_bytes([a_b]) );
1434
1430
///
1435
1431
/// let bva = BitvSet::from_bitv(bitv::from_bytes([a]));
1436
1432
/// let mut bvb = BitvSet::from_bitv(bitv::from_bytes([b]));
1437
1433
///
1438
1434
/// bvb.difference_with(&bva);
1439
- /// assert_eq!(bvb, bvb_a );
1435
+ /// assert_eq!(bvb.unwrap(), bitv::from_bytes([b_a]) );
1440
1436
/// ```
1441
1437
#[ inline]
1442
1438
pub fn difference_with ( & mut self , other : & BitvSet ) {
@@ -1458,10 +1454,9 @@ impl BitvSet {
1458
1454
///
1459
1455
/// let mut a = BitvSet::from_bitv(bitv::from_bytes([a]));
1460
1456
/// let b = BitvSet::from_bitv(bitv::from_bytes([b]));
1461
- /// let res = BitvSet::from_bitv(bitv::from_bytes([res]));
1462
1457
///
1463
1458
/// a.symmetric_difference_with(&b);
1464
- /// assert_eq!(a, res);
1459
+ /// assert_eq!(a.unwrap(), bitv::from_bytes([ res]) );
1465
1460
/// ```
1466
1461
#[ inline]
1467
1462
pub fn symmetric_difference_with ( & mut self , other : & BitvSet ) {
@@ -1543,14 +1538,20 @@ impl MutableSet<uint> for BitvSet {
1543
1538
if self . contains ( & value) {
1544
1539
return false ;
1545
1540
}
1546
-
1547
- // Ensure we have enough space to hold the new element
1548
1541
if value >= self . capacity ( ) {
1549
1542
let new_cap = cmp:: max ( value + 1 , self . capacity ( ) * 2 ) ;
1550
1543
self . reserve ( new_cap) ;
1551
1544
}
1552
-
1553
1545
let & BitvSet ( ref mut bitv) = self ;
1546
+ if value >= bitv. nbits {
1547
+ // If we are increasing nbits, make sure we mask out any previously-unconsidered bits
1548
+ let old_rem = bitv. nbits % uint:: BITS ;
1549
+ if old_rem != 0 {
1550
+ let old_last_word = ( bitv. nbits + uint:: BITS - 1 ) / uint:: BITS - 1 ;
1551
+ * bitv. storage . get_mut ( old_last_word) &= ( 1 << old_rem) - 1 ;
1552
+ }
1553
+ bitv. nbits = value + 1 ;
1554
+ }
1554
1555
bitv. set ( value, true ) ;
1555
1556
return true ;
1556
1557
}
@@ -2224,15 +2225,14 @@ mod tests {
2224
2225
assert ! ( a. insert( 160 ) ) ;
2225
2226
assert ! ( a. insert( 19 ) ) ;
2226
2227
assert ! ( a. insert( 24 ) ) ;
2227
- assert ! ( a. insert( 200 ) ) ;
2228
2228
2229
2229
assert ! ( b. insert( 1 ) ) ;
2230
2230
assert ! ( b. insert( 5 ) ) ;
2231
2231
assert ! ( b. insert( 9 ) ) ;
2232
2232
assert ! ( b. insert( 13 ) ) ;
2233
2233
assert ! ( b. insert( 19 ) ) ;
2234
2234
2235
- let expected = [ 1 , 3 , 5 , 9 , 11 , 13 , 19 , 24 , 160 , 200 ] ;
2235
+ let expected = [ 1 , 3 , 5 , 9 , 11 , 13 , 19 , 24 , 160 ] ;
2236
2236
let actual = a. union ( & b) . collect :: < Vec < uint > > ( ) ;
2237
2237
assert_eq ! ( actual. as_slice( ) , expected. as_slice( ) ) ;
2238
2238
}
@@ -2281,27 +2281,6 @@ mod tests {
2281
2281
assert ! ( c. is_disjoint( & b) )
2282
2282
}
2283
2283
2284
- #[ test]
2285
- fn test_bitv_set_union_with ( ) {
2286
- //a should grow to include larger elements
2287
- let mut a = BitvSet :: new ( ) ;
2288
- a. insert ( 0 ) ;
2289
- let mut b = BitvSet :: new ( ) ;
2290
- b. insert ( 5 ) ;
2291
- let expected = BitvSet :: from_bitv ( from_bytes ( [ 0b10000100 ] ) ) ;
2292
- a. union_with ( & b) ;
2293
- assert_eq ! ( a, expected) ;
2294
-
2295
- // Standard
2296
- let mut a = BitvSet :: from_bitv ( from_bytes ( [ 0b10100010 ] ) ) ;
2297
- let mut b = BitvSet :: from_bitv ( from_bytes ( [ 0b01100010 ] ) ) ;
2298
- let c = a. clone ( ) ;
2299
- a. union_with ( & b) ;
2300
- b. union_with ( & c) ;
2301
- assert_eq ! ( a. len( ) , 4 ) ;
2302
- assert_eq ! ( b. len( ) , 4 ) ;
2303
- }
2304
-
2305
2284
#[ test]
2306
2285
fn test_bitv_set_intersect_with ( ) {
2307
2286
// Explicitly 0'ed bits
@@ -2332,59 +2311,6 @@ mod tests {
2332
2311
assert_eq ! ( b. len( ) , 2 ) ;
2333
2312
}
2334
2313
2335
- #[ test]
2336
- fn test_bitv_set_difference_with ( ) {
2337
- // Explicitly 0'ed bits
2338
- let mut a = BitvSet :: from_bitv ( from_bytes ( [ 0b00000000 ] ) ) ;
2339
- let b = BitvSet :: from_bitv ( from_bytes ( [ 0b10100010 ] ) ) ;
2340
- a. difference_with ( & b) ;
2341
- assert ! ( a. is_empty( ) ) ;
2342
-
2343
- // Uninitialized bits should behave like 0's
2344
- let mut a = BitvSet :: new ( ) ;
2345
- let b = BitvSet :: from_bitv ( from_bytes ( [ 0b11111111 ] ) ) ;
2346
- a. difference_with ( & b) ;
2347
- assert ! ( a. is_empty( ) ) ;
2348
-
2349
- // Standard
2350
- let mut a = BitvSet :: from_bitv ( from_bytes ( [ 0b10100010 ] ) ) ;
2351
- let mut b = BitvSet :: from_bitv ( from_bytes ( [ 0b01100010 ] ) ) ;
2352
- let c = a. clone ( ) ;
2353
- a. difference_with ( & b) ;
2354
- b. difference_with ( & c) ;
2355
- assert_eq ! ( a. len( ) , 1 ) ;
2356
- assert_eq ! ( b. len( ) , 1 ) ;
2357
- }
2358
-
2359
- #[ test]
2360
- fn test_bitv_set_symmetric_difference_with ( ) {
2361
- //a should grow to include larger elements
2362
- let mut a = BitvSet :: new ( ) ;
2363
- a. insert ( 0 ) ;
2364
- a. insert ( 1 ) ;
2365
- let mut b = BitvSet :: new ( ) ;
2366
- b. insert ( 1 ) ;
2367
- b. insert ( 5 ) ;
2368
- let expected = BitvSet :: from_bitv ( from_bytes ( [ 0b10000100 ] ) ) ;
2369
- a. symmetric_difference_with ( & b) ;
2370
- assert_eq ! ( a, expected) ;
2371
-
2372
- let mut a = BitvSet :: from_bitv ( from_bytes ( [ 0b10100010 ] ) ) ;
2373
- let b = BitvSet :: new ( ) ;
2374
- let c = a. clone ( ) ;
2375
- a. symmetric_difference_with ( & b) ;
2376
- assert_eq ! ( a, c) ;
2377
-
2378
- // Standard
2379
- let mut a = BitvSet :: from_bitv ( from_bytes ( [ 0b11100010 ] ) ) ;
2380
- let mut b = BitvSet :: from_bitv ( from_bytes ( [ 0b01101010 ] ) ) ;
2381
- let c = a. clone ( ) ;
2382
- a. symmetric_difference_with ( & b) ;
2383
- b. symmetric_difference_with ( & c) ;
2384
- assert_eq ! ( a. len( ) , 2 ) ;
2385
- assert_eq ! ( b. len( ) , 2 ) ;
2386
- }
2387
-
2388
2314
#[ test]
2389
2315
fn test_bitv_set_eq ( ) {
2390
2316
let a = BitvSet :: from_bitv ( from_bytes ( [ 0b10100010 ] ) ) ;
0 commit comments