use crate::u4;
#[inline]
pub fn len(bytes: &[u8]) -> usize {
bytes.len() * 2
}
#[inline]
pub fn is_empty(bytes: &[u8]) -> bool {
bytes.is_empty()
}
#[inline]
pub fn get(bytes: &[u8], index: usize) -> Option<u4> {
if index >= len(bytes) {
return None;
}
let byte = unsafe { *bytes.get_unchecked(index >> 1) };
let val = if (index & 1) == 0 {
byte & 0x0F
} else {
(byte >> 4) & 0x0F
};
Some(u4::new(val))
}
#[derive(Debug, Clone)]
pub struct Iter<'a> {
bytes: &'a [u8],
index: usize,
len: usize,
}
impl<'a> Iterator for Iter<'a> {
type Item = u4;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.len {
return None;
}
let item = get(self.bytes, self.index);
self.index += 1;
item
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
let remaining = self.len.saturating_sub(self.index);
(remaining, Some(remaining))
}
}
impl<'a> ExactSizeIterator for Iter<'a> {}
#[inline]
pub fn iter(bytes: &[u8]) -> Iter<'_> {
Iter {
bytes,
index: 0,
len: len(bytes),
}
}