use std::fmt::{Debug, Formatter};
use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign};
#[derive(Copy, Clone, Eq, PartialEq)]
#[repr(transparent)]
pub struct ConstantMask(u64);
impl ConstantMask {
pub const CAPACITY: u8 = 64;
pub const FULLY_CONSTANT: ConstantMask = ConstantMask(u64::MAX);
pub const FULLY_DYNAMIC: ConstantMask = ConstantMask(0);
#[inline]
pub const fn from_bits(bits: u64) -> Self {
ConstantMask(bits)
}
#[inline]
pub const fn to_bits(&self) -> u64 {
self.0
}
#[inline]
pub const fn iter(&self) -> ConstantMaskIter {
ConstantMaskIter(self.0)
}
#[inline]
pub const fn is_channel_constant(&self, channel_index: u64) -> bool {
if channel_index > 63 {
return false;
}
(self.0 & (1 << channel_index)) != 0
}
#[inline]
pub const fn set_channel_constant(&mut self, channel_index: u64, value: bool) {
if channel_index > 63 {
return;
}
if value {
self.0 |= 1 << channel_index
} else {
self.0 &= !(1 << channel_index)
}
}
}
impl Debug for ConstantMask {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
core::fmt::Binary::fmt(&self.0, f)
}
}
impl Default for ConstantMask {
#[inline]
fn default() -> Self {
ConstantMask::FULLY_DYNAMIC
}
}
impl IntoIterator for ConstantMask {
type Item = bool;
type IntoIter = ConstantMaskIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl IntoIterator for &ConstantMask {
type Item = bool;
type IntoIter = ConstantMaskIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl IntoIterator for &mut ConstantMask {
type Item = bool;
type IntoIter = ConstantMaskIter;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl BitAnd for ConstantMask {
type Output = ConstantMask;
#[inline]
fn bitand(self, rhs: Self) -> Self::Output {
ConstantMask(self.0 & rhs.0)
}
}
impl BitOr for ConstantMask {
type Output = ConstantMask;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
ConstantMask(self.0 | rhs.0)
}
}
impl BitXor for ConstantMask {
type Output = ConstantMask;
#[inline]
fn bitxor(self, rhs: Self) -> Self::Output {
ConstantMask(self.0 ^ rhs.0)
}
}
impl BitAndAssign for ConstantMask {
#[inline]
fn bitand_assign(&mut self, rhs: Self) {
self.0 &= rhs.0
}
}
impl BitOrAssign for ConstantMask {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
self.0 |= rhs.0
}
}
impl BitXorAssign for ConstantMask {
#[inline]
fn bitxor_assign(&mut self, rhs: Self) {
self.0 ^= rhs.0
}
}
#[derive(Copy, Clone)]
pub struct ConstantMaskIter(u64);
impl Iterator for ConstantMaskIter {
type Item = bool;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let is_constant = self.0 & 1 != 0;
self.0 >>= 1;
Some(is_constant)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(usize::MAX, None)
}
#[inline]
fn nth(&mut self, n: usize) -> Option<bool> {
self.0 >>= n;
self.next()
}
}