Skip to content

Commit c78dd25

Browse files
Auto merge of #147630 - nnethercote:bitset-cleanups, r=<try>
Bitset cleanups
2 parents 3545698 + 448c48c commit c78dd25

File tree

1 file changed

+18
-82
lines changed

1 file changed

+18
-82
lines changed

compiler/rustc_index/src/bit_set.rs

Lines changed: 18 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<T: Idx> DenseBitSet<T> {
160160

161161
/// Count the number of set bits in the set.
162162
pub fn count(&self) -> usize {
163-
self.words.iter().map(|e| e.count_ones() as usize).sum()
163+
count_ones(&self.words)
164164
}
165165

166166
/// Returns `true` if `self` contains `elem`.
@@ -789,7 +789,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
789789

790790
match (&mut self_chunk, &other_chunk) {
791791
(_, Zeros) | (Ones, _) => {}
792-
(Zeros, Ones) | (Mixed(..), Ones) | (Zeros, Mixed(..)) => {
792+
(Zeros, _) | (Mixed(..), Ones) => {
793793
// `other_chunk` fully overwrites `self_chunk`
794794
*self_chunk = other_chunk.clone();
795795
changed = true;
@@ -817,10 +817,8 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
817817
op,
818818
);
819819
debug_assert!(has_changed);
820-
*self_chunk_count = self_chunk_words[0..num_words]
821-
.iter()
822-
.map(|w| w.count_ones() as ChunkSize)
823-
.sum();
820+
*self_chunk_count =
821+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
824822
if *self_chunk_count == chunk_domain_size {
825823
*self_chunk = Ones;
826824
}
@@ -853,7 +851,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
853851

854852
match (&mut self_chunk, &other_chunk) {
855853
(Zeros, _) | (_, Zeros) => {}
856-
(Ones | Mixed(_, _), Ones) => {
854+
(Ones | Mixed(..), Ones) => {
857855
changed = true;
858856
*self_chunk = Zeros;
859857
}
@@ -871,10 +869,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
871869
let self_chunk_count = chunk_domain_size - *other_chunk_count;
872870
debug_assert_eq!(
873871
self_chunk_count,
874-
self_chunk_words[0..num_words]
875-
.iter()
876-
.map(|w| w.count_ones() as ChunkSize)
877-
.sum()
872+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize
878873
);
879874
*self_chunk = Mixed(self_chunk_count, Rc::new(self_chunk_words));
880875
}
@@ -897,10 +892,8 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
897892
op,
898893
);
899894
debug_assert!(has_changed);
900-
*self_chunk_count = self_chunk_words[0..num_words]
901-
.iter()
902-
.map(|w| w.count_ones() as ChunkSize)
903-
.sum();
895+
*self_chunk_count =
896+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
904897
if *self_chunk_count == 0 {
905898
*self_chunk = Zeros;
906899
}
@@ -956,10 +949,8 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
956949
op,
957950
);
958951
debug_assert!(has_changed);
959-
*self_chunk_count = self_chunk_words[0..num_words]
960-
.iter()
961-
.map(|w| w.count_ones() as ChunkSize)
962-
.sum();
952+
*self_chunk_count =
953+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
963954
if *self_chunk_count == 0 {
964955
*self_chunk = Zeros;
965956
}
@@ -973,48 +964,6 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
973964
}
974965
}
975966

976-
impl<T: Idx> BitRelations<ChunkedBitSet<T>> for DenseBitSet<T> {
977-
fn union(&mut self, other: &ChunkedBitSet<T>) -> bool {
978-
sequential_update(|elem| self.insert(elem), other.iter())
979-
}
980-
981-
fn subtract(&mut self, _other: &ChunkedBitSet<T>) -> bool {
982-
unimplemented!("implement if/when necessary");
983-
}
984-
985-
fn intersect(&mut self, other: &ChunkedBitSet<T>) -> bool {
986-
assert_eq!(self.domain_size(), other.domain_size);
987-
let mut changed = false;
988-
for (i, chunk) in other.chunks.iter().enumerate() {
989-
let mut words = &mut self.words[i * CHUNK_WORDS..];
990-
if words.len() > CHUNK_WORDS {
991-
words = &mut words[..CHUNK_WORDS];
992-
}
993-
match chunk {
994-
Zeros => {
995-
for word in words {
996-
if *word != 0 {
997-
changed = true;
998-
*word = 0;
999-
}
1000-
}
1001-
}
1002-
Ones => (),
1003-
Mixed(_, data) => {
1004-
for (i, word) in words.iter_mut().enumerate() {
1005-
let new_val = *word & data[i];
1006-
if new_val != *word {
1007-
changed = true;
1008-
*word = new_val;
1009-
}
1010-
}
1011-
}
1012-
}
1013-
}
1014-
changed
1015-
}
1016-
}
1017-
1018967
impl<T> Clone for ChunkedBitSet<T> {
1019968
fn clone(&self) -> Self {
1020969
ChunkedBitSet {
@@ -1088,21 +1037,12 @@ impl Chunk {
10881037
assert!(0 < count && count < chunk_domain_size);
10891038

10901039
// Check the number of set bits matches `count`.
1091-
assert_eq!(
1092-
words.iter().map(|w| w.count_ones() as ChunkSize).sum::<ChunkSize>(),
1093-
count
1094-
);
1040+
assert_eq!(count_ones(words) as ChunkSize, count);
10951041

10961042
// Check the not-in-use words are all zeroed.
10971043
let num_words = num_words(chunk_domain_size as usize);
10981044
if num_words < CHUNK_WORDS {
1099-
assert_eq!(
1100-
words[num_words..]
1101-
.iter()
1102-
.map(|w| w.count_ones() as ChunkSize)
1103-
.sum::<ChunkSize>(),
1104-
0
1105-
);
1045+
assert_eq!(count_ones(words[num_words..]) as ChunkSize, 0);
11061046
}
11071047
}
11081048
}
@@ -1125,15 +1065,6 @@ enum ChunkIter<'a> {
11251065
Finished,
11261066
}
11271067

1128-
// Applies a function to mutate a bitset, and returns true if any
1129-
// of the applications return true
1130-
fn sequential_update<T: Idx>(
1131-
mut self_update: impl FnMut(T) -> bool,
1132-
it: impl Iterator<Item = T>,
1133-
) -> bool {
1134-
it.fold(false, |changed, elem| self_update(elem) | changed)
1135-
}
1136-
11371068
impl<T: Idx> fmt::Debug for ChunkedBitSet<T> {
11381069
fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
11391070
w.debug_list().entries(self.iter()).finish()
@@ -1593,7 +1524,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
15931524
/// Returns the number of elements in `row`.
15941525
pub fn count(&self, row: R) -> usize {
15951526
let (start, end) = self.range(row);
1596-
self.words[start..end].iter().map(|e| e.count_ones() as usize).sum()
1527+
count_ones(&self.words[start..end])
15971528
}
15981529
}
15991530

@@ -1804,6 +1735,11 @@ fn max_bit(word: Word) -> usize {
18041735
WORD_BITS - 1 - word.leading_zeros() as usize
18051736
}
18061737

1738+
#[inline]
1739+
fn count_ones(words: &[Word]) -> usize {
1740+
words.iter().map(|word| word.count_ones() as usize).sum()
1741+
}
1742+
18071743
/// Integral type used to represent the bit set.
18081744
pub trait FiniteBitSetTy:
18091745
BitAnd<Output = Self>

0 commit comments

Comments
 (0)