|
1 |
| -// Copyright 2018 Developers of the Rand project. |
| 1 | +// Copyright 2018-2020 Developers of the Rand project. |
2 | 2 | // Copyright 2017 The Rust Project Developers.
|
3 | 3 | //
|
4 | 4 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
@@ -110,7 +110,7 @@ use core::ops::{Range, RangeInclusive};
|
110 | 110 | use crate::distributions::float::IntoFloat;
|
111 | 111 | use crate::distributions::utils::{BoolAsSIMD, FloatAsSIMD, FloatSIMDUtils, WideningMultiply};
|
112 | 112 | use crate::distributions::Distribution;
|
113 |
| -use crate::Rng; |
| 113 | +use crate::{Rng, RngCore}; |
114 | 114 |
|
115 | 115 | #[cfg(not(feature = "std"))]
|
116 | 116 | #[allow(unused_imports)] // rustc doesn't detect that this is actually used
|
@@ -338,6 +338,43 @@ where Borrowed: SampleUniform
|
338 | 338 | }
|
339 | 339 | }
|
340 | 340 |
|
| 341 | +/// Range that supports generating a single sample efficiently. |
| 342 | +/// |
| 343 | +/// Any type implementing this trait can be used to specify the sampled range |
| 344 | +/// for `Rng::gen_range`. |
| 345 | +pub trait SampleRange<T> { |
| 346 | + /// Generate a sample from the given range. |
| 347 | + fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T; |
| 348 | + |
| 349 | + /// Check whether the range is empty. |
| 350 | + fn is_empty(&self) -> bool; |
| 351 | +} |
| 352 | + |
| 353 | +impl<T: SampleUniform + PartialOrd> SampleRange<T> for Range<T> { |
| 354 | + #[inline] |
| 355 | + fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T { |
| 356 | + T::Sampler::sample_single(self.start, self.end, rng) |
| 357 | + } |
| 358 | + |
| 359 | + #[inline] |
| 360 | + fn is_empty(&self) -> bool { |
| 361 | + !(self.start < self.end) |
| 362 | + } |
| 363 | +} |
| 364 | + |
| 365 | +impl<T: SampleUniform + PartialOrd> SampleRange<T> for RangeInclusive<T> { |
| 366 | + #[inline] |
| 367 | + fn sample_single<R: RngCore + ?Sized>(self, rng: &mut R) -> T { |
| 368 | + T::Sampler::sample_single_inclusive(self.start(), self.end(), rng) |
| 369 | + } |
| 370 | + |
| 371 | + #[inline] |
| 372 | + fn is_empty(&self) -> bool { |
| 373 | + !(self.start() <= self.end()) |
| 374 | + } |
| 375 | +} |
| 376 | + |
| 377 | + |
341 | 378 | ////////////////////////////////////////////////////////////////////////////////
|
342 | 379 |
|
343 | 380 | // What follows are all back-ends.
|
|
0 commit comments