use super::hash::LevelIndex;
use std::fmt;
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct SmallBitmap(u32);
impl fmt::Debug for SmallBitmap {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SmallBitmap {:032b}", self.0)
}
}
impl SmallBitmap {
pub const fn new() -> Self {
SmallBitmap(0u32)
}
#[inline]
pub const fn is_empty(self) -> bool {
self.0 == 0
}
#[inline]
pub const fn present(self) -> usize {
self.0.count_ones() as usize
}
#[inline]
pub const fn once(b: LevelIndex) -> Self {
SmallBitmap(b.mask())
}
#[inline]
pub const fn get_index_sparse(self, b: LevelIndex) -> ArrayIndex {
let mask = b.mask();
if self.0 & mask == 0 {
ArrayIndex::not_found()
} else {
ArrayIndex::create((self.0 & (mask - 1)).count_ones() as usize)
}
}
#[inline]
pub const fn get_sparse_pos(self, b: LevelIndex) -> ArrayIndex {
let mask = b.mask();
ArrayIndex::create((self.0 & (mask - 1)).count_ones() as usize)
}
pub const fn is_set(self, b: LevelIndex) -> bool {
(self.0 & b.mask()) != 0
}
#[inline]
pub const fn set_index(self, b: LevelIndex) -> Self {
SmallBitmap(self.0 | b.mask())
}
#[inline]
pub const fn clear_index(self, b: LevelIndex) -> Self {
SmallBitmap(self.0 & !b.mask())
}
}
#[derive(Debug, Clone, Copy)]
pub struct ArrayIndex(usize);
impl ArrayIndex {
pub const fn is_not_found(self) -> bool {
self.0 == 0xff
}
pub const fn get_found(self) -> usize {
self.0
}
pub const fn not_found() -> Self {
ArrayIndex(0xff)
}
pub const fn create(s: usize) -> Self {
ArrayIndex(s)
}
}