use crate::Separator;
#[derive(Debug, Clone, Copy)]
pub struct Chunk {
pub index: u64,
pub size: u64,
pub separator_hash: u64,
}
#[derive(Debug)]
pub struct ChunkIter<Iter> {
separators: Iter,
stream_length: u64,
last_separator_index: u64,
}
impl<Iter: Iterator<Item = Separator>> ChunkIter<Iter> {
pub fn new(iter: Iter, stream_length: u64) -> Self {
Self {
separators: iter,
stream_length,
last_separator_index: 0,
}
}
}
impl<Iter: Iterator<Item = Separator>> Iterator for ChunkIter<Iter> {
type Item = Chunk;
fn next(&mut self) -> Option<Self::Item> {
if let Some(separator) = self.separators.next() {
let chunk_size = separator.index - self.last_separator_index;
self.last_separator_index = separator.index;
Some(Chunk {
index: self.last_separator_index,
size: chunk_size,
separator_hash: separator.hash,
})
} else {
let chunk_size = self.stream_length - self.last_separator_index;
self.last_separator_index = self.stream_length;
if chunk_size > 0 {
Some(Chunk {
index: self.last_separator_index,
size: chunk_size,
separator_hash: 0, })
} else {
None
}
}
}
}