Skip to content

Commit

Permalink
feat(mask): unset bit, count set bits, index of the first set bit (#58)
Browse files Browse the repository at this point in the history
* feat(mask): unset bit

* more mask methods

* return option if the mask is empty
  • Loading branch information
shekhirin authored Oct 16, 2024
1 parent 8a118b6 commit db0def2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,31 @@ impl TrieMask {
self.0 == 0
}

/// Returns the number of bits set in the mask.
#[inline]
pub const fn count_bits(self) -> u8 {
self.0.count_ones() as u8
}

/// Returns the index of the first bit set in the mask, or `None` if the mask is empty.
#[inline]
pub const fn first_set_bit_index(self) -> Option<u8> {
if self.is_empty() {
None
} else {
Some(self.0.trailing_zeros() as u8)
}
}

/// Set bit at a specified index.
#[inline]
pub fn set_bit(&mut self, index: u8) {
self.0 |= 1u16 << index;
}

/// Unset bit at a specified index.
#[inline]
pub fn unset_bit(&mut self, index: u8) {
self.0 &= !(1u16 << index);
}
}

0 comments on commit db0def2

Please sign in to comment.