@@ -26,7 +26,7 @@ pub const WORD_BITS: usize = WORD_BYTES * 8;
26
26
/// It does not support resizing after creation; use `GrowableBitSet` for that.
27
27
#[ derive( Clone , Eq , PartialEq ) ]
28
28
pub struct BitSet < T : Idx > {
29
- data : Vec < Word > ,
29
+ words : Vec < Word > ,
30
30
marker : PhantomData < T > ,
31
31
}
32
32
@@ -35,7 +35,7 @@ impl<T: Idx> BitSet<T> {
35
35
pub fn new_empty ( domain_size : usize ) -> BitSet < T > {
36
36
let num_words = num_words ( domain_size) ;
37
37
BitSet {
38
- data : vec ! [ 0 ; num_words] ,
38
+ words : vec ! [ 0 ; num_words] ,
39
39
marker : PhantomData ,
40
40
}
41
41
}
@@ -44,7 +44,7 @@ impl<T: Idx> BitSet<T> {
44
44
pub fn new_filled ( domain_size : usize ) -> BitSet < T > {
45
45
let num_words = num_words ( domain_size) ;
46
46
let mut result = BitSet {
47
- data : vec ! [ !0 ; num_words] ,
47
+ words : vec ! [ !0 ; num_words] ,
48
48
marker : PhantomData ,
49
49
} ;
50
50
result. clear_above ( domain_size) ;
@@ -53,14 +53,14 @@ impl<T: Idx> BitSet<T> {
53
53
54
54
#[ inline]
55
55
pub fn clear ( & mut self ) {
56
- for word in & mut self . data {
56
+ for word in & mut self . words {
57
57
* word = 0 ;
58
58
}
59
59
}
60
60
61
61
/// Sets all elements up to and including `size`.
62
62
pub fn set_up_to ( & mut self , bit : usize ) {
63
- for word in & mut self . data {
63
+ for word in & mut self . words {
64
64
* word = !0 ;
65
65
}
66
66
self . clear_above ( bit) ;
@@ -70,14 +70,14 @@ impl<T: Idx> BitSet<T> {
70
70
fn clear_above ( & mut self , bit : usize ) {
71
71
let first_clear_block = bit / WORD_BITS ;
72
72
73
- if first_clear_block < self . data . len ( ) {
73
+ if first_clear_block < self . words . len ( ) {
74
74
// Within `first_clear_block`, the `bit % WORD_BITS` LSBs should
75
75
// remain.
76
76
let mask = ( 1 << ( bit % WORD_BITS ) ) - 1 ;
77
- self . data [ first_clear_block] &= mask;
77
+ self . words [ first_clear_block] &= mask;
78
78
79
79
// All the blocks above `first_clear_block` are fully cleared.
80
- for word in & mut self . data [ first_clear_block + 1 ..] {
80
+ for word in & mut self . words [ first_clear_block + 1 ..] {
81
81
* word = 0 ;
82
82
}
83
83
}
@@ -86,63 +86,63 @@ impl<T: Idx> BitSet<T> {
86
86
/// Efficiently overwrite `self` with `other`. Panics if `self` and `other`
87
87
/// don't have the same length.
88
88
pub fn overwrite ( & mut self , other : & BitSet < T > ) {
89
- self . words_mut ( ) . clone_from_slice ( other. words ( ) ) ;
89
+ self . words . clone_from_slice ( & other. words ) ;
90
90
}
91
91
92
92
/// Count the number of set bits in the set.
93
93
pub fn count ( & self ) -> usize {
94
- self . data . iter ( ) . map ( |e| e. count_ones ( ) as usize ) . sum ( )
94
+ self . words . iter ( ) . map ( |e| e. count_ones ( ) as usize ) . sum ( )
95
95
}
96
96
97
97
/// True if `self` contains the bit `bit`.
98
98
#[ inline]
99
99
pub fn contains ( & self , bit : T ) -> bool {
100
- let ( word , mask) = word_mask ( bit) ;
101
- ( self . data [ word ] & mask) != 0
100
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
101
+ ( self . words [ word_index ] & mask) != 0
102
102
}
103
103
104
104
/// True if `self` is a (non-strict) superset of `other`.
105
105
///
106
- /// The two vectors must have the same length .
106
+ /// The two sets must have the same domain_size .
107
107
#[ inline]
108
108
pub fn superset ( & self , other : & BitSet < T > ) -> bool {
109
- assert_eq ! ( self . data . len( ) , other. data . len( ) ) ;
110
- self . data . iter ( ) . zip ( & other. data ) . all ( |( a, b) | ( a & b) == * b)
109
+ assert_eq ! ( self . words . len( ) , other. words . len( ) ) ;
110
+ self . words . iter ( ) . zip ( & other. words ) . all ( |( a, b) | ( a & b) == * b)
111
111
}
112
112
113
113
/// Is the set empty?
114
114
#[ inline]
115
115
pub fn is_empty ( & self ) -> bool {
116
- self . data . iter ( ) . all ( |a| * a == 0 )
116
+ self . words . iter ( ) . all ( |a| * a == 0 )
117
117
}
118
118
119
119
/// Insert a bit. Returns true if the bit has changed.
120
120
#[ inline]
121
121
pub fn insert ( & mut self , bit : T ) -> bool {
122
- let ( word , mask) = word_mask ( bit) ;
123
- let data = & mut self . data [ word ] ;
124
- let value = * data ;
125
- let new_value = value | mask;
126
- * data = new_value ;
127
- new_value != value
122
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
123
+ let word_ref = & mut self . words [ word_index ] ;
124
+ let word = * word_ref ;
125
+ let new_word = word | mask;
126
+ * word_ref = new_word ;
127
+ new_word != word
128
128
}
129
129
130
130
/// Sets all bits to true.
131
131
pub fn insert_all ( & mut self ) {
132
- for word in & mut self . data {
132
+ for word in & mut self . words {
133
133
* word = !0 ;
134
134
}
135
135
}
136
136
137
137
/// Returns true if the bit has changed.
138
138
#[ inline]
139
139
pub fn remove ( & mut self , bit : T ) -> bool {
140
- let ( word , mask) = word_mask ( bit) ;
141
- let data = & mut self . data [ word ] ;
142
- let value = * data ;
143
- let new_value = value & !mask;
144
- * data = new_value ;
145
- new_value != value
140
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
141
+ let word_ref = & mut self . words [ word_index ] ;
142
+ let word = * word_ref ;
143
+ let new_word = word & !mask;
144
+ * word_ref = new_word ;
145
+ new_word != word
146
146
}
147
147
148
148
/// Set `self = self | other` and return true if `self` changed
@@ -160,25 +160,20 @@ impl<T: Idx> BitSet<T> {
160
160
/// Set `self = self & other` and return true if `self` changed.
161
161
/// (i.e., if any bits were removed).
162
162
pub fn intersect ( & mut self , other : & BitSet < T > ) -> bool {
163
- bitwise ( self . words_mut ( ) , other. words ( ) , |a, b| { a & b } )
163
+ bitwise ( & mut self . words , & other. words , |a, b| { a & b } )
164
164
}
165
165
166
166
/// Get a slice of the underlying words.
167
167
pub fn words ( & self ) -> & [ Word ] {
168
- & self . data
169
- }
170
-
171
- /// Get a mutable slice of the underlying words.
172
- pub fn words_mut ( & mut self ) -> & mut [ Word ] {
173
- & mut self . data
168
+ & self . words
174
169
}
175
170
176
171
/// Iterates over the indices of set bits in a sorted order.
177
172
#[ inline]
178
173
pub fn iter < ' a > ( & ' a self ) -> BitIter < ' a , T > {
179
174
BitIter {
180
175
cur : None ,
181
- iter : self . data . iter ( ) . enumerate ( ) ,
176
+ iter : self . words . iter ( ) . enumerate ( ) ,
182
177
marker : PhantomData ,
183
178
}
184
179
}
@@ -187,7 +182,7 @@ impl<T: Idx> BitSet<T> {
187
182
pub fn to_hybrid ( & self ) -> HybridBitSet < T > {
188
183
// This domain_size may be slightly larger than the one specified
189
184
// upon creation, due to rounding up to a whole word. That's ok.
190
- let domain_size = self . words ( ) . len ( ) * WORD_BITS ;
185
+ let domain_size = self . words . len ( ) * WORD_BITS ;
191
186
192
187
// Note: we currently don't bother trying to make a Sparse set.
193
188
HybridBitSet :: Dense ( self . to_owned ( ) , domain_size)
@@ -201,19 +196,19 @@ impl<T: Idx> BitSet<T> {
201
196
202
197
// i tracks how many bits we have printed so far.
203
198
let mut i = 0 ;
204
- for word in & self . data {
205
- let mut v = * word;
206
- for _ in 0 ..WORD_BYTES { // for each byte in `v `:
199
+ for word in & self . words {
200
+ let mut word = * word;
201
+ for _ in 0 ..WORD_BYTES { // for each byte in `word `:
207
202
let remain = bits - i;
208
203
// If less than a byte remains, then mask just that many bits.
209
204
let mask = if remain <= 8 { ( 1 << remain) - 1 } else { 0xFF } ;
210
205
assert ! ( mask <= 0xFF ) ;
211
- let byte = v & mask;
206
+ let byte = word & mask;
212
207
213
208
result. push_str ( & format ! ( "{}{:02x}" , sep, byte) ) ;
214
209
215
210
if remain <= 8 { break ; }
216
- v >>= 8 ;
211
+ word >>= 8 ;
217
212
i += 8 ;
218
213
sep = '-' ;
219
214
}
@@ -241,13 +236,13 @@ pub trait SubtractFromBitSet<T: Idx> {
241
236
242
237
impl < T : Idx > UnionIntoBitSet < T > for BitSet < T > {
243
238
fn union_into ( & self , other : & mut BitSet < T > ) -> bool {
244
- bitwise ( other. words_mut ( ) , self . words ( ) , |a, b| { a | b } )
239
+ bitwise ( & mut other. words , & self . words , |a, b| { a | b } )
245
240
}
246
241
}
247
242
248
243
impl < T : Idx > SubtractFromBitSet < T > for BitSet < T > {
249
244
fn subtract_from ( & self , other : & mut BitSet < T > ) -> bool {
250
- bitwise ( other. words_mut ( ) , self . words ( ) , |a, b| { a & !b } )
245
+ bitwise ( & mut other. words , & self . words , |a, b| { a & !b } )
251
246
}
252
247
}
253
248
@@ -261,15 +256,15 @@ impl<T: Idx> fmt::Debug for BitSet<T> {
261
256
262
257
impl < T : Idx > rustc_serialize:: Encodable for BitSet < T > {
263
258
fn encode < E : rustc_serialize:: Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , E :: Error > {
264
- self . data . encode ( encoder)
259
+ self . words . encode ( encoder)
265
260
}
266
261
}
267
262
268
263
impl < T : Idx > rustc_serialize:: Decodable for BitSet < T > {
269
264
fn decode < D : rustc_serialize:: Decoder > ( d : & mut D ) -> Result < BitSet < T > , D :: Error > {
270
265
let words: Vec < Word > = rustc_serialize:: Decodable :: decode ( d) ?;
271
266
Ok ( BitSet {
272
- data : words,
267
+ words,
273
268
marker : PhantomData ,
274
269
} )
275
270
}
@@ -529,8 +524,8 @@ pub struct GrowableBitSet<T: Idx> {
529
524
impl < T : Idx > GrowableBitSet < T > {
530
525
pub fn grow ( & mut self , domain_size : T ) {
531
526
let num_words = num_words ( domain_size) ;
532
- if self . bit_set . data . len ( ) <= num_words {
533
- self . bit_set . data . resize ( num_words + 1 , 0 )
527
+ if self . bit_set . words . len ( ) <= num_words {
528
+ self . bit_set . words . resize ( num_words + 1 , 0 )
534
529
}
535
530
}
536
531
@@ -551,8 +546,8 @@ impl<T: Idx> GrowableBitSet<T> {
551
546
552
547
#[ inline]
553
548
pub fn contains ( & self , bit : T ) -> bool {
554
- let ( word , mask) = word_mask ( bit) ;
555
- if let Some ( word) = self . bit_set . data . get ( word ) {
549
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
550
+ if let Some ( word) = self . bit_set . words . get ( word_index ) {
556
551
( word & mask) != 0
557
552
} else {
558
553
false
@@ -564,7 +559,7 @@ impl<T: Idx> GrowableBitSet<T> {
564
559
#[ derive( Clone , Debug ) ]
565
560
pub struct BitMatrix < R : Idx , C : Idx > {
566
561
columns : usize ,
567
- vector : Vec < Word > ,
562
+ words : Vec < Word > ,
568
563
marker : PhantomData < ( R , C ) > ,
569
564
}
570
565
@@ -576,7 +571,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
576
571
let words_per_row = num_words ( columns) ;
577
572
BitMatrix {
578
573
columns,
579
- vector : vec ! [ 0 ; rows * words_per_row] ,
574
+ words : vec ! [ 0 ; rows * words_per_row] ,
580
575
marker : PhantomData ,
581
576
}
582
577
}
@@ -595,12 +590,12 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
595
590
/// Returns true if this changed the matrix, and false otherwise.
596
591
pub fn insert ( & mut self , row : R , column : R ) -> bool {
597
592
let ( start, _) = self . range ( row) ;
598
- let ( word , mask) = word_mask ( column) ;
599
- let vector = & mut self . vector [ ..] ;
600
- let v1 = vector [ start + word ] ;
601
- let v2 = v1 | mask;
602
- vector [ start + word ] = v2 ;
603
- v1 != v2
593
+ let ( word_index , mask) = word_index_and_mask ( column) ;
594
+ let words = & mut self . words [ ..] ;
595
+ let word = words [ start + word_index ] ;
596
+ let new_word = word | mask;
597
+ words [ start + word_index ] = new_word ;
598
+ word != new_word
604
599
}
605
600
606
601
/// Do the bits from `row` contain `column`? Put another way, is
@@ -609,8 +604,8 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
609
604
/// `row` reach `column`?
610
605
pub fn contains ( & self , row : R , column : R ) -> bool {
611
606
let ( start, _) = self . range ( row) ;
612
- let ( word , mask) = word_mask ( column) ;
613
- ( self . vector [ start + word ] & mask) != 0
607
+ let ( word_index , mask) = word_index_and_mask ( column) ;
608
+ ( self . words [ start + word_index ] & mask) != 0
614
609
}
615
610
616
611
/// Returns those indices that are true in rows `a` and `b`. This
@@ -622,7 +617,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
622
617
let ( b_start, b_end) = self . range ( b) ;
623
618
let mut result = Vec :: with_capacity ( self . columns ) ;
624
619
for ( base, ( i, j) ) in ( a_start..a_end) . zip ( b_start..b_end) . enumerate ( ) {
625
- let mut v = self . vector [ i] & self . vector [ j] ;
620
+ let mut v = self . words [ i] & self . words [ j] ;
626
621
for bit in 0 ..WORD_BITS {
627
622
if v == 0 {
628
623
break ;
@@ -646,13 +641,13 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
646
641
pub fn union_rows ( & mut self , read : R , write : R ) -> bool {
647
642
let ( read_start, read_end) = self . range ( read) ;
648
643
let ( write_start, write_end) = self . range ( write) ;
649
- let vector = & mut self . vector [ ..] ;
644
+ let words = & mut self . words [ ..] ;
650
645
let mut changed = false ;
651
646
for ( read_index, write_index) in ( read_start..read_end) . zip ( write_start..write_end) {
652
- let v1 = vector [ write_index] ;
653
- let v2 = v1 | vector [ read_index] ;
654
- vector [ write_index] = v2 ;
655
- changed |= v1 != v2 ;
647
+ let word = words [ write_index] ;
648
+ let new_word = word | words [ read_index] ;
649
+ words [ write_index] = new_word ;
650
+ changed |= word != new_word ;
656
651
}
657
652
changed
658
653
}
@@ -663,7 +658,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
663
658
let ( start, end) = self . range ( row) ;
664
659
BitIter {
665
660
cur : None ,
666
- iter : self . vector [ start..end] . iter ( ) . enumerate ( ) ,
661
+ iter : self . words [ start..end] . iter ( ) . enumerate ( ) ,
667
662
marker : PhantomData ,
668
663
}
669
664
}
@@ -778,11 +773,11 @@ fn num_words<T: Idx>(elements: T) -> usize {
778
773
}
779
774
780
775
#[ inline]
781
- fn word_mask < T : Idx > ( index : T ) -> ( usize , Word ) {
776
+ fn word_index_and_mask < T : Idx > ( index : T ) -> ( usize , Word ) {
782
777
let index = index. index ( ) ;
783
- let word = index / WORD_BITS ;
778
+ let word_index = index / WORD_BITS ;
784
779
let mask = 1 << ( index % WORD_BITS ) ;
785
- ( word , mask)
780
+ ( word_index , mask)
786
781
}
787
782
788
783
#[ test]
0 commit comments