1 use super::imp
::{BitMaskWord, BITMASK_MASK, BITMASK_STRIDE}
;
2 #[cfg(feature = "nightly")]
5 /// A bit mask which contains the result of a `Match` operation on a `Group` and
6 /// allows iterating through them.
8 /// The bit mask is arranged so that low-order bits represent lower memory
9 /// addresses for group match results.
11 /// For implementation reasons, the bits in the set may be sparsely packed, so
12 /// that there is only one bit-per-byte used (the high bit, 7). If this is the
13 /// case, `BITMASK_STRIDE` will be 8 to indicate a divide-by-8 should be
14 /// performed on counts/indices to normalize this difference. `BITMASK_MASK` is
15 /// similarly a mask of all the actually-used bits.
16 #[derive(Copy, Clone)]
17 pub struct BitMask(pub BitMaskWord
);
19 #[allow(clippy::use_self)]
21 /// Returns a new `BitMask` with all bits inverted.
24 pub fn invert(self) -> Self {
25 BitMask(self.0 ^ BITMASK_MASK
)
28 /// Flip the bit in the mask for the entry at the given index.
30 /// Returns the bit's previous state.
32 #[allow(clippy::cast_ptr_alignment)]
33 #[cfg(feature = "raw")]
34 pub unsafe fn flip(&mut self, index
: usize) -> bool
{
35 // NOTE: The + BITMASK_STRIDE - 1 is to set the high bit.
36 let mask
= 1 << (index
* BITMASK_STRIDE
+ BITMASK_STRIDE
- 1);
38 // The bit was set if the bit is now 0.
42 /// Returns a new `BitMask` with the lowest bit removed.
45 pub fn remove_lowest_bit(self) -> Self {
46 BitMask(self.0 & (self.0 - 1))
48 /// Returns whether the `BitMask` has at least one set bit.
50 pub fn any_bit_set(self) -> bool
{
54 /// Returns the first set bit in the `BitMask`, if there is one.
56 pub fn lowest_set_bit(self) -> Option
<usize> {
60 Some(unsafe { self.lowest_set_bit_nonzero() }
)
64 /// Returns the first set bit in the `BitMask`, if there is one. The
65 /// bitmask must not be empty.
67 #[cfg(feature = "nightly")]
68 pub unsafe fn lowest_set_bit_nonzero(self) -> usize {
69 intrinsics
::cttz_nonzero(self.0) as usize / BITMASK_STRIDE
72 #[cfg(not(feature = "nightly"))]
73 pub unsafe fn lowest_set_bit_nonzero(self) -> usize {
77 /// Returns the number of trailing zeroes in the `BitMask`.
79 pub fn trailing_zeros(self) -> usize {
80 // ARM doesn't have a trailing_zeroes instruction, and instead uses
81 // reverse_bits (RBIT) + leading_zeroes (CLZ). However older ARM
82 // versions (pre-ARMv7) don't have RBIT and need to emulate it
83 // instead. Since we only have 1 bit set in each byte on ARM, we can
84 // use swap_bytes (REV) + leading_zeroes instead.
85 if cfg
!(target_arch
= "arm") && BITMASK_STRIDE
% 8 == 0 {
86 self.0.swap_bytes().leading_zeros() as usize / BITMASK_STRIDE
88 self.0.trailing_zeros() as usize / BITMASK_STRIDE
92 /// Returns the number of leading zeroes in the `BitMask`.
94 pub fn leading_zeros(self) -> usize {
95 self.0.leading_zeros() as usize / BITMASK_STRIDE
99 impl IntoIterator
for BitMask
{
101 type IntoIter
= BitMaskIter
;
104 fn into_iter(self) -> BitMaskIter
{
109 /// Iterator over the contents of a `BitMask`, returning the indicies of set
111 pub struct BitMaskIter(BitMask
);
113 impl Iterator
for BitMaskIter
{
117 fn next(&mut self) -> Option
<usize> {
118 let bit
= self.0.lowest_set_bit()?
;
119 self.0 = self.0.remove_lowest_bit();