Skip to content

Commit

Permalink
Move SampleRange to distributions::uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
vks committed Jul 31, 2020
1 parent f727b95 commit 03062f1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
41 changes: 39 additions & 2 deletions src/distributions/uniform.rs
@@ -1,4 +1,4 @@
// Copyright 2018 Developers of the Rand project.
// Copyright 2018-2020 Developers of the Rand project.
// Copyright 2017 The Rust Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
Expand Down Expand Up @@ -110,7 +110,7 @@ use core::ops::{Range, RangeInclusive};
use crate::distributions::float::IntoFloat;
use crate::distributions::utils::{BoolAsSIMD, FloatAsSIMD, FloatSIMDUtils, WideningMultiply};
use crate::distributions::Distribution;
use crate::Rng;
use crate::{Rng, RngCore};

#[cfg(not(feature = "std"))]
#[allow(unused_imports)] // rustc doesn't detect that this is actually used
Expand Down Expand Up @@ -338,6 +338,43 @@ where Borrowed: SampleUniform
}
}

/// Range that supports generating a single sample efficiently.
///
/// Any type implementing this trait can be used to specify the sampled range
/// for `Rng::gen_range`.
pub trait SampleRange<T> {
/// Generate a sample from the given range.
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T;

/// Check whether the range is empty.
fn is_empty(&self) -> bool;
}

impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T> {
#[inline]
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
T::Sampler::sample_single(self.start, self.end, rng)
}

#[inline]
fn is_empty(&self) -> bool {
!(self.start < self.end)
}
}

impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> {
#[inline]
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
T::Sampler::sample_single_inclusive(self.start(), self.end(), rng)
}

#[inline]
fn is_empty(&self) -> bool {
!(self.start() <= self.end())
}
}


////////////////////////////////////////////////////////////////////////////////

// What follows are all back-ends.
Expand Down
39 changes: 1 addition & 38 deletions src/rng.rs
Expand Up @@ -10,47 +10,10 @@
//! [`Rng`] trait

use rand_core::{Error, RngCore};
use crate::distributions::uniform::{SampleUniform, UniformSampler};
use crate::distributions::uniform::{SampleRange, SampleUniform};
use crate::distributions::{self, Distribution, Standard};
use core::num::Wrapping;
use core::{mem, slice};
use core::ops::{Range, RangeInclusive};

/// Range that supports generating a single sample efficiently.
///
/// Any type implementing this trait can be used to specify the sampled range
/// for `Rng::gen_range`.
pub trait SampleRange<T> {
/// Generate a sample from the given range.
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T;

/// Check whether the range is empty.
fn is_empty(&self) -> bool;
}

impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T> {
#[inline]
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
T::Sampler::sample_single(self.start, self.end, rng)
}

#[inline]
fn is_empty(&self) -> bool {
!(self.start < self.end)
}
}

impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> {
#[inline]
fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T {
T::Sampler::sample_single_inclusive(self.start(), self.end(), rng)
}

#[inline]
fn is_empty(&self) -> bool {
!(self.start() <= self.end())
}
}

/// An automatically-implemented extension trait on [`RngCore`] providing high-level
/// generic methods for sampling values and other convenience methods.
Expand Down

0 comments on commit 03062f1

Please sign in to comment.