use super::BitmapSize;
use core::fmt::Formatter;
use serde::{Deserialize, Serialize};
use std::{
fmt::{Debug, Display},
mem,
ops::{
Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Deref, Div,
DivAssign, Mul, MulAssign, Not, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign,
},
};
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Default, Serialize, Deserialize)]
pub struct Bitmap128(u128);
impl Bitmap128 {
pub fn capacity() -> usize {
Bitmap128::MAP_LENGTH
}
pub fn to_u128(&self) -> u128 {
self.0
}
pub fn new(value: bool) -> Bitmap128 {
Bitmap128(if value { u128::MAX } else { 0 })
}
pub fn create_bit_mask(begin: usize, end: usize, value: bool) -> Bitmap128 {
if value {
if begin >= Bitmap128::MAP_LENGTH || end < 1 {
Bitmap128(0)
} else if end >= Bitmap128::MAP_LENGTH {
Bitmap128(u128::MAX << begin)
} else {
Bitmap128(u128::MAX << begin & u128::MAX >> Bitmap128::MAP_LENGTH - end)
}
} else {
!Bitmap128::create_bit_mask(begin, end, true)
}
}
pub fn from_set(index: usize) -> Option<Bitmap128> {
if index >= Bitmap128::MAP_LENGTH {
return None;
}
let mut bitmap = Bitmap128::default();
bitmap.set(index, true).unwrap();
Some(bitmap)
}
pub fn set(&mut self, index: usize, value: bool) -> Result<(), String> {
if index >= Bitmap128::MAP_LENGTH {
return Err(String::from(
"Tried to set bit that's out of range of the bitmap (range: ",
) + &Bitmap128::MAP_LENGTH.to_string()
+ ", index: "
+ &index.to_string()
+ ")");
}
if value {
let mask = 1 << index;
self.0 |= mask;
} else {
let mask = u128::MAX - (1 << index);
self.0 &= mask;
}
Ok(())
}
pub fn set_range(&mut self, begin: usize, end: usize, value: bool) {
if value {
*self |= Bitmap128::create_bit_mask(begin, end, true);
} else {
*self &= Bitmap128::create_bit_mask(begin, end, false);
}
}
pub fn get(&self, index: usize) -> Result<bool, String> {
if index >= Bitmap128::MAP_LENGTH {
return Err(String::from(
"Tried to get bit that's out of range of the bitmap (range: ",
) + &Bitmap128::MAP_LENGTH.to_string()
+ ", index: "
+ &index.to_string()
+ ")");
}
let mask = 1 << index;
Ok(self.0 & mask > 0)
}
}
impl Display for Bitmap128 {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{:b}", self.0)
}
}
impl Debug for Bitmap128 {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Bitmap128({:X})", self.0)
}
}
impl From<u128> for Bitmap128 {
fn from(value: u128) -> Self {
Bitmap128(value)
}
}
impl BitmapSize for Bitmap128 {
const MAP_LENGTH: usize = mem::size_of::<u128>() * 8;
}
impl BitAnd for Bitmap128 {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self(self.0 & rhs.0)
}
}
impl BitAndAssign for Bitmap128 {
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0;
}
}
impl BitOr for Bitmap128 {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self(self.0 | rhs.0)
}
}
impl BitOrAssign for Bitmap128 {
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0;
}
}
impl BitXor for Bitmap128 {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self(self.0 ^ rhs.0)
}
}
impl BitXorAssign for Bitmap128 {
fn bitxor_assign(&mut self, rhs: Self) {
self.0 ^= rhs.0;
}
}
impl Add for Bitmap128 {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl AddAssign for Bitmap128 {
fn add_assign(&mut self, rhs: Self) {
self.0 += rhs.0;
}
}
impl Sub for Bitmap128 {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl SubAssign for Bitmap128 {
fn sub_assign(&mut self, rhs: Self) {
self.0 -= rhs.0;
}
}
impl Mul for Bitmap128 {
type Output = Self;
fn mul(self, rhs: Self) -> Self::Output {
Self(self.0 * rhs.0)
}
}
impl MulAssign for Bitmap128 {
fn mul_assign(&mut self, rhs: Self) {
self.0 *= rhs.0;
}
}
impl Div for Bitmap128 {
type Output = Self;
fn div(self, rhs: Self) -> Self::Output {
Self(self.0 / rhs.0)
}
}
impl DivAssign for Bitmap128 {
fn div_assign(&mut self, rhs: Self) {
self.0 /= rhs.0;
}
}
impl BitAnd<u128> for Bitmap128 {
type Output = Self;
fn bitand(self, rhs: u128) -> Self::Output {
Self(self.0 & rhs)
}
}
impl BitAndAssign<u128> for Bitmap128 {
fn bitand_assign(&mut self, rhs: u128) {
self.0 &= rhs;
}
}
impl BitOr<u128> for Bitmap128 {
type Output = Self;
fn bitor(self, rhs: u128) -> Self::Output {
Self(self.0 | rhs)
}
}
impl BitOrAssign<u128> for Bitmap128 {
fn bitor_assign(&mut self, rhs: u128) {
self.0 |= rhs;
}
}
impl BitXor<u128> for Bitmap128 {
type Output = Self;
fn bitxor(self, rhs: u128) -> Self::Output {
Self(self.0 ^ rhs)
}
}
impl BitXorAssign<u128> for Bitmap128 {
fn bitxor_assign(&mut self, rhs: u128) {
self.0 ^= rhs;
}
}
impl Add<u128> for Bitmap128 {
type Output = Self;
fn add(self, rhs: u128) -> Self::Output {
Self(self.0 + rhs)
}
}
impl AddAssign<u128> for Bitmap128 {
fn add_assign(&mut self, rhs: u128) {
self.0 += rhs;
}
}
impl Sub<u128> for Bitmap128 {
type Output = Self;
fn sub(self, rhs: u128) -> Self::Output {
Self(self.0 - rhs)
}
}
impl SubAssign<u128> for Bitmap128 {
fn sub_assign(&mut self, rhs: u128) {
self.0 -= rhs;
}
}
impl Mul<u128> for Bitmap128 {
type Output = Self;
fn mul(self, rhs: u128) -> Self::Output {
Self(self.0 * rhs)
}
}
impl MulAssign<u128> for Bitmap128 {
fn mul_assign(&mut self, rhs: u128) {
self.0 *= rhs;
}
}
impl Div<u128> for Bitmap128 {
type Output = Self;
fn div(self, rhs: u128) -> Self::Output {
Self(self.0 / rhs)
}
}
impl DivAssign<u128> for Bitmap128 {
fn div_assign(&mut self, rhs: u128) {
self.0 /= rhs;
}
}
impl Shl<usize> for Bitmap128 {
type Output = Self;
fn shl(self, rhs: usize) -> Self::Output {
Self(self.0 << rhs)
}
}
impl ShlAssign<usize> for Bitmap128 {
fn shl_assign(&mut self, rhs: usize) {
self.0 <<= rhs;
}
}
impl Shr<usize> for Bitmap128 {
type Output = Self;
fn shr(self, rhs: usize) -> Self::Output {
Self(self.0 >> rhs)
}
}
impl ShrAssign<usize> for Bitmap128 {
fn shr_assign(&mut self, rhs: usize) {
self.0 >>= rhs;
}
}
impl Not for Bitmap128 {
type Output = Self;
fn not(self) -> Self::Output {
Self(self.0 ^ u128::MAX)
}
}
impl Deref for Bitmap128 {
type Target = u128;
fn deref(&self) -> &Self::Target {
&self.0
}
}