Skip to content

Commit 15ef6a4

Browse files
authored
Rollup merge of rust-lang#147630 - nnethercote:bitset-cleanups, r=cjgillot,Zalathar
Bitset cleanups Some minor cleanups I did while working on rust-lang#147619. r? ``@Zalathar``
2 parents e100792 + 8386278 commit 15ef6a4

File tree

2 files changed

+18
-110
lines changed

2 files changed

+18
-110
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`.
@@ -786,7 +786,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
786786

787787
match (&mut self_chunk, &other_chunk) {
788788
(_, Zeros) | (Ones, _) => {}
789-
(Zeros, Ones) | (Mixed(..), Ones) | (Zeros, Mixed(..)) => {
789+
(Zeros, _) | (Mixed(..), Ones) => {
790790
// `other_chunk` fully overwrites `self_chunk`
791791
*self_chunk = other_chunk.clone();
792792
changed = true;
@@ -814,10 +814,8 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
814814
op,
815815
);
816816
debug_assert!(has_changed);
817-
*self_chunk_count = self_chunk_words[0..num_words]
818-
.iter()
819-
.map(|w| w.count_ones() as ChunkSize)
820-
.sum();
817+
*self_chunk_count =
818+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
821819
if *self_chunk_count == chunk_domain_size {
822820
*self_chunk = Ones;
823821
}
@@ -850,7 +848,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
850848

851849
match (&mut self_chunk, &other_chunk) {
852850
(Zeros, _) | (_, Zeros) => {}
853-
(Ones | Mixed(_, _), Ones) => {
851+
(Ones | Mixed(..), Ones) => {
854852
changed = true;
855853
*self_chunk = Zeros;
856854
}
@@ -868,10 +866,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
868866
let self_chunk_count = chunk_domain_size - *other_chunk_count;
869867
debug_assert_eq!(
870868
self_chunk_count,
871-
self_chunk_words[0..num_words]
872-
.iter()
873-
.map(|w| w.count_ones() as ChunkSize)
874-
.sum()
869+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize
875870
);
876871
*self_chunk = Mixed(self_chunk_count, Rc::new(self_chunk_words));
877872
}
@@ -894,10 +889,8 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
894889
op,
895890
);
896891
debug_assert!(has_changed);
897-
*self_chunk_count = self_chunk_words[0..num_words]
898-
.iter()
899-
.map(|w| w.count_ones() as ChunkSize)
900-
.sum();
892+
*self_chunk_count =
893+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
901894
if *self_chunk_count == 0 {
902895
*self_chunk = Zeros;
903896
}
@@ -953,10 +946,8 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
953946
op,
954947
);
955948
debug_assert!(has_changed);
956-
*self_chunk_count = self_chunk_words[0..num_words]
957-
.iter()
958-
.map(|w| w.count_ones() as ChunkSize)
959-
.sum();
949+
*self_chunk_count =
950+
count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
960951
if *self_chunk_count == 0 {
961952
*self_chunk = Zeros;
962953
}
@@ -970,48 +961,6 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
970961
}
971962
}
972963

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

10871036
// Check the number of set bits matches `count`.
1088-
assert_eq!(
1089-
words.iter().map(|w| w.count_ones() as ChunkSize).sum::<ChunkSize>(),
1090-
count
1091-
);
1037+
assert_eq!(count_ones(words.as_slice()) as ChunkSize, count);
10921038

10931039
// Check the not-in-use words are all zeroed.
10941040
let num_words = num_words(chunk_domain_size as usize);
10951041
if num_words < CHUNK_WORDS {
1096-
assert_eq!(
1097-
words[num_words..]
1098-
.iter()
1099-
.map(|w| w.count_ones() as ChunkSize)
1100-
.sum::<ChunkSize>(),
1101-
0
1102-
);
1042+
assert_eq!(count_ones(&words[num_words..]) as ChunkSize, 0);
11031043
}
11041044
}
11051045
}
@@ -1122,15 +1062,6 @@ enum ChunkIter<'a> {
11221062
Finished,
11231063
}
11241064

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

@@ -1801,6 +1732,11 @@ fn max_bit(word: Word) -> usize {
18011732
WORD_BITS - 1 - word.leading_zeros() as usize
18021733
}
18031734

1735+
#[inline]
1736+
fn count_ones(words: &[Word]) -> usize {
1737+
words.iter().map(|word| word.count_ones() as usize).sum()
1738+
}
1739+
18041740
/// Integral type used to represent the bit set.
18051741
pub trait FiniteBitSetTy:
18061742
BitAnd<Output = Self>

compiler/rustc_index/src/bit_set/tests.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -306,34 +306,6 @@ fn with_elements_chunked(elements: &[usize], domain_size: usize) -> ChunkedBitSe
306306
s
307307
}
308308

309-
fn with_elements_standard(elements: &[usize], domain_size: usize) -> DenseBitSet<usize> {
310-
let mut s = DenseBitSet::new_empty(domain_size);
311-
for &e in elements {
312-
assert!(s.insert(e));
313-
}
314-
s
315-
}
316-
317-
#[test]
318-
fn chunked_bitset_into_bitset_operations() {
319-
let a = vec![1, 5, 7, 11, 15, 2000, 3000];
320-
let b = vec![3, 4, 11, 3000, 4000];
321-
let aub = vec![1, 3, 4, 5, 7, 11, 15, 2000, 3000, 4000];
322-
let aib = vec![11, 3000];
323-
324-
let b = with_elements_chunked(&b, 9876);
325-
326-
let mut union = with_elements_standard(&a, 9876);
327-
assert!(union.union(&b));
328-
assert!(!union.union(&b));
329-
assert!(union.iter().eq(aub.iter().copied()));
330-
331-
let mut intersection = with_elements_standard(&a, 9876);
332-
assert!(intersection.intersect(&b));
333-
assert!(!intersection.intersect(&b));
334-
assert!(intersection.iter().eq(aib.iter().copied()));
335-
}
336-
337309
#[test]
338310
fn chunked_bitset_iter() {
339311
fn check_iter(bit: &ChunkedBitSet<usize>, vec: &Vec<usize>) {

0 commit comments

Comments
 (0)