@@ -152,7 +152,7 @@ impl<T: Idx> BitSet<T> {
152
152
153
153
/// Count the number of set bits in the set.
154
154
pub fn count ( & self ) -> usize {
155
- self . words . iter ( ) . map ( |e| e . count_ones ( ) as usize ) . sum ( )
155
+ bit_count ( & self . words )
156
156
}
157
157
158
158
/// Returns `true` if `self` contains `elem`.
@@ -628,10 +628,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
628
628
op,
629
629
) ;
630
630
debug_assert ! ( has_changed) ;
631
- * self_chunk_count = self_chunk_words[ 0 ..num_words]
632
- . iter ( )
633
- . map ( |w| w. count_ones ( ) as ChunkSize )
634
- . sum ( ) ;
631
+ * self_chunk_count = bit_count ( & self_chunk_words[ 0 ..num_words] ) as ChunkSize ;
635
632
if * self_chunk_count == * self_chunk_domain_size {
636
633
* self_chunk = Ones ( * self_chunk_domain_size) ;
637
634
}
@@ -705,21 +702,12 @@ impl Chunk {
705
702
assert ! ( 0 < count && count < chunk_domain_size) ;
706
703
707
704
// Check the number of set bits matches `count`.
708
- assert_eq ! (
709
- words. iter( ) . map( |w| w. count_ones( ) as ChunkSize ) . sum:: <ChunkSize >( ) ,
710
- count
711
- ) ;
705
+ assert_eq ! ( bit_count( words. as_ref( ) ) as ChunkSize , count) ;
712
706
713
707
// Check the not-in-use words are all zeroed.
714
708
let num_words = num_words ( chunk_domain_size as usize ) ;
715
709
if num_words < CHUNK_WORDS {
716
- assert_eq ! (
717
- words[ num_words..]
718
- . iter( )
719
- . map( |w| w. count_ones( ) as ChunkSize )
720
- . sum:: <ChunkSize >( ) ,
721
- 0
722
- ) ;
710
+ assert_eq ! ( bit_count( & words[ num_words..] ) , 0 ) ;
723
711
}
724
712
}
725
713
}
@@ -1585,7 +1573,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
1585
1573
/// Returns the number of elements in `row`.
1586
1574
pub fn count ( & self , row : R ) -> usize {
1587
1575
let ( start, end) = self . range ( row) ;
1588
- self . words [ start..end] . iter ( ) . map ( |e| e . count_ones ( ) as usize ) . sum ( )
1576
+ bit_count ( & self . words [ start..end] )
1589
1577
}
1590
1578
}
1591
1579
@@ -1796,6 +1784,11 @@ fn max_bit(word: Word) -> usize {
1796
1784
WORD_BITS - 1 - word. leading_zeros ( ) as usize
1797
1785
}
1798
1786
1787
+ #[ inline]
1788
+ fn bit_count ( words : & [ Word ] ) -> usize {
1789
+ words. iter ( ) . map ( |w| w. count_ones ( ) as usize ) . sum ( )
1790
+ }
1791
+
1799
1792
/// Integral type used to represent the bit set.
1800
1793
pub trait FiniteBitSetTy :
1801
1794
BitAnd < Output = Self >
0 commit comments