@@ -247,6 +247,8 @@ impl<T: Idx> BitRelations<BitSet<T>> for BitSet<T> {
247247 }
248248}
249249
250+ // Applies a function to mutate a bitset, and returns true if any
251+ // of the applications return true
250252fn sequential_update < T : Idx > (
251253 mut self_update : impl FnMut ( T ) -> bool ,
252254 it : impl Iterator < Item = T > ,
@@ -258,6 +260,8 @@ fn sequential_update<T: Idx>(
258260 changed
259261}
260262
263+ // Optimization of intersection for SparseBitSet that's generic
264+ // over the RHS
261265fn sparse_intersect < T : Idx > (
262266 set : & mut SparseBitSet < T > ,
263267 other_contains : impl Fn ( & T ) -> bool ,
@@ -267,6 +271,10 @@ fn sparse_intersect<T: Idx>(
267271 set. elems . len ( ) != size
268272}
269273
274+ // Optimization of dense/sparse intersection. The resulting set is
275+ // guaranteed to be at most the size of the sparse set, and hence can be
276+ // represented as a sparse set. Therefore the sparse set is copied and filtered,
277+ // then returned as the new set.
270278fn dense_sparse_intersect < T : Idx > (
271279 dense : & BitSet < T > ,
272280 sparse : & SparseBitSet < T > ,
@@ -303,6 +311,10 @@ impl<T: Idx> BitRelations<HybridBitSet<T>> for BitSet<T> {
303311 match other {
304312 HybridBitSet :: Sparse ( sparse) => {
305313 let ( updated, changed) = dense_sparse_intersect ( self , sparse) ;
314+
315+ // We can't directly assign the BitSet to the SparseBitSet, and
316+ // doing `*self = updated.to_dense()` would cause a drop / reallocation. Instead,
317+ // the BitSet is cleared and `updated` is copied into `self`.
306318 self . clear ( ) ;
307319 for elem in updated. iter ( ) {
308320 self . insert ( * elem) ;
0 commit comments