use std::fmt::Debug;
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Shl, Shr};
pub trait BitStorage:
Copy
+ Default
+ Debug
+ BitAnd<Output = Self>
+ BitAndAssign
+ BitOr<Output = Self>
+ BitOrAssign
+ BitXor<Output = Self>
+ BitXorAssign
+ Not<Output = Self>
+ From<u8>
+ Shl<usize, Output = Self>
+ Shr<usize, Output = Self>
+ PartialEq
+ Eq
{
const BITS: usize;
fn trailing_zeros(self) -> u32;
fn count_ones(self) -> u32;
fn count_zeros(self) -> u32;
}
macro_rules! impl_bit_storage_for_type {
($($t:ty),*) => {
$(
impl BitStorage for $t {
const BITS: usize = std::mem::size_of::<$t>() * 8;
#[inline]
fn trailing_zeros(self) -> u32 {
self.trailing_zeros()
}
#[inline]
fn count_ones(self) -> u32 {
self.count_ones()
}
#[inline]
fn count_zeros(self) -> u32 {
self.count_zeros()
}
}
)*
};
}
impl_bit_storage_for_type!(u8, u16, u32, u64, u128);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Bitmap<T: BitStorage> {
val: T,
}
pub struct PositiveIter<T: BitStorage> {
bitmap: Bitmap<T>,
}
pub struct NegativeIter<T: BitStorage> {
bitmap: Bitmap<T>,
}
impl<T: BitStorage> From<NegativeIter<T>> for PositiveIter<T> {
fn from(value: NegativeIter<T>) -> Self {
let NegativeIter { bitmap } = value;
Self { bitmap }
}
}
impl<T: BitStorage> From<PositiveIter<T>> for NegativeIter<T> {
fn from(value: PositiveIter<T>) -> Self {
let PositiveIter { bitmap } = value;
Self { bitmap }
}
}
impl<T: BitStorage> Iterator for PositiveIter<T> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
if self.bitmap.val == T::from(0) {
return None;
}
let next_bit_pos = self.bitmap.val.trailing_zeros() as usize;
let mask = T::from(1) << next_bit_pos;
self.bitmap.val &= !mask;
Some(next_bit_pos)
}
}
impl<T: BitStorage> Iterator for NegativeIter<T> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
if self.bitmap.val == !T::from(0) {
return None;
}
let next_bit_pos = (!self.bitmap.val).trailing_zeros() as usize;
let mask = T::from(1) << next_bit_pos;
self.bitmap.val |= mask;
Some(next_bit_pos)
}
}
impl<T: BitStorage> PositiveIter<T> {
#[inline]
pub fn invert(self) -> NegativeIter<T> {
self.into()
}
}
impl<T: BitStorage> NegativeIter<T> {
#[inline]
pub fn invert(self) -> PositiveIter<T> {
self.into()
}
}
impl<'a, T: BitStorage> IntoIterator for &'a Bitmap<T> {
type Item = usize;
type IntoIter = PositiveIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: BitStorage> IntoIterator for Bitmap<T> {
type Item = usize;
type IntoIter = PositiveIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<T: BitStorage> From<T> for Bitmap<T> {
#[inline]
fn from(val: T) -> Self {
Self { val }
}
}
impl<T: BitStorage> Bitmap<T> {
#[inline]
pub fn new_empty() -> Self {
Self { val: T::from(0) }
}
#[inline]
pub fn new_full() -> Self {
Self { val: !T::from(0) }
}
#[inline]
pub fn new() -> Self {
Self::new_empty()
}
#[inline]
pub fn iter(&self) -> PositiveIter<T> {
PositiveIter { bitmap: *self }
}
#[inline]
pub fn iter_zeros(&self) -> NegativeIter<T> {
NegativeIter { bitmap: *self }
}
#[inline]
pub fn set(&mut self, idx: usize, set: bool) {
debug_assert!(idx < T::BITS, "Index out of bounds");
let mask = T::from(1) << idx;
if set {
self.val |= mask;
} else {
self.val &= !mask;
}
}
#[inline]
pub fn get(&self, idx: usize) -> bool {
debug_assert!(idx < T::BITS, "Index out of bounds");
let mask = T::from(1) << idx;
(self.val & mask) != T::from(0)
}
#[inline]
pub fn is_true_on(&self, idx: usize) -> bool {
self.get(idx)
}
#[inline]
pub fn is_false_on(&self, idx: usize) -> bool {
!self.get(idx)
}
#[inline]
pub fn merge(self, rhs: Bitmap<T>) -> Bitmap<T> {
self | rhs
}
#[inline]
pub fn count_ones(&self) -> u32 {
self.val.count_ones()
}
#[inline]
pub fn count_zeros(&self) -> u32 {
T::BITS as u32 - self.val.count_ones()
}
#[inline]
pub fn any(&self) -> bool {
self.val != T::from(0)
}
#[inline]
pub fn all(&self) -> bool {
self.val == !T::from(0)
}
#[inline]
pub fn none(&self) -> bool {
self.val == T::from(0)
}
#[inline]
pub fn first_one(&self) -> Option<usize> {
if self.none() {
None
} else {
Some(self.val.trailing_zeros() as usize)
}
}
}
impl<T: BitStorage> BitAnd for Bitmap<T> {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self {
val: self.val & rhs.val,
}
}
}
impl<T: BitStorage> BitOr for Bitmap<T> {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self {
val: self.val | rhs.val,
}
}
}
impl<T: BitStorage> BitXor for Bitmap<T> {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self {
val: self.val ^ rhs.val,
}
}
}
impl<T: BitStorage> Not for Bitmap<T> {
type Output = Self;
fn not(self) -> Self::Output {
Self { val: !self.val }
}
}