Skip to content

[WIP] experiment - compile-time precision #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions arithmetic-coding-core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ pub trait Model {
/// The internal representation to use for storing integers
type B: BitStore = u32;

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
const MAX_DENOMINATOR: Self::B;

/// Given a symbol, return an interval representing the probability of that
/// symbol occurring.
///
Expand Down Expand Up @@ -98,17 +106,9 @@ pub trait Model {
/// [`Encoder`](crate::Encoder) and [`Decoder`](crate::Decoder) to panic due
/// to overflow or underflow.
fn denominator(&self) -> Self::B {
self.max_denominator()
Self::MAX_DENOMINATOR
}

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
fn max_denominator(&self) -> Self::B;

/// Given a value, return the symbol whose probability range it falls in.
///
/// `None` indicates `EOF`
Expand Down
30 changes: 14 additions & 16 deletions arithmetic-coding-core/src/model/fixed_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ pub trait Model {
/// The internal representation to use for storing integers
type B: BitStore = u32;

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
const MAX_DENOMINATOR: Self::B;

/// Given a symbol, return an interval representing the probability of that
/// symbol occurring.
///
Expand Down Expand Up @@ -100,17 +108,9 @@ pub trait Model {
/// [`Encoder`](crate::Encoder) and [`Decoder`](crate::Decoder) to panic due
/// to overflow or underflow.
fn denominator(&self) -> Self::B {
self.max_denominator()
Self::MAX_DENOMINATOR
}

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
fn max_denominator(&self) -> Self::B;

/// Given a value, return the symbol whose probability range it falls in.
///
/// `None` indicates `EOF`
Expand Down Expand Up @@ -154,9 +154,11 @@ impl<M> crate::Model for Wrapper<M>
where
M: Model,
{
type B = M::B;
type Symbol = M::Symbol;
type ValueError = Error<M::ValueError>;
type B = M::B;

const MAX_DENOMINATOR: Self::B = M::MAX_DENOMINATOR;

fn probability(
&self,
Expand All @@ -179,8 +181,8 @@ where
}
}

fn max_denominator(&self) -> Self::B {
self.model.max_denominator()
fn denominator(&self) -> Self::B {
self.model.denominator()
}

fn symbol(&self, value: Self::B) -> Option<Self::Symbol> {
Expand All @@ -191,10 +193,6 @@ where
}
}

fn denominator(&self) -> Self::B {
self.model.denominator()
}

fn update(&mut self, symbol: Option<&Self::Symbol>) {
if let Some(s) = symbol {
self.model.update(s);
Expand Down
24 changes: 11 additions & 13 deletions arithmetic-coding-core/src/model/max_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ pub trait Model {
/// The internal representation to use for storing integers
type B: BitStore = u32;

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
const MAX_DENOMINATOR: Self::B;

/// Given a symbol, return an interval representing the probability of that
/// symbol occurring.
///
Expand Down Expand Up @@ -107,17 +115,9 @@ pub trait Model {
/// [`Encoder`](crate::Encoder) and [`Decoder`](crate::Decoder) to panic due
/// to overflow or underflow.
fn denominator(&self) -> Self::B {
self.max_denominator()
Self::MAX_DENOMINATOR
}

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
fn max_denominator(&self) -> Self::B;

/// Given a value, return the symbol whose probability range it falls in.
///
/// `None` indicates `EOF`
Expand Down Expand Up @@ -183,10 +183,6 @@ where
}
}

fn max_denominator(&self) -> Self::B {
self.model.max_denominator()
}

fn symbol(&self, value: Self::B) -> Option<Self::Symbol> {
if self.remaining > 0 {
self.model.symbol(value)
Expand All @@ -205,6 +201,8 @@ where
self.remaining -= 1;
}
}

const MAX_DENOMINATOR: Self::B = M::MAX_DENOMINATOR;
}

/// Fixed-length encoding/decoding errors
Expand Down
24 changes: 11 additions & 13 deletions arithmetic-coding-core/src/model/one_shot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ pub trait Model {
/// The internal representation to use for storing integers
type B: BitStore = u32;

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
const MAX_DENOMINATOR: Self::B;

/// Given a symbol, return an interval representing the probability of that
/// symbol occurring.
///
Expand All @@ -87,14 +95,6 @@ pub trait Model {
/// This returns a custom error if the given symbol is not valid
fn probability(&self, symbol: &Self::Symbol) -> Result<Range<Self::B>, Self::ValueError>;

/// The maximum denominator used for probability ranges. See
/// [`Model::probability`].
///
/// This value is used to calculate an appropriate precision for the
/// encoding, therefore this value must not change, and
/// [`Model::denominator`] must never exceed it.
fn max_denominator(&self) -> Self::B;

/// Given a value, return the symbol whose probability range it falls in.
///
/// `None` indicates `EOF`
Expand All @@ -115,10 +115,6 @@ where
Model::probability(self, symbol)
}

fn max_denominator(&self) -> Self::B {
self.max_denominator()
}

fn symbol(&self, value: Self::B) -> Self::Symbol {
Model::symbol(self, value)
}
Expand All @@ -128,6 +124,8 @@ where
}

fn denominator(&self) -> Self::B {
self.max_denominator()
Self::MAX_DENOMINATOR
}

const MAX_DENOMINATOR: Self::B = Self::MAX_DENOMINATOR;
}
12 changes: 6 additions & 6 deletions benches/sherlock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ use fenwick_model::{simple::FenwickModel, ValueError};

mod common;

const MAX_DENOMINATOR: u64 = 1 << 20;

#[derive(Debug, Clone)]
pub struct StringModel {
fenwick_model: FenwickModel,
fenwick_model: FenwickModel<MAX_DENOMINATOR>,
}

impl StringModel {
#[must_use]
pub fn new(symbols: usize) -> Self {
let fenwick_model = FenwickModel::builder(symbols, 1 << 20)
let fenwick_model = FenwickModel::builder(symbols)
.panic_on_saturation()
.build();
Self { fenwick_model }
Expand Down Expand Up @@ -41,13 +43,11 @@ impl Model for StringModel {
self.fenwick_model.symbol(value).map(|x| x as u8)
}

fn max_denominator(&self) -> Self::B {
self.fenwick_model.max_denominator()
}

fn denominator(&self) -> Self::B {
self.fenwick_model.denominator()
}

const MAX_DENOMINATOR: Self::B = MAX_DENOMINATOR;
}

fn round_trip(input: &[u8]) {
Expand Down
8 changes: 2 additions & 6 deletions examples/concatenated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ mod integer {
}
}

fn max_denominator(&self) -> u32 {
4
}
const MAX_DENOMINATOR: Self::B = 4;
}
}

Expand Down Expand Up @@ -81,9 +79,7 @@ mod symbolic {
}
}

fn max_denominator(&self) -> u32 {
4
}
const MAX_DENOMINATOR: Self::B = 4;
}
}

