Coverage Report

Created: 2025-11-28 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/hashbrown-0.15.2/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
24.1k
    pub(crate) fn invert(self) -> Self {
31
24.1k
        BitMask(self.0 ^ BITMASK_MASK)
32
24.1k
    }
<hashbrown::control::bitmask::BitMask>::invert
Line
Count
Source
30
24.1k
    pub(crate) fn invert(self) -> Self {
31
24.1k
        BitMask(self.0 ^ BITMASK_MASK)
32
24.1k
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::invert
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::invert
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::invert
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.15M
    fn remove_lowest_bit(self) -> Self {
38
1.15M
        BitMask(self.0 & (self.0 - 1))
39
1.15M
    }
<hashbrown::control::bitmask::BitMask>::remove_lowest_bit
Line
Count
Source
37
1.15M
    fn remove_lowest_bit(self) -> Self {
38
1.15M
        BitMask(self.0 & (self.0 - 1))
39
1.15M
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::remove_lowest_bit
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::remove_lowest_bit
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::remove_lowest_bit
<hashbrown::control::bitmask::BitMask>::remove_lowest_bit
Line
Count
Source
37
2.54k
    fn remove_lowest_bit(self) -> Self {
38
2.54k
        BitMask(self.0 & (self.0 - 1))
39
2.54k
    }
40
41
    /// Returns whether the `BitMask` has at least one set bit.
42
    #[inline]
43
85.6k
    pub(crate) fn any_bit_set(self) -> bool {
44
85.6k
        self.0 != 0
45
85.6k
    }
<hashbrown::control::bitmask::BitMask>::any_bit_set
Line
Count
Source
43
33.4k
    pub(crate) fn any_bit_set(self) -> bool {
44
33.4k
        self.0 != 0
45
33.4k
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::any_bit_set
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::any_bit_set
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::any_bit_set
<hashbrown::control::bitmask::BitMask>::any_bit_set
Line
Count
Source
43
52.2k
    pub(crate) fn any_bit_set(self) -> bool {
44
52.2k
        self.0 != 0
45
52.2k
    }
46
47
    /// Returns the first set bit in the `BitMask`, if there is one.
48
    #[inline]
49
1.35M
    pub(crate) fn lowest_set_bit(self) -> Option<usize> {
50
1.35M
        if let Some(nonzero) = NonZeroBitMaskWord::new(self.0) {
51
1.26M
            Some(Self::nonzero_trailing_zeros(nonzero))
52
        } else {
53
88.5k
            None
54
        }
55
1.35M
    }
<hashbrown::control::bitmask::BitMask>::lowest_set_bit
Line
Count
Source
49
1.30M
    pub(crate) fn lowest_set_bit(self) -> Option<usize> {
50
1.30M
        if let Some(nonzero) = NonZeroBitMaskWord::new(self.0) {
51
1.26M
            Some(Self::nonzero_trailing_zeros(nonzero))
52
        } else {
53
36.3k
            None
54
        }
55
1.30M
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::lowest_set_bit
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::lowest_set_bit
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::lowest_set_bit
<hashbrown::control::bitmask::BitMask>::lowest_set_bit
Line
Count
Source
49
54.8k
    pub(crate) fn lowest_set_bit(self) -> Option<usize> {
50
54.8k
        if let Some(nonzero) = NonZeroBitMaskWord::new(self.0) {
51
2.54k
            Some(Self::nonzero_trailing_zeros(nonzero))
52
        } else {
53
52.2k
            None
54
        }
55
54.8k
    }
56
57
    /// Returns the number of trailing zeroes in the `BitMask`.
58
    #[inline]
59
204
    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
204
        if cfg!(target_arch = "arm") && BITMASK_STRIDE % 8 == 0 {
66
0
            self.0.swap_bytes().leading_zeros() as usize / BITMASK_STRIDE
67
        } else {
68
204
            self.0.trailing_zeros() as usize / BITMASK_STRIDE
69
        }
70
204
    }
<hashbrown::control::bitmask::BitMask>::trailing_zeros
Line
Count
Source
59
204
    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
204
        if cfg!(target_arch = "arm") && BITMASK_STRIDE % 8 == 0 {
66
0
            self.0.swap_bytes().leading_zeros() as usize / BITMASK_STRIDE
67
        } else {
68
204
            self.0.trailing_zeros() as usize / BITMASK_STRIDE
69
        }
70
204
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::trailing_zeros
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::trailing_zeros
71
72
    /// Same as above but takes a `NonZeroBitMaskWord`.
73
    #[inline]
74
1.26M
    fn nonzero_trailing_zeros(nonzero: NonZeroBitMaskWord) -> usize {
75
1.26M
        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
1.26M
            nonzero.trailing_zeros() as usize / BITMASK_STRIDE
81
        }
82
1.26M
    }
<hashbrown::control::bitmask::BitMask>::nonzero_trailing_zeros
Line
Count
Source
74
1.26M
    fn nonzero_trailing_zeros(nonzero: NonZeroBitMaskWord) -> usize {
75
1.26M
        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
1.26M
            nonzero.trailing_zeros() as usize / BITMASK_STRIDE
81
        }
82
1.26M
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::nonzero_trailing_zeros
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::nonzero_trailing_zeros
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::nonzero_trailing_zeros
<hashbrown::control::bitmask::BitMask>::nonzero_trailing_zeros
Line
Count
Source
74
2.54k
    fn nonzero_trailing_zeros(nonzero: NonZeroBitMaskWord) -> usize {
75
2.54k
        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.54k
            nonzero.trailing_zeros() as usize / BITMASK_STRIDE
81
        }
82
2.54k
    }
83
84
    /// Returns the number of leading zeroes in the `BitMask`.
85
    #[inline]
86
204
    pub(crate) fn leading_zeros(self) -> usize {
87
204
        self.0.leading_zeros() as usize / BITMASK_STRIDE
88
204
    }
<hashbrown::control::bitmask::BitMask>::leading_zeros
Line
Count
Source
86
204
    pub(crate) fn leading_zeros(self) -> usize {
87
204
        self.0.leading_zeros() as usize / BITMASK_STRIDE
88
204
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask>::leading_zeros
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
1.21M
    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
1.21M
        BitMaskIter(BitMask(self.0 & BITMASK_ITER_MASK))
100
1.21M
    }
<hashbrown::control::bitmask::BitMask as core::iter::traits::collect::IntoIterator>::into_iter
Line
Count
Source
96
1.21M
    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
1.21M
        BitMaskIter(BitMask(self.0 & BITMASK_ITER_MASK))
100
1.21M
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask as core::iter::traits::collect::IntoIterator>::into_iter
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask as core::iter::traits::collect::IntoIterator>::into_iter
Unexecuted instantiation: <hashbrown::control::bitmask::BitMask as core::iter::traits::collect::IntoIterator>::into_iter
<hashbrown::control::bitmask::BitMask as core::iter::traits::collect::IntoIterator>::into_iter
Line
Count
Source
96
1.35k
    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
1.35k
        BitMaskIter(BitMask(self.0 & BITMASK_ITER_MASK))
100
1.35k
    }
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
1.24M
    fn next(&mut self) -> Option<usize> {
113
1.24M
        let bit = self.0.lowest_set_bit()?;
114
1.15M
        self.0 = self.0.remove_lowest_bit();
115
1.15M
        Some(bit)
116
1.24M
    }
<hashbrown::control::bitmask::BitMaskIter as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
112
1.18M
    fn next(&mut self) -> Option<usize> {
113
1.18M
        let bit = self.0.lowest_set_bit()?;
114
1.15M
        self.0 = self.0.remove_lowest_bit();
115
1.15M
        Some(bit)
116
1.18M
    }
Unexecuted instantiation: <hashbrown::control::bitmask::BitMaskIter as core::iter::traits::iterator::Iterator>::next
Unexecuted instantiation: <hashbrown::control::bitmask::BitMaskIter as core::iter::traits::iterator::Iterator>::next
Unexecuted instantiation: <hashbrown::control::bitmask::BitMaskIter as core::iter::traits::iterator::Iterator>::next
<hashbrown::control::bitmask::BitMaskIter as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
112
54.8k
    fn next(&mut self) -> Option<usize> {
113
54.8k
        let bit = self.0.lowest_set_bit()?;
114
2.54k
        self.0 = self.0.remove_lowest_bit();
115
2.54k
        Some(bit)
116
54.8k
    }
117
}