Skip to content

Commit

Permalink
Update to probability 0.20
Browse files Browse the repository at this point in the history
This one is `no_std` compatible
  • Loading branch information
robamler committed Dec 18, 2022
1 parent d197e39 commit 3648102
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 89 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ name = "constriction"

[features]
default = ["std"]
libm = ["num-traits/libm"]
std = ["probability", "num-traits/std"]
std = []

# Use feature `pybindings` to compile the python extension module that provides
# access to this library from python. This feature is turned off by default
Expand All @@ -31,10 +30,10 @@ pybindings = ["ndarray", "numpy", "pyo3"]

[dependencies]
hashbrown = "0.12.3"
num-traits = {version = "0.2.15", default-features = false}
num-traits = {version = "0.2.15", default-features = false, features = ["libm"]}
smallvec = "1.6.1"

probability = {version = "0.19.1", optional = true}
probability = {version = "0.20"}

ndarray = {version = "0.15", optional = true}
numpy = {version = "0.17.1", optional = true}
Expand All @@ -43,7 +42,6 @@ pyo3 = {version = "0.17.1", features = ["extension-module"], optional = true}
[dev-dependencies]
byteorder = "1.4.2"
criterion = "0.3"
probability = "0.19.1"
rand = "0.8.3"
rand_pcg = "0.3"
rand_xoshiro = "0.6"
Expand Down
26 changes: 26 additions & 0 deletions ensure_no_std/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ensure_no_std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ version = "0.1.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
constriction = {path = "..", default-features = false, features = ["libm"]}
constriction = {path = "..", default-features = false}
simple-chunk-allocator = "0.1.5"

[profile.dev]
Expand Down
79 changes: 2 additions & 77 deletions src/stream/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,110 +133,38 @@ use alloc::{boxed::Box, vec::Vec};
use core::{borrow::Borrow, fmt::Debug, hash::Hash, marker::PhantomData, ops::RangeInclusive};
use num_traits::{float::FloatCore, AsPrimitive, One, PrimInt, WrappingAdd, WrappingSub, Zero};

/// Re-export or replacement of [`probability::distribution::Distribution`].
/// Re-export of [`probability::distribution::Distribution`].
///
/// Most users will never have to interact with this trait directly. When a method requires
/// a type that implements `Distribution`, most users will likely use a predefined type from
/// the [`probability`] crate. You only need to implement this trait if you want to use a
/// probability distribution that is not (yet) provided by the `probability` crate.
///
/// # Technical Details
///
/// - For most users, this trait is just a re-export (similar to a type alias) of the
/// [`Distribution` trait from the `probability` crate]. You'll need a type that
/// implements `Distribution` when you call [`LeakyQuantizer::quantize`]. The
/// `probability` crate provides implementations of several common `Distribution`s.
/// - Unfortunately, the `probability` crate does not support `no_std` mode. Thus, if you
/// compile `constriction` in `no_std` mode (by setting `default-features = false` for
/// `constriction` in your `Cargo.toml`) then you can't use the `probability` crate . In
/// this case, the present trait (`constriction::stream::model::Distribution`) is not a
/// re-export but instead a literal copy of its definition in the `probability` trait.
///
/// # Advice for Implementors
///
/// If you implement your own probability distribution (rather than using a pre-defined
/// distribution from the `probability` crate) then it is usually better to implement *this*
/// trait rather than `probability::distribution::Distribution`. This way, your code will
/// work as expected both in `std` and in `no_std` mode.
///
/// # See Also
///
/// - [`Inverse`]
///
/// [`probability::distribution::Distribution`]:
/// https://docs.rs/probability/latest/probability/distribution/trait.Distribution.html
/// [`Distribution` trait from the `probability` crate]:
/// https://docs.rs/probability/latest/probability/distribution/trait.Distribution.html
/// [`probability`]: https://docs.rs/probability/latest/probability/
#[cfg(feature = "probability")]
pub use probability::distribution::Distribution;