Expand Down
12 changes: 6 additions & 6 deletions examples/fenwick_adaptive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ use fenwick_model::{simple::FenwickModel, ValueError};
const ALPHABET: &str =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,\n-':()[]#*;\"!?*&é/àâè%@$";

const MAX_DENOMINATOR: u64 = 1 << 20;

#[derive(Debug, Clone)]
pub struct StringModel {
alphabet: Vec<char>,
fenwick_model: FenwickModel,
fenwick_model: FenwickModel<MAX_DENOMINATOR>,
}

impl StringModel {
#[must_use]
pub fn new(alphabet: Vec<char>) -> Self {
let fenwick_model = FenwickModel::builder(alphabet.len(), 1 << 20)
let fenwick_model = FenwickModel::builder(alphabet.len())
.panic_on_saturation()
.build();
Self {
Expand Down Expand Up @@ -50,10 +52,6 @@ impl Model for StringModel {
self.alphabet.get(index).copied()
}

fn max_denominator(&self) -> Self::B {
self.fenwick_model.max_denominator()
}

fn denominator(&self) -> Self::B {
self.fenwick_model.denominator()
}
Expand All @@ -62,6 +60,8 @@ impl Model for StringModel {
let fenwick_symbol = symbol.map(|c| self.alphabet.iter().position(|x| x == c).unwrap());
self.fenwick_model.update(fenwick_symbol.as_ref());
}

const MAX_DENOMINATOR: Self::B = MAX_DENOMINATOR;
}

fn main() {
Expand Down
12 changes: 6 additions & 6 deletions examples/fenwick_context_switching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ use fenwick_model::{context_switching::FenwickModel, ValueError};
const ALPHABET: &str =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .,\n-':()[]#*;\"!?*&é/àâè%@$";

const MAX_DENOMINATOR: u64 = 1 << 17;

#[derive(Debug, Clone)]
pub struct StringModel {
alphabet: Vec<char>,
fenwick_model: FenwickModel,
fenwick_model: FenwickModel<MAX_DENOMINATOR>,
}

impl StringModel {
#[must_use]
pub fn new(alphabet: Vec<char>) -> Self {
let fenwick_model = FenwickModel::with_symbols(alphabet.len(), 1 << 17);
let fenwick_model = FenwickModel::with_symbols(alphabet.len());
Self {
alphabet,
fenwick_model,
Expand Down Expand Up @@ -48,10 +50,6 @@ impl Model for StringModel {
self.alphabet.get(index).copied()
}

fn max_denominator(&self) -> Self::B {
self.fenwick_model.max_denominator()
}

fn denominator(&self) -> Self::B {
self.fenwick_model.denominator()
}
Expand All @@ -60,6 +58,8 @@ impl Model for StringModel {
let fenwick_symbol = symbol.map(|c| self.alphabet.iter().position(|x| x == c).unwrap());
self.fenwick_model.update(fenwick_symbol.as_ref());
}

const MAX_DENOMINATOR: Self::B = MAX_DENOMINATOR;
}

fn main() {
Expand Down
6 changes: 2 additions & 4 deletions examples/fixed_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ impl fixed_length::Model for MyModel {
}
}

fn max_denominator(&self) -> u32 {
3
}

fn length(&self) -> usize {
3
}

const MAX_DENOMINATOR: Self::B = 3;
}

fn main() {
Expand Down
4 changes: 1 addition & 3 deletions examples/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ impl Model for MyModel {
}
}

fn max_denominator(&self) -> u32 {
4
}
const MAX_DENOMINATOR: Self::B = 4;
}

fn main() {
Expand Down
8 changes: 4 additions & 4 deletions examples/max_length.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ impl max_length::Model for MyModel {
}
}

fn max_denominator(&self) -> u32 {
4
}

fn max_length(&self) -> usize {
3
}

type B = u32;

const MAX_DENOMINATOR: Self::B = 4;
}

fn main() {
Expand Down
Loading