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