Coverage Report

Created: 2026-11-03 08:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.16.1/src/control/bitmask.rs
Line
Count
Source
1
use super::group::{
2
    BitMaskWord, NonZeroBitMaskWord, BITMASK_ITER_MASK, BITMASK_MASK, BITMASK_STRIDE,
3
};
4
5
/// A bit mask which contains the result of a `Match` operation on a `Group` and
6
/// allows iterating through them.
7
///
8
/// The bit mask is arranged so that low-order bits represent lower memory
9
/// addresses for group match results.
10
///
11
/// For implementation reasons, the bits in the set may be sparsely packed with
12
/// groups of 8 bits representing one element. If any of these bits are non-zero
13
/// then this element is considered to true in the mask. If this is the
14
/// case, `BITMASK_STRIDE` will be 8 to indicate a divide-by-8 should be
15
/// performed on counts/indices to normalize this difference. `BITMASK_MASK` is
16
/// similarly a mask of all the actually-used bits.
17
///
18
/// To iterate over a bit mask, it must be converted to a form where only 1 bit
19
/// is set per element. This is done by applying `BITMASK_ITER_MASK` on the
20
/// mask bits.
21
#[derive(Copy, Clone)]
22
pub(crate) struct BitMask(pub(crate) BitMaskWord);
23
24
#[allow(clippy::use_self)]
25
impl BitMask {
26
    /// Returns a new `BitMask` with all bits inverted.
27
    #[inline]
28
    #[must_use]
29
    #[allow(dead_code)]
30
177k
    pub(crate) fn invert(self) -> Self {
31
177k
        BitMask(self.0 ^ BITMASK_MASK)
32
177k
    }
<hashbrown::control::bitmask::BitMask>::invert
Line
Count
Source
30
177k
    pub(crate) fn invert(self) -> Self {
31
177k
        BitMask(self.0 ^ BITMASK_MASK)
32
177k
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::invert
33
34
    /// Returns a new `BitMask` with the lowest bit removed.
35
    #[inline]
36
    #[must_use]
37
1.14M
    fn remove_lowest_bit(self) -> Self {
38
1.14M
        BitMask(self.0 & (self.0 - 1))
39
1.14M
    }
<hashbrown::control::bitmask::BitMask>::remove_lowest_bit
Line
Count
Source
37
1.03M
    fn remove_lowest_bit(self) -> Self {
38
1.03M
        BitMask(self.0 & (self.0 - 1))
39
1.03M
    }
<hashbrown::control::bitmask::BitMask>::remove_lowest_bit
Line
Count
Source
37
110k
    fn remove_lowest_bit(self) -> Self {
38
110k
        BitMask(self.0 & (self.0 - 1))
39
110k
    }
40
41
    /// Returns whether the `BitMask` has at least one set bit.
42
    #[inline]
43
1.46M
    pub(crate) fn any_bit_set(self) -> bool {
44
1.46M
        self.0 != 0
45
1.46M
    }
<hashbrown::control::bitmask::BitMask>::any_bit_set
Line
Count
Source
43
830k
    pub(crate) fn any_bit_set(self) -> bool {
44
830k
        self.0 != 0
45
830k
    }
<hashbrown::control::bitmask::BitMask>::any_bit_set
Line
Count
Source
43
634k
    pub(crate) fn any_bit_set(self) -> bool {
44
634k
        self.0 != 0
45
634k
    }
46
47
    /// Returns the first set bit in the `BitMask`, if there is one.
48
    #[inline]
49
3.68M
    pub(crate) fn lowest_set_bit(self) -> Option<usize> {
50
3.68M
        if let Some(nonzero) = NonZeroBitMaskWord::new(self.0) {
51
2.20M
            Some(Self::nonzero_trailing_zeros(nonzero))
52
        } else {
53
1.47M
            None
54
        }
55
3.68M
    }
<hashbrown::control::bitmask::BitMask>::lowest_set_bit
Line
Count
Source
49
2.93M
    pub(crate) fn lowest_set_bit(self) -> Option<usize> {
50
2.93M
        if let Some(nonzero) = NonZeroBitMaskWord::new(self.0) {
51
2.09M
            Some(Self::nonzero_trailing_zeros(nonzero))
52
        } else {
53
843k
            None
54
        }
55
2.93M
    }
<hashbrown::control::bitmask::BitMask>::lowest_set_bit
Line
Count
Source
49
744k
    pub(crate) fn lowest_set_bit(self) -> Option<usize> {
50
744k
        if let Some(nonzero) = NonZeroBitMaskWord::new(self.0) {
51
110k
            Some(Self::nonzero_trailing_zeros(nonzero))
52
        } else {
53
634k
            None
54
        }
55
744k
    }
56
57
    /// Returns the number of trailing zeroes in the `BitMask`.
58
    #[inline]
59
30.0k
    pub(crate) fn trailing_zeros(self) -> usize {
60
        // ARM doesn't have a trailing_zeroes instruction, and instead uses
61
        // reverse_bits (RBIT) + leading_zeroes (CLZ). However older ARM
62
        // versions (pre-ARMv7) don't have RBIT and need to emulate it
63
        // instead. Since we only have 1 bit set in each byte on ARM, we can
64
        // use swap_bytes (REV) + leading_zeroes instead.
65
30.0k
        if cfg!(target_arch = "arm") && BITMASK_STRIDE % 8 == 0 {
66
0
            self.0.swap_bytes().leading_zeros() as usize / BITMASK_STRIDE
67
        } else {
68
30.0k
            self.0.trailing_zeros() as usize / BITMASK_STRIDE
69
        }
70
30.0k
    }
<hashbrown::control::bitmask::BitMask>::trailing_zeros
Line
Count
Source
59
30.0k
    pub(crate) fn trailing_zeros(self) -> usize {
60
        // ARM doesn't have a trailing_zeroes instruction, and instead uses
61
        // reverse_bits (RBIT) + leading_zeroes (CLZ). However older ARM
62
        // versions (pre-ARMv7) don't have RBIT and need to emulate it
63
        // instead. Since we only have 1 bit set in each byte on ARM, we can
64
        // use swap_bytes (REV) + leading_zeroes instead.
65
30.0k
        if cfg!(target_arch = "arm") && BITMASK_STRIDE % 8 == 0 {
66
0
            self.0.swap_bytes().leading_zeros() as usize / BITMASK_STRIDE
67
        } else {
68
30.0k
            self.0.trailing_zeros() as usize / BITMASK_STRIDE
69
        }
70
30.0k
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::trailing_zeros
71
72
    /// Same as above but takes a `NonZeroBitMaskWord`.
73
    #[inline]
74
2.20M
    fn nonzero_trailing_zeros(nonzero: NonZeroBitMaskWord) -> usize {
75
2.20M
        if cfg!(target_arch = "arm") && BITMASK_STRIDE % 8 == 0 {
76
            // SAFETY: A byte-swapped non-zero value is still non-zero.
77
0
            let swapped = unsafe { NonZeroBitMaskWord::new_unchecked(nonzero.get().swap_bytes()) };
78
0
            swapped.leading_zeros() as usize / BITMASK_STRIDE
79
        } else {
80
2.20M
            nonzero.trailing_zeros() as usize / BITMASK_STRIDE
81
        }
82
2.20M
    }
<hashbrown::control::bitmask::BitMask>::nonzero_trailing_zeros
Line
Count
Source
74
2.09M
    fn nonzero_trailing_zeros(nonzero: NonZeroBitMaskWord) -> usize {
75
2.09M
        if cfg!(target_arch = "arm") && BITMASK_STRIDE % 8 == 0 {
76
            // SAFETY: A byte-swapped non-zero value is still non-zero.
77
0
            let swapped = unsafe { NonZeroBitMaskWord::new_unchecked(nonzero.get().swap_bytes()) };
78
0
            swapped.leading_zeros() as usize / BITMASK_STRIDE
79
        } else {
80
2.09M
            nonzero.trailing_zeros() as usize / BITMASK_STRIDE
81
        }
82
2.09M
    }
<hashbrown::control::bitmask::BitMask>::nonzero_trailing_zeros
Line
Count
Source
74
110k
    fn nonzero_trailing_zeros(nonzero: NonZeroBitMaskWord) -> usize {
75
110k
        if cfg!(target_arch = "arm") && BITMASK_STRIDE % 8 == 0 {
76
            // SAFETY: A byte-swapped non-zero value is still non-zero.
77
0
            let swapped = unsafe { NonZeroBitMaskWord::new_unchecked(nonzero.get().swap_bytes()) };
78
0
            swapped.leading_zeros() as usize / BITMASK_STRIDE
79
        } else {
80
110k
            nonzero.trailing_zeros() as usize / BITMASK_STRIDE
81
        }
82
110k
    }
83
84
    /// Returns the number of leading zeroes in the `BitMask`.
85
    #[inline]
86
30.0k
    pub(crate) fn leading_zeros(self) -> usize {
87
30.0k
        self.0.leading_zeros() as usize / BITMASK_STRIDE
88
30.0k
    }
<hashbrown::control::bitmask::BitMask>::leading_zeros
Line
Count
Source
86
30.0k
    pub(crate) fn leading_zeros(self) -> usize {
87
30.0k
        self.0.leading_zeros() as usize / BITMASK_STRIDE
88
30.0k
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::leading_zeros
89
}
90
91
impl IntoIterator for BitMask {
92
    type Item = usize;
93
    type IntoIter = BitMaskIter;
94
95
    #[inline]
96
2.42M
    fn into_iter(self) -> BitMaskIter {
97
        // A BitMask only requires each element (group of bits) to be non-zero.
98
        // However for iteration we need each element to only contain 1 bit.
99
2.42M
        BitMaskIter(BitMask(self.0 & BITMASK_ITER_MASK))
100
2.42M
    }
<hashbrown::control::bitmask::BitMask as core::iter::traits::collect::IntoIterator>::into_iter
Line
Count
Source
96
2.41M
    fn into_iter(self) -> BitMaskIter {
97
        // A BitMask only requires each element (group of bits) to be non-zero.
98
        // However for iteration we need each element to only contain 1 bit.
99
2.41M
        BitMaskIter(BitMask(self.0 & BITMASK_ITER_MASK))
100
2.41M
    }
<hashbrown::control::bitmask::BitMask as core::iter::traits::collect::IntoIterator>::into_iter
Line
Count
Source
96
9.82k
    fn into_iter(self) -> BitMaskIter {
97
        // A BitMask only requires each element (group of bits) to be non-zero.
98
        // However for iteration we need each element to only contain 1 bit.
99
9.82k
        BitMaskIter(BitMask(self.0 & BITMASK_ITER_MASK))
100
9.82k
    }
101
}
102
103
/// Iterator over the contents of a `BitMask`, returning the indices of set
104
/// bits.
105
#[derive(Clone)]
106
pub(crate) struct BitMaskIter(pub(crate) BitMask);
107
108
impl Iterator for BitMaskIter {
109
    type Item = usize;
110
111
    #[inline]
112
2.61M
    fn next(&mut self) -> Option<usize> {
113
2.61M
        let bit = self.0.lowest_set_bit()?;
114
1.14M
        self.0 = self.0.remove_lowest_bit();
115
1.14M
        Some(bit)
116
2.61M
    }
<hashbrown::control::bitmask::BitMaskIter as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
112
1.87M
    fn next(&mut self) -> Option<usize> {
113
1.87M
        let bit = self.0.lowest_set_bit()?;
114
1.03M
        self.0 = self.0.remove_lowest_bit();
115
1.03M
        Some(bit)
116
1.87M
    }
<hashbrown::control::bitmask::BitMaskIter as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
112
744k
    fn next(&mut self) -> Option<usize> {
113
744k
        let bit = self.0.lowest_set_bit()?;
114
110k
        self.0 = self.0.remove_lowest_bit();
115
110k
        Some(bit)
116
744k
    }
117
}