Skip to content

Commit 4f85172

Browse files
committed
Update to probability 0.20
This one is `no_std` compatible
1 parent 5994eec commit 4f85172

5 files changed

Lines changed: 38 additions & 89 deletions

File tree

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ name = "constriction"
2020

2121
[features]
2222
default = ["std"]
23-
libm = ["num-traits/libm"]
24-
std = ["probability", "num-traits/std"]
23+
std = []
2524

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

3231
[dependencies]
3332
hashbrown = "0.12.3"
34-
num-traits = {version = "0.2.15", default-features = false}
33+
num-traits = {version = "0.2.15", default-features = false, features = ["libm"]}
3534
smallvec = "1.6.1"
3635

37-
probability = {version = "0.19.1", optional = true}
36+
probability = {version = "0.20"}
3837

3938
ndarray = {version = "0.15", optional = true}
4039
numpy = {version = "0.17.1", optional = true}
@@ -43,7 +42,6 @@ pyo3 = {version = "0.17.1", features = ["extension-module"], optional = true}
4342
[dev-dependencies]
4443
byteorder = "1.4.2"
4544
criterion = "0.3"
46-
probability = "0.19.1"
4745
rand = "0.8.3"
4846
rand_pcg = "0.3"
4947
rand_xoshiro = "0.6"

ensure_no_std/Cargo.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ensure_no_std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ version = "0.1.0"
1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515

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

2020
[profile.dev]

src/stream/model.rs

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -133,110 +133,38 @@ use alloc::{boxed::Box, vec::Vec};
133133
use core::{borrow::Borrow, fmt::Debug, hash::Hash, marker::PhantomData, ops::RangeInclusive};
134134
use num_traits::{float::FloatCore, AsPrimitive, One, PrimInt, WrappingAdd, WrappingSub, Zero};
135135

136-
/// Re-export or replacement of [`probability::distribution::Distribution`].
136+
/// Re-export of [`probability::distribution::Distribution`].
137137
///
138138
/// Most users will never have to interact with this trait directly. When a method requires
139139
/// a type that implements `Distribution`, most users will likely use a predefined type from
140140
/// the [`probability`] crate. You only need to implement this trait if you want to use a
141141
/// probability distribution that is not (yet) provided by the `probability` crate.
142142
///
143-
/// # Technical Details
144-
///
145-
/// - For most users, this trait is just a re-export (similar to a type alias) of the
146-
/// [`Distribution` trait from the `probability` crate]. You'll need a type that
147-
/// implements `Distribution` when you call [`LeakyQuantizer::quantize`]. The
148-
/// `probability` crate provides implementations of several common `Distribution`s.
149-
/// - Unfortunately, the `probability` crate does not support `no_std` mode. Thus, if you
150-
/// compile `constriction` in `no_std` mode (by setting `default-features = false` for
151-
/// `constriction` in your `Cargo.toml`) then you can't use the `probability` crate . In
152-
/// this case, the present trait (`constriction::stream::model::Distribution`) is not a
153-
/// re-export but instead a literal copy of its definition in the `probability` trait.
154-
///
155-
/// # Advice for Implementors
156-
///
157-
/// If you implement your own probability distribution (rather than using a pre-defined
158-
/// distribution from the `probability` crate) then it is usually better to implement *this*
159-
/// trait rather than `probability::distribution::Distribution`. This way, your code will
160-
/// work as expected both in `std` and in `no_std` mode.
161-
///
162143
/// # See Also
163144
///
164145
/// - [`Inverse`]
165146
///
166147
/// [`probability::distribution::Distribution`]:
167148
/// https://docs.rs/probability/latest/probability/distribution/trait.Distribution.html
168-
/// [`Distribution` trait from the `probability` crate]:
169-
/// https://docs.rs/probability/latest/probability/distribution/trait.Distribution.html
170149
/// [`probability`]: https://docs.rs/probability/latest/probability/
171-
#[cfg(feature = "probability")]
172150
pub use probability::distribution::Distribution;
173151

