Skip to content

Commit b505555

Browse files
committed
Extract bit-counting of a slice of Words into a function
1 parent 6a70556 commit b505555

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

compiler/rustc_index/src/bit_set.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl<T: Idx> BitSet<T> {
152152

153153
/// Count the number of set bits in the set.
154154
pub fn count(&self) -> usize {
155-
self.words.iter().map(|e| e.count_ones() as usize).sum()
155+
bit_count(&self.words)
156156
}
157157

158158
/// Returns `true` if `self` contains `elem`.
@@ -628,10 +628,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
628628
op,
629629
);
630630
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;
635632
if *self_chunk_count == *self_chunk_domain_size {
636633
*self_chunk = Ones(*self_chunk_domain_size);
637634
}
@@ -705,21 +702,12 @@ impl Chunk {
705702
assert!(0 < count && count < chunk_domain_size);
706703

707704
// 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);
712706

713707
// Check the not-in-use words are all zeroed.
714708
let num_words = num_words(chunk_domain_size as usize);
715709
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);
723711
}
724712
}
725713
}
@@ -1585,7 +1573,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
15851573
/// Returns the number of elements in `row`.
15861574
pub fn count(&self, row: R) -> usize {
15871575
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])
15891577
}
15901578
}
15911579

@@ -1796,6 +1784,11 @@ fn max_bit(word: Word) -> usize {
17961784
WORD_BITS - 1 - word.leading_zeros() as usize
17971785
}
17981786

1787+
#[inline]
1788+
fn bit_count(words: &[Word]) -> usize {
1789+
words.iter().map(|w| w.count_ones() as usize).sum()
1790+
}
1791+
17991792
/// Integral type used to represent the bit set.
18001793
pub trait FiniteBitSetTy:
18011794
BitAnd<Output = Self>

0 commit comments

Comments
 (0)