Skip to main content

rustc_index/
bit_set.rs

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