@@ -28,7 +28,7 @@ pub const WORD_BITS: usize = WORD_BYTES * 8;
2828/// just be `usize`.
2929#[ derive( Clone , Eq , PartialEq ) ]
3030pub struct BitSet < T : Idx > {
31- data : Vec < Word > ,
31+ words : Vec < Word > ,
3232 marker : PhantomData < T > ,
3333}
3434
@@ -37,7 +37,7 @@ impl<T: Idx> BitSet<T> {
3737 pub fn new_empty ( domain_size : usize ) -> BitSet < T > {
3838 let num_words = num_words ( domain_size) ;
3939 BitSet {
40- data : vec ! [ 0 ; num_words] ,
40+ words : vec ! [ 0 ; num_words] ,
4141 marker : PhantomData ,
4242 }
4343 }
@@ -46,7 +46,7 @@ impl<T: Idx> BitSet<T> {
4646 pub fn new_filled ( domain_size : usize ) -> BitSet < T > {
4747 let num_words = num_words ( domain_size) ;
4848 let mut result = BitSet {
49- data : vec ! [ !0 ; num_words] ,
49+ words : vec ! [ !0 ; num_words] ,
5050 marker : PhantomData ,
5151 } ;
5252 result. clear_above ( domain_size) ;
@@ -55,14 +55,14 @@ impl<T: Idx> BitSet<T> {
5555
5656 #[ inline]
5757 pub fn clear ( & mut self ) {
58- for word in & mut self . data {
58+ for word in & mut self . words {
5959 * word = 0 ;
6060 }
6161 }
6262
6363 /// Sets all elements up to and including `size`.
6464 pub fn set_up_to ( & mut self , bit : usize ) {
65- for word in & mut self . data {
65+ for word in & mut self . words {
6666 * word = !0 ;
6767 }
6868 self . clear_above ( bit) ;
@@ -72,14 +72,14 @@ impl<T: Idx> BitSet<T> {
7272 fn clear_above ( & mut self , bit : usize ) {
7373 let first_clear_block = bit / WORD_BITS ;
7474
75- if first_clear_block < self . data . len ( ) {
75+ if first_clear_block < self . words . len ( ) {
7676 // Within `first_clear_block`, the `bit % WORD_BITS` LSBs should
7777 // remain.
7878 let mask = ( 1 << ( bit % WORD_BITS ) ) - 1 ;
79- self . data [ first_clear_block] &= mask;
79+ self . words [ first_clear_block] &= mask;
8080
8181 // 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 ..] {
8383 * word = 0 ;
8484 }
8585 }
@@ -88,63 +88,63 @@ impl<T: Idx> BitSet<T> {
8888 /// Efficiently overwrite `self` with `other`. Panics if `self` and `other`
8989 /// don't have the same length.
9090 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 ) ;
9292 }
9393
9494 /// Count the number of set bits in the set.
9595 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 ( )
9797 }
9898
9999 /// True if `self` contains the bit `bit`.
100100 #[ inline]
101101 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
104104 }
105105
106106 /// True if `self` is a (non-strict) superset of `other`.
107107 ///
108- /// The two vectors must have the same length .
108+ /// The two sets must have the same domain_size .
109109 #[ inline]
110110 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)
113113 }
114114
115115 /// Is the set empty?
116116 #[ inline]
117117 pub fn is_empty ( & self ) -> bool {
118- self . data . iter ( ) . all ( |a| * a == 0 )
118+ self . words . iter ( ) . all ( |a| * a == 0 )
119119 }
120120
121121 /// Insert a bit. Returns true if the bit has changed.
122122 #[ inline]
123123 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
130130 }
131131
132132 /// Sets all bits to true.
133133 pub fn insert_all ( & mut self ) {
134- for word in & mut self . data {
134+ for word in & mut self . words {
135135 * word = !0 ;
136136 }
137137 }
138138
139139 /// Returns true if the bit has changed.
140140 #[ inline]
141141 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
148148 }
149149
150150 /// Set `self = self | other` and return true if `self` changed
@@ -162,25 +162,20 @@ impl<T: Idx> BitSet<T> {
162162 /// Set `self = self & other` and return true if `self` changed.
163163 /// (i.e., if any bits were removed).
164164 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 } )
166166 }
167167
168168 /// Get a slice of the underlying words.
169169 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
176171 }
177172
178173 /// Iterates over the indices of set bits in a sorted order.
179174 #[ inline]
180175 pub fn iter < ' a > ( & ' a self ) -> BitIter < ' a , T > {
181176 BitIter {
182177 cur : None ,
183- iter : self . data . iter ( ) . enumerate ( ) ,
178+ iter : self . words . iter ( ) . enumerate ( ) ,
184179 marker : PhantomData ,
185180 }
186181 }
@@ -189,7 +184,7 @@ impl<T: Idx> BitSet<T> {
189184 pub fn to_hybrid ( & self ) -> HybridBitSet < T > {
190185 // This domain_size may be slightly larger than the one specified
191186 // 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 ;
193188
194189 // Note: we currently don't bother trying to make a Sparse set.
195190 HybridBitSet :: Dense ( self . to_owned ( ) , domain_size)
@@ -203,19 +198,19 @@ impl<T: Idx> BitSet<T> {
203198
204199 // i tracks how many bits we have printed so far.
205200 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 `:
209204 let remain = bits - i;
210205 // If less than a byte remains, then mask just that many bits.
211206 let mask = if remain <= 8 { ( 1 << remain) - 1 } else { 0xFF } ;
212207 assert ! ( mask <= 0xFF ) ;
213- let byte = v & mask;
208+ let byte = word & mask;
214209
215210 result. push_str ( & format ! ( "{}{:02x}" , sep, byte) ) ;
216211
217212 if remain <= 8 { break ; }
218- v >>= 8 ;
213+ word >>= 8 ;
219214 i += 8 ;
220215 sep = '-' ;
221216 }
@@ -243,13 +238,13 @@ pub trait SubtractFromBitSet<T: Idx> {
243238
244239impl < T : Idx > UnionIntoBitSet < T > for BitSet < T > {
245240 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 } )
247242 }
248243}
249244
250245impl < T : Idx > SubtractFromBitSet < T > for BitSet < T > {
251246 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 } )
253248 }
254249}
255250
@@ -263,15 +258,15 @@ impl<T: Idx> fmt::Debug for BitSet<T> {
263258
264259impl < T : Idx > rustc_serialize:: Encodable for BitSet < T > {
265260 fn encode < E : rustc_serialize:: Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , E :: Error > {
266- self . data . encode ( encoder)
261+ self . words . encode ( encoder)
267262 }
268263}
269264
270265impl < T : Idx > rustc_serialize:: Decodable for BitSet < T > {
271266 fn decode < D : rustc_serialize:: Decoder > ( d : & mut D ) -> Result < BitSet < T > , D :: Error > {
272267 let words: Vec < Word > = rustc_serialize:: Decodable :: decode ( d) ?;
273268 Ok ( BitSet {
274- data : words,
269+ words,
275270 marker : PhantomData ,
276271 } )
277272 }
@@ -539,8 +534,8 @@ pub struct GrowableBitSet<T: Idx> {
539534impl < T : Idx > GrowableBitSet < T > {
540535 pub fn grow ( & mut self , domain_size : T ) {
541536 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 )
544539 }
545540 }
546541
@@ -561,8 +556,8 @@ impl<T: Idx> GrowableBitSet<T> {
561556
562557 #[ inline]
563558 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 ) {
566561 ( word & mask) != 0
567562 } else {
568563 false
@@ -578,7 +573,7 @@ impl<T: Idx> GrowableBitSet<T> {
578573#[ derive( Clone , Debug ) ]
579574pub struct BitMatrix < R : Idx , C : Idx > {
580575 columns : usize ,
581- vector : Vec < Word > ,
576+ words : Vec < Word > ,
582577 marker : PhantomData < ( R , C ) > ,
583578}
584579
@@ -590,7 +585,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
590585 let words_per_row = num_words ( columns) ;
591586 BitMatrix {
592587 columns,
593- vector : vec ! [ 0 ; rows * words_per_row] ,
588+ words : vec ! [ 0 ; rows * words_per_row] ,
594589 marker : PhantomData ,
595590 }
596591 }
@@ -609,12 +604,12 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
609604 /// Returns true if this changed the matrix, and false otherwise.
610605 pub fn insert ( & mut self , row : R , column : R ) -> bool {
611606 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
618613 }
619614
620615 /// Do the bits from `row` contain `column`? Put another way, is
@@ -623,8 +618,8 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
623618 /// `row` reach `column`?
624619 pub fn contains ( & self , row : R , column : R ) -> bool {
625620 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
628623 }
629624
630625 /// Returns those indices that are true in rows `a` and `b`. This
@@ -636,7 +631,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
636631 let ( b_start, b_end) = self . range ( b) ;
637632 let mut result = Vec :: with_capacity ( self . columns ) ;
638633 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] ;
640635 for bit in 0 ..WORD_BITS {
641636 if v == 0 {
642637 break ;
@@ -660,13 +655,13 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
660655 pub fn union_rows ( & mut self , read : R , write : R ) -> bool {
661656 let ( read_start, read_end) = self . range ( read) ;
662657 let ( write_start, write_end) = self . range ( write) ;
663- let vector = & mut self . vector [ ..] ;
658+ let words = & mut self . words [ ..] ;
664659 let mut changed = false ;
665660 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 ;
670665 }
671666 changed
672667 }
@@ -677,7 +672,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
677672 let ( start, end) = self . range ( row) ;
678673 BitIter {
679674 cur : None ,
680- iter : self . vector [ start..end] . iter ( ) . enumerate ( ) ,
675+ iter : self . words [ start..end] . iter ( ) . enumerate ( ) ,
681676 marker : PhantomData ,
682677 }
683678 }
@@ -795,11 +790,11 @@ fn num_words<T: Idx>(elements: T) -> usize {
795790}
796791
797792#[ inline]
798- fn word_mask < T : Idx > ( index : T ) -> ( usize , Word ) {
793+ fn word_index_and_mask < T : Idx > ( index : T ) -> ( usize , Word ) {
799794 let index = index. index ( ) ;
800- let word = index / WORD_BITS ;
795+ let word_index = index / WORD_BITS ;
801796 let mask = 1 << ( index % WORD_BITS ) ;
802- ( word , mask)
797+ ( word_index , mask)
803798}
804799
805800#[ test]
0 commit comments