use crate::binary::Binary;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ZeroIndices<T: Binary> {
n: T,
}
impl<T: Binary> ZeroIndices<T> {
pub const fn new(n: T) -> Self {
Self { n }
}
}
impl<T: Binary> Iterator for ZeroIndices<T> {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
self.n.has_zero().then(|| {
let i = self.n.trailing_ones();
self.n ^= T::ONE << i;
i
})
}
}
impl<T: Binary> DoubleEndedIterator for ZeroIndices<T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.n.has_zero().then(|| {
let i = T::MAX_BIT_IDX - self.n.leading_ones();
self.n ^= T::ONE << i;
i
})
}
}