@@ -34,12 +34,6 @@ const CHUNK_BITS: usize = CHUNK_WORDS * WORD_BITS; // 2048 bits
3434type ChunkSize = u16 ;
3535const _: ( ) = assert ! ( CHUNK_BITS <= ChunkSize :: MAX as usize ) ;
3636
37- pub trait BitRelations < Rhs > {
38- fn union ( & mut self , other : & Rhs ) -> bool ;
39- fn subtract ( & mut self , other : & Rhs ) -> bool ;
40- fn intersect ( & mut self , other : & Rhs ) -> bool ;
41- }
42-
4337#[ inline]
4438fn inclusive_start_end < T : Idx > (
4539 range : impl RangeBounds < T > ,
@@ -63,37 +57,6 @@ fn inclusive_start_end<T: Idx>(
6357 Some ( ( start, end) )
6458}
6559
66- macro_rules! bit_relations_inherent_impls {
67- ( ) => {
68- /// Sets `self = self | other` and returns `true` if `self` changed
69- /// (i.e., if new bits were added).
70- pub fn union <Rhs >( & mut self , other: & Rhs ) -> bool
71- where
72- Self : BitRelations <Rhs >,
73- {
74- <Self as BitRelations <Rhs >>:: union ( self , other)
75- }
76-
77- /// Sets `self = self - other` and returns `true` if `self` changed.
78- /// (i.e., if any bits were removed).
79- pub fn subtract<Rhs >( & mut self , other: & Rhs ) -> bool
80- where
81- Self : BitRelations <Rhs >,
82- {
83- <Self as BitRelations <Rhs >>:: subtract( self , other)
84- }
85-
86- /// Sets `self = self & other` and return `true` if `self` changed.
87- /// (i.e., if any bits were removed).
88- pub fn intersect<Rhs >( & mut self , other: & Rhs ) -> bool
89- where
90- Self : BitRelations <Rhs >,
91- {
92- <Self as BitRelations <Rhs >>:: intersect( self , other)
93- }
94- } ;
95- }
96-
9760/// A fixed-size bitset type with a dense representation.
9861///
9962/// Note 1: Since this bitset is dense, if your domain is big, and/or relatively
@@ -320,12 +283,7 @@ impl<T: Idx> DenseBitSet<T> {
320283 None
321284 }
322285
323- bit_relations_inherent_impls ! { }
324-
325286 /// Sets `self = self | !other`.
326- ///
327- /// FIXME: Incorporate this into [`BitRelations`] and fill out
328- /// implementations for other bitset types, if needed.
329287 pub fn union_not ( & mut self , other : & DenseBitSet < T > ) {
330288 assert_eq ! ( self . domain_size, other. domain_size) ;
331289
@@ -339,21 +297,21 @@ impl<T: Idx> DenseBitSet<T> {
339297 // out-of-domain bits, so we need to clear them.
340298 self . clear_excess_bits ( ) ;
341299 }
342- }
343300
344- // dense REL dense
345- impl < T : Idx > BitRelations < DenseBitSet < T > > for DenseBitSet < T > {
346- fn union ( & mut self , other : & DenseBitSet < T > ) -> bool {
301+ /// Returns true if `self` was modified.
302+ pub fn union ( & mut self , other : & DenseBitSet < T > ) -> bool {
347303 assert_eq ! ( self . domain_size, other. domain_size) ;
348304 update_words ( & mut self . words , & other. words , |a, b| a | b)
349305 }
350306
351- fn subtract ( & mut self , other : & DenseBitSet < T > ) -> bool {
307+ /// Returns true if `self` was modified.
308+ pub fn subtract ( & mut self , other : & DenseBitSet < T > ) -> bool {
352309 assert_eq ! ( self . domain_size, other. domain_size) ;
353310 update_words ( & mut self . words , & other. words , |a, b| a & !b)
354311 }
355312
356- fn intersect ( & mut self , other : & DenseBitSet < T > ) -> bool {
313+ /// Returns true if `self` was modified.
314+ pub fn intersect ( & mut self , other : & DenseBitSet < T > ) -> bool {
357315 assert_eq ! ( self . domain_size, other. domain_size) ;
358316 update_words ( & mut self . words , & other. words , |a, b| a & b)
359317 }
@@ -761,10 +719,7 @@ impl<T: Idx> ChunkedBitSet<T> {
761719 }
762720 }
763721
764- bit_relations_inherent_impls ! { }
765- }
766-
767- impl < T : Idx > BitRelations < ChunkedBitSet < T > > for ChunkedBitSet < T > {
722+ /// Returns true if `self` was modified.
768723 fn union ( & mut self , other : & ChunkedBitSet < T > ) -> bool {
769724 assert_eq ! ( self . domain_size, other. domain_size) ;
770725
@@ -830,6 +785,7 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
830785 changed
831786 }
832787
788+ /// Returns true if `self` was modified.
833789 fn subtract ( & mut self , other : & ChunkedBitSet < T > ) -> bool {
834790 assert_eq ! ( self . domain_size, other. domain_size) ;
835791
@@ -906,56 +862,6 @@ impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
906862 }
907863 changed
908864 }
909-
910- fn intersect ( & mut self , other : & ChunkedBitSet < T > ) -> bool {
911- assert_eq ! ( self . domain_size, other. domain_size) ;
912-
913- let mut changed = false ;
914- for ( mut self_chunk, other_chunk) in self . chunks . iter_mut ( ) . zip ( other. chunks . iter ( ) ) {
915- match ( & mut self_chunk, & other_chunk) {
916- ( Zeros { .. } , _) | ( _, Ones { .. } ) => { }
917- ( Ones { .. } , Zeros { .. } | Mixed { .. } ) | ( Mixed { .. } , Zeros { .. } ) => {
918- changed = true ;
919- * self_chunk = other_chunk. clone ( ) ;
920- }
921- (
922- Mixed {
923- chunk_domain_size,
924- ones_count : self_chunk_ones_count,
925- words : self_chunk_words,
926- } ,
927- Mixed { words : other_chunk_words, .. } ,
928- ) => {
929- // See `ChunkedBitSet::union` for details on what is happening here.
930- let num_words = num_words ( * chunk_domain_size as usize ) ;
931- let op = |a, b| a & b;
932- if !would_modify_words (
933- & self_chunk_words[ 0 ..num_words] ,
934- & other_chunk_words[ 0 ..num_words] ,
935- op,
936- ) {
937- continue ;
938- }
939-
940- let self_chunk_words = Rc :: make_mut ( self_chunk_words) ;
941- let has_changed = update_words (
942- & mut self_chunk_words[ 0 ..num_words] ,
943- & other_chunk_words[ 0 ..num_words] ,
944- op,
945- ) ;
946- debug_assert ! ( has_changed) ;
947- * self_chunk_ones_count =
948- count_ones ( & self_chunk_words[ 0 ..num_words] ) as ChunkSize ;
949- if * self_chunk_ones_count == 0 {
950- * self_chunk = Zeros { chunk_domain_size : * chunk_domain_size } ;
951- }
952- changed = true ;
953- }
954- }
955- }
956-
957- changed
958- }
959865}
960866
961867impl < T > Clone for ChunkedBitSet < T > {
@@ -1225,7 +1131,23 @@ impl<T: Idx> MixedBitSet<T> {
12251131 }
12261132 }
12271133
1228- bit_relations_inherent_impls ! { }
1134+ /// Returns true if `self` was modified.
1135+ pub fn union ( & mut self , other : & MixedBitSet < T > ) -> bool {
1136+ match ( self , other) {
1137+ ( MixedBitSet :: Small ( set) , MixedBitSet :: Small ( other) ) => set. union ( other) ,
1138+ ( MixedBitSet :: Large ( set) , MixedBitSet :: Large ( other) ) => set. union ( other) ,
1139+ _ => panic ! ( "MixedBitSet size mismatch" ) ,
1140+ }
1141+ }
1142+
1143+ /// Returns true if `self` was modified.
1144+ pub fn subtract ( & mut self , other : & MixedBitSet < T > ) -> bool {
1145+ match ( self , other) {
1146+ ( MixedBitSet :: Small ( set) , MixedBitSet :: Small ( other) ) => set. subtract ( other) ,
1147+ ( MixedBitSet :: Large ( set) , MixedBitSet :: Large ( other) ) => set. subtract ( other) ,
1148+ _ => panic ! ( "MixedBitSet size mismatch" ) ,
1149+ }
1150+ }
12291151}
12301152
12311153impl < T > Clone for MixedBitSet < T > {
@@ -1249,28 +1171,6 @@ impl<T> Clone for MixedBitSet<T> {
12491171 }
12501172}
12511173
1252- impl < T : Idx > BitRelations < MixedBitSet < T > > for MixedBitSet < T > {
1253- fn union ( & mut self , other : & MixedBitSet < T > ) -> bool {
1254- match ( self , other) {
1255- ( MixedBitSet :: Small ( set) , MixedBitSet :: Small ( other) ) => set. union ( other) ,
1256- ( MixedBitSet :: Large ( set) , MixedBitSet :: Large ( other) ) => set. union ( other) ,
1257- _ => panic ! ( "MixedBitSet size mismatch" ) ,
1258- }
1259- }
1260-
1261- fn subtract ( & mut self , other : & MixedBitSet < T > ) -> bool {
1262- match ( self , other) {
1263- ( MixedBitSet :: Small ( set) , MixedBitSet :: Small ( other) ) => set. subtract ( other) ,
1264- ( MixedBitSet :: Large ( set) , MixedBitSet :: Large ( other) ) => set. subtract ( other) ,
1265- _ => panic ! ( "MixedBitSet size mismatch" ) ,
1266- }
1267- }
1268-
1269- fn intersect ( & mut self , _other : & MixedBitSet < T > ) -> bool {
1270- unimplemented ! ( "implement if/when necessary" ) ;
1271- }
1272- }
1273-
12741174impl < T : Idx > fmt:: Debug for MixedBitSet < T > {
12751175 fn fmt ( & self , w : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
12761176 match self {
@@ -1701,45 +1601,6 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
17011601 pub fn row ( & self , row : R ) -> Option < & DenseBitSet < C > > {
17021602 self . rows . get ( row) ?. as_ref ( )
17031603 }
1704-
1705- /// Intersects `row` with `set`. `set` can be either `DenseBitSet` or
1706- /// `ChunkedBitSet`. Has no effect if `row` does not exist.
1707- ///
1708- /// Returns true if the row was changed.
1709- pub fn intersect_row < Set > ( & mut self , row : R , set : & Set ) -> bool
1710- where
1711- DenseBitSet < C > : BitRelations < Set > ,
1712- {
1713- match self . rows . get_mut ( row) {
1714- Some ( Some ( row) ) => row. intersect ( set) ,
1715- _ => false ,
1716- }
1717- }
1718-
1719- /// Subtracts `set` from `row`. `set` can be either `DenseBitSet` or
1720- /// `ChunkedBitSet`. Has no effect if `row` does not exist.
1721- ///
1722- /// Returns true if the row was changed.
1723- pub fn subtract_row < Set > ( & mut self , row : R , set : & Set ) -> bool
1724- where
1725- DenseBitSet < C > : BitRelations < Set > ,
1726- {
1727- match self . rows . get_mut ( row) {
1728- Some ( Some ( row) ) => row. subtract ( set) ,
1729- _ => false ,
1730- }
1731- }
1732-
1733- /// Unions `row` with `set`. `set` can be either `DenseBitSet` or
1734- /// `ChunkedBitSet`.
1735- ///
1736- /// Returns true if the row was changed.
1737- pub fn union_row < Set > ( & mut self , row : R , set : & Set ) -> bool
1738- where
1739- DenseBitSet < C > : BitRelations < Set > ,
1740- {
1741- self . ensure_row ( row) . union ( set)
1742- }
17431604}
17441605
17451606#[ inline]
0 commit comments