@@ -28,7 +28,7 @@ pub const WORD_BITS: usize = WORD_BYTES * 8;
28
28
/// just be `usize`.
29
29
#[ derive( Clone , Eq , PartialEq ) ]
30
30
pub struct BitSet < T : Idx > {
31
- data : Vec < Word > ,
31
+ words : Vec < Word > ,
32
32
marker : PhantomData < T > ,
33
33
}
34
34
@@ -37,7 +37,7 @@ impl<T: Idx> BitSet<T> {
37
37
pub fn new_empty ( domain_size : usize ) -> BitSet < T > {
38
38
let num_words = num_words ( domain_size) ;
39
39
BitSet {
40
- data : vec ! [ 0 ; num_words] ,
40
+ words : vec ! [ 0 ; num_words] ,
41
41
marker : PhantomData ,
42
42
}
43
43
}
@@ -46,7 +46,7 @@ impl<T: Idx> BitSet<T> {
46
46
pub fn new_filled ( domain_size : usize ) -> BitSet < T > {
47
47
let num_words = num_words ( domain_size) ;
48
48
let mut result = BitSet {
49
- data : vec ! [ !0 ; num_words] ,
49
+ words : vec ! [ !0 ; num_words] ,
50
50
marker : PhantomData ,
51
51
} ;
52
52
result. clear_above ( domain_size) ;
@@ -55,14 +55,14 @@ impl<T: Idx> BitSet<T> {
55
55
56
56
#[ inline]
57
57
pub fn clear ( & mut self ) {
58
- for word in & mut self . data {
58
+ for word in & mut self . words {
59
59
* word = 0 ;
60
60
}
61
61
}
62
62
63
63
/// Sets all elements up to and including `size`.
64
64
pub fn set_up_to ( & mut self , bit : usize ) {
65
- for word in & mut self . data {
65
+ for word in & mut self . words {
66
66
* word = !0 ;
67
67
}
68
68
self . clear_above ( bit) ;
@@ -72,14 +72,14 @@ impl<T: Idx> BitSet<T> {
72
72
fn clear_above ( & mut self , bit : usize ) {
73
73
let first_clear_block = bit / WORD_BITS ;
74
74
75
- if first_clear_block < self . data . len ( ) {
75
+ if first_clear_block < self . words . len ( ) {
76
76
// Within `first_clear_block`, the `bit % WORD_BITS` LSBs should
77
77
// remain.
78
78
let mask = ( 1 << ( bit % WORD_BITS ) ) - 1 ;
79
- self . data [ first_clear_block] &= mask;
79
+ self . words [ first_clear_block] &= mask;
80
80
81
81
// All the blocks above `first_clear_block` are fully cleared.
82
- for word in & mut self . data [ first_clear_block + 1 ..] {
82
+ for word in & mut self . words [ first_clear_block + 1 ..] {
83
83
* word = 0 ;
84
84
}
85
85
}
@@ -88,63 +88,63 @@ impl<T: Idx> BitSet<T> {
88
88
/// Efficiently overwrite `self` with `other`. Panics if `self` and `other`
89
89
/// don't have the same length.
90
90
pub fn overwrite ( & mut self , other : & BitSet < T > ) {
91
- self . words_mut ( ) . clone_from_slice ( other. words ( ) ) ;
91
+ self . words . clone_from_slice ( & other. words ) ;
92
92
}
93
93
94
94
/// Count the number of set bits in the set.
95
95
pub fn count ( & self ) -> usize {
96
- self . data . iter ( ) . map ( |e| e. count_ones ( ) as usize ) . sum ( )
96
+ self . words . iter ( ) . map ( |e| e. count_ones ( ) as usize ) . sum ( )
97
97
}
98
98
99
99
/// True if `self` contains the bit `bit`.
100
100
#[ inline]
101
101
pub fn contains ( & self , bit : T ) -> bool {
102
- let ( word , mask) = word_mask ( bit) ;
103
- ( self . data [ word ] & mask) != 0
102
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
103
+ ( self . words [ word_index ] & mask) != 0
104
104
}
105
105
106
106
/// True if `self` is a (non-strict) superset of `other`.
107
107
///
108
- /// The two vectors must have the same length .
108
+ /// The two sets must have the same domain_size .
109
109
#[ inline]
110
110
pub fn superset ( & self , other : & BitSet < T > ) -> bool {
111
- assert_eq ! ( self . data . len( ) , other. data . len( ) ) ;
112
- self . data . iter ( ) . zip ( & other. data ) . all ( |( a, b) | ( a & b) == * b)
111
+ assert_eq ! ( self . words . len( ) , other. words . len( ) ) ;
112
+ self . words . iter ( ) . zip ( & other. words ) . all ( |( a, b) | ( a & b) == * b)
113
113
}
114
114
115
115
/// Is the set empty?
116
116
#[ inline]
117
117
pub fn is_empty ( & self ) -> bool {
118
- self . data . iter ( ) . all ( |a| * a == 0 )
118
+ self . words . iter ( ) . all ( |a| * a == 0 )
119
119
}
120
120
121
121
/// Insert a bit. Returns true if the bit has changed.
122
122
#[ inline]
123
123
pub fn insert ( & mut self , bit : T ) -> bool {
124
- let ( word , mask) = word_mask ( bit) ;
125
- let data = & mut self . data [ word ] ;
126
- let value = * data ;
127
- let new_value = value | mask;
128
- * data = new_value ;
129
- new_value != value
124
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
125
+ let word_ref = & mut self . words [ word_index ] ;
126
+ let word = * word_ref ;
127
+ let new_word = word | mask;
128
+ * word_ref = new_word ;
129
+ new_word != word
130
130
}
131
131
132
132
/// Sets all bits to true.
133
133
pub fn insert_all ( & mut self ) {
134
- for word in & mut self . data {
134
+ for word in & mut self . words {
135
135
* word = !0 ;
136
136
}
137
137
}
138
138
139
139
/// Returns true if the bit has changed.
140
140
#[ inline]
141
141
pub fn remove ( & mut self , bit : T ) -> bool {
142
- let ( word , mask) = word_mask ( bit) ;
143
- let data = & mut self . data [ word ] ;
144
- let value = * data ;
145
- let new_value = value & !mask;
146
- * data = new_value ;
147
- new_value != value
142
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
143
+ let word_ref = & mut self . words [ word_index ] ;
144
+ let word = * word_ref ;
145
+ let new_word = word & !mask;
146
+ * word_ref = new_word ;
147
+ new_word != word
148
148
}
149
149
150
150
/// Set `self = self | other` and return true if `self` changed
@@ -162,25 +162,20 @@ impl<T: Idx> BitSet<T> {
162
162
/// Set `self = self & other` and return true if `self` changed.
163
163
/// (i.e., if any bits were removed).
164
164
pub fn intersect ( & mut self , other : & BitSet < T > ) -> bool {
165
- bitwise ( self . words_mut ( ) , other. words ( ) , |a, b| { a & b } )
165
+ bitwise ( & mut self . words , & other. words , |a, b| { a & b } )
166
166
}
167
167
168
168
/// Get a slice of the underlying words.
169
169
pub fn words ( & self ) -> & [ Word ] {
170
- & self . data
171
- }
172
-
173
- /// Get a mutable slice of the underlying words.
174
- pub fn words_mut ( & mut self ) -> & mut [ Word ] {
175
- & mut self . data
170
+ & self . words
176
171
}
177
172
178
173
/// Iterates over the indices of set bits in a sorted order.
179
174
#[ inline]
180
175
pub fn iter < ' a > ( & ' a self ) -> BitIter < ' a , T > {
181
176
BitIter {
182
177
cur : None ,
183
- iter : self . data . iter ( ) . enumerate ( ) ,
178
+ iter : self . words . iter ( ) . enumerate ( ) ,
184
179
marker : PhantomData ,
185
180
}
186
181
}
@@ -189,7 +184,7 @@ impl<T: Idx> BitSet<T> {
189
184
pub fn to_hybrid ( & self ) -> HybridBitSet < T > {
190
185
// This domain_size may be slightly larger than the one specified
191
186
// upon creation, due to rounding up to a whole word. That's ok.
192
- let domain_size = self . words ( ) . len ( ) * WORD_BITS ;
187
+ let domain_size = self . words . len ( ) * WORD_BITS ;
193
188
194
189
// Note: we currently don't bother trying to make a Sparse set.
195
190
HybridBitSet :: Dense ( self . to_owned ( ) , domain_size)
@@ -203,19 +198,19 @@ impl<T: Idx> BitSet<T> {
203
198
204
199
// i tracks how many bits we have printed so far.
205
200
let mut i = 0 ;
206
- for word in & self . data {
207
- let mut v = * word;
208
- for _ in 0 ..WORD_BYTES { // for each byte in `v `:
201
+ for word in & self . words {
202
+ let mut word = * word;
203
+ for _ in 0 ..WORD_BYTES { // for each byte in `word `:
209
204
let remain = bits - i;
210
205
// If less than a byte remains, then mask just that many bits.
211
206
let mask = if remain <= 8 { ( 1 << remain) - 1 } else { 0xFF } ;
212
207
assert ! ( mask <= 0xFF ) ;
213
- let byte = v & mask;
208
+ let byte = word & mask;
214
209
215
210
result. push_str ( & format ! ( "{}{:02x}" , sep, byte) ) ;
216
211
217
212
if remain <= 8 { break ; }
218
- v >>= 8 ;
213
+ word >>= 8 ;
219
214
i += 8 ;
220
215
sep = '-' ;
221
216
}
@@ -243,13 +238,13 @@ pub trait SubtractFromBitSet<T: Idx> {
243
238
244
239
impl < T : Idx > UnionIntoBitSet < T > for BitSet < T > {
245
240
fn union_into ( & self , other : & mut BitSet < T > ) -> bool {
246
- bitwise ( other. words_mut ( ) , self . words ( ) , |a, b| { a | b } )
241
+ bitwise ( & mut other. words , & self . words , |a, b| { a | b } )
247
242
}
248
243
}
249
244
250
245
impl < T : Idx > SubtractFromBitSet < T > for BitSet < T > {
251
246
fn subtract_from ( & self , other : & mut BitSet < T > ) -> bool {
252
- bitwise ( other. words_mut ( ) , self . words ( ) , |a, b| { a & !b } )
247
+ bitwise ( & mut other. words , & self . words , |a, b| { a & !b } )
253
248
}
254
249
}
255
250
@@ -263,15 +258,15 @@ impl<T: Idx> fmt::Debug for BitSet<T> {
263
258
264
259
impl < T : Idx > rustc_serialize:: Encodable for BitSet < T > {
265
260
fn encode < E : rustc_serialize:: Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , E :: Error > {
266
- self . data . encode ( encoder)
261
+ self . words . encode ( encoder)
267
262
}
268
263
}
269
264
270
265
impl < T : Idx > rustc_serialize:: Decodable for BitSet < T > {
271
266
fn decode < D : rustc_serialize:: Decoder > ( d : & mut D ) -> Result < BitSet < T > , D :: Error > {
272
267
let words: Vec < Word > = rustc_serialize:: Decodable :: decode ( d) ?;
273
268
Ok ( BitSet {
274
- data : words,
269
+ words,
275
270
marker : PhantomData ,
276
271
} )
277
272
}
@@ -539,8 +534,8 @@ pub struct GrowableBitSet<T: Idx> {
539
534
impl < T : Idx > GrowableBitSet < T > {
540
535
pub fn grow ( & mut self , domain_size : T ) {
541
536
let num_words = num_words ( domain_size) ;
542
- if self . bit_set . data . len ( ) <= num_words {
543
- self . bit_set . data . resize ( num_words + 1 , 0 )
537
+ if self . bit_set . words . len ( ) <= num_words {
538
+ self . bit_set . words . resize ( num_words + 1 , 0 )
544
539
}
545
540
}
546
541
@@ -561,8 +556,8 @@ impl<T: Idx> GrowableBitSet<T> {
561
556
562
557
#[ inline]
563
558
pub fn contains ( & self , bit : T ) -> bool {
564
- let ( word , mask) = word_mask ( bit) ;
565
- if let Some ( word) = self . bit_set . data . get ( word ) {
559
+ let ( word_index , mask) = word_index_and_mask ( bit) ;
560
+ if let Some ( word) = self . bit_set . words . get ( word_index ) {
566
561
( word & mask) != 0
567
562
} else {
568
563
false
@@ -578,7 +573,7 @@ impl<T: Idx> GrowableBitSet<T> {
578
573
#[ derive( Clone , Debug ) ]
579
574
pub struct BitMatrix < R : Idx , C : Idx > {
580
575
columns : usize ,
581
- vector : Vec < Word > ,
576
+ words : Vec < Word > ,
582
577
marker : PhantomData < ( R , C ) > ,
583
578
}
584
579
@@ -590,7 +585,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
590
585
let words_per_row = num_words ( columns) ;
591
586
BitMatrix {
592
587
columns,
593
- vector : vec ! [ 0 ; rows * words_per_row] ,
588
+ words : vec ! [ 0 ; rows * words_per_row] ,
594
589
marker : PhantomData ,
595
590
}
596
591
}
@@ -609,12 +604,12 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
609
604
/// Returns true if this changed the matrix, and false otherwise.
610
605
pub fn insert ( & mut self , row : R , column : R ) -> bool {
611
606
let ( start, _) = self . range ( row) ;
612
- let ( word , mask) = word_mask ( column) ;
613
- let vector = & mut self . vector [ ..] ;
614
- let v1 = vector [ start + word ] ;
615
- let v2 = v1 | mask;
616
- vector [ start + word ] = v2 ;
617
- v1 != v2
607
+ let ( word_index , mask) = word_index_and_mask ( column) ;
608
+ let words = & mut self . words [ ..] ;
609
+ let word = words [ start + word_index ] ;
610
+ let new_word = word | mask;
611
+ words [ start + word_index ] = new_word ;
612
+ word != new_word
618
613
}
619
614
620
615
/// Do the bits from `row` contain `column`? Put another way, is
@@ -623,8 +618,8 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
623
618
/// `row` reach `column`?
624
619
pub fn contains ( & self , row : R , column : R ) -> bool {
625
620
let ( start, _) = self . range ( row) ;
626
- let ( word , mask) = word_mask ( column) ;
627
- ( self . vector [ start + word ] & mask) != 0
621
+ let ( word_index , mask) = word_index_and_mask ( column) ;
622
+ ( self . words [ start + word_index ] & mask) != 0
628
623
}
629
624
630
625
/// Returns those indices that are true in rows `a` and `b`. This
@@ -636,7 +631,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
636
631
let ( b_start, b_end) = self . range ( b) ;
637
632
let mut result = Vec :: with_capacity ( self . columns ) ;
638
633
for ( base, ( i, j) ) in ( a_start..a_end) . zip ( b_start..b_end) . enumerate ( ) {
639
- let mut v = self . vector [ i] & self . vector [ j] ;
634
+ let mut v = self . words [ i] & self . words [ j] ;
640
635
for bit in 0 ..WORD_BITS {
641
636
if v == 0 {
642
637
break ;
@@ -660,13 +655,13 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
660
655
pub fn union_rows ( & mut self , read : R , write : R ) -> bool {
661
656
let ( read_start, read_end) = self . range ( read) ;
662
657
let ( write_start, write_end) = self . range ( write) ;
663
- let vector = & mut self . vector [ ..] ;
658
+ let words = & mut self . words [ ..] ;
664
659
let mut changed = false ;
665
660
for ( read_index, write_index) in ( read_start..read_end) . zip ( write_start..write_end) {
666
- let v1 = vector [ write_index] ;
667
- let v2 = v1 | vector [ read_index] ;
668
- vector [ write_index] = v2 ;
669
- changed |= v1 != v2 ;
661
+ let word = words [ write_index] ;
662
+ let new_word = word | words [ read_index] ;
663
+ words [ write_index] = new_word ;
664
+ changed |= word != new_word ;
670
665
}
671
666
changed
672
667
}
@@ -677,7 +672,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
677
672
let ( start, end) = self . range ( row) ;
678
673
BitIter {
679
674
cur : None ,
680
- iter : self . vector [ start..end] . iter ( ) . enumerate ( ) ,
675
+ iter : self . words [ start..end] . iter ( ) . enumerate ( ) ,
681
676
marker : PhantomData ,
682
677
}
683
678
}
@@ -795,11 +790,11 @@ fn num_words<T: Idx>(elements: T) -> usize {
795
790
}
796
791
797
792
#[ inline]
798
- fn word_mask < T : Idx > ( index : T ) -> ( usize , Word ) {
793
+ fn word_index_and_mask < T : Idx > ( index : T ) -> ( usize , Word ) {
799
794
let index = index. index ( ) ;
800
- let word = index / WORD_BITS ;
795
+ let word_index = index / WORD_BITS ;
801
796
let mask = 1 << ( index % WORD_BITS ) ;
802
- ( word , mask)
797
+ ( word_index , mask)
803
798
}
804
799
805
800
#[ test]
0 commit comments