8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use std :: collections :: BTreeMap ;
11
+ use indexed_vec :: { Idx , IndexVec } ;
12
12
use std:: collections:: btree_map:: Entry ;
13
- use std:: marker :: PhantomData ;
13
+ use std:: collections :: BTreeMap ;
14
14
use std:: iter:: FromIterator ;
15
- use indexed_vec :: { Idx , IndexVec } ;
15
+ use std :: marker :: PhantomData ;
16
16
17
17
type Word = u128 ;
18
18
const WORD_BITS : usize = 128 ;
@@ -317,14 +317,25 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
317
317
if read != write {
318
318
let ( bit_set_read, bit_set_write) = self . vector . pick2_mut ( read, write) ;
319
319
320
- for read_val in bit_set_read. iter ( ) {
321
- changed = changed | bit_set_write. insert ( read_val ) ;
320
+ for read_chunk in bit_set_read. chunks ( ) {
321
+ changed = changed | bit_set_write. insert_chunk ( read_chunk ) . any ( ) ;
322
322
}
323
323
}
324
324
325
325
changed
326
326
}
327
327
328
+ /// True if `sub` is a subset of `sup`
329
+ pub fn is_subset ( & self , sub : R , sup : R ) -> bool {
330
+ sub == sup || {
331
+ let bit_set_sub = & self . vector [ sub] ;
332
+ let bit_set_sup = & self . vector [ sup] ;
333
+ bit_set_sub
334
+ . chunks ( )
335
+ . all ( |read_chunk| read_chunk. bits_eq ( bit_set_sup. contains_chunk ( read_chunk) ) )
336
+ }
337
+ }
338
+
328
339
/// Iterates through all the columns set to true in a given row of
329
340
/// the matrix.
330
341
pub fn iter < ' a > ( & ' a self , row : R ) -> impl Iterator < Item = C > + ' a {
@@ -346,6 +357,7 @@ pub struct SparseChunk<I> {
346
357
}
347
358
348
359
impl < I : Idx > SparseChunk < I > {
360
+ #[ inline]
349
361
pub fn one ( index : I ) -> Self {
350
362
let index = index. index ( ) ;
351
363
let key_usize = index / 128 ;
@@ -358,10 +370,16 @@ impl<I: Idx> SparseChunk<I> {
358
370
}
359
371
}
360
372
373
+ #[ inline]
361
374
pub fn any ( & self ) -> bool {
362
375
self . bits != 0
363
376
}
364
377
378
+ #[ inline]
379
+ pub fn bits_eq ( & self , other : SparseChunk < I > ) -> bool {
380
+ self . bits == other. bits
381
+ }
382
+
365
383
pub fn iter ( & self ) -> impl Iterator < Item = I > {
366
384
let base = self . key as usize * 128 ;
367
385
let mut bits = self . bits ;
@@ -394,6 +412,10 @@ impl<I: Idx> SparseBitSet<I> {
394
412
self . chunk_bits . len ( ) * 128
395
413
}
396
414
415
+ /// Returns a chunk containing only those bits that are already
416
+ /// present. You can test therefore if `self` contains all the
417
+ /// bits in chunk already by doing `chunk ==
418
+ /// self.contains_chunk(chunk)`.
397
419
pub fn contains_chunk ( & self , chunk : SparseChunk < I > ) -> SparseChunk < I > {
398
420
SparseChunk {
399
421
bits : self . chunk_bits
@@ -403,6 +425,11 @@ impl<I: Idx> SparseBitSet<I> {
403
425
}
404
426
}
405
427
428
+ /// Modifies `self` to contain all the bits from `chunk` (in
429
+ /// addition to any pre-existing bits); returns a new chunk that
430
+ /// contains only those bits that were newly added. You can test
431
+ /// if anything was inserted by invoking `any()` on the returned
432
+ /// value.
406
433
pub fn insert_chunk ( & mut self , chunk : SparseChunk < I > ) -> SparseChunk < I > {
407
434
if chunk. bits == 0 {
408
435
return chunk;
0 commit comments