use std::io;
pub use minimum_redundancy::DecodingResult;
use std::borrow::Borrow;
use std::iter::FusedIterator;
pub use minimum_redundancy;
mod mr;
pub use mr::*;
mod geom;
pub use geom::*;
#[derive(Default, Copy, Clone)]
pub struct U8Code {
pub content: u8,
pub len: u8
}
pub trait Decoder {
type Value;
type Decoded: Borrow<Self::Value>;
fn consume_checked(&mut self, fragment: u8) -> DecodingResult<Self::Decoded>;
#[inline(always)] fn consume(&mut self, fragment: u8) -> DecodingResult<Self::Decoded> {
self.consume_checked(fragment)
}
}
pub trait Coding {
type Value;
type Decoder<'d>: Decoder<Value=Self::Value> where Self: 'd;
type Encoder<'e> where Self: 'e;
type Codeword: Copy + Sized + Sync;
fn bits_per_fragment(&self) -> u8;
fn max_fragment_value(&self) -> u8 {
1u8.checked_shl(self.bits_per_fragment() as u32).map_or(u8::MAX, |v| v-1)
}
fn decoder(&self) -> Self::Decoder<'_>;
fn encoder(&self) -> Self::Encoder<'_>;
fn len_of(&self, code: Self::Codeword) -> u8;
fn fragment_of(&self, code: Self::Codeword, index: u8) -> u8;
#[inline] fn rev_fragment_of(&self, code: Self::Codeword, index: u8) -> u8 {
self.fragment_of(code, self.len_of(code)-index-1)
}
#[inline] fn fragments_of(&self, code: Self::Codeword) -> FragmentsIterator<'_, Self> {
FragmentsIterator { coding: &self, code }
}
#[inline] fn is_code_empty(&self, code: Self::Codeword) -> bool { self.len_of(code) == 0 }
#[inline] fn first_fragment_of(&self, code: Self::Codeword) -> u8 { self.fragment_of(code, 0) }
fn extract_first_fragment_of(&self, code: &mut Self::Codeword) -> Option<u8> {
(!self.is_code_empty(*code)).then(|| {
let result = self.first_fragment_of(*code);
self.remove_first_fragment_of(code);
result
})
}
fn remove_first_fragment_of(&self, code: &mut Self::Codeword) -> bool;
fn code_of<'e, Q>(&self, encoder: &Self::Encoder<'e>, to_encode: &Q) -> Self::Codeword where Q: Borrow<Self::Value>;
#[inline(always)] fn len_of_encoded<'e, Q>(&self, encoder: &Self::Encoder<'e>, to_encode: &Q) -> u8 where Q: Borrow<Self::Value> {
self.len_of(self.code_of(encoder, to_encode))
}
#[inline] fn fragments_of_encoded<'e, Q>(&self, encoder: &Self::Encoder<'e>, to_encode: &Q) -> FragmentsIterator<'_, Self>
where Q: Borrow<Self::Value>
{
self.fragments_of(self.code_of(encoder, to_encode))
}
}
pub struct FragmentsIterator<'c, C: Coding + ?Sized> {
coding: &'c C,
code: C::Codeword
}
impl<'c, C: Coding> FusedIterator for FragmentsIterator<'c, C> {}
impl<'c, C: Coding> ExactSizeIterator for FragmentsIterator<'c, C> {
#[inline] fn len(&self) -> usize {
self.coding.len_of(self.code) as usize
}
}
impl<'c, C: Coding> Iterator for FragmentsIterator<'c, C> {
type Item = u8;
#[inline] fn next(&mut self) -> Option<Self::Item> {
self.coding.extract_first_fragment_of(&mut self.code)
}
#[inline] fn size_hint(&self) -> (usize, Option<usize>) {
let l = self.len(); (l, Some(l))
}
}
pub trait SerializableCoding: Coding {
fn write_bytes(&self, bytes_per_value: usize) -> usize;
fn write<F>(&self, output: &mut dyn io::Write, write_value: F) -> io::Result<()>
where F: FnMut(&mut dyn io::Write, &Self::Value) -> io::Result<()>;
fn read<F>(input: &mut dyn io::Read, read_value: F) -> io::Result<Self>
where F: FnMut(&mut dyn io::Read) -> io::Result<Self::Value>, Self: Sized;
}
pub trait BuildCoding<V> {
type Coding: Coding<Value=V>;
fn name(&self) -> String;
fn build_from_iter<Iter>(&self, iter: Iter, bits_per_fragment: u8) -> Self::Coding
where Iter: IntoIterator, Iter::Item: Borrow<<Self::Coding as Coding>::Value>;
}