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