174-
/// Re-export or replacement of [`probability::distribution::Inverse`].
152+
/// Re-export of [`probability::distribution::Inverse`].
175153
///
176154
/// Most users will never have to interact with this trait directly. When a method requires
177155
/// a type that implements `Inverse`, most users will likely use a predefined type from
178156
/// the [`probability`] crate. You only need to implement this trait if you want to use a
179157
/// probability distribution that is not (yet) provided by the `probability` crate.
180158
///
181-
/// # Technical Details
182-
///
183-
/// - For most users, this trait is just a re-export (similar to a type alias) of the
184-
/// [`Inverse` trait from the `probability` crate]. You'll need a type that implements
185-
/// `Inverse` when you call [`LeakyQuantizer::quantize`] and then use the resulting
186-
/// [`LeakilyQuantizedDistribution`] for *decoding*. The `probability` crate provides
187-
/// implementations of `Inverse` for several common probability distributions.
188-
/// - Unfortunately, the `probability` crate does not support `no_std` mode. Thus, if you
189-
/// compile `constriction` in `no_std` mode (by setting `default-features = false` for
190-
/// `constriction` in your `Cargo.toml`) then you can't use the `probability` crate . In
191-
/// this case, the present trait (`constriction::stream::model::Inverse`) is not a
192-
/// re-export but instead a literal copy of its definition in the `probability` trait.
193-
///
194-
/// # Advice for Implementors
195-
///
196-
/// If you implement your own probability distribution (rather than using a pre-defined
197-
/// distribution from the `probability` crate) then it is usually better to implement *this*
198-
/// trait rather than `probability::distribution::Inverse`. This way, your code will
199-
/// work as expected both in `std` and in `no_std` mode.
200-
///
201159
/// # See Also
202160
///
203161
/// - [`Distribution`]
204162
///
205163
/// [`probability::distribution::Inverse`]:
206164
/// https://docs.rs/probability/latest/probability/distribution/trait.Inverse.html
207-
/// [`Inverse` trait from the `probability` crate]:
208-
/// https://docs.rs/probability/latest/probability/distribution/trait.Inverse.html
209165
/// [`probability`]: https://docs.rs/probability/latest/probability/
210-
#[cfg(feature = "probability")]
211166
pub use probability::distribution::Inverse;
212167

213-
/// Mock replacement for [`probability::distribution::Distribution`] in a `no_std` context
214-
///
215-
/// This trait is only exported if `constriction` is used in a `no_std` context (i.e., with
216-
/// `default-features = false`). In this case, we can't use the `probability` crate because
217-
/// it uses a lot of FFI calls. However, for many things, we really only need the trait
218-
/// definitions for `Distribution` and for [`Inverse`], so we copy them here.
219-
#[cfg(not(feature = "probability"))]
220-
pub trait Distribution {
221-
/// The type of outcomes.
222-
type Value;
223-
224-
/// Compute the cumulative distribution function.
225-
fn distribution(&self, x: f64) -> f64;
226-
}
227-
228-
/// Mock replacement for [`probability::distribution::Distribution`] in a `no_std` context
229-
///
230-
/// This trait is only exported if `constriction` is used in a `no_std` context (i.e., with
231-
/// `default-features = false`). In this case, we can't use the `probability` crate because
232-
/// it uses a lot of FFI calls. However, for many things, we really only need the trait
233-
/// definitions for [`Distribution`] and for `Inverse`, so we copy them here.
234-
#[cfg(not(feature = "probability"))]
235-
pub trait Inverse: Distribution {
236-
/// Compute the inverse of the cumulative distribution function.
237-
fn inverse(&self, p: f64) -> Self::Value;
238-
}
239-
240168
use crate::{wrapping_pow2, BitArray, NonZeroBitArray};
241169

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

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

0 commit comments

Comments
 (0)