/// Re-export or replacement of [`probability::distribution::Inverse`].
/// Re-export of [`probability::distribution::Inverse`].
///
/// Most users will never have to interact with this trait directly. When a method requires
/// a type that implements `Inverse`, most users will likely use a predefined type from
/// the [`probability`] crate. You only need to implement this trait if you want to use a
/// probability distribution that is not (yet) provided by the `probability` crate.
///
/// # Technical Details
///
/// - For most users, this trait is just a re-export (similar to a type alias) of the
/// [`Inverse` trait from the `probability` crate]. You'll need a type that implements
/// `Inverse` when you call [`LeakyQuantizer::quantize`] and then use the resulting
/// [`LeakilyQuantizedDistribution`] for *decoding*. The `probability` crate provides
/// implementations of `Inverse` for several common probability distributions.
/// - Unfortunately, the `probability` crate does not support `no_std` mode. Thus, if you
/// compile `constriction` in `no_std` mode (by setting `default-features = false` for
/// `constriction` in your `Cargo.toml`) then you can't use the `probability` crate . In
/// this case, the present trait (`constriction::stream::model::Inverse`) is not a
/// re-export but instead a literal copy of its definition in the `probability` trait.
///
/// # Advice for Implementors
///
/// If you implement your own probability distribution (rather than using a pre-defined
/// distribution from the `probability` crate) then it is usually better to implement *this*
/// trait rather than `probability::distribution::Inverse`. This way, your code will
/// work as expected both in `std` and in `no_std` mode.
///
/// # See Also
///
/// - [`Distribution`]
///
/// [`probability::distribution::Inverse`]:
/// https://docs.rs/probability/latest/probability/distribution/trait.Inverse.html
/// [`Inverse` trait from the `probability` crate]:
/// https://docs.rs/probability/latest/probability/distribution/trait.Inverse.html
/// [`probability`]: https://docs.rs/probability/latest/probability/
#[cfg(feature = "probability")]
pub use probability::distribution::Inverse;

/// Mock replacement for [`probability::distribution::Distribution`] in a `no_std` context
///
/// This trait is only exported if `constriction` is used in a `no_std` context (i.e., with
/// `default-features = false`). In this case, we can't use the `probability` crate because
/// it uses a lot of FFI calls. However, for many things, we really only need the trait
/// definitions for `Distribution` and for [`Inverse`], so we copy them here.
#[cfg(not(feature = "probability"))]
pub trait Distribution {
/// The type of outcomes.
type Value;

/// Compute the cumulative distribution function.
fn distribution(&self, x: f64) -> f64;
}

/// Mock replacement for [`probability::distribution::Distribution`] in a `no_std` context
///
/// This trait is only exported if `constriction` is used in a `no_std` context (i.e., with
/// `default-features = false`). In this case, we can't use the `probability` crate because
/// it uses a lot of FFI calls. However, for many things, we really only need the trait
/// definitions for [`Distribution`] and for `Inverse`, so we copy them here.
#[cfg(not(feature = "probability"))]
pub trait Inverse: Distribution {
/// Compute the inverse of the cumulative distribution function.
fn inverse(&self, p: f64) -> Self::Value;
}

use crate::{wrapping_pow2, BitArray, NonZeroBitArray};

/// Base trait for probabilistic models of a data source.
Expand Down Expand Up @@ -441,7 +369,6 @@ pub trait IterableEntropyModel<'m, const PRECISION: usize>: EntropyModel<PRECISI
/// Note that calling this method on a [`LeakilyQuantizedDistribution`] will return the
/// entropy *after quantization*, not the differential entropy of the underlying
/// continuous probability distribution.
#[cfg(any(feature = "std", feature = "libm"))]
fn entropy_base2<F>(&'m self) -> F
where
F: num_traits::Float + core::iter::Sum,
Expand Down Expand Up @@ -789,7 +716,6 @@ where
(*self).symbol_table()
}

#[cfg(any(feature = "std", feature = "libm"))]
fn entropy_base2<F>(&'m self) -> F
where
F: num_traits::Float + core::iter::Sum,
Expand Down Expand Up @@ -3147,7 +3073,6 @@ where
/// - because the order in which entries are stored will generally be different on each
/// program execution, rounding errors will be slightly different across multiple
/// program executions.
#[cfg(any(feature = "std", feature = "libm"))]
pub fn entropy_base2<F>(&self) -> F
where
F: num_traits::Float + core::iter::Sum,
Expand Down

0 comments on commit 3648102

Please sign in to comment.