Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mask): unset bit, count set bits, index of the first set bit #58

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,27 @@ 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.
#[inline]
pub const fn first_set_bit_index(self) -> u8 {
self.0.trailing_zeros() as u8
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this return Option? no bits might be set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep right, fixed


/// 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);
}
}
Loading