pub mod fmt;
pub mod from;
pub mod macros;
pub mod ops;
pub mod refs;
pub mod ptr;
mod traits;
#[derive(Clone)]
pub struct Bitmap<const BYTES: usize> {
bits: [u8; BYTES],
}
pub trait BitsManage {
fn count(&self) -> usize;
fn find_first_one(&self) -> Option<usize>;
fn find_first_zero(&self) -> Option<usize>;
fn get_bool(&self, index: usize) -> bool;
fn set(&mut self, index: usize) -> &mut Self;
fn reset(&mut self, index: usize) -> &mut Self;
fn flip(&mut self, index: usize) -> &mut Self;
fn set_all(&mut self) -> &mut Self;
fn reset_all(&mut self) -> &mut Self;
fn flip_all(&mut self) -> &mut Self;
#[inline]
fn all(&self) -> bool {
self.find_first_zero().is_none()
}
#[inline]
fn any(&self) -> bool {
self.find_first_one().is_some()
}
#[inline]
fn none(&self) -> bool {
self.find_first_one().is_none()
}
#[inline]
fn get_01(&self, index: usize) -> u8 {
self.get_bool(index).into()
}
#[inline]
fn test(&self, index: usize) -> bool {
self.get_bool(index)
}
}
impl<const BYTES: usize> Bitmap<BYTES> {
pub fn new() -> Self {
Bitmap { bits: [0; BYTES] }
}
#[inline]
pub const fn bit_len(&self) -> usize {
BYTES * 8
}
#[inline]
pub const fn byte_len(&self) -> usize {
BYTES
}
}
impl<const BYTES: usize> BitsManage for Bitmap<BYTES> {
#[inline]
fn get_bool(&self, index: usize) -> bool {
self.__get_bool(__idx_get_byte(index), __idx_get_bit(index))
}
#[inline]
fn find_first_one(&self) -> Option<usize> {
let mut index: usize = 0;
for b in &self.bits {
let test = (*b) & (!(*b)).wrapping_add(1);
index += match test {
0 => 8,
1 => 0,
2 => 1,
4 => 2,
8 => 3,
16 => 4,
32 => 5,
64 => 6,
128 => 7,
_ => panic!("Unexpected arithmetic error"),
};
if test > 0 {
break;
}
}
match index >= BYTES * 8 {
true => None,
false => Some(index),
}
}
#[inline]
fn find_first_zero(&self) -> Option<usize> {
let mut index: usize = 0;
for b in &self.bits {
let test = (*b).wrapping_add(1) & !(*b);
index += match test {
0 => 8,
1 => 0,
2 => 1,
4 => 2,
8 => 3,
16 => 4,
32 => 5,
64 => 6,
128 => 7,
_ => panic!("Unexpected arithmetic error"),
};
if test > 0 {
break;
}
}
match index >= BYTES * 8 {
true => None,
false => Some(index),
}
}
#[inline]
fn count(&self) -> usize {
let mut cnt: usize = 0;
for b in &self.bits {
let mut temp = *b;
temp = ((temp & 0b10101010) >> 1) + (temp & 0b01010101);
temp = ((temp & 0b11001100) >> 2) + (temp & 0b00110011);
temp = (temp >> 4) + (temp & 0b1111);
cnt += temp as usize;
}
cnt
}
fn set(&mut self, index: usize) -> &mut Self {
if __out_bound(BYTES, index) {
panic!("Bitmap: setting out of range");
}
let content = 1u8 << __idx_get_bit(index);
let byte = self.__get_mut_u8(__idx_get_byte(index));
__byte_or_u8(byte, content);
self
}
fn reset(&mut self, index: usize) -> &mut Self {
if __out_bound(BYTES, index) {
panic!("Bitmap: resetting out of range");
}
let content = !(1u8 << __idx_get_bit(index));
let byte = self.__get_mut_u8(__idx_get_byte(index));
__byte_and_u8(byte, content);
self
}
fn flip(&mut self, index: usize) -> &mut Self {
if __out_bound(BYTES, index) {
panic!("Bitmap: flipping out of range");
}
let byte = self.__get_mut_u8(__idx_get_byte(index));
*byte ^= 1 << __idx_get_bit(index);
self
}
fn set_all(&mut self) -> &mut Self {
*&mut self.bits = [255; BYTES];
self
}
fn reset_all(&mut self) -> &mut Self {
*&mut self.bits = [0; BYTES];
self
}
fn flip_all(&mut self) -> &mut Self {
let arr = &mut self.bits;
for i in arr {
*i = !*i;
}
self
}
}
impl<const BYTES: usize> Bitmap<BYTES> {
#[inline]
fn __get_bool(&self, byte: usize, bit: usize) -> bool {
&self.bits[byte] & (1 << bit) != 0
}
#[inline]
fn __copy_u8<'map>(&'map self, byte: usize) -> u8 {
self.bits[byte].clone()
}
#[inline]
fn __get_mut_u8<'map>(&'map mut self, byte: usize) -> &'map mut u8 {
&mut self.bits[byte]
}
}
use crate::tools::inner_use::*;
pub use crate::{he_lang, newmap};
pub use refs::*;
pub use traits::FillPrefix;