mod map;
mod special;
pub mod distribution;
use std::fmt::Debug;
use std::mem;
use std::io::{self,Write};
use std::iter::Iterator;
use std::cmp::Ordering;
pub trait Model<T> {
fn push(&mut self, s: Index) -> Option<T>;
fn next_distr(&mut self) -> Box<dyn UnivariateDistribution>;
}
impl<A: Model<T>, T,
B: Model<U>, U> Model<(T,U)> for (Option<T>,(A,B)) {
fn push(&mut self, s: Index) -> Option<(T,U)> {
match self.0 {
None => {
self.0 = self.1.0.push(s);
None
}
Some(_) => {
let u = self.1.1.push(s)?;
Some((std::mem::take(&mut self.0).unwrap(), u))
}
}
}
fn next_distr(&mut self) -> Box<dyn UnivariateDistribution> {
match self.0 {
None => self.1.0.next_distr(),
Some(_) => self.1.1.next_distr()
}
}
}
pub trait UnivariateDistribution: Debug {
fn truncated(&self) -> Box<dyn TruncatedDistribution>;
}
pub struct Sample<D,T> {
pub distr: D,
pub resolve: Box<dyn FnMut(Index) -> T>
}
impl<D: UnivariateDistribution + Clone + 'static,
T> Model<T> for Sample<D,T> {
fn push(&mut self, s: Index) -> Option<T> {
Some((self.resolve)(s)) }
fn next_distr(&mut self) -> Box<dyn UnivariateDistribution> {
Box::new(self.distr.clone())
}
}
pub struct Samples<D,T> {
pub count: usize,
pub vec: Vec<T>,
pub sampl: Sample<D,T>
}
impl<D,T> Samples<D,T> {
pub fn repeatedly(sampl: Sample<D,T>, count: usize) -> Self {
Samples{ count, vec: Vec::new(), sampl }
}
}
impl<D: UnivariateDistribution + Clone + 'static,
T> Model<Vec<T>> for Samples<D,T> {
fn push(&mut self, s: Index) -> Option<Vec<T>> {
let val = (self.sampl.resolve)(s); self.vec.push(val);
self.count -= 1;
if self.count == 0 { Some(std::mem::take(&mut self.vec)) }
else { None }
}
fn next_distr(&mut self) -> Box<dyn UnivariateDistribution> {
Box::new(self.sampl.distr.clone())
}
}
pub type Index = i64;
pub trait TruncatedDistribution: Debug {
fn quantile(&self, cp: f64) -> (Index, f64); fn truncate(&mut self, cp: f64, s: Index, s_rem: f64, bit: bool);
fn lo(&self) -> Index;
fn hi(&self) -> Index;
fn is_resolved(&self) -> bool { self.lo() == self.hi() }
}
pub struct Encoder<'a> {
pub head: Vec<(Index,Box<dyn TruncatedDistribution>)>, pub tail: Box<dyn Iterator<Item=(Index,Box<dyn TruncatedDistribution>)> + 'a>,
}
impl<'a> Encoder<'a> {
pub fn new<T,M,I>(mut model: M, atoms: I) -> Encoder<'a>
where
M: Model<T> + 'a,
I: Iterator<Item=Index> + 'a
{
Encoder{
head: Vec::new(),
tail: Box::new(
atoms.map(move |s| {
let udistr = model.next_distr();
let tdistr = udistr.truncated();
model.push(s); (s,tdistr)
}) .filter(|(_,tdistr)| !tdistr.is_resolved()) )
}
}
}
impl<'a> Iterator for Encoder<'a> {
type Item = bool;
fn next(&mut self) -> Option<bool> {
let mut stack = Vec::new(); let mut cp = 0.5; let mut mbit = None;
let mut head_iter = mem::take(&mut self.head).into_iter();
while let Some((target, distr)) =
head_iter.next().or_else(|| self.tail.next()) {
let (s, s_rem) = distr.quantile(cp);
stack.push((target, distr, cp, s, s_rem));
match s.cmp(&target) {
Ordering::Less => {
mbit = Some(true); break
}
Ordering::Equal => {
if s_rem > 0.0 {
cp = s_rem;
continue
} else { debug_assert!(s_rem == 0.0);
mbit = Some(true); break
}
}
Ordering::Greater => {
mbit = Some(false); break
}
}
}
if stack.is_empty() { return None } let bit = mbit.unwrap_or(
cp < 0.5
);
for (target, mut distr, cp, s, s_rem) in stack {
distr.truncate(cp, s, s_rem, bit);
if !distr.is_resolved() { self.head.push((target,distr)) }
}
for tc in head_iter {
self.head.push(tc)
}
Some(bit)
}
}
pub struct Encoder8<'a>(Encoder<'a>);
impl<'a> Encoder8<'a> {
pub fn new<T,S,I>(s: S, atoms: I) -> Encoder8<'a>
where
S: Model<T> + 'a,
I: Iterator<Item=Index> + 'a
{
Encoder8(Encoder::new::<T,S,I>(s,atoms))
}
}
impl<'a> Iterator for Encoder8<'a> {
type Item = u8;
fn next(&mut self) -> Option<Self::Item> {
let mut byte = (self.0.next()? as u8) << 7; for bit_position in 1..8 {
if self.0.next().unwrap_or(false) {
byte |= 1 << (7 - bit_position);
}
}
io::stdout().flush().unwrap();
Some(byte)
}
}
pub struct Decoder<M: Model<T>, I: Iterator<Item=bool>, T> {
pub code: I, pub model: M, t_phantom: std::marker::PhantomData<T>,
pub distr: Box<dyn TruncatedDistribution>,
pub lo_splits: Vec<f64>,
pub hi_splits: Vec<f64>,
pub last_bit: bool
}
impl<M,I,T> Decoder<M,I,T>
where
M: Model<T>,
I: Iterator<Item=bool>,
{
pub fn new(mut model: M, code: I) -> Decoder<M,I,T> {
let distr = model.next_distr().truncated(); let lo_splits = Vec::new();
let hi_splits = Vec::new();
let last_bit = false; Decoder{ code, model, t_phantom: std::marker::PhantomData,
distr, lo_splits, hi_splits, last_bit }
}
pub fn decode(&mut self) -> T { loop {
let half = 0.5;
while !self.distr.is_resolved() {
let (s, s_rem) = self.distr.quantile(half);
self.last_bit = self.code.next()
.expect("Bitstream ended before a value was decoded");
if self.last_bit {
if s != self.distr.lo() { self.lo_splits = vec![] }
self.lo_splits.push(s_rem);
} else {
if s != self.distr.hi() { self.hi_splits = vec![] }
self.hi_splits.push(s_rem);
}
self.distr.truncate(half, s, s_rem, self.last_bit);
}
while self.distr.is_resolved() {
let target = self.distr.lo();
if let Some(res) = self.model.push(target) {
return res }
let udistr = self.model.next_distr();
self.distr = udistr.truncated();
}
if self.last_bit {
for cp in mem::take(&mut self.hi_splits) {
let (s, s_rem) = self.distr.quantile(cp);
if s != self.distr.hi() { self.hi_splits = vec![] }
self.distr.truncate(cp, s, s_rem, false); self.hi_splits.push(s_rem);
}
debug_assert!(self.lo_splits.len() == 1);
let lo_cp = self.lo_splits[0];
let (s, s_rem) = self.distr.quantile(lo_cp);
self.distr.truncate(lo_cp, s, s_rem, true); self.lo_splits = vec![s_rem];
} else {
for cp in mem::take(&mut self.lo_splits) {
let (s, s_rem) = self.distr.quantile(cp);
if s != self.distr.lo() { self.lo_splits = vec![] }
self.distr.truncate(cp, s, s_rem, true); self.lo_splits.push(s_rem);
}
debug_assert!(self.hi_splits.len() == 1);
let hi_cp = self.hi_splits[0];
let (s, s_rem) = self.distr.quantile(hi_cp);
self.distr.truncate(hi_cp, s, s_rem, false); self.hi_splits = vec![s_rem];
}
}}
}
#[derive(Clone,PartialEq,Eq)]
struct BytesToBits<I: Iterator<Item=u8>> {
current_byte: u8,
shift: i8,
bytes: I,
}
impl<I> BytesToBits<I> where I: Iterator<Item=u8> {
fn new(bytes: I) -> BytesToBits<I> {
let current_byte = 0;
let shift = -1;
BytesToBits{ current_byte, shift, bytes }
}
}
impl<I> Iterator for BytesToBits<I> where I: Iterator<Item=u8> {
type Item = bool;
fn next(&mut self) -> Option<Self::Item> {
if self.shift < 0 {
self.current_byte = self.bytes.next()?;
self.shift = 7;
}
let bit = self.current_byte & (1 << self.shift) != 0;
self.shift -= 1;
Some(bit)
}
}
pub struct Decoder8< S: Model<T>, I: Iterator<Item=u8>, T>
(Decoder<S,BytesToBits<I>,T>);
impl<S,I,T> Decoder8<S,I,T>
where
I: Iterator<Item=u8>,
S: Model<T>,
{
pub fn new(model: S, bytes: I) -> Decoder8<S,I,T> {
Decoder8(Decoder::new(model, BytesToBits::new(bytes)))
}
pub fn decode(&mut self) -> T { self.0.decode() }
}