use crate::BitBuffer;
#[derive(Clone, Copy)]
pub(super) enum MaskBits<'a> {
Bitmap(&'a BitBuffer),
Words { words: &'a [u64], len: usize },
}
#[inline]
pub(super) fn for_each_mask_word(bits: MaskBits<'_>, f: impl FnMut(u64, usize, usize)) {
match bits {
MaskBits::Bitmap(bits) => for_each_bitmap_word(bits, f),
MaskBits::Words { words, len } => for_each_slice_word(words, len, f),
}
}
#[inline]
pub(super) fn for_each_set_mask_word(bits: MaskBits<'_>, mut f: impl FnMut(u64, usize, usize)) {
match bits {
MaskBits::Bitmap(bits) => {
let unaligned = bits.unaligned_chunks();
let lead = unaligned.lead_padding();
let mut base = 0;
if let Some(prefix) = unaligned.prefix() {
let len = (64 - lead).min(bits.len());
let word = prefix >> lead;
if word != 0 {
f(word, base, len);
}
base += len;
}
let chunks = unaligned.chunks();
for_each_set_word(chunks, |word, index| f(word, base + index * 64, 64));
base += chunks.len() * 64;
if let Some(suffix) = unaligned.suffix()
&& suffix != 0
{
f(suffix, base, bits.len() - base);
}
}
MaskBits::Words { words, len } => {
debug_assert_eq!(words.len(), len.div_ceil(64));
for_each_set_word(words, |word, index| {
let word_start = index * 64;
f(word, word_start, (len - word_start).min(64));
});
}
}
}
#[inline]
fn for_each_set_word(words: &[u64], mut f: impl FnMut(u64, usize)) {
let mut index = 0;
while let Some(next) = next_set_word(words, index) {
f(words[next], next);
index = next + 1;
}
}
const SCAN_BLOCK_WORDS: usize = 8;
#[inline]
fn next_set_word(words: &[u64], from: usize) -> Option<usize> {
if words.get(from).is_some_and(|&word| word != 0) {
return Some(from);
}
let mut index = from;
while let Some(block) = words[index..].first_chunk::<SCAN_BLOCK_WORDS>() {
if block.iter().fold(0, |any, &word| any | word) != 0 {
break;
}
index += SCAN_BLOCK_WORDS;
}
words[index..]
.iter()
.position(|&word| word != 0)
.map(|skip| index + skip)
}
pub(super) fn all_set_mask_words(bits: MaskBits<'_>, mut f: impl FnMut(u64) -> bool) -> bool {
match bits {
MaskBits::Bitmap(bits) => {
let unaligned = bits.unaligned_chunks();
unaligned.prefix().is_none_or(|word| word == 0 || f(word))
&& all_set_words(unaligned.chunks(), &mut f)
&& unaligned.suffix().is_none_or(|word| word == 0 || f(word))
}
MaskBits::Words { words, .. } => all_set_words(words, &mut f),
}
}
#[inline]
fn all_set_words(words: &[u64], f: &mut impl FnMut(u64) -> bool) -> bool {
let mut index = 0;
while let Some(next) = next_set_word(words, index) {
if !f(words[next]) {
return false;
}
index = next + 1;
}
true
}
#[inline]
fn for_each_slice_word(words: &[u64], len: usize, mut f: impl FnMut(u64, usize, usize)) {
debug_assert_eq!(words.len(), len.div_ceil(64));
for (word_idx, &word) in words.iter().enumerate() {
let word_start = word_idx * 64;
f(word, word_start, (len - word_start).min(64));
}
}
#[inline]
fn for_each_bitmap_word(bits: &BitBuffer, mut f: impl FnMut(u64, usize, usize)) {
let unaligned = bits.unaligned_chunks();
let lead = unaligned.lead_padding();
let mut base = 0;
if let Some(prefix) = unaligned.prefix() {
let len = (64 - lead).min(bits.len());
f(prefix >> lead, base, len);
base += len;
}
for &word in unaligned.chunks() {
f(word, base, 64);
base += 64;
}
if let Some(suffix) = unaligned.suffix() {
let len = bits.len() - base;
f(suffix, base, len);
base += len;
}
debug_assert_eq!(base, bits.len());
}
#[inline]
pub(super) fn low_bits_mask(len: usize) -> u64 {
debug_assert!(len <= 64);
if len == 64 {
u64::MAX
} else {
(1u64 << len) - 1
}
}