From 3648102afb7eb22621b4e1534e8fd96a54d621b7 Mon Sep 17 00:00:00 2001 From: Robert Bamler Date: Mon, 7 Nov 2022 02:25:27 +0100 Subject: [PATCH] Update to `probability` 0.20 This one is `no_std` compatible --- Cargo.lock | 12 +++--- Cargo.toml | 8 ++-- ensure_no_std/Cargo.lock | 26 +++++++++++++ ensure_no_std/Cargo.toml | 2 +- src/stream/model.rs | 79 +--------------------------------------- 5 files changed, 38 insertions(+), 89 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9c02c22..2c3db12 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -478,9 +478,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "probability" -version = "0.19.1" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52bcca6a0257cb7ea4599f2d3bd8141f63a4becee52230c040a4d815bea66f2a" +checksum = "1d5506784a003ab90372c5ce00c4b5d68a14d2b90b7170de1be0dfe3faa43bb6" dependencies = [ "random", "special", @@ -614,9 +614,9 @@ dependencies = [ [[package]] name = "random" -version = "0.12.2" +version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97d13a3485349981c90c79112a11222c3e6e75de1d52b87a7525b3bf5361420f" +checksum = "474c42c904f04dfe2a595a02f71e1a0e5e92ffb5761cc9a4c02140b93b8dd504" [[package]] name = "rawpointer" @@ -745,9 +745,9 @@ checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" [[package]] name = "special" -version = "0.9.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f70115e5d5fe08b5f02f7c709c4bf72dc299528bca63e46183e0389a12a0a645" +checksum = "d256a3d3497ee65685b3c0267cf13eb9d32920e58eac5bf0e457cb12c3c4f5bb" dependencies = [ "libm", ] diff --git a/Cargo.toml b/Cargo.toml index bd3f686..e1350f9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 @@ -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} @@ -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" diff --git a/ensure_no_std/Cargo.lock b/ensure_no_std/Cargo.lock index 84de1cc..4ea550b 100644 --- a/ensure_no_std/Cargo.lock +++ b/ensure_no_std/Cargo.lock @@ -31,6 +31,7 @@ version = "0.2.5" dependencies = [ "hashbrown", "num-traits", + "probability", "smallvec", ] @@ -109,6 +110,22 @@ version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860" +[[package]] +name = "probability" +version = "0.20.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d5506784a003ab90372c5ce00c4b5d68a14d2b90b7170de1be0dfe3faa43bb6" +dependencies = [ + "random", + "special", +] + +[[package]] +name = "random" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "474c42c904f04dfe2a595a02f71e1a0e5e92ffb5761cc9a4c02140b93b8dd504" + [[package]] name = "scopeguard" version = "1.1.0" @@ -132,6 +149,15 @@ version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507befe795404456341dfab10cef66ead4c041f62b8b11bbb92bffe5d0953e0" +[[package]] +name = "special" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d256a3d3497ee65685b3c0267cf13eb9d32920e58eac5bf0e457cb12c3c4f5bb" +dependencies = [ + "libm", +] + [[package]] name = "spin" version = "0.9.4" diff --git a/ensure_no_std/Cargo.toml b/ensure_no_std/Cargo.toml index face307..cffe7a2 100644 --- a/ensure_no_std/Cargo.toml +++ b/ensure_no_std/Cargo.toml @@ -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] diff --git a/src/stream/model.rs b/src/stream/model.rs index b0cf65a..bfbcf1c 100644 --- a/src/stream/model.rs +++ b/src/stream/model.rs @@ -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. @@ -441,7 +369,6 @@ pub trait IterableEntropyModel<'m, const PRECISION: usize>: EntropyModel(&'m self) -> F where F: num_traits::Float + core::iter::Sum, @@ -789,7 +716,6 @@ where (*self).symbol_table() } - #[cfg(any(feature = "std", feature = "libm"))] fn entropy_base2(&'m self) -> F where F: num_traits::Float + core::iter::Sum, @@ -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(&self) -> F where F: num_traits::Float + core::iter::Sum,