@@ -19,6 +19,20 @@ use bitslice::{bitwise, Union, Subtract, Intersect};
1919use indexed_vec:: Idx ;
2020use rustc_serialize;
2121
22+ /// This is implemented by all the index sets so that IdxSet::union() can be
23+ /// passed any type of index set.
24+ pub trait UnionIntoIdxSet < T : Idx > {
25+ // Performs `other = other | self`.
26+ fn union_into ( & self , other : & mut IdxSet < T > ) -> bool ;
27+ }
28+
29+ /// This is implemented by all the index sets so that IdxSet::subtract() can be
30+ /// passed any type of index set.
31+ pub trait SubtractFromIdxSet < T : Idx > {
32+ // Performs `other = other - self`.
33+ fn subtract_from ( & self , other : & mut IdxSet < T > ) -> bool ;
34+ }
35+
2236/// Represents a set of some element type E, where each E is identified by some
2337/// unique index type `T`.
2438///
@@ -164,48 +178,14 @@ impl<T: Idx> IdxSet<T> {
164178
165179 /// Set `self = self | other` and return true if `self` changed
166180 /// (i.e., if new bits were added).
167- pub fn union ( & mut self , other : & IdxSet < T > ) -> bool {
168- bitwise ( self . words_mut ( ) , other. words ( ) , & Union )
169- }
170-
171- /// Like `union()`, but takes a `SparseIdxSet` argument.
172- fn union_sparse ( & mut self , other : & SparseIdxSet < T > ) -> bool {
173- let mut changed = false ;
174- for elem in other. iter ( ) {
175- changed |= self . add ( & elem) ;
176- }
177- changed
178- }
179-
180- /// Like `union()`, but takes a `HybridIdxSet` argument.
181- pub fn union_hybrid ( & mut self , other : & HybridIdxSet < T > ) -> bool {
182- match other {
183- HybridIdxSet :: Sparse ( sparse, _) => self . union_sparse ( sparse) ,
184- HybridIdxSet :: Dense ( dense, _) => self . union ( dense) ,
185- }
181+ pub fn union ( & mut self , other : & impl UnionIntoIdxSet < T > ) -> bool {
182+ other. union_into ( self )
186183 }
187184
188185 /// Set `self = self - other` and return true if `self` changed.
189186 /// (i.e., if any bits were removed).
190- pub fn subtract ( & mut self , other : & IdxSet < T > ) -> bool {
191- bitwise ( self . words_mut ( ) , other. words ( ) , & Subtract )
192- }
193-
194- /// Like `subtract()`, but takes a `SparseIdxSet` argument.
195- fn subtract_sparse ( & mut self , other : & SparseIdxSet < T > ) -> bool {
196- let mut changed = false ;
197- for elem in other. iter ( ) {
198- changed |= self . remove ( & elem) ;
199- }
200- changed
201- }
202-
203- /// Like `subtract()`, but takes a `HybridIdxSet` argument.
204- pub fn subtract_hybrid ( & mut self , other : & HybridIdxSet < T > ) -> bool {
205- match other {
206- HybridIdxSet :: Sparse ( sparse, _) => self . subtract_sparse ( sparse) ,
207- HybridIdxSet :: Dense ( dense, _) => self . subtract ( dense) ,
208- }
187+ pub fn subtract ( & mut self , other : & impl SubtractFromIdxSet < T > ) -> bool {
188+ other. subtract_from ( self )
209189 }
210190
211191 /// Set `self = self & other` and return true if `self` changed.
@@ -223,6 +203,18 @@ impl<T: Idx> IdxSet<T> {
223203 }
224204}
225205
206+ impl < T : Idx > UnionIntoIdxSet < T > for IdxSet < T > {
207+ fn union_into ( & self , other : & mut IdxSet < T > ) -> bool {
208+ bitwise ( other. words_mut ( ) , self . words ( ) , & Union )
209+ }
210+ }
211+
212+ impl < T : Idx > SubtractFromIdxSet < T > for IdxSet < T > {
213+ fn subtract_from ( & self , other : & mut IdxSet < T > ) -> bool {
214+ bitwise ( other. words_mut ( ) , self . words ( ) , & Subtract )
215+ }
216+ }
217+
226218pub struct Iter < ' a , T : Idx > {
227219 cur : Option < ( Word , usize ) > ,
228220 iter : iter:: Enumerate < slice:: Iter < ' a , Word > > ,
@@ -308,6 +300,26 @@ impl<T: Idx> SparseIdxSet<T> {
308300 }
309301}
310302
303+ impl < T : Idx > UnionIntoIdxSet < T > for SparseIdxSet < T > {
304+ fn union_into ( & self , other : & mut IdxSet < T > ) -> bool {
305+ let mut changed = false ;
306+ for elem in self . iter ( ) {
307+ changed |= other. add ( & elem) ;
308+ }
309+ changed
310+ }
311+ }
312+
313+ impl < T : Idx > SubtractFromIdxSet < T > for SparseIdxSet < T > {
314+ fn subtract_from ( & self , other : & mut IdxSet < T > ) -> bool {
315+ let mut changed = false ;
316+ for elem in self . iter ( ) {
317+ changed |= other. remove ( & elem) ;
318+ }
319+ changed
320+ }
321+ }
322+
311323pub struct SparseIter < ' a , T : Idx > {
312324 iter : slice:: Iter < ' a , T > ,
313325}
@@ -411,6 +423,24 @@ impl<T: Idx> HybridIdxSet<T> {
411423 }
412424}
413425
426+ impl < T : Idx > UnionIntoIdxSet < T > for HybridIdxSet < T > {
427+ fn union_into ( & self , other : & mut IdxSet < T > ) -> bool {
428+ match self {
429+ HybridIdxSet :: Sparse ( sparse, _) => sparse. union_into ( other) ,
430+ HybridIdxSet :: Dense ( dense, _) => dense. union_into ( other) ,
431+ }
432+ }
433+ }
434+
435+ impl < T : Idx > SubtractFromIdxSet < T > for HybridIdxSet < T > {
436+ fn subtract_from ( & self , other : & mut IdxSet < T > ) -> bool {
437+ match self {
438+ HybridIdxSet :: Sparse ( sparse, _) => sparse. subtract_from ( other) ,
439+ HybridIdxSet :: Dense ( dense, _) => dense. subtract_from ( other) ,
440+ }
441+ }
442+ }
443+
414444pub enum HybridIter < ' a , T : Idx > {
415445 Sparse ( SparseIter < ' a , T > ) ,
416446 Dense ( Iter < ' a , T > ) ,
0 commit comments