Skip to content
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

Remove Sized constraint for generic Rng #175

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions src/chacha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
}

impl Rand for ChaChaRng {
fn rand<R: Rng>(other: &mut R) -> ChaChaRng {
fn rand<R: Rng + ?Sized>(other: &mut R) -> ChaChaRng {
let mut key : [u32; KEY_WORDS] = [0; KEY_WORDS];
for word in key.iter_mut() {
*word = other.gen();
*word = other.next_u32();
}
SeedableRng::from_seed(&key[..])
}
Expand Down
12 changes: 6 additions & 6 deletions src/distributions/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ pub struct Exp1(pub f64);
// This could be done via `-rng.gen::<f64>().ln()` but that is slower.
impl Rand for Exp1 {
#[inline]
fn rand<R:Rng>(rng: &mut R) -> Exp1 {
fn rand<R:Rng + ?Sized>(rng: &mut R) -> Exp1 {
#[inline]
fn pdf(x: f64) -> f64 {
(-x).exp()
}
#[inline]
fn zero_case<R:Rng>(rng: &mut R, _u: f64) -> f64 {
ziggurat_tables::ZIG_EXP_R - rng.gen::<f64>().ln()
fn zero_case<R:Rng + ?Sized>(rng: &mut R, _u: f64) -> f64 {
ziggurat_tables::ZIG_EXP_R - rng.next_f64().ln()
}

Exp1(ziggurat(rng, false,
Expand Down Expand Up @@ -88,11 +88,11 @@ impl Exp {
}

impl Sample<f64> for Exp {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for Exp {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
let Exp1(n) = rng.gen::<Exp1>();
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let Exp1(n) = Exp1::rand(rng);
n * self.lambda_inverse
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/distributions/gamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use self::GammaRepr::*;
use self::ChiSquaredRepr::*;

use {Rng, Open01};
use {Rand, Rng, Open01};
use super::normal::StandardNormal;
use super::{IndependentSample, Sample, Exp};

Expand Down Expand Up @@ -134,17 +134,17 @@ impl GammaLargeShape {
}

impl Sample<f64> for Gamma {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl Sample<f64> for GammaSmallShape {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl Sample<f64> for GammaLargeShape {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}

impl IndependentSample<f64> for Gamma {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
match self.repr {
Small(ref g) => g.ind_sample(rng),
One(ref g) => g.ind_sample(rng),
Expand All @@ -153,23 +153,23 @@ impl IndependentSample<f64> for Gamma {
}
}
impl IndependentSample<f64> for GammaSmallShape {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
let Open01(u) = rng.gen::<Open01<f64>>();
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let Open01(u) = Open01::<f64>::rand(rng);

self.large_shape.ind_sample(rng) * u.powf(self.inv_shape)
}
}
impl IndependentSample<f64> for GammaLargeShape {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
loop {
let StandardNormal(x) = rng.gen::<StandardNormal>();
let StandardNormal(x) = StandardNormal::rand(rng);
let v_cbrt = 1.0 + self.c * x;
if v_cbrt <= 0.0 { // a^3 <= 0 iff a <= 0
continue
}

let v = v_cbrt * v_cbrt * v_cbrt;
let Open01(u) = rng.gen::<Open01<f64>>();
let Open01(u) = Open01::<f64>::rand(rng);

let x_sqr = x * x;
if u < 1.0 - 0.0331 * x_sqr * x_sqr ||
Expand Down Expand Up @@ -225,14 +225,14 @@ impl ChiSquared {
}
}
impl Sample<f64> for ChiSquared {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for ChiSquared {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
match self.repr {
DoFExactlyOne => {
// k == 1 => N(0,1)^2
let StandardNormal(norm) = rng.gen::<StandardNormal>();
let StandardNormal(norm) = StandardNormal::rand(rng);
norm * norm
}
DoFAnythingElse(ref g) => g.ind_sample(rng)
Expand Down Expand Up @@ -279,10 +279,10 @@ impl FisherF {
}
}
impl Sample<f64> for FisherF {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for FisherF {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
self.numer.ind_sample(rng) / self.denom.ind_sample(rng) * self.dof_ratio
}
}
Expand Down Expand Up @@ -317,11 +317,11 @@ impl StudentT {
}
}
impl Sample<f64> for StudentT {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for StudentT {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
let StandardNormal(norm) = rng.gen::<StandardNormal>();
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let StandardNormal(norm) = StandardNormal::rand(rng);
norm * (self.dof / self.chi.ind_sample(rng)).sqrt()
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/distributions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub mod exponential;
pub trait Sample<Support> {
/// Generate a random value of `Support`, using `rng` as the
/// source of randomness.
fn sample<R: Rng>(&mut self, rng: &mut R) -> Support;
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Support;
}

/// `Sample`s that do not require keeping track of state.
Expand All @@ -48,7 +48,7 @@ pub trait Sample<Support> {
// trait called `Sample` and the other should be `DependentSample`.
pub trait IndependentSample<Support>: Sample<Support> {
/// Generate a random value.
fn ind_sample<R: Rng>(&self, &mut R) -> Support;
fn ind_sample<R: Rng + ?Sized>(&self, &mut R) -> Support;
}

/// A wrapper for generating types that implement `Rand` via the
Expand All @@ -64,12 +64,12 @@ impl<Sup> Clone for RandSample<Sup> {
}

impl<Sup: Rand> Sample<Sup> for RandSample<Sup> {
fn sample<R: Rng>(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) }
}

impl<Sup: Rand> IndependentSample<Sup> for RandSample<Sup> {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> Sup {
rng.gen()
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Sup {
Sup::rand(rng)
}
}

Expand Down Expand Up @@ -156,11 +156,11 @@ impl<'a, T: Clone> WeightedChoice<'a, T> {
}

impl<'a, T: Clone> Sample<T> for WeightedChoice<'a, T> {
fn sample<R: Rng>(&mut self, rng: &mut R) -> T { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> T { self.ind_sample(rng) }
}

impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T> {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> T {
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T {
// we want to find the first element that has cumulative
// weight > sample_weight, which we do by binary since the
// cumulative weights of self.items are sorted.
Expand Down Expand Up @@ -220,7 +220,7 @@ mod ziggurat_tables;
// the perf improvement (25-50%) is definitely worth the extra code
// size from force-inlining.
#[inline(always)]
fn ziggurat<R: Rng, P, Z>(
fn ziggurat<R: Rng + ?Sized, P, Z>(
rng: &mut R,
symmetric: bool,
x_tab: ziggurat_tables::ZigTable,
Expand All @@ -243,7 +243,7 @@ fn ziggurat<R: Rng, P, Z>(
// efficiently and overload next_f32/f64, so by not calling it
// this may be slower than it would be otherwise.)
// FIXME: investigate/optimise for the above.
let bits: u64 = rng.gen();
let bits: u64 = rng.next_u64();
let i = (bits & 0xff) as usize;
let f = (bits >> 11) as f64 / SCALE;

Expand All @@ -262,7 +262,7 @@ fn ziggurat<R: Rng, P, Z>(
return zero_case(rng, u);
}
// algebraically equivalent to f1 + DRanU()*(f0 - f1) < 1
if f_tab[i + 1] + (f_tab[i] - f_tab[i + 1]) * rng.gen::<f64>() < pdf(x) {
if f_tab[i + 1] + (f_tab[i] - f_tab[i + 1]) * rng.next_f64() < pdf(x) {
return x;
}
}
Expand All @@ -277,7 +277,7 @@ mod tests {
#[derive(PartialEq, Debug)]
struct ConstRand(usize);
impl Rand for ConstRand {
fn rand<R: Rng>(_: &mut R) -> ConstRand {
fn rand<R: Rng + ?Sized>(_: &mut R) -> ConstRand {
ConstRand(0)
}
}
Expand Down
18 changes: 9 additions & 9 deletions src/distributions/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ use distributions::{ziggurat, ziggurat_tables, Sample, IndependentSample};
pub struct StandardNormal(pub f64);

impl Rand for StandardNormal {
fn rand<R:Rng>(rng: &mut R) -> StandardNormal {
fn rand<R:Rng + ?Sized>(rng: &mut R) -> StandardNormal {
#[inline]
fn pdf(x: f64) -> f64 {
(-x*x/2.0).exp()
}
#[inline]
fn zero_case<R:Rng>(rng: &mut R, u: f64) -> f64 {
fn zero_case<R:Rng + ?Sized>(rng: &mut R, u: f64) -> f64 {
// compute a random number in the tail by hand

// strange initial conditions, because the loop is not
Expand All @@ -54,8 +54,8 @@ impl Rand for StandardNormal {
let mut y = 0.0f64;

while -2.0 * y < x * x {
let Open01(x_) = rng.gen::<Open01<f64>>();
let Open01(y_) = rng.gen::<Open01<f64>>();
let Open01(x_) = Open01::<f64>::rand(rng);
let Open01(y_) = Open01::<f64>::rand(rng);

x = x_.ln() / ziggurat_tables::ZIG_NORM_R;
y = y_.ln();
Expand Down Expand Up @@ -111,11 +111,11 @@ impl Normal {
}
}
impl Sample<f64> for Normal {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for Normal {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
let StandardNormal(n) = rng.gen::<StandardNormal>();
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
let StandardNormal(n) = StandardNormal::rand(rng);
self.mean + self.std_dev * n
}
}
Expand Down Expand Up @@ -155,10 +155,10 @@ impl LogNormal {
}
}
impl Sample<f64> for LogNormal {
fn sample<R: Rng>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> f64 { self.ind_sample(rng) }
}
impl IndependentSample<f64> for LogNormal {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> f64 {
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> f64 {
self.norm.ind_sample(rng).exp()
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/distributions/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::num::Wrapping as w;

use Rng;
use {Rand, Rng};
use distributions::{Sample, IndependentSample};

/// Sample values uniformly between two bounds.
Expand Down Expand Up @@ -64,10 +64,10 @@ impl<X: SampleRange + PartialOrd> Range<X> {

impl<Sup: SampleRange> Sample<Sup> for Range<Sup> {
#[inline]
fn sample<R: Rng>(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) }
fn sample<R: Rng + ?Sized>(&mut self, rng: &mut R) -> Sup { self.ind_sample(rng) }
}
impl<Sup: SampleRange> IndependentSample<Sup> for Range<Sup> {
fn ind_sample<R: Rng>(&self, rng: &mut R) -> Sup {
fn ind_sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Sup {
SampleRange::sample_range(self, rng)
}
}
Expand All @@ -84,7 +84,7 @@ pub trait SampleRange : Sized {

/// Sample a value from the given `Range` with the given `Rng` as
/// a source of randomness.
fn sample_range<R: Rng>(r: &Range<Self>, rng: &mut R) -> Self;
fn sample_range<R: Rng + ?Sized>(r: &Range<Self>, rng: &mut R) -> Self;
}

macro_rules! integer_impl {
Expand Down Expand Up @@ -115,10 +115,10 @@ macro_rules! integer_impl {
}

#[inline]
fn sample_range<R: Rng>(r: &Range<$ty>, rng: &mut R) -> $ty {
fn sample_range<R: Rng + ?Sized>(r: &Range<$ty>, rng: &mut R) -> $ty {
loop {
// rejection sample
let v = rng.gen::<$unsigned>();
let v = <$unsigned>::rand(rng);
// until we find something that fits into the
// region which r.range evenly divides (this will
// be uniformly distributed)
Expand Down Expand Up @@ -153,8 +153,8 @@ macro_rules! float_impl {
accept_zone: 0.0 // unused
}
}
fn sample_range<R: Rng>(r: &Range<$ty>, rng: &mut R) -> $ty {
r.low + r.range * rng.gen::<$ty>()
fn sample_range<R: Rng + ?Sized>(r: &Range<$ty>, rng: &mut R) -> $ty {
r.low + r.range * <$ty>::rand(rng)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a> SeedableRng<&'a [u32]> for IsaacRng {
}

impl Rand for IsaacRng {
fn rand<R: Rng>(other: &mut R) -> IsaacRng {
fn rand<R: Rng + ?Sized>(other: &mut R) -> IsaacRng {
let mut ret = EMPTY;
unsafe {
let ptr = ret.rsl.as_mut_ptr() as *mut u8;
Expand Down Expand Up @@ -492,7 +492,7 @@ impl<'a> SeedableRng<&'a [u64]> for Isaac64Rng {
}

impl Rand for Isaac64Rng {
fn rand<R: Rng>(other: &mut R) -> Isaac64Rng {
fn rand<R: Rng + ?Sized>(other: &mut R) -> Isaac64Rng {
let mut ret = EMPTY_64;
unsafe {
let ptr = ret.rsl.as_mut_ptr() as *mut u8;
Expand Down
Loading