pub const U8_MASK: u8 = 7;
pub const U8_WIDTH: u8 = 8;
#[inline]
pub fn kth_bit(x: u8, k: u8) -> bool {
((1 << (U8_MASK - k)) & x) != 0
}
#[inline]
pub fn kth_bit_iter<'a, I>(it: &mut std::iter::Peekable<I>, k: u8) -> bool
where
I: Iterator<Item = &'a u8>,
{
match it.peek() {
Some(&&seg) => kth_bit(seg, k),
_ => false,
}
}
#[inline]
pub fn rotate_add(x: u8, y: u8) -> u8 {
(x + y) & U8_MASK
}
#[inline]
pub fn rotate_consume<'a, I>(it: &mut I, cursor: &mut u8, bits: u8) -> Option<bool>
where
I: Iterator<Item = &'a u8>,
{
*cursor = rotate_add(*cursor, bits);
let rotated = *cursor == 0 && bits != 0;
if rotated {
it.next()?;
}
Some(rotated)
}
#[inline]
pub fn rotate_incr<'a, I>(it: &mut I, cursor: &mut u8) -> Option<bool>
where
I: Iterator<Item = &'a u8>,
{
*cursor = rotate_add(*cursor, 1);
let rotated = *cursor == 0;
if rotated {
it.next()?;
}
Some(rotated)
}