Skip to main content

rustc_index/
bit_set.rs

1use std::marker::PhantomData;
2#[cfg(not(feature = "nightly"))]
3use std::mem;
4use std::ops::{Bound, Range, RangeBounds};
5use std::rc::Rc;
6use std::{fmt, iter, slice};
7
8use Chunk::*;
9#[cfg(feature = "nightly")]
10use rustc_macros::{Decodable_NoContext, Encodable_NoContext};
11
12use crate::{Idx, IndexVec};
13
14#[cfg(test)]
15mod tests;
16
17type Word = u64;
18const WORD_BYTES: usize = size_of::<Word>();
19const WORD_BITS: usize = WORD_BYTES * 8;
20
21// The choice of chunk size has some trade-offs.
22//
23// A big chunk size tends to favour cases where many large `ChunkedBitSet`s are
24// present, because they require fewer `Chunk`s, reducing the number of
25// allocations and reducing peak memory usage. Also, fewer chunk operations are
26// required, though more of them might be `Mixed`.
27//
28// A small chunk size tends to favour cases where many small `ChunkedBitSet`s
29// are present, because less space is wasted at the end of the final chunk (if
30// it's not full).
31const CHUNK_WORDS: usize = 32;
32const CHUNK_BITS: usize = CHUNK_WORDS * WORD_BITS; // 2048 bits
33
34/// ChunkSize is small to keep `Chunk` small. The static assertion ensures it's
35/// not too small.
36type ChunkSize = u16;
37const _: () = if !(CHUNK_BITS <= ChunkSize::MAX as usize) {
    ::core::panicking::panic("assertion failed: CHUNK_BITS <= ChunkSize::MAX as usize")
}assert!(CHUNK_BITS <= ChunkSize::MAX as usize);
38
39pub trait BitRelations<Rhs> {
40    fn union(&mut self, other: &Rhs) -> bool;
41    fn subtract(&mut self, other: &Rhs) -> bool;
42    fn intersect(&mut self, other: &Rhs) -> bool;
43}
44
45#[inline]
46fn inclusive_start_end<T: Idx>(
47    range: impl RangeBounds<T>,
48    domain: usize,
49) -> Option<(usize, usize)> {
50    // Both start and end are inclusive.
51    let start = match range.start_bound().cloned() {
52        Bound::Included(start) => start.index(),
53        Bound::Excluded(start) => start.index() + 1,
54        Bound::Unbounded => 0,
55    };
56    let end = match range.end_bound().cloned() {
57        Bound::Included(end) => end.index(),
58        Bound::Excluded(end) => end.index().checked_sub(1)?,
59        Bound::Unbounded => domain - 1,
60    };
61    if !(end < domain) {
    ::core::panicking::panic("assertion failed: end < domain")
};assert!(end < domain);
62    if start > end {
63        return None;
64    }
65    Some((start, end))
66}
67
68macro_rules! bit_relations_inherent_impls {
69    () => {
70        /// Sets `self = self | other` and returns `true` if `self` changed
71        /// (i.e., if new bits were added).
72        pub fn union<Rhs>(&mut self, other: &Rhs) -> bool
73        where
74            Self: BitRelations<Rhs>,
75        {
76            <Self as BitRelations<Rhs>>::union(self, other)
77        }
78
79        /// Sets `self = self - other` and returns `true` if `self` changed.
80        /// (i.e., if any bits were removed).
81        pub fn subtract<Rhs>(&mut self, other: &Rhs) -> bool
82        where
83            Self: BitRelations<Rhs>,
84        {
85            <Self as BitRelations<Rhs>>::subtract(self, other)
86        }
87
88        /// Sets `self = self & other` and return `true` if `self` changed.
89        /// (i.e., if any bits were removed).
90        pub fn intersect<Rhs>(&mut self, other: &Rhs) -> bool
91        where
92            Self: BitRelations<Rhs>,
93        {
94            <Self as BitRelations<Rhs>>::intersect(self, other)
95        }
96    };
97}
98
99/// A fixed-size bitset type with a dense representation.
100///
101/// Note 1: Since this bitset is dense, if your domain is big, and/or relatively
102/// homogeneous (for example, with long runs of bits set or unset), then it may
103/// be preferable to instead use a [MixedBitSet], or an
104/// [IntervalSet](crate::interval::IntervalSet). They should be more suited to
105/// sparse, or highly-compressible, domains.
106///
107/// Note 2: Use [`GrowableBitSet`] if you need support for resizing after creation.
108///
109/// `T` is an index type, typically a newtyped `usize` wrapper, but it can also
110/// just be `usize`.
111///
112/// All operations that involve an element will panic if the element is equal
113/// to or greater than the domain size. All operations that involve two bitsets
114/// will panic if the bitsets have differing domain sizes.
115///
116#[cfg_attr(feature = "nightly", derive(const _: () =
    {
        impl<T, __D: ::rustc_serialize::Decoder>
            ::rustc_serialize::Decodable<__D> for DenseBitSet<T> where
            PhantomData<T>: ::rustc_serialize::Decodable<__D> {
            fn decode(__decoder: &mut __D) -> Self {
                DenseBitSet {
                    domain_size: ::rustc_serialize::Decodable::decode(__decoder),
                    words: ::rustc_serialize::Decodable::decode(__decoder),
                    marker: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable_NoContext, const _: () =
    {
        impl<T, __E: ::rustc_serialize::Encoder>
            ::rustc_serialize::Encodable<__E> for DenseBitSet<T> where
            PhantomData<T>: ::rustc_serialize::Encodable<__E> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    DenseBitSet {
                        domain_size: ref __binding_0,
                        words: ref __binding_1,
                        marker: ref __binding_2 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                    }
                }
            }
        }
    };Encodable_NoContext))]
117#[derive(#[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for DenseBitSet<T> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<usize>;
        let _: ::core::cmp::AssertParamIsEq<Vec<Word>>;
        let _: ::core::cmp::AssertParamIsEq<PhantomData<T>>;
    }
}Eq, #[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for DenseBitSet<T> {
    #[inline]
    fn eq(&self, other: &DenseBitSet<T>) -> bool {
        self.domain_size == other.domain_size && self.words == other.words &&
            self.marker == other.marker
    }
}PartialEq, #[automatically_derived]
impl<T: ::core::hash::Hash> ::core::hash::Hash for DenseBitSet<T> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.domain_size, state);
        ::core::hash::Hash::hash(&self.words, state);
        ::core::hash::Hash::hash(&self.marker, state)
    }
}Hash)]
118pub struct DenseBitSet<T> {
119    domain_size: usize,
120    words: Vec<Word>,
121    marker: PhantomData<T>,
122}
123
124impl<T> DenseBitSet<T> {
125    /// Gets the domain size.
126    pub fn domain_size(&self) -> usize {
127        self.domain_size
128    }
129}
130
131impl<T: Idx> DenseBitSet<T> {
132    /// Creates a new, empty bitset with a given `domain_size`.
133    #[inline]
134    pub fn new_empty(domain_size: usize) -> DenseBitSet<T> {
135        let num_words = num_words(domain_size);
136        DenseBitSet { domain_size, words: ::alloc::vec::from_elem(0, num_words)vec![0; num_words], marker: PhantomData }
137    }
138
139    /// Creates a new, filled bitset with a given `domain_size`.
140    #[inline]
141    pub fn new_filled(domain_size: usize) -> DenseBitSet<T> {
142        let num_words = num_words(domain_size);
143        let mut result =
144            DenseBitSet { domain_size, words: ::alloc::vec::from_elem(!0, num_words)vec![!0; num_words], marker: PhantomData };
145        result.clear_excess_bits();
146        result
147    }
148
149    /// Clear all elements.
150    #[inline]
151    pub fn clear(&mut self) {
152        self.words.fill(0);
153    }
154
155    /// Clear excess bits in the final word.
156    fn clear_excess_bits(&mut self) {
157        clear_excess_bits_in_final_word(self.domain_size, &mut self.words);
158    }
159
160    /// Count the number of set bits in the set.
161    pub fn count(&self) -> usize {
162        count_ones(&self.words)
163    }
164
165    /// Returns `true` if `self` contains `elem`.
166    #[inline]
167    pub fn contains(&self, elem: T) -> bool {
168        if !(elem.index() < self.domain_size) {
    ::core::panicking::panic("assertion failed: elem.index() < self.domain_size")
};assert!(elem.index() < self.domain_size);
169        let (word_index, mask) = word_index_and_mask(elem);
170        (self.words[word_index] & mask) != 0
171    }
172
173    /// Is `self` is a (non-strict) superset of `other`?
174    #[inline]
175    pub fn superset(&self, other: &DenseBitSet<T>) -> bool {
176        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
177        self.words.iter().zip(&other.words).all(|(a, b)| (a & b) == *b)
178    }
179
180    /// Is the set empty?
181    #[inline]
182    pub fn is_empty(&self) -> bool {
183        self.words.iter().all(|a| *a == 0)
184    }
185
186    /// Insert `elem`. Returns whether the set has changed.
187    #[inline]
188    pub fn insert(&mut self, elem: T) -> bool {
189        if !(elem.index() < self.domain_size) {
    {
        ::core::panicking::panic_fmt(format_args!("inserting element at index {0} but domain size is {1}",
                elem.index(), self.domain_size));
    }
};assert!(
190            elem.index() < self.domain_size,
191            "inserting element at index {} but domain size is {}",
192            elem.index(),
193            self.domain_size,
194        );
195        let (word_index, mask) = word_index_and_mask(elem);
196        let word_ref = &mut self.words[word_index];
197        let word = *word_ref;
198        let new_word = word | mask;
199        *word_ref = new_word;
200        new_word != word
201    }
202
203    #[inline]
204    pub fn insert_range(&mut self, elems: impl RangeBounds<T>) {
205        let Some((start, end)) = inclusive_start_end(elems, self.domain_size) else {
206            return;
207        };
208
209        let (start_word_index, start_mask) = word_index_and_mask(start);
210        let (end_word_index, end_mask) = word_index_and_mask(end);
211
212        // Set all words in between start and end (exclusively of both).
213        for word_index in (start_word_index + 1)..end_word_index {
214            self.words[word_index] = !0;
215        }
216
217        if start_word_index != end_word_index {
218            // Start and end are in different words, so we handle each in turn.
219            //
220            // We set all leading bits. This includes the start_mask bit.
221            self.words[start_word_index] |= !(start_mask - 1);
222            // And all trailing bits (i.e. from 0..=end) in the end word,
223            // including the end.
224            self.words[end_word_index] |= end_mask | (end_mask - 1);
225        } else {
226            self.words[start_word_index] |= end_mask | (end_mask - start_mask);
227        }
228    }
229
230    /// Sets all bits to true.
231    pub fn insert_all(&mut self) {
232        self.words.fill(!0);
233        self.clear_excess_bits();
234    }
235
236    /// Checks whether any bit in the given range is a 1.
237    #[inline]
238    pub fn contains_any(&self, elems: impl RangeBounds<T>) -> bool {
239        let Some((start, end)) = inclusive_start_end(elems, self.domain_size) else {
240            return false;
241        };
242        let (start_word_index, start_mask) = word_index_and_mask(start);
243        let (end_word_index, end_mask) = word_index_and_mask(end);
244
245        if start_word_index == end_word_index {
246            self.words[start_word_index] & (end_mask | (end_mask - start_mask)) != 0
247        } else {
248            if self.words[start_word_index] & !(start_mask - 1) != 0 {
249                return true;
250            }
251
252            let remaining = start_word_index + 1..end_word_index;
253            if remaining.start <= remaining.end {
254                self.words[remaining].iter().any(|&w| w != 0)
255                    || self.words[end_word_index] & (end_mask | (end_mask - 1)) != 0
256            } else {
257                false
258            }
259        }
260    }
261
262    /// Returns `true` if the set has changed.
263    #[inline]
264    pub fn remove(&mut self, elem: T) -> bool {
265        if !(elem.index() < self.domain_size) {
    ::core::panicking::panic("assertion failed: elem.index() < self.domain_size")
};assert!(elem.index() < self.domain_size);
266        let (word_index, mask) = word_index_and_mask(elem);
267        let word_ref = &mut self.words[word_index];
268        let word = *word_ref;
269        let new_word = word & !mask;
270        *word_ref = new_word;
271        new_word != word
272    }
273
274    /// Iterates over the indices of set bits in a sorted order.
275    #[inline]
276    pub fn iter(&self) -> BitIter<'_, T> {
277        BitIter::new(&self.words)
278    }
279
280    pub fn last_set_in(&self, range: impl RangeBounds<T>) -> Option<T> {
281        let (start, end) = inclusive_start_end(range, self.domain_size)?;
282        let (start_word_index, _) = word_index_and_mask(start);
283        let (end_word_index, end_mask) = word_index_and_mask(end);
284
285        let end_word = self.words[end_word_index] & (end_mask | (end_mask - 1));
286        if end_word != 0 {
287            let pos = max_bit(end_word) + WORD_BITS * end_word_index;
288            if start <= pos {
289                return Some(T::new(pos));
290            }
291        }
292
293        // We exclude end_word_index from the range here, because we don't want
294        // to limit ourselves to *just* the last word: the bits set it in may be
295        // after `end`, so it may not work out.
296        if let Some(offset) =
297            self.words[start_word_index..end_word_index].iter().rposition(|&w| w != 0)
298        {
299            let word_idx = start_word_index + offset;
300            let start_word = self.words[word_idx];
301            let pos = max_bit(start_word) + WORD_BITS * word_idx;
302            if start <= pos {
303                return Some(T::new(pos));
304            }
305        }
306
307        None
308    }
309
310    self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::union(self, other);
Self
Rhs
&mut Self
self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::subtract(self, other);
Self
Rhs
&mut Self
self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::intersect(self, other);bit_relations_inherent_impls! {}
311
312    /// Sets `self = self | !other`.
313    ///
314    /// FIXME: Incorporate this into [`BitRelations`] and fill out
315    /// implementations for other bitset types, if needed.
316    pub fn union_not(&mut self, other: &DenseBitSet<T>) {
317        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
318
319        // FIXME(Zalathar): If we were to forcibly _set_ all excess bits before
320        // the bitwise update, and then clear them again afterwards, we could
321        // quickly and accurately detect whether the update changed anything.
322        // But that's only worth doing if there's an actual use-case.
323
324        bitwise(&mut self.words, &other.words, |a, b| a | !b);
325        // The bitwise update `a | !b` can result in the last word containing
326        // out-of-domain bits, so we need to clear them.
327        self.clear_excess_bits();
328    }
329}
330
331// dense REL dense
332impl<T: Idx> BitRelations<DenseBitSet<T>> for DenseBitSet<T> {
333    fn union(&mut self, other: &DenseBitSet<T>) -> bool {
334        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
335        bitwise(&mut self.words, &other.words, |a, b| a | b)
336    }
337
338    fn subtract(&mut self, other: &DenseBitSet<T>) -> bool {
339        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
340        bitwise(&mut self.words, &other.words, |a, b| a & !b)
341    }
342
343    fn intersect(&mut self, other: &DenseBitSet<T>) -> bool {
344        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
345        bitwise(&mut self.words, &other.words, |a, b| a & b)
346    }
347}
348
349impl<T: Idx> From<GrowableBitSet<T>> for DenseBitSet<T> {
350    fn from(bit_set: GrowableBitSet<T>) -> Self {
351        bit_set.bit_set
352    }
353}
354
355impl<T> Clone for DenseBitSet<T> {
356    fn clone(&self) -> Self {
357        DenseBitSet {
358            domain_size: self.domain_size,
359            words: self.words.clone(),
360            marker: PhantomData,
361        }
362    }
363
364    fn clone_from(&mut self, from: &Self) {
365        self.domain_size = from.domain_size;
366        self.words.clone_from(&from.words);
367    }
368}
369
370impl<T: Idx> fmt::Debug for DenseBitSet<T> {
371    fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
372        w.debug_list().entries(self.iter()).finish()
373    }
374}
375
376impl<T: Idx> ToString for DenseBitSet<T> {
377    fn to_string(&self) -> String {
378        let mut result = String::new();
379        let mut sep = '[';
380
381        // Note: this is a little endian printout of bytes.
382
383        // i tracks how many bits we have printed so far.
384        let mut i = 0;
385        for word in &self.words {
386            let mut word = *word;
387            for _ in 0..WORD_BYTES {
388                // for each byte in `word`:
389                let remain = self.domain_size - i;
390                // If less than a byte remains, then mask just that many bits.
391                let mask = if remain <= 8 { (1 << remain) - 1 } else { 0xFF };
392                if !(mask <= 0xFF) {
    ::core::panicking::panic("assertion failed: mask <= 0xFF")
};assert!(mask <= 0xFF);
393                let byte = word & mask;
394
395                result.push_str(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1:02x}", sep, byte))
    })format!("{sep}{byte:02x}"));
396
397                if remain <= 8 {
398                    break;
399                }
400                word >>= 8;
401                i += 8;
402                sep = '-';
403            }
404            sep = '|';
405        }
406        result.push(']');
407
408        result
409    }
410}
411
412pub struct BitIter<'a, T: Idx> {
413    /// A copy of the current word, but with any already-visited bits cleared.
414    /// (This lets us use `trailing_zeros()` to find the next set bit.) When it
415    /// is reduced to 0, we move onto the next word.
416    word: Word,
417
418    /// The offset (measured in bits) of the current word.
419    offset: usize,
420
421    /// Underlying iterator over the words.
422    iter: slice::Iter<'a, Word>,
423
424    marker: PhantomData<T>,
425}
426
427impl<'a, T: Idx> BitIter<'a, T> {
428    #[inline]
429    fn new(words: &'a [Word]) -> BitIter<'a, T> {
430        // We initialize `word` and `offset` to degenerate values. On the first
431        // call to `next()` we will fall through to getting the first word from
432        // `iter`, which sets `word` to the first word (if there is one) and
433        // `offset` to 0. Doing it this way saves us from having to maintain
434        // additional state about whether we have started.
435        BitIter {
436            word: 0,
437            offset: usize::MAX - (WORD_BITS - 1),
438            iter: words.iter(),
439            marker: PhantomData,
440        }
441    }
442}
443
444impl<'a, T: Idx> Iterator for BitIter<'a, T> {
445    type Item = T;
446    fn next(&mut self) -> Option<T> {
447        loop {
448            if self.word != 0 {
449                // Get the position of the next set bit in the current word,
450                // then clear the bit.
451                let bit_pos = self.word.trailing_zeros() as usize;
452                self.word ^= 1 << bit_pos;
453                return Some(T::new(bit_pos + self.offset));
454            }
455
456            // Move onto the next word. `wrapping_add()` is needed to handle
457            // the degenerate initial value given to `offset` in `new()`.
458            self.word = *self.iter.next()?;
459            self.offset = self.offset.wrapping_add(WORD_BITS);
460        }
461    }
462}
463
464/// A fixed-size bitset type with a partially dense, partially sparse
465/// representation. The bitset is broken into chunks, and chunks that are all
466/// zeros or all ones are represented and handled very efficiently.
467///
468/// This type is especially efficient for sets that typically have a large
469/// `domain_size` with significant stretches of all zeros or all ones, and also
470/// some stretches with lots of 0s and 1s mixed in a way that causes trouble
471/// for `IntervalSet`.
472///
473/// Best used via `MixedBitSet`, rather than directly, because `MixedBitSet`
474/// has better performance for small bitsets.
475///
476/// `T` is an index type, typically a newtyped `usize` wrapper, but it can also
477/// just be `usize`.
478///
479/// All operations that involve an element will panic if the element is equal
480/// to or greater than the domain size. All operations that involve two bitsets
481/// will panic if the bitsets have differing domain sizes.
482#[derive(#[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for ChunkedBitSet<T> {
    #[inline]
    fn eq(&self, other: &ChunkedBitSet<T>) -> bool {
        self.domain_size == other.domain_size && self.chunks == other.chunks
            && self.marker == other.marker
    }
}PartialEq, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for ChunkedBitSet<T> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<usize>;
        let _: ::core::cmp::AssertParamIsEq<Box<[Chunk]>>;
        let _: ::core::cmp::AssertParamIsEq<PhantomData<T>>;
    }
}Eq)]
483pub struct ChunkedBitSet<T> {
484    domain_size: usize,
485
486    /// The chunks. Each one contains exactly CHUNK_BITS values, except the
487    /// last one which contains 1..=CHUNK_BITS values.
488    chunks: Box<[Chunk]>,
489
490    marker: PhantomData<T>,
491}
492
493// NOTE: The chunk size is computed on-the-fly on each manipulation of a chunk.
494// This avoids storing it, as it's almost always CHUNK_BITS except for the last one.
495#[derive(#[automatically_derived]
impl ::core::clone::Clone for Chunk {
    #[inline]
    fn clone(&self) -> Chunk {
        match self {
            Chunk::Zeros => Chunk::Zeros,
            Chunk::Ones => Chunk::Ones,
            Chunk::Mixed { ones_count: __self_0, words: __self_1 } =>
                Chunk::Mixed {
                    ones_count: ::core::clone::Clone::clone(__self_0),
                    words: ::core::clone::Clone::clone(__self_1),
                },
        }
    }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Chunk {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Chunk::Zeros => ::core::fmt::Formatter::write_str(f, "Zeros"),
            Chunk::Ones => ::core::fmt::Formatter::write_str(f, "Ones"),
            Chunk::Mixed { ones_count: __self_0, words: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f, "Mixed",
                    "ones_count", __self_0, "words", &__self_1),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for Chunk {
    #[inline]
    fn eq(&self, other: &Chunk) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Chunk::Mixed { ones_count: __self_0, words: __self_1 },
                    Chunk::Mixed { ones_count: __arg1_0, words: __arg1_1 }) =>
                    __self_0 == __arg1_0 && __self_1 == __arg1_1,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Chunk {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<ChunkSize>;
        let _: ::core::cmp::AssertParamIsEq<Rc<[Word; CHUNK_WORDS]>>;
    }
}Eq)]
496enum Chunk {
497    /// A chunk that is all zeros; we don't represent the zeros explicitly.
498    Zeros,
499
500    /// A chunk that is all ones; we don't represent the ones explicitly.
501    Ones,
502
503    /// A chunk that has a mix of zeros and ones, which are represented
504    /// explicitly and densely. It never has all zeros or all ones.
505    ///
506    /// If this is the final chunk there may be excess, unused words. This
507    /// turns out to be both simpler and have better performance than
508    /// allocating the minimum number of words, largely because we avoid having
509    /// to store the length, which would make this type larger. These excess
510    /// words are always zero, as are any excess bits in the final in-use word.
511    ///
512    /// The words are within an `Rc` because it's surprisingly common to
513    /// duplicate an entire chunk, e.g. in `ChunkedBitSet::clone_from()`, or
514    /// when a `Mixed` chunk is union'd into a `Zeros` chunk. When we do need
515    /// to modify a chunk we use `Rc::make_mut`.
516    Mixed {
517        /// Count of set bits (1s) in this chunk's words.
518        ///
519        /// Invariant: `0 < ones_count < chunk_domain_size`.
520        ///
521        /// Tracking this separately allows individual insert/remove calls to
522        /// know that the chunk has become all-zeroes or all-ones, in O(1) time.
523        ones_count: ChunkSize,
524        words: Rc<[Word; CHUNK_WORDS]>,
525    },
526}
527
528// This type is used a lot. Make sure it doesn't unintentionally get bigger.
529#[cfg(target_pointer_width = "64")]
530const _: [(); 16] = [(); ::std::mem::size_of::<Chunk>()];crate::static_assert_size!(Chunk, 16);
531
532impl<T> ChunkedBitSet<T> {
533    pub fn domain_size(&self) -> usize {
534        self.domain_size
535    }
536
537    #[inline]
538    fn last_chunk_size(&self) -> ChunkSize {
539        let n = self.domain_size % CHUNK_BITS;
540        if n == 0 { CHUNK_BITS as ChunkSize } else { n as ChunkSize }
541    }
542
543    /// All the chunks have a chunk_domain_size of `CHUNK_BITS` except the final one.
544    #[inline]
545    fn chunk_domain_size(&self, chunk: usize) -> ChunkSize {
546        if chunk == self.chunks.len() - 1 {
547            self.last_chunk_size()
548        } else {
549            CHUNK_BITS as ChunkSize
550        }
551    }
552
553    #[cfg(test)]
554    fn assert_valid(&self) {
555        if self.domain_size == 0 {
556            assert!(self.chunks.is_empty());
557            return;
558        }
559
560        assert!((self.chunks.len() - 1) * CHUNK_BITS <= self.domain_size);
561        assert!(self.chunks.len() * CHUNK_BITS >= self.domain_size);
562        for (chunk_index, chunk) in self.chunks.iter().enumerate() {
563            let chunk_domain_size = self.chunk_domain_size(chunk_index);
564            chunk.assert_valid(chunk_domain_size);
565        }
566    }
567}
568
569impl<T: Idx> ChunkedBitSet<T> {
570    /// Creates a new bitset with a given `domain_size` and chunk kind.
571    fn new(domain_size: usize, is_empty: bool) -> Self {
572        let chunks = if domain_size == 0 {
573            Box::new([])
574        } else {
575            ::alloc::vec::from_elem(if is_empty { Zeros } else { Ones },
    num_chunks(domain_size))vec![if is_empty { Zeros } else { Ones }; num_chunks(domain_size)].into_boxed_slice()
576        };
577        ChunkedBitSet { domain_size, chunks, marker: PhantomData }
578    }
579
580    /// Creates a new, empty bitset with a given `domain_size`.
581    #[inline]
582    pub fn new_empty(domain_size: usize) -> Self {
583        ChunkedBitSet::new(domain_size, /* is_empty */ true)
584    }
585
586    /// Creates a new, filled bitset with a given `domain_size`.
587    #[inline]
588    pub fn new_filled(domain_size: usize) -> Self {
589        ChunkedBitSet::new(domain_size, /* is_empty */ false)
590    }
591
592    pub fn clear(&mut self) {
593        self.chunks.fill_with(|| Chunk::Zeros);
594    }
595
596    #[cfg(test)]
597    fn chunks(&self) -> &[Chunk] {
598        &self.chunks
599    }
600
601    /// Count the number of bits in the set.
602    pub fn count(&self) -> usize {
603        self.chunks
604            .iter()
605            .enumerate()
606            .map(|(index, chunk)| chunk.count(self.chunk_domain_size(index)))
607            .sum()
608    }
609
610    pub fn is_empty(&self) -> bool {
611        self.chunks.iter().all(|chunk| #[allow(non_exhaustive_omitted_patterns)] match chunk {
    Zeros => true,
    _ => false,
}matches!(chunk, Zeros))
612    }
613
614    /// Returns `true` if `self` contains `elem`.
615    #[inline]
616    pub fn contains(&self, elem: T) -> bool {
617        if !(elem.index() < self.domain_size) {
    ::core::panicking::panic("assertion failed: elem.index() < self.domain_size")
};assert!(elem.index() < self.domain_size);
618        let chunk = &self.chunks[chunk_index(elem)];
619        match &chunk {
620            Zeros => false,
621            Ones => true,
622            Mixed { ones_count: _, words } => {
623                let (word_index, mask) = chunk_word_index_and_mask(elem);
624                (words[word_index] & mask) != 0
625            }
626        }
627    }
628
629    #[inline]
630    pub fn iter(&self) -> ChunkedBitIter<'_, T> {
631        ChunkedBitIter::new(self)
632    }
633
634    /// Insert `elem`. Returns whether the set has changed.
635    pub fn insert(&mut self, elem: T) -> bool {
636        if !(elem.index() < self.domain_size) {
    ::core::panicking::panic("assertion failed: elem.index() < self.domain_size")
};assert!(elem.index() < self.domain_size);
637        let chunk_index = chunk_index(elem);
638        let chunk_domain_size = self.chunk_domain_size(chunk_index);
639        let chunk = &mut self.chunks[chunk_index];
640        match *chunk {
641            Zeros => {
642                if chunk_domain_size > 1 {
643                    let mut words = {
644                        // We take some effort to avoid copying the words.
645                        let words = Rc::<[Word; CHUNK_WORDS]>::new_zeroed();
646                        // SAFETY: `words` can safely be all zeroes.
647                        unsafe { words.assume_init() }
648                    };
649                    let words_ref = Rc::get_mut(&mut words).unwrap();
650
651                    let (word_index, mask) = chunk_word_index_and_mask(elem);
652                    words_ref[word_index] |= mask;
653                    *chunk = Mixed { ones_count: 1, words };
654                } else {
655                    *chunk = Ones;
656                }
657                true
658            }
659            Ones => false,
660            Mixed { ref mut ones_count, ref mut words } => {
661                // We skip all the work if the bit is already set.
662                let (word_index, mask) = chunk_word_index_and_mask(elem);
663                if (words[word_index] & mask) == 0 {
664                    *ones_count += 1;
665                    if *ones_count < chunk_domain_size {
666                        let words = Rc::make_mut(words);
667                        words[word_index] |= mask;
668                    } else {
669                        *chunk = Ones;
670                    }
671                    true
672                } else {
673                    false
674                }
675            }
676        }
677    }
678
679    /// Sets all bits to true.
680    pub fn insert_all(&mut self) {
681        self.chunks.fill_with(|| Chunk::Ones);
682    }
683
684    /// Returns `true` if the set has changed.
685    pub fn remove(&mut self, elem: T) -> bool {
686        if !(elem.index() < self.domain_size) {
    ::core::panicking::panic("assertion failed: elem.index() < self.domain_size")
};assert!(elem.index() < self.domain_size);
687        let chunk_index = chunk_index(elem);
688        let chunk_domain_size = self.chunk_domain_size(chunk_index);
689        let chunk = &mut self.chunks[chunk_index];
690        match *chunk {
691            Zeros => false,
692            Ones => {
693                if chunk_domain_size > 1 {
694                    let mut words = {
695                        // We take some effort to avoid copying the words.
696                        let words = Rc::<[Word; CHUNK_WORDS]>::new_zeroed();
697                        // SAFETY: `words` can safely be all zeroes.
698                        unsafe { words.assume_init() }
699                    };
700                    let words_ref = Rc::get_mut(&mut words).unwrap();
701
702                    // Set only the bits in use.
703                    let num_words = num_words(chunk_domain_size as usize);
704                    words_ref[..num_words].fill(!0);
705                    clear_excess_bits_in_final_word(
706                        chunk_domain_size as usize,
707                        &mut words_ref[..num_words],
708                    );
709                    let (word_index, mask) = chunk_word_index_and_mask(elem);
710                    words_ref[word_index] &= !mask;
711                    *chunk = Mixed { ones_count: chunk_domain_size - 1, words };
712                } else {
713                    *chunk = Zeros;
714                }
715                true
716            }
717            Mixed { ref mut ones_count, ref mut words } => {
718                // We skip all the work if the bit is already clear.
719                let (word_index, mask) = chunk_word_index_and_mask(elem);
720                if (words[word_index] & mask) != 0 {
721                    *ones_count -= 1;
722                    if *ones_count > 0 {
723                        let words = Rc::make_mut(words);
724                        words[word_index] &= !mask;
725                    } else {
726                        *chunk = Zeros
727                    }
728                    true
729                } else {
730                    false
731                }
732            }
733        }
734    }
735
736    fn chunk_iter(&self, chunk_index: usize) -> ChunkIter<'_> {
737        let chunk_domain_size = self.chunk_domain_size(chunk_index);
738        match self.chunks.get(chunk_index) {
739            Some(Zeros) => ChunkIter::Zeros,
740            Some(Ones) => ChunkIter::Ones(0..chunk_domain_size as usize),
741            Some(Mixed { ones_count: _, words }) => {
742                let num_words = num_words(chunk_domain_size as usize);
743                ChunkIter::Mixed(BitIter::new(&words[0..num_words]))
744            }
745            None => ChunkIter::Finished,
746        }
747    }
748
749    self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::union(self, other);
Self
Rhs
&mut Self
self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::subtract(self, other);
Self
Rhs
&mut Self
self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::intersect(self, other);bit_relations_inherent_impls! {}
750}
751
752impl<T: Idx> BitRelations<ChunkedBitSet<T>> for ChunkedBitSet<T> {
753    fn union(&mut self, other: &ChunkedBitSet<T>) -> bool {
754        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
755
756        let num_chunks = self.chunks.len();
757        if true {
    match (&num_chunks, &other.chunks.len()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(num_chunks, other.chunks.len());
758
759        let last_chunk_size = self.last_chunk_size();
760        if true {
    match (&last_chunk_size, &other.last_chunk_size()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(last_chunk_size, other.last_chunk_size());
761
762        let mut changed = false;
763        for (chunk_index, (mut self_chunk, other_chunk)) in
764            self.chunks.iter_mut().zip(other.chunks.iter()).enumerate()
765        {
766            let chunk_domain_size = if chunk_index + 1 == num_chunks {
767                last_chunk_size
768            } else {
769                CHUNK_BITS as ChunkSize
770            };
771
772            match (&mut self_chunk, &other_chunk) {
773                (_, Zeros) | (Ones, _) => {}
774                (Zeros, _) | (Mixed { .. }, Ones) => {
775                    // `other_chunk` fully overwrites `self_chunk`
776                    *self_chunk = other_chunk.clone();
777                    changed = true;
778                }
779                (
780                    Mixed { ones_count: self_chunk_ones, words: self_chunk_words },
781                    Mixed { ones_count: _, words: other_chunk_words },
782                ) => {
783                    // First check if the operation would change
784                    // `self_chunk.words`. If not, we can avoid allocating some
785                    // words, and this happens often enough that it's a
786                    // performance win. Also, we only need to operate on the
787                    // in-use words, hence the slicing.
788                    let num_words = num_words(chunk_domain_size as usize);
789
790                    // If both sides are the same, nothing will change. This
791                    // case is very common and it's a pretty fast check, so
792                    // it's a performance win to do it.
793                    if self_chunk_words[0..num_words] == other_chunk_words[0..num_words] {
794                        continue;
795                    }
796
797                    // Do a more precise "will anything change?" test. Also a
798                    // performance win.
799                    let op = |a, b| a | b;
800                    if !bitwise_changes(
801                        &self_chunk_words[0..num_words],
802                        &other_chunk_words[0..num_words],
803                        op,
804                    ) {
805                        continue;
806                    }
807
808                    // If we reach here, `self_chunk_words` is definitely changing.
809                    let self_chunk_words = Rc::make_mut(self_chunk_words);
810                    let has_changed = bitwise(
811                        &mut self_chunk_words[0..num_words],
812                        &other_chunk_words[0..num_words],
813                        op,
814                    );
815                    if true {
    if !has_changed {
        ::core::panicking::panic("assertion failed: has_changed")
    };
};debug_assert!(has_changed);
816                    *self_chunk_ones = count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
817                    if *self_chunk_ones == chunk_domain_size {
818                        *self_chunk = Ones;
819                    }
820                    changed = true;
821                }
822            }
823        }
824        changed
825    }
826
827    fn subtract(&mut self, other: &ChunkedBitSet<T>) -> bool {
828        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
829
830        let num_chunks = self.chunks.len();
831        if true {
    match (&num_chunks, &other.chunks.len()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(num_chunks, other.chunks.len());
832
833        let last_chunk_size = self.last_chunk_size();
834        if true {
    match (&last_chunk_size, &other.last_chunk_size()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(last_chunk_size, other.last_chunk_size());
835
836        let mut changed = false;
837        for (chunk_index, (mut self_chunk, other_chunk)) in
838            self.chunks.iter_mut().zip(other.chunks.iter()).enumerate()
839        {
840            let chunk_domain_size = if chunk_index + 1 == num_chunks {
841                last_chunk_size
842            } else {
843                CHUNK_BITS as ChunkSize
844            };
845
846            match (&mut self_chunk, &other_chunk) {
847                (Zeros, _) | (_, Zeros) => {}
848                (Ones | Mixed { .. }, Ones) => {
849                    changed = true;
850                    *self_chunk = Zeros;
851                }
852                (Ones, Mixed { ones_count: other_chunk_ones, words: other_chunk_words }) => {
853                    changed = true;
854                    let num_words = num_words(chunk_domain_size as usize);
855                    if true {
    if !(num_words > 0 && num_words <= CHUNK_WORDS) {
        ::core::panicking::panic("assertion failed: num_words > 0 && num_words <= CHUNK_WORDS")
    };
};debug_assert!(num_words > 0 && num_words <= CHUNK_WORDS);
856                    let mut tail_mask =
857                        1 << (chunk_domain_size - ((num_words - 1) * WORD_BITS) as u16) - 1;
858                    let mut self_chunk_words = **other_chunk_words;
859                    for word in self_chunk_words[0..num_words].iter_mut().rev() {
860                        *word = !*word & tail_mask;
861                        tail_mask = Word::MAX;
862                    }
863                    let self_chunk_ones = chunk_domain_size - *other_chunk_ones;
864                    if true {
    match (&self_chunk_ones,
            &(count_ones(&self_chunk_words[0..num_words]) as ChunkSize)) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(
865                        self_chunk_ones,
866                        count_ones(&self_chunk_words[0..num_words]) as ChunkSize
867                    );
868                    *self_chunk =
869                        Mixed { ones_count: self_chunk_ones, words: Rc::new(self_chunk_words) };
870                }
871                (
872                    Mixed { ones_count: self_chunk_ones, words: self_chunk_words },
873                    Mixed { ones_count: _, words: other_chunk_words },
874                ) => {
875                    // See `ChunkedBitSet::union` for details on what is happening here.
876                    let num_words = num_words(chunk_domain_size as usize);
877                    let op = |a: Word, b: Word| a & !b;
878                    if !bitwise_changes(
879                        &self_chunk_words[0..num_words],
880                        &other_chunk_words[0..num_words],
881                        op,
882                    ) {
883                        continue;
884                    }
885
886                    let self_chunk_words = Rc::make_mut(self_chunk_words);
887                    let has_changed = bitwise(
888                        &mut self_chunk_words[0..num_words],
889                        &other_chunk_words[0..num_words],
890                        op,
891                    );
892                    if true {
    if !has_changed {
        ::core::panicking::panic("assertion failed: has_changed")
    };
};debug_assert!(has_changed);
893                    *self_chunk_ones = count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
894                    if *self_chunk_ones == 0 {
895                        *self_chunk = Zeros;
896                    }
897                    changed = true;
898                }
899            }
900        }
901        changed
902    }
903
904    fn intersect(&mut self, other: &ChunkedBitSet<T>) -> bool {
905        match (&self.domain_size, &other.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, other.domain_size);
906
907        let num_chunks = self.chunks.len();
908        if true {
    match (&num_chunks, &other.chunks.len()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(num_chunks, other.chunks.len());
909
910        let last_chunk_size = self.last_chunk_size();
911        if true {
    match (&last_chunk_size, &other.last_chunk_size()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(last_chunk_size, other.last_chunk_size());
912
913        let mut changed = false;
914        for (chunk_index, (mut self_chunk, other_chunk)) in
915            self.chunks.iter_mut().zip(other.chunks.iter()).enumerate()
916        {
917            let chunk_domain_size = if chunk_index + 1 == num_chunks {
918                last_chunk_size
919            } else {
920                CHUNK_BITS as ChunkSize
921            };
922
923            match (&mut self_chunk, &other_chunk) {
924                (Zeros, _) | (_, Ones) => {}
925                (Ones, Zeros | Mixed { .. }) | (Mixed { .. }, Zeros) => {
926                    changed = true;
927                    *self_chunk = other_chunk.clone();
928                }
929                (
930                    Mixed { ones_count: self_chunk_ones, words: self_chunk_words },
931                    Mixed { ones_count: _, words: other_chunk_words },
932                ) => {
933                    // See `ChunkedBitSet::union` for details on what is happening here.
934                    let num_words = num_words(chunk_domain_size as usize);
935                    let op = |a, b| a & b;
936                    if !bitwise_changes(
937                        &self_chunk_words[0..num_words],
938                        &other_chunk_words[0..num_words],
939                        op,
940                    ) {
941                        continue;
942                    }
943
944                    let self_chunk_words = Rc::make_mut(self_chunk_words);
945                    let has_changed = bitwise(
946                        &mut self_chunk_words[0..num_words],
947                        &other_chunk_words[0..num_words],
948                        op,
949                    );
950                    if true {
    if !has_changed {
        ::core::panicking::panic("assertion failed: has_changed")
    };
};debug_assert!(has_changed);
951                    *self_chunk_ones = count_ones(&self_chunk_words[0..num_words]) as ChunkSize;
952                    if *self_chunk_ones == 0 {
953                        *self_chunk = Zeros;
954                    }
955                    changed = true;
956                }
957            }
958        }
959
960        changed
961    }
962}
963
964impl<T> Clone for ChunkedBitSet<T> {
965    fn clone(&self) -> Self {
966        ChunkedBitSet {
967            domain_size: self.domain_size,
968            chunks: self.chunks.clone(),
969            marker: PhantomData,
970        }
971    }
972
973    /// WARNING: this implementation of clone_from will panic if the two
974    /// bitsets have different domain sizes. This constraint is not inherent to
975    /// `clone_from`, but it works with the existing call sites and allows a
976    /// faster implementation, which is important because this function is hot.
977    fn clone_from(&mut self, from: &Self) {
978        match (&self.domain_size, &from.domain_size) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(self.domain_size, from.domain_size);
979        if true {
    match (&self.chunks.len(), &from.chunks.len()) {
        (left_val, right_val) => {
            if !(*left_val == *right_val) {
                let kind = ::core::panicking::AssertKind::Eq;
                ::core::panicking::assert_failed(kind, &*left_val,
                    &*right_val, ::core::option::Option::None);
            }
        }
    };
};debug_assert_eq!(self.chunks.len(), from.chunks.len());
980
981        self.chunks.clone_from(&from.chunks)
982    }
983}
984
985pub struct ChunkedBitIter<'a, T: Idx> {
986    bit_set: &'a ChunkedBitSet<T>,
987
988    // The index of the current chunk.
989    chunk_index: usize,
990
991    // The sub-iterator for the current chunk.
992    chunk_iter: ChunkIter<'a>,
993}
994
995impl<'a, T: Idx> ChunkedBitIter<'a, T> {
996    #[inline]
997    fn new(bit_set: &'a ChunkedBitSet<T>) -> ChunkedBitIter<'a, T> {
998        ChunkedBitIter { bit_set, chunk_index: 0, chunk_iter: bit_set.chunk_iter(0) }
999    }
1000}
1001
1002impl<'a, T: Idx> Iterator for ChunkedBitIter<'a, T> {
1003    type Item = T;
1004
1005    fn next(&mut self) -> Option<T> {
1006        loop {
1007            match &mut self.chunk_iter {
1008                ChunkIter::Zeros => {}
1009                ChunkIter::Ones(iter) => {
1010                    if let Some(next) = iter.next() {
1011                        return Some(T::new(next + self.chunk_index * CHUNK_BITS));
1012                    }
1013                }
1014                ChunkIter::Mixed(iter) => {
1015                    if let Some(next) = iter.next() {
1016                        return Some(T::new(next + self.chunk_index * CHUNK_BITS));
1017                    }
1018                }
1019                ChunkIter::Finished => return None,
1020            }
1021            self.chunk_index += 1;
1022            self.chunk_iter = self.bit_set.chunk_iter(self.chunk_index);
1023        }
1024    }
1025}
1026
1027impl Chunk {
1028    #[cfg(test)]
1029    fn assert_valid(&self, chunk_domain_size: ChunkSize) {
1030        assert!(chunk_domain_size as usize <= CHUNK_BITS);
1031        match *self {
1032            Zeros | Ones => {}
1033            Mixed { ones_count, ref words } => {
1034                assert!(0 < ones_count && ones_count < chunk_domain_size);
1035
1036                // Check the number of set bits matches `count`.
1037                assert_eq!(count_ones(words.as_slice()) as ChunkSize, ones_count);
1038
1039                // Check the not-in-use words are all zeroed.
1040                let num_words = num_words(chunk_domain_size as usize);
1041                if num_words < CHUNK_WORDS {
1042                    assert_eq!(count_ones(&words[num_words..]) as ChunkSize, 0);
1043                }
1044            }
1045        }
1046    }
1047
1048    /// Count the number of 1s in the chunk.
1049    fn count(&self, chunk_domain_size: ChunkSize) -> usize {
1050        match *self {
1051            Zeros => 0,
1052            Ones => chunk_domain_size as usize,
1053            Mixed { ones_count, words: _ } => usize::from(ones_count),
1054        }
1055    }
1056}
1057
1058enum ChunkIter<'a> {
1059    Zeros,
1060    Ones(Range<usize>),
1061    Mixed(BitIter<'a, usize>),
1062    Finished,
1063}
1064
1065impl<T: Idx> fmt::Debug for ChunkedBitSet<T> {
1066    fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
1067        w.debug_list().entries(self.iter()).finish()
1068    }
1069}
1070
1071/// Sets `out_vec[i] = op(out_vec[i], in_vec[i])` for each index `i` in both
1072/// slices. The slices must have the same length.
1073///
1074/// Returns true if at least one bit in `out_vec` was changed.
1075///
1076/// ## Warning
1077/// Some bitwise operations (e.g. union-not, xor) can set output bits that were
1078/// unset in in both inputs. If this happens in the last word/chunk of a bitset,
1079/// it can cause the bitset to contain out-of-domain values, which need to
1080/// be cleared with `clear_excess_bits_in_final_word`. This also makes the
1081/// "changed" return value unreliable, because the change might have only
1082/// affected excess bits.
1083#[inline]
1084fn bitwise<Op>(out_vec: &mut [Word], in_vec: &[Word], op: Op) -> bool
1085where
1086    Op: Fn(Word, Word) -> Word,
1087{
1088    match (&out_vec.len(), &in_vec.len()) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(out_vec.len(), in_vec.len());
1089    let mut changed = 0;
1090    for (out_elem, in_elem) in iter::zip(out_vec, in_vec) {
1091        let old_val = *out_elem;
1092        let new_val = op(old_val, *in_elem);
1093        *out_elem = new_val;
1094        // This is essentially equivalent to a != with changed being a bool, but
1095        // in practice this code gets auto-vectorized by the compiler for most
1096        // operators. Using != here causes us to generate quite poor code as the
1097        // compiler tries to go back to a boolean on each loop iteration.
1098        changed |= old_val ^ new_val;
1099    }
1100    changed != 0
1101}
1102
1103/// Does this bitwise operation change `out_vec`?
1104#[inline]
1105fn bitwise_changes<Op>(out_vec: &[Word], in_vec: &[Word], op: Op) -> bool
1106where
1107    Op: Fn(Word, Word) -> Word,
1108{
1109    match (&out_vec.len(), &in_vec.len()) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(out_vec.len(), in_vec.len());
1110    for (out_elem, in_elem) in iter::zip(out_vec, in_vec) {
1111        let old_val = *out_elem;
1112        let new_val = op(old_val, *in_elem);
1113        if old_val != new_val {
1114            return true;
1115        }
1116    }
1117    false
1118}
1119
1120/// A bitset with a mixed representation, using `DenseBitSet` for small and
1121/// medium bitsets, and `ChunkedBitSet` for large bitsets, i.e. those with
1122/// enough bits for at least two chunks. This is a good choice for many bitsets
1123/// that can have large domain sizes (e.g. 5000+).
1124///
1125/// `T` is an index type, typically a newtyped `usize` wrapper, but it can also
1126/// just be `usize`.
1127///
1128/// All operations that involve an element will panic if the element is equal
1129/// to or greater than the domain size. All operations that involve two bitsets
1130/// will panic if the bitsets have differing domain sizes.
1131#[derive(#[automatically_derived]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for MixedBitSet<T> {
    #[inline]
    fn eq(&self, other: &MixedBitSet<T>) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (MixedBitSet::Small(__self_0), MixedBitSet::Small(__arg1_0))
                    => __self_0 == __arg1_0,
                (MixedBitSet::Large(__self_0), MixedBitSet::Large(__arg1_0))
                    => __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq, #[automatically_derived]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for MixedBitSet<T> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<DenseBitSet<T>>;
        let _: ::core::cmp::AssertParamIsEq<ChunkedBitSet<T>>;
    }
}Eq)]
1132pub enum MixedBitSet<T> {
1133    Small(DenseBitSet<T>),
1134    Large(ChunkedBitSet<T>),
1135}
1136
1137impl<T> MixedBitSet<T> {
1138    pub fn domain_size(&self) -> usize {
1139        match self {
1140            MixedBitSet::Small(set) => set.domain_size(),
1141            MixedBitSet::Large(set) => set.domain_size(),
1142        }
1143    }
1144}
1145
1146impl<T: Idx> MixedBitSet<T> {
1147    #[inline]
1148    pub fn new_empty(domain_size: usize) -> MixedBitSet<T> {
1149        if domain_size <= CHUNK_BITS {
1150            MixedBitSet::Small(DenseBitSet::new_empty(domain_size))
1151        } else {
1152            MixedBitSet::Large(ChunkedBitSet::new_empty(domain_size))
1153        }
1154    }
1155
1156    #[inline]
1157    pub fn is_empty(&self) -> bool {
1158        match self {
1159            MixedBitSet::Small(set) => set.is_empty(),
1160            MixedBitSet::Large(set) => set.is_empty(),
1161        }
1162    }
1163
1164    #[inline]
1165    pub fn contains(&self, elem: T) -> bool {
1166        match self {
1167            MixedBitSet::Small(set) => set.contains(elem),
1168            MixedBitSet::Large(set) => set.contains(elem),
1169        }
1170    }
1171
1172    #[inline]
1173    pub fn insert(&mut self, elem: T) -> bool {
1174        match self {
1175            MixedBitSet::Small(set) => set.insert(elem),
1176            MixedBitSet::Large(set) => set.insert(elem),
1177        }
1178    }
1179
1180    pub fn insert_all(&mut self) {
1181        match self {
1182            MixedBitSet::Small(set) => set.insert_all(),
1183            MixedBitSet::Large(set) => set.insert_all(),
1184        }
1185    }
1186
1187    #[inline]
1188    pub fn remove(&mut self, elem: T) -> bool {
1189        match self {
1190            MixedBitSet::Small(set) => set.remove(elem),
1191            MixedBitSet::Large(set) => set.remove(elem),
1192        }
1193    }
1194
1195    pub fn iter(&self) -> MixedBitIter<'_, T> {
1196        match self {
1197            MixedBitSet::Small(set) => MixedBitIter::Small(set.iter()),
1198            MixedBitSet::Large(set) => MixedBitIter::Large(set.iter()),
1199        }
1200    }
1201
1202    #[inline]
1203    pub fn clear(&mut self) {
1204        match self {
1205            MixedBitSet::Small(set) => set.clear(),
1206            MixedBitSet::Large(set) => set.clear(),
1207        }
1208    }
1209
1210    self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::union(self, other);
Self
Rhs
&mut Self
self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::subtract(self, other);
Self
Rhs
&mut Self
self
&Rhs
other
bool
<Self as BitRelations<Rhs>>::intersect(self, other);bit_relations_inherent_impls! {}
1211}
1212
1213impl<T> Clone for MixedBitSet<T> {
1214    fn clone(&self) -> Self {
1215        match self {
1216            MixedBitSet::Small(set) => MixedBitSet::Small(set.clone()),
1217            MixedBitSet::Large(set) => MixedBitSet::Large(set.clone()),
1218        }
1219    }
1220
1221    /// WARNING: this implementation of clone_from may panic if the two
1222    /// bitsets have different domain sizes. This constraint is not inherent to
1223    /// `clone_from`, but it works with the existing call sites and allows a
1224    /// faster implementation, which is important because this function is hot.
1225    fn clone_from(&mut self, from: &Self) {
1226        match (self, from) {
1227            (MixedBitSet::Small(set), MixedBitSet::Small(from)) => set.clone_from(from),
1228            (MixedBitSet::Large(set), MixedBitSet::Large(from)) => set.clone_from(from),
1229            _ => { ::core::panicking::panic_fmt(format_args!("MixedBitSet size mismatch")); }panic!("MixedBitSet size mismatch"),
1230        }
1231    }
1232}
1233
1234impl<T: Idx> BitRelations<MixedBitSet<T>> for MixedBitSet<T> {
1235    fn union(&mut self, other: &MixedBitSet<T>) -> bool {
1236        match (self, other) {
1237            (MixedBitSet::Small(set), MixedBitSet::Small(other)) => set.union(other),
1238            (MixedBitSet::Large(set), MixedBitSet::Large(other)) => set.union(other),
1239            _ => { ::core::panicking::panic_fmt(format_args!("MixedBitSet size mismatch")); }panic!("MixedBitSet size mismatch"),
1240        }
1241    }
1242
1243    fn subtract(&mut self, other: &MixedBitSet<T>) -> bool {
1244        match (self, other) {
1245            (MixedBitSet::Small(set), MixedBitSet::Small(other)) => set.subtract(other),
1246            (MixedBitSet::Large(set), MixedBitSet::Large(other)) => set.subtract(other),
1247            _ => { ::core::panicking::panic_fmt(format_args!("MixedBitSet size mismatch")); }panic!("MixedBitSet size mismatch"),
1248        }
1249    }
1250
1251    fn intersect(&mut self, _other: &MixedBitSet<T>) -> bool {
1252        {
    ::core::panicking::panic_fmt(format_args!("not implemented: {0}",
            format_args!("implement if/when necessary")));
};unimplemented!("implement if/when necessary");
1253    }
1254}
1255
1256impl<T: Idx> fmt::Debug for MixedBitSet<T> {
1257    fn fmt(&self, w: &mut fmt::Formatter<'_>) -> fmt::Result {
1258        match self {
1259            MixedBitSet::Small(set) => set.fmt(w),
1260            MixedBitSet::Large(set) => set.fmt(w),
1261        }
1262    }
1263}
1264
1265pub enum MixedBitIter<'a, T: Idx> {
1266    Small(BitIter<'a, T>),
1267    Large(ChunkedBitIter<'a, T>),
1268}
1269
1270impl<'a, T: Idx> Iterator for MixedBitIter<'a, T> {
1271    type Item = T;
1272    fn next(&mut self) -> Option<T> {
1273        match self {
1274            MixedBitIter::Small(iter) => iter.next(),
1275            MixedBitIter::Large(iter) => iter.next(),
1276        }
1277    }
1278}
1279
1280/// A resizable bitset type with a dense representation.
1281///
1282/// `T` is an index type, typically a newtyped `usize` wrapper, but it can also
1283/// just be `usize`.
1284///
1285/// All operations that involve an element will panic if the element is equal
1286/// to or greater than the domain size.
1287#[derive(#[automatically_derived]
impl<T: ::core::clone::Clone + Idx> ::core::clone::Clone for GrowableBitSet<T>
    {
    #[inline]
    fn clone(&self) -> GrowableBitSet<T> {
        GrowableBitSet { bit_set: ::core::clone::Clone::clone(&self.bit_set) }
    }
}Clone, #[automatically_derived]
impl<T: ::core::fmt::Debug + Idx> ::core::fmt::Debug for GrowableBitSet<T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "GrowableBitSet", "bit_set", &&self.bit_set)
    }
}Debug, #[automatically_derived]
impl<T: ::core::cmp::PartialEq + Idx> ::core::cmp::PartialEq for
    GrowableBitSet<T> {
    #[inline]
    fn eq(&self, other: &GrowableBitSet<T>) -> bool {
        self.bit_set == other.bit_set
    }
}PartialEq)]
1288pub struct GrowableBitSet<T: Idx> {
1289    bit_set: DenseBitSet<T>,
1290}
1291
1292impl<T: Idx> Default for GrowableBitSet<T> {
1293    fn default() -> Self {
1294        GrowableBitSet::new_empty()
1295    }
1296}
1297
1298impl<T: Idx> GrowableBitSet<T> {
1299    /// Ensure that the set can hold at least `min_domain_size` elements.
1300    pub fn ensure(&mut self, min_domain_size: usize) {
1301        if self.bit_set.domain_size < min_domain_size {
1302            self.bit_set.domain_size = min_domain_size;
1303        }
1304
1305        let min_num_words = num_words(min_domain_size);
1306        if self.bit_set.words.len() < min_num_words {
1307            self.bit_set.words.resize(min_num_words, 0)
1308        }
1309    }
1310
1311    pub fn new_empty() -> GrowableBitSet<T> {
1312        GrowableBitSet { bit_set: DenseBitSet::new_empty(0) }
1313    }
1314
1315    pub fn with_capacity(capacity: usize) -> GrowableBitSet<T> {
1316        GrowableBitSet { bit_set: DenseBitSet::new_empty(capacity) }
1317    }
1318
1319    /// Returns `true` if the set has changed.
1320    #[inline]
1321    pub fn insert(&mut self, elem: T) -> bool {
1322        self.ensure(elem.index() + 1);
1323        self.bit_set.insert(elem)
1324    }
1325
1326    #[inline]
1327    pub fn insert_range(&mut self, elems: Range<T>) {
1328        self.ensure(elems.end.index());
1329        self.bit_set.insert_range(elems);
1330    }
1331
1332    /// Returns `true` if the set has changed.
1333    #[inline]
1334    pub fn remove(&mut self, elem: T) -> bool {
1335        self.ensure(elem.index() + 1);
1336        self.bit_set.remove(elem)
1337    }
1338
1339    #[inline]
1340    pub fn clear(&mut self) {
1341        self.bit_set.clear();
1342    }
1343
1344    #[inline]
1345    pub fn count(&self) -> usize {
1346        self.bit_set.count()
1347    }
1348
1349    #[inline]
1350    pub fn is_empty(&self) -> bool {
1351        self.bit_set.is_empty()
1352    }
1353
1354    #[inline]
1355    pub fn contains(&self, elem: T) -> bool {
1356        let (word_index, mask) = word_index_and_mask(elem);
1357        self.bit_set.words.get(word_index).is_some_and(|word| (word & mask) != 0)
1358    }
1359
1360    #[inline]
1361    pub fn contains_any(&self, elems: Range<T>) -> bool {
1362        elems.start.index() < self.bit_set.domain_size
1363            && self
1364                .bit_set
1365                .contains_any(elems.start..T::new(elems.end.index().min(self.bit_set.domain_size)))
1366    }
1367
1368    #[inline]
1369    pub fn iter(&self) -> BitIter<'_, T> {
1370        self.bit_set.iter()
1371    }
1372
1373    #[inline]
1374    pub fn len(&self) -> usize {
1375        self.bit_set.count()
1376    }
1377}
1378
1379impl<T: Idx> From<DenseBitSet<T>> for GrowableBitSet<T> {
1380    fn from(bit_set: DenseBitSet<T>) -> Self {
1381        Self { bit_set }
1382    }
1383}
1384
1385/// A fixed-size 2D bit matrix type with a dense representation.
1386///
1387/// `R` and `C` are index types used to identify rows and columns respectively;
1388/// typically newtyped `usize` wrappers, but they can also just be `usize`.
1389///
1390/// All operations that involve a row and/or column index will panic if the
1391/// index exceeds the relevant bound.
1392#[cfg_attr(feature = "nightly", derive(const _: () =
    {
        impl<R: Idx, C: Idx, __D: ::rustc_serialize::Decoder>
            ::rustc_serialize::Decodable<__D> for BitMatrix<R, C> where
            PhantomData<(R, C)>: ::rustc_serialize::Decodable<__D> {
            fn decode(__decoder: &mut __D) -> Self {
                BitMatrix {
                    num_rows: ::rustc_serialize::Decodable::decode(__decoder),
                    num_columns: ::rustc_serialize::Decodable::decode(__decoder),
                    words: ::rustc_serialize::Decodable::decode(__decoder),
                    marker: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable_NoContext, const _: () =
    {
        impl<R: Idx, C: Idx, __E: ::rustc_serialize::Encoder>
            ::rustc_serialize::Encodable<__E> for BitMatrix<R, C> where
            PhantomData<(R, C)>: ::rustc_serialize::Encodable<__E> {
            fn encode(&self, __encoder: &mut __E) {
                match *self {
                    BitMatrix {
                        num_rows: ref __binding_0,
                        num_columns: ref __binding_1,
                        words: ref __binding_2,
                        marker: ref __binding_3 } => {
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_2,
                            __encoder);
                        ::rustc_serialize::Encodable::<__E>::encode(__binding_3,
                            __encoder);
                    }
                }
            }
        }
    };Encodable_NoContext))]
1393#[derive(#[automatically_derived]
impl<R: ::core::clone::Clone + Idx, C: ::core::clone::Clone + Idx>
    ::core::clone::Clone for BitMatrix<R, C> {
    #[inline]
    fn clone(&self) -> BitMatrix<R, C> {
        BitMatrix {
            num_rows: ::core::clone::Clone::clone(&self.num_rows),
            num_columns: ::core::clone::Clone::clone(&self.num_columns),
            words: ::core::clone::Clone::clone(&self.words),
            marker: ::core::clone::Clone::clone(&self.marker),
        }
    }
}Clone, #[automatically_derived]
impl<R: ::core::cmp::Eq + Idx, C: ::core::cmp::Eq + Idx> ::core::cmp::Eq for
    BitMatrix<R, C> {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<usize>;
        let _: ::core::cmp::AssertParamIsEq<Vec<Word>>;
        let _: ::core::cmp::AssertParamIsEq<PhantomData<(R, C)>>;
    }
}Eq, #[automatically_derived]
impl<R: ::core::cmp::PartialEq + Idx, C: ::core::cmp::PartialEq + Idx>
    ::core::cmp::PartialEq for BitMatrix<R, C> {
    #[inline]
    fn eq(&self, other: &BitMatrix<R, C>) -> bool {
        self.num_rows == other.num_rows &&
                    self.num_columns == other.num_columns &&
                self.words == other.words && self.marker == other.marker
    }
}PartialEq, #[automatically_derived]
impl<R: ::core::hash::Hash + Idx, C: ::core::hash::Hash + Idx>
    ::core::hash::Hash for BitMatrix<R, C> {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.num_rows, state);
        ::core::hash::Hash::hash(&self.num_columns, state);
        ::core::hash::Hash::hash(&self.words, state);
        ::core::hash::Hash::hash(&self.marker, state)
    }
}Hash)]
1394pub struct BitMatrix<R: Idx, C: Idx> {
1395    num_rows: usize,
1396    num_columns: usize,
1397    words: Vec<Word>,
1398    marker: PhantomData<(R, C)>,
1399}
1400
1401impl<R: Idx, C: Idx> BitMatrix<R, C> {
1402    /// Creates a new `rows x columns` matrix, initially empty.
1403    pub fn new(num_rows: usize, num_columns: usize) -> BitMatrix<R, C> {
1404        // For every element, we need one bit for every other
1405        // element. Round up to an even number of words.
1406        let words_per_row = num_words(num_columns);
1407        BitMatrix {
1408            num_rows,
1409            num_columns,
1410            words: ::alloc::vec::from_elem(0, num_rows * words_per_row)vec![0; num_rows * words_per_row],
1411            marker: PhantomData,
1412        }
1413    }
1414
1415    /// Creates a new matrix, with `row` used as the value for every row.
1416    pub fn from_row_n(row: &DenseBitSet<C>, num_rows: usize) -> BitMatrix<R, C> {
1417        let num_columns = row.domain_size();
1418        let words_per_row = num_words(num_columns);
1419        match (&words_per_row, &row.words.len()) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(words_per_row, row.words.len());
1420        BitMatrix {
1421            num_rows,
1422            num_columns,
1423            words: iter::repeat_n(&row.words, num_rows).flatten().cloned().collect(),
1424            marker: PhantomData,
1425        }
1426    }
1427
1428    pub fn rows(&self) -> impl Iterator<Item = R> {
1429        (0..self.num_rows).map(R::new)
1430    }
1431
1432    /// The range of bits for a given row.
1433    fn range(&self, row: R) -> (usize, usize) {
1434        let words_per_row = num_words(self.num_columns);
1435        let start = row.index() * words_per_row;
1436        (start, start + words_per_row)
1437    }
1438
1439    /// Sets the cell at `(row, column)` to true. Put another way, insert
1440    /// `column` to the bitset for `row`.
1441    ///
1442    /// Returns `true` if this changed the matrix.
1443    pub fn insert(&mut self, row: R, column: C) -> bool {
1444        if !(row.index() < self.num_rows && column.index() < self.num_columns) {
    ::core::panicking::panic("assertion failed: row.index() < self.num_rows && column.index() < self.num_columns")
};assert!(row.index() < self.num_rows && column.index() < self.num_columns);
1445        let (start, _) = self.range(row);
1446        let (word_index, mask) = word_index_and_mask(column);
1447        let words = &mut self.words[..];
1448        let word = words[start + word_index];
1449        let new_word = word | mask;
1450        words[start + word_index] = new_word;
1451        word != new_word
1452    }
1453
1454    /// Do the bits from `row` contain `column`? Put another way, is
1455    /// the matrix cell at `(row, column)` true?  Put yet another way,
1456    /// if the matrix represents (transitive) reachability, can
1457    /// `row` reach `column`?
1458    pub fn contains(&self, row: R, column: C) -> bool {
1459        if !(row.index() < self.num_rows && column.index() < self.num_columns) {
    ::core::panicking::panic("assertion failed: row.index() < self.num_rows && column.index() < self.num_columns")
};assert!(row.index() < self.num_rows && column.index() < self.num_columns);
1460        let (start, _) = self.range(row);
1461        let (word_index, mask) = word_index_and_mask(column);
1462        (self.words[start + word_index] & mask) != 0
1463    }
1464
1465    /// Returns those indices that are true in rows `a` and `b`. This
1466    /// is an *O*(*n*) operation where *n* is the number of elements
1467    /// (somewhat independent from the actual size of the
1468    /// intersection, in particular).
1469    pub fn intersect_rows(&self, row1: R, row2: R) -> Vec<C> {
1470        if !(row1.index() < self.num_rows && row2.index() < self.num_rows) {
    ::core::panicking::panic("assertion failed: row1.index() < self.num_rows && row2.index() < self.num_rows")
};assert!(row1.index() < self.num_rows && row2.index() < self.num_rows);
1471        let (row1_start, row1_end) = self.range(row1);
1472        let (row2_start, row2_end) = self.range(row2);
1473        let mut result = Vec::with_capacity(self.num_columns);
1474        for (base, (i, j)) in (row1_start..row1_end).zip(row2_start..row2_end).enumerate() {
1475            let mut v = self.words[i] & self.words[j];
1476            for bit in 0..WORD_BITS {
1477                if v == 0 {
1478                    break;
1479                }
1480                if v & 0x1 != 0 {
1481                    result.push(C::new(base * WORD_BITS + bit));
1482                }
1483                v >>= 1;
1484            }
1485        }
1486        result
1487    }
1488
1489    /// Adds the bits from row `read` to the bits from row `write`, and
1490    /// returns `true` if anything changed.
1491    ///
1492    /// This is used when computing transitive reachability because if
1493    /// you have an edge `write -> read`, because in that case
1494    /// `write` can reach everything that `read` can (and
1495    /// potentially more).
1496    pub fn union_rows(&mut self, read: R, write: R) -> bool {
1497        if !(read.index() < self.num_rows && write.index() < self.num_rows) {
    ::core::panicking::panic("assertion failed: read.index() < self.num_rows && write.index() < self.num_rows")
};assert!(read.index() < self.num_rows && write.index() < self.num_rows);
1498        let (read_start, read_end) = self.range(read);
1499        let (write_start, write_end) = self.range(write);
1500        let words = &mut self.words[..];
1501        let mut changed = 0;
1502        for (read_index, write_index) in iter::zip(read_start..read_end, write_start..write_end) {
1503            let word = words[write_index];
1504            let new_word = word | words[read_index];
1505            words[write_index] = new_word;
1506            // See `bitwise` for the rationale.
1507            changed |= word ^ new_word;
1508        }
1509        changed != 0
1510    }
1511
1512    /// Adds the bits from `with` to the bits from row `write`, and
1513    /// returns `true` if anything changed.
1514    pub fn union_row_with(&mut self, with: &DenseBitSet<C>, write: R) -> bool {
1515        if !(write.index() < self.num_rows) {
    ::core::panicking::panic("assertion failed: write.index() < self.num_rows")
};assert!(write.index() < self.num_rows);
1516        match (&with.domain_size(), &self.num_columns) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(with.domain_size(), self.num_columns);
1517        let (write_start, write_end) = self.range(write);
1518        bitwise(&mut self.words[write_start..write_end], &with.words, |a, b| a | b)
1519    }
1520
1521    /// Sets every cell in `row` to true.
1522    pub fn insert_all_into_row(&mut self, row: R) {
1523        if !(row.index() < self.num_rows) {
    ::core::panicking::panic("assertion failed: row.index() < self.num_rows")
};assert!(row.index() < self.num_rows);
1524        let (start, end) = self.range(row);
1525        let words = &mut self.words[..];
1526        for index in start..end {
1527            words[index] = !0;
1528        }
1529        clear_excess_bits_in_final_word(self.num_columns, &mut self.words[..end]);
1530    }
1531
1532    /// Gets a slice of the underlying words.
1533    pub fn words(&self) -> &[Word] {
1534        &self.words
1535    }
1536
1537    /// Iterates through all the columns set to true in a given row of
1538    /// the matrix.
1539    pub fn iter(&self, row: R) -> BitIter<'_, C> {
1540        if !(row.index() < self.num_rows) {
    ::core::panicking::panic("assertion failed: row.index() < self.num_rows")
};assert!(row.index() < self.num_rows);
1541        let (start, end) = self.range(row);
1542        BitIter::new(&self.words[start..end])
1543    }
1544
1545    /// Returns the number of elements in `row`.
1546    pub fn count(&self, row: R) -> usize {
1547        let (start, end) = self.range(row);
1548        count_ones(&self.words[start..end])
1549    }
1550}
1551
1552impl<R: Idx, C: Idx> fmt::Debug for BitMatrix<R, C> {
1553    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1554        /// Forces its contents to print in regular mode instead of alternate mode.
1555        struct OneLinePrinter<T>(T);
1556        impl<T: fmt::Debug> fmt::Debug for OneLinePrinter<T> {
1557            fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1558                fmt.write_fmt(format_args!("{0:?}", self.0))write!(fmt, "{:?}", self.0)
1559            }
1560        }
1561
1562        fmt.write_fmt(format_args!("BitMatrix({0}x{1}) ", self.num_rows,
        self.num_columns))write!(fmt, "BitMatrix({}x{}) ", self.num_rows, self.num_columns)?;
1563        let items = self.rows().flat_map(|r| self.iter(r).map(move |c| (r, c)));
1564        fmt.debug_set().entries(items.map(OneLinePrinter)).finish()
1565    }
1566}
1567
1568/// A fixed-column-size, variable-row-size 2D bit matrix with a moderately
1569/// sparse representation.
1570///
1571/// Initially, every row has no explicit representation. If any bit within a row
1572/// is set, the entire row is instantiated as `Some(<DenseBitSet>)`.
1573/// Furthermore, any previously uninstantiated rows prior to it will be
1574/// instantiated as `None`. Those prior rows may themselves become fully
1575/// instantiated later on if any of their bits are set.
1576///
1577/// `R` and `C` are index types used to identify rows and columns respectively;
1578/// typically newtyped `usize` wrappers, but they can also just be `usize`.
1579#[derive(#[automatically_derived]
impl<R: ::core::clone::Clone, C: ::core::clone::Clone> ::core::clone::Clone
    for SparseBitMatrix<R, C> where R: Idx, C: Idx {
    #[inline]
    fn clone(&self) -> SparseBitMatrix<R, C> {
        SparseBitMatrix {
            num_columns: ::core::clone::Clone::clone(&self.num_columns),
            rows: ::core::clone::Clone::clone(&self.rows),
        }
    }
}Clone, #[automatically_derived]
impl<R: ::core::fmt::Debug, C: ::core::fmt::Debug> ::core::fmt::Debug for
    SparseBitMatrix<R, C> where R: Idx, C: Idx {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "SparseBitMatrix", "num_columns", &self.num_columns, "rows",
            &&self.rows)
    }
}Debug)]
1580pub struct SparseBitMatrix<R, C>
1581where
1582    R: Idx,
1583    C: Idx,
1584{
1585    num_columns: usize,
1586    rows: IndexVec<R, Option<DenseBitSet<C>>>,
1587}
1588
1589impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
1590    /// Creates a new empty sparse bit matrix with no rows or columns.
1591    pub fn new(num_columns: usize) -> Self {
1592        Self { num_columns, rows: IndexVec::new() }
1593    }
1594
1595    fn ensure_row(&mut self, row: R) -> &mut DenseBitSet<C> {
1596        // Instantiate any missing rows up to and including row `row` with an empty `DenseBitSet`.
1597        // Then replace row `row` with a full `DenseBitSet` if necessary.
1598        self.rows.get_or_insert_with(row, || DenseBitSet::new_empty(self.num_columns))
1599    }
1600
1601    /// Sets the cell at `(row, column)` to true. Put another way, insert
1602    /// `column` to the bitset for `row`.
1603    ///
1604    /// Returns `true` if this changed the matrix.
1605    pub fn insert(&mut self, row: R, column: C) -> bool {
1606        self.ensure_row(row).insert(column)
1607    }
1608
1609    /// Sets the cell at `(row, column)` to false. Put another way, delete
1610    /// `column` from the bitset for `row`. Has no effect if `row` does not
1611    /// exist.
1612    ///
1613    /// Returns `true` if this changed the matrix.
1614    pub fn remove(&mut self, row: R, column: C) -> bool {
1615        match self.rows.get_mut(row) {
1616            Some(Some(row)) => row.remove(column),
1617            _ => false,
1618        }
1619    }
1620
1621    /// Sets all columns at `row` to false. Has no effect if `row` does
1622    /// not exist.
1623    pub fn clear(&mut self, row: R) {
1624        if let Some(Some(row)) = self.rows.get_mut(row) {
1625            row.clear();
1626        }
1627    }
1628
1629    /// Do the bits from `row` contain `column`? Put another way, is
1630    /// the matrix cell at `(row, column)` true?  Put yet another way,
1631    /// if the matrix represents (transitive) reachability, can
1632    /// `row` reach `column`?
1633    pub fn contains(&self, row: R, column: C) -> bool {
1634        self.row(row).is_some_and(|r| r.contains(column))
1635    }
1636
1637    /// Adds the bits from row `read` to the bits from row `write`, and
1638    /// returns `true` if anything changed.
1639    ///
1640    /// This is used when computing transitive reachability because if
1641    /// you have an edge `write -> read`, because in that case
1642    /// `write` can reach everything that `read` can (and
1643    /// potentially more).
1644    pub fn union_rows(&mut self, read: R, write: R) -> bool {
1645        if read == write || self.row(read).is_none() {
1646            return false;
1647        }
1648
1649        self.ensure_row(write);
1650        if let (Some(read_row), Some(write_row)) = self.rows.pick2_mut(read, write) {
1651            write_row.union(read_row)
1652        } else {
1653            ::core::panicking::panic("internal error: entered unreachable code")unreachable!()
1654        }
1655    }
1656
1657    /// Insert all bits in the given row.
1658    pub fn insert_all_into_row(&mut self, row: R) {
1659        self.ensure_row(row).insert_all();
1660    }
1661
1662    pub fn rows(&self) -> impl Iterator<Item = R> {
1663        self.rows.indices()
1664    }
1665
1666    /// Iterates through all the columns set to true in a given row of
1667    /// the matrix.
1668    pub fn iter(&self, row: R) -> impl Iterator<Item = C> {
1669        self.row(row).into_iter().flat_map(|r| r.iter())
1670    }
1671
1672    pub fn row(&self, row: R) -> Option<&DenseBitSet<C>> {
1673        self.rows.get(row)?.as_ref()
1674    }
1675
1676    /// Intersects `row` with `set`. `set` can be either `DenseBitSet` or
1677    /// `ChunkedBitSet`. Has no effect if `row` does not exist.
1678    ///
1679    /// Returns true if the row was changed.
1680    pub fn intersect_row<Set>(&mut self, row: R, set: &Set) -> bool
1681    where
1682        DenseBitSet<C>: BitRelations<Set>,
1683    {
1684        match self.rows.get_mut(row) {
1685            Some(Some(row)) => row.intersect(set),
1686            _ => false,
1687        }
1688    }
1689
1690    /// Subtracts `set` from `row`. `set` can be either `DenseBitSet` or
1691    /// `ChunkedBitSet`. Has no effect if `row` does not exist.
1692    ///
1693    /// Returns true if the row was changed.
1694    pub fn subtract_row<Set>(&mut self, row: R, set: &Set) -> bool
1695    where
1696        DenseBitSet<C>: BitRelations<Set>,
1697    {
1698        match self.rows.get_mut(row) {
1699            Some(Some(row)) => row.subtract(set),
1700            _ => false,
1701        }
1702    }
1703
1704    /// Unions `row` with `set`. `set` can be either `DenseBitSet` or
1705    /// `ChunkedBitSet`.
1706    ///
1707    /// Returns true if the row was changed.
1708    pub fn union_row<Set>(&mut self, row: R, set: &Set) -> bool
1709    where
1710        DenseBitSet<C>: BitRelations<Set>,
1711    {
1712        self.ensure_row(row).union(set)
1713    }
1714}
1715
1716#[inline]
1717fn num_words<T: Idx>(domain_size: T) -> usize {
1718    domain_size.index().div_ceil(WORD_BITS)
1719}
1720
1721#[inline]
1722fn num_chunks<T: Idx>(domain_size: T) -> usize {
1723    if !(domain_size.index() > 0) {
    ::core::panicking::panic("assertion failed: domain_size.index() > 0")
};assert!(domain_size.index() > 0);
1724    domain_size.index().div_ceil(CHUNK_BITS)
1725}
1726
1727#[inline]
1728fn word_index_and_mask<T: Idx>(elem: T) -> (usize, Word) {
1729    let elem = elem.index();
1730    let word_index = elem / WORD_BITS;
1731    let mask = 1 << (elem % WORD_BITS);
1732    (word_index, mask)
1733}
1734
1735#[inline]
1736fn chunk_index<T: Idx>(elem: T) -> usize {
1737    elem.index() / CHUNK_BITS
1738}
1739
1740#[inline]
1741fn chunk_word_index_and_mask<T: Idx>(elem: T) -> (usize, Word) {
1742    let chunk_elem = elem.index() % CHUNK_BITS;
1743    word_index_and_mask(chunk_elem)
1744}
1745
1746fn clear_excess_bits_in_final_word(domain_size: usize, words: &mut [Word]) {
1747    let num_bits_in_final_word = domain_size % WORD_BITS;
1748    if num_bits_in_final_word > 0 {
1749        let mask = (1 << num_bits_in_final_word) - 1;
1750        words[words.len() - 1] &= mask;
1751    }
1752}
1753
1754#[inline]
1755fn max_bit(word: Word) -> usize {
1756    WORD_BITS - 1 - word.leading_zeros() as usize
1757}
1758
1759#[inline]
1760fn count_ones(words: &[Word]) -> usize {
1761    words.iter().map(|word| word.count_ones() as usize).sum()
1762}