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_basic_types {
($($storage_type: ty), *) => {
$(
impl BitStorage for $storage_type {
const BITS: usize = std::mem::size_of::<$storage_type>() * 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_basic_types!(u8, u16, u32, u64, u128);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Bitmap<T: BitStorage> {
inner: 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(NegativeIter { bitmap }: NegativeIter<T>) -> Self {
Self { bitmap }
}
}
impl<T: BitStorage> From<PositiveIter<T>> for NegativeIter<T> {
fn from(PositiveIter { bitmap }: PositiveIter<T>) -> Self {
Self { bitmap }
}
}
impl<T: BitStorage> Iterator for PositiveIter<T> {
type Item = usize;
fn next(&mut self) -> Option<Self::Item> {
if self.bitmap.inner == T::from(0) {
return None;
}
let next_bit_pos = self.bitmap.inner.trailing_zeros() as usize;
let mask = T::from(1) << next_bit_pos;
self.bitmap.inner &= !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.inner == !T::from(0) {
return None;
}
let next_bit_pos = (!self.bitmap.inner).trailing_zeros() as usize;
let mask = T::from(1) << next_bit_pos;
self.bitmap.inner |= 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<T: BitStorage> IntoIterator for &Bitmap<T> {
type Item = usize;
type IntoIter = PositiveIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_ones()
}
}
impl<T: BitStorage> IntoIterator for Bitmap<T> {
type Item = usize;
type IntoIter = PositiveIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter_ones()
}
}
impl<T: BitStorage> From<T> for Bitmap<T> {
#[inline]
fn from(val: T) -> Self {
Self { inner: val }
}
}
impl<T: BitStorage> Bitmap<T> {
#[inline]
pub fn new_empty() -> Self {
Self { inner: T::from(0) }
}
#[inline]
pub fn new_full() -> Self {
Self { inner: !T::from(0) }
}
#[inline]
pub fn new() -> Self {
Self::new_empty()
}
#[inline]
pub const fn iter_ones(&self) -> PositiveIter<T> {
PositiveIter { bitmap: *self }
}
#[inline]
pub const 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.inner |= mask;
} else {
self.inner &= !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.inner & mask) != T::from(0)
}
#[inline]
pub fn is_one_on(&self, idx: usize) -> bool {
self.get(idx)
}
#[inline]
pub fn is_zero_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.inner.count_ones()
}
#[inline]
pub fn count_zeros(&self) -> u32 {
T::BITS as u32 - self.inner.count_ones()
}
#[inline]
pub fn any(&self) -> bool {
self.inner != T::from(0)
}
#[inline]
pub fn all(&self) -> bool {
self.inner == !T::from(0)
}
#[inline]
pub fn none(&self) -> bool {
self.inner == T::from(0)
}
#[inline]
pub fn first_one(&self) -> Option<usize> {
if self.none() {
None
} else {
Some(self.inner.trailing_zeros() as usize)
}
}
}
impl<T: BitStorage> BitAnd for Bitmap<T> {
type Output = Self;
fn bitand(self, rhs: Self) -> Self::Output {
Self {
inner: self.inner & rhs.inner,
}
}
}
impl<T: BitStorage> BitAndAssign for Bitmap<T> {
fn bitand_assign(&mut self, rhs: Self) {
self.inner &= rhs.inner
}
}
impl<T: BitStorage> BitOr for Bitmap<T> {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
Self {
inner: self.inner | rhs.inner,
}
}
}
impl<T: BitStorage> BitOrAssign for Bitmap<T> {
fn bitor_assign(&mut self, rhs: Self) {
self.inner |= rhs.inner
}
}
impl<T: BitStorage> BitXor for Bitmap<T> {
type Output = Self;
fn bitxor(self, rhs: Self) -> Self::Output {
Self {
inner: self.inner ^ rhs.inner,
}
}
}
impl<T: BitStorage> BitXorAssign for Bitmap<T> {
fn bitxor_assign(&mut self, rhs: Self) {
self.inner ^= rhs.inner
}
}
impl<T: BitStorage> Not for Bitmap<T> {
type Output = Self;
fn not(self) -> Self::Output {
Self { inner: !self.inner }
}
}