@@ -189,17 +189,17 @@ fn blocks_for_bits(bits: usize) -> usize {
189
189
//
190
190
// Note that we can technically avoid this branch with the expression
191
191
// `(nbits + u32::BITS - 1) / 32::BITS`, but if nbits is almost usize::MAX this will overflow.
192
- if bits % u32:: BITS == 0 {
193
- bits / u32:: BITS
192
+ if bits % u32:: BITS as usize == 0 {
193
+ bits / u32:: BITS as usize
194
194
} else {
195
- bits / u32:: BITS + 1
195
+ bits / u32:: BITS as usize + 1
196
196
}
197
197
}
198
198
199
199
/// Computes the bitmask for the final word of the vector
200
200
fn mask_for_bits ( bits : usize ) -> u32 {
201
201
// Note especially that a perfect multiple of u32::BITS should mask all 1s.
202
- !0u32 >> ( u32:: BITS - bits % u32:: BITS ) % u32:: BITS
202
+ !0u32 >> ( u32:: BITS as usize - bits % u32:: BITS as usize ) % u32:: BITS as usize
203
203
}
204
204
205
205
impl BitVec {
@@ -237,7 +237,7 @@ impl BitVec {
237
237
/// An operation might screw up the unused bits in the last block of the
238
238
/// `BitVec`. As per (3), it's assumed to be all 0s. This method fixes it up.
239
239
fn fix_last_block ( & mut self ) {
240
- let extra_bits = self . len ( ) % u32:: BITS ;
240
+ let extra_bits = self . len ( ) % u32:: BITS as usize ;
241
241
if extra_bits > 0 {
242
242
let mask = ( 1 << extra_bits) - 1 ;
243
243
let storage_len = self . storage . len ( ) ;
@@ -313,7 +313,7 @@ impl BitVec {
313
313
/// false, false, true, false]));
314
314
/// ```
315
315
pub fn from_bytes ( bytes : & [ u8 ] ) -> BitVec {
316
- let len = bytes. len ( ) . checked_mul ( u8:: BITS ) . expect ( "capacity overflow" ) ;
316
+ let len = bytes. len ( ) . checked_mul ( u8:: BITS as usize ) . expect ( "capacity overflow" ) ;
317
317
let mut bit_vec = BitVec :: with_capacity ( len) ;
318
318
let complete_words = bytes. len ( ) / 4 ;
319
319
let extra_bytes = bytes. len ( ) % 4 ;
@@ -380,8 +380,8 @@ impl BitVec {
380
380
if i >= self . nbits {
381
381
return None ;
382
382
}
383
- let w = i / u32:: BITS ;
384
- let b = i % u32:: BITS ;
383
+ let w = i / u32:: BITS as usize ;
384
+ let b = i % u32:: BITS as usize ;
385
385
self . storage . get ( w) . map ( |& block|
386
386
( block & ( 1 << b) ) != 0
387
387
)
@@ -407,8 +407,8 @@ impl BitVec {
407
407
reason = "panic semantics are likely to change in the future" ) ]
408
408
pub fn set ( & mut self , i : usize , x : bool ) {
409
409
assert ! ( i < self . nbits) ;
410
- let w = i / u32:: BITS ;
411
- let b = i % u32:: BITS ;
410
+ let w = i / u32:: BITS as usize ;
411
+ let b = i % u32:: BITS as usize ;
412
412
let flag = 1 << b;
413
413
let val = if x { self . storage [ w] | flag }
414
414
else { self . storage [ w] & !flag } ;
@@ -789,7 +789,7 @@ impl BitVec {
789
789
#[ inline]
790
790
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
791
791
pub fn capacity ( & self ) -> usize {
792
- self . storage . capacity ( ) . checked_mul ( u32:: BITS ) . unwrap_or ( usize:: MAX )
792
+ self . storage . capacity ( ) . checked_mul ( u32:: BITS as usize ) . unwrap_or ( usize:: MAX )
793
793
}
794
794
795
795
/// Grows the `BitVec` in-place, adding `n` copies of `value` to the `BitVec`.
@@ -819,7 +819,7 @@ impl BitVec {
819
819
820
820
// Correct the old tail word, setting or clearing formerly unused bits
821
821
let old_last_word = blocks_for_bits ( self . nbits ) - 1 ;
822
- if self . nbits % u32:: BITS > 0 {
822
+ if self . nbits % u32:: BITS as usize > 0 {
823
823
let mask = mask_for_bits ( self . nbits ) ;
824
824
if value {
825
825
self . storage [ old_last_word] |= !mask;
@@ -868,7 +868,7 @@ impl BitVec {
868
868
// (3)
869
869
self . set ( i, false ) ;
870
870
self . nbits = i;
871
- if self . nbits % u32:: BITS == 0 {
871
+ if self . nbits % u32:: BITS as usize == 0 {
872
872
// (2)
873
873
self . storage . pop ( ) ;
874
874
}
@@ -890,7 +890,7 @@ impl BitVec {
890
890
/// ```
891
891
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
892
892
pub fn push ( & mut self , elem : bool ) {
893
- if self . nbits % u32:: BITS == 0 {
893
+ if self . nbits % u32:: BITS as usize == 0 {
894
894
self . storage . push ( 0 ) ;
895
895
}
896
896
let insert_pos = self . nbits ;
@@ -1406,7 +1406,7 @@ impl BitSet {
1406
1406
// Truncate
1407
1407
let trunc_len = cmp:: max ( old_len - n, 1 ) ;
1408
1408
bit_vec. storage . truncate ( trunc_len) ;
1409
- bit_vec. nbits = trunc_len * u32:: BITS ;
1409
+ bit_vec. nbits = trunc_len * u32:: BITS as usize ;
1410
1410
}
1411
1411
1412
1412
/// Iterator over each u32 stored in the `BitSet`.
@@ -1663,7 +1663,7 @@ impl BitSet {
1663
1663
#[ inline]
1664
1664
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1665
1665
pub fn len ( & self ) -> usize {
1666
- self . bit_vec . blocks ( ) . fold ( 0 , |acc, n| acc + n. count_ones ( ) )
1666
+ self . bit_vec . blocks ( ) . fold ( 0 , |acc, n| acc + n. count_ones ( ) as usize )
1667
1667
}
1668
1668
1669
1669
/// Returns whether there are no bits set in this set
@@ -1831,13 +1831,13 @@ impl<'a> Iterator for TwoBitPositions<'a> {
1831
1831
fn next ( & mut self ) -> Option < usize > {
1832
1832
while self . next_idx < self . set . bit_vec . len ( ) ||
1833
1833
self . next_idx < self . other . bit_vec . len ( ) {
1834
- let bit_idx = self . next_idx % u32:: BITS ;
1834
+ let bit_idx = self . next_idx % u32:: BITS as usize ;
1835
1835
if bit_idx == 0 {
1836
1836
let s_bit_vec = & self . set . bit_vec ;
1837
1837
let o_bit_vec = & self . other . bit_vec ;
1838
1838
// Merging the two words is a bit of an awkward dance since
1839
1839
// one BitVec might be longer than the other
1840
- let word_idx = self . next_idx / u32:: BITS ;
1840
+ let word_idx = self . next_idx / u32:: BITS as usize ;
1841
1841
let w1 = if word_idx < s_bit_vec. storage . len ( ) {
1842
1842
s_bit_vec. storage [ word_idx]
1843
1843
} else { 0 } ;
@@ -2441,70 +2441,70 @@ mod tests {
2441
2441
2442
2442
#[ test]
2443
2443
fn test_bit_vec_push_pop ( ) {
2444
- let mut s = BitVec :: from_elem ( 5 * u32:: BITS - 2 , false ) ;
2445
- assert_eq ! ( s. len( ) , 5 * u32 :: BITS - 2 ) ;
2446
- assert_eq ! ( s[ 5 * u32 :: BITS - 3 ] , false ) ;
2444
+ let mut s = BitVec :: from_elem ( 5 * u32:: BITS as usize - 2 , false ) ;
2445
+ assert_eq ! ( s. len( ) , 5 * u32 :: BITS as usize - 2 ) ;
2446
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize - 3 ] , false ) ;
2447
2447
s. push ( true ) ;
2448
2448
s. push ( true ) ;
2449
- assert_eq ! ( s[ 5 * u32 :: BITS - 2 ] , true ) ;
2450
- assert_eq ! ( s[ 5 * u32 :: BITS - 1 ] , true ) ;
2449
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize - 2 ] , true ) ;
2450
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize - 1 ] , true ) ;
2451
2451
// Here the internal vector will need to be extended
2452
2452
s. push ( false ) ;
2453
- assert_eq ! ( s[ 5 * u32 :: BITS ] , false ) ;
2453
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize ] , false ) ;
2454
2454
s. push ( false ) ;
2455
- assert_eq ! ( s[ 5 * u32 :: BITS + 1 ] , false ) ;
2456
- assert_eq ! ( s. len( ) , 5 * u32 :: BITS + 2 ) ;
2455
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize + 1 ] , false ) ;
2456
+ assert_eq ! ( s. len( ) , 5 * u32 :: BITS as usize + 2 ) ;
2457
2457
// Pop it all off
2458
2458
assert_eq ! ( s. pop( ) , Some ( false ) ) ;
2459
2459
assert_eq ! ( s. pop( ) , Some ( false ) ) ;
2460
2460
assert_eq ! ( s. pop( ) , Some ( true ) ) ;
2461
2461
assert_eq ! ( s. pop( ) , Some ( true ) ) ;
2462
- assert_eq ! ( s. len( ) , 5 * u32 :: BITS - 2 ) ;
2462
+ assert_eq ! ( s. len( ) , 5 * u32 :: BITS as usize - 2 ) ;
2463
2463
}
2464
2464
2465
2465
#[ test]
2466
2466
fn test_bit_vec_truncate ( ) {
2467
- let mut s = BitVec :: from_elem ( 5 * u32:: BITS , true ) ;
2467
+ let mut s = BitVec :: from_elem ( 5 * u32:: BITS as usize , true ) ;
2468
2468
2469
- assert_eq ! ( s, BitVec :: from_elem( 5 * u32 :: BITS , true ) ) ;
2470
- assert_eq ! ( s. len( ) , 5 * u32 :: BITS ) ;
2471
- s. truncate ( 4 * u32:: BITS ) ;
2472
- assert_eq ! ( s, BitVec :: from_elem( 4 * u32 :: BITS , true ) ) ;
2473
- assert_eq ! ( s. len( ) , 4 * u32 :: BITS ) ;
2469
+ assert_eq ! ( s, BitVec :: from_elem( 5 * u32 :: BITS as usize , true ) ) ;
2470
+ assert_eq ! ( s. len( ) , 5 * u32 :: BITS as usize ) ;
2471
+ s. truncate ( 4 * u32:: BITS as usize ) ;
2472
+ assert_eq ! ( s, BitVec :: from_elem( 4 * u32 :: BITS as usize , true ) ) ;
2473
+ assert_eq ! ( s. len( ) , 4 * u32 :: BITS as usize ) ;
2474
2474
// Truncating to a size > s.len() should be a noop
2475
- s. truncate ( 5 * u32:: BITS ) ;
2476
- assert_eq ! ( s, BitVec :: from_elem( 4 * u32 :: BITS , true ) ) ;
2477
- assert_eq ! ( s. len( ) , 4 * u32 :: BITS ) ;
2478
- s. truncate ( 3 * u32:: BITS - 10 ) ;
2479
- assert_eq ! ( s, BitVec :: from_elem( 3 * u32 :: BITS - 10 , true ) ) ;
2480
- assert_eq ! ( s. len( ) , 3 * u32 :: BITS - 10 ) ;
2475
+ s. truncate ( 5 * u32:: BITS as usize ) ;
2476
+ assert_eq ! ( s, BitVec :: from_elem( 4 * u32 :: BITS as usize , true ) ) ;
2477
+ assert_eq ! ( s. len( ) , 4 * u32 :: BITS as usize ) ;
2478
+ s. truncate ( 3 * u32:: BITS as usize - 10 ) ;
2479
+ assert_eq ! ( s, BitVec :: from_elem( 3 * u32 :: BITS as usize - 10 , true ) ) ;
2480
+ assert_eq ! ( s. len( ) , 3 * u32 :: BITS as usize - 10 ) ;
2481
2481
s. truncate ( 0 ) ;
2482
2482
assert_eq ! ( s, BitVec :: from_elem( 0 , true ) ) ;
2483
2483
assert_eq ! ( s. len( ) , 0 ) ;
2484
2484
}
2485
2485
2486
2486
#[ test]
2487
2487
fn test_bit_vec_reserve ( ) {
2488
- let mut s = BitVec :: from_elem ( 5 * u32:: BITS , true ) ;
2488
+ let mut s = BitVec :: from_elem ( 5 * u32:: BITS as usize , true ) ;
2489
2489
// Check capacity
2490
- assert ! ( s. capacity( ) >= 5 * u32 :: BITS ) ;
2491
- s. reserve ( 2 * u32:: BITS ) ;
2492
- assert ! ( s. capacity( ) >= 7 * u32 :: BITS ) ;
2493
- s. reserve ( 7 * u32:: BITS ) ;
2494
- assert ! ( s. capacity( ) >= 12 * u32 :: BITS ) ;
2495
- s. reserve_exact ( 7 * u32:: BITS ) ;
2496
- assert ! ( s. capacity( ) >= 12 * u32 :: BITS ) ;
2497
- s. reserve ( 7 * u32:: BITS + 1 ) ;
2498
- assert ! ( s. capacity( ) >= 12 * u32 :: BITS + 1 ) ;
2490
+ assert ! ( s. capacity( ) >= 5 * u32 :: BITS as usize ) ;
2491
+ s. reserve ( 2 * u32:: BITS as usize ) ;
2492
+ assert ! ( s. capacity( ) >= 7 * u32 :: BITS as usize ) ;
2493
+ s. reserve ( 7 * u32:: BITS as usize ) ;
2494
+ assert ! ( s. capacity( ) >= 12 * u32 :: BITS as usize ) ;
2495
+ s. reserve_exact ( 7 * u32:: BITS as usize ) ;
2496
+ assert ! ( s. capacity( ) >= 12 * u32 :: BITS as usize ) ;
2497
+ s. reserve ( 7 * u32:: BITS as usize + 1 ) ;
2498
+ assert ! ( s. capacity( ) >= 12 * u32 :: BITS as usize + 1 ) ;
2499
2499
// Check that length hasn't changed
2500
- assert_eq ! ( s. len( ) , 5 * u32 :: BITS ) ;
2500
+ assert_eq ! ( s. len( ) , 5 * u32 :: BITS as usize ) ;
2501
2501
s. push ( true ) ;
2502
2502
s. push ( false ) ;
2503
2503
s. push ( true ) ;
2504
- assert_eq ! ( s[ 5 * u32 :: BITS - 1 ] , true ) ;
2505
- assert_eq ! ( s[ 5 * u32 :: BITS - 0 ] , true ) ;
2506
- assert_eq ! ( s[ 5 * u32 :: BITS + 1 ] , false ) ;
2507
- assert_eq ! ( s[ 5 * u32 :: BITS + 2 ] , true ) ;
2504
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize - 1 ] , true ) ;
2505
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize - 0 ] , true ) ;
2506
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize + 1 ] , false ) ;
2507
+ assert_eq ! ( s[ 5 * u32 :: BITS as usize + 2 ] , true ) ;
2508
2508
}
2509
2509
2510
2510
#[ test]
@@ -2557,7 +2557,7 @@ mod bit_vec_bench {
2557
2557
let mut bit_vec = 0 as usize ;
2558
2558
b. iter ( || {
2559
2559
for _ in 0 ..100 {
2560
- bit_vec |= 1 << ( ( r. next_u32 ( ) as usize ) % u32:: BITS ) ;
2560
+ bit_vec |= 1 << ( ( r. next_u32 ( ) as usize ) % u32:: BITS as usize ) ;
2561
2561
}
2562
2562
black_box ( & bit_vec) ;
2563
2563
} ) ;
@@ -2590,10 +2590,10 @@ mod bit_vec_bench {
2590
2590
#[ bench]
2591
2591
fn bench_bit_set_small ( b : & mut Bencher ) {
2592
2592
let mut r = rng ( ) ;
2593
- let mut bit_vec = BitVec :: from_elem ( u32:: BITS , false ) ;
2593
+ let mut bit_vec = BitVec :: from_elem ( u32:: BITS as usize , false ) ;
2594
2594
b. iter ( || {
2595
2595
for _ in 0 ..100 {
2596
- bit_vec. set ( ( r. next_u32 ( ) as usize ) % u32:: BITS , true ) ;
2596
+ bit_vec. set ( ( r. next_u32 ( ) as usize ) % u32:: BITS as usize , true ) ;
2597
2597
}
2598
2598
black_box ( & bit_vec) ;
2599
2599
} ) ;
@@ -2610,7 +2610,7 @@ mod bit_vec_bench {
2610
2610
2611
2611
#[ bench]
2612
2612
fn bench_bit_vec_small_iter ( b : & mut Bencher ) {
2613
- let bit_vec = BitVec :: from_elem ( u32:: BITS , false ) ;
2613
+ let bit_vec = BitVec :: from_elem ( u32:: BITS as usize , false ) ;
2614
2614
b. iter ( || {
2615
2615
let mut sum = 0 ;
2616
2616
for _ in 0 ..10 {
@@ -3052,7 +3052,7 @@ mod bit_set_bench {
3052
3052
let mut bit_vec = BitSet :: new ( ) ;
3053
3053
b. iter ( || {
3054
3054
for _ in 0 ..100 {
3055
- bit_vec. insert ( ( r. next_u32 ( ) as usize ) % u32:: BITS ) ;
3055
+ bit_vec. insert ( ( r. next_u32 ( ) as usize ) % u32:: BITS as usize ) ;
3056
3056
}
3057
3057
black_box ( & bit_vec) ;
3058
3058
} ) ;
0 commit comments