| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use crate::Rng; |
| 13 |
use core::iter; |
| 14 |
#[cfg(feature = "alloc")] |
| 15 |
use alloc::string::String; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
pub trait Distribution<T> { |
| 36 |
|
| 37 |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
fn sample_iter<R>(self, rng: R) -> DistIter<Self, R, T> |
| 74 |
where |
| 75 |
R: Rng, |
| 76 |
Self: Sized, |
| 77 |
{ |
| 78 |
DistIter { |
| 79 |
distr: self, |
| 80 |
rng, |
| 81 |
phantom: ::core::marker::PhantomData, |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
fn map<F, S>(self, func: F) -> DistMap<Self, F, T, S> |
| 103 |
where |
| 104 |
F: Fn(T) -> S, |
| 105 |
Self: Sized, |
| 106 |
{ |
| 107 |
DistMap { |
| 108 |
distr: self, |
| 109 |
func, |
| 110 |
phantom: ::core::marker::PhantomData, |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
impl<'a, T, D: Distribution<T>> Distribution<T> for &'a D { |
| 116 |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> T { |
| 117 |
(*self).sample(rng) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
#[derive(Debug)] |
| 129 |
pub struct DistIter<D, R, T> { |
| 130 |
distr: D, |
| 131 |
rng: R, |
| 132 |
phantom: ::core::marker::PhantomData<T>, |
| 133 |
} |
| 134 |
|
| 135 |
impl<D, R, T> Iterator for DistIter<D, R, T> |
| 136 |
where |
| 137 |
D: Distribution<T>, |
| 138 |
R: Rng, |
| 139 |
{ |
| 140 |
type Item = T; |
| 141 |
|
| 142 |
#[inline(always)] |
| 143 |
fn next(&mut self) -> Option<T> { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
Some(self.distr.sample(&mut self.rng)) |
| 148 |
} |
| 149 |
|
| 150 |
fn size_hint(&self) -> (usize, Option<usize>) { |
| 151 |
(usize::max_value(), None) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
impl<D, R, T> iter::FusedIterator for DistIter<D, R, T> |
| 156 |
where |
| 157 |
D: Distribution<T>, |
| 158 |
R: Rng, |
| 159 |
{ |
| 160 |
} |
| 161 |
|
| 162 |
#[cfg(feature = "nightly")] |
| 163 |
unsafe impl<D, R, T> iter::TrustedLen for DistIter<D, R, T> |
| 164 |
where |
| 165 |
D: Distribution<T>, |
| 166 |
R: Rng, |
| 167 |
{ |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#[derive(Debug)] |
| 176 |
pub struct DistMap<D, F, T, S> { |
| 177 |
distr: D, |
| 178 |
func: F, |
| 179 |
phantom: ::core::marker::PhantomData<fn(T) -> S>, |
| 180 |
} |
| 181 |
|
| 182 |
impl<D, F, T, S> Distribution<S> for DistMap<D, F, T, S> |
| 183 |
where |
| 184 |
D: Distribution<T>, |
| 185 |
F: Fn(T) -> S, |
| 186 |
{ |
| 187 |
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> S { |
| 188 |
(self.func)(self.distr.sample(rng)) |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
#[cfg(feature = "alloc")] |
| 197 |
pub trait DistString { |
| 198 |
|
| 199 |
fn append_string<R: Rng + ?Sized>(&self, rng: &mut R, string: &mut String, len: usize); |
| 200 |
|
| 201 |
|
| 202 |
#[inline] |
| 203 |
fn sample_string<R: Rng + ?Sized>(&self, rng: &mut R, len: usize) -> String { |
| 204 |
let mut s = String::new(); |
| 205 |
self.append_string(rng, &mut s, len); |
| 206 |
s |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
#[cfg(test)] |
| 211 |
mod tests { |
| 212 |
use crate::distributions::{Distribution, Uniform}; |
| 213 |
use crate::Rng; |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn test_distributions_iter() { |
| 217 |
use crate::distributions::Open01; |
| 218 |
let mut rng = crate::test::rng(210); |
| 219 |
let distr = Open01; |
| 220 |
let mut iter = Distribution::<f32>::sample_iter(distr, &mut rng); |
| 221 |
let mut sum: f32 = 0.; |
| 222 |
for _ in 0..100 { |
| 223 |
sum += iter.next().unwrap(); |
| 224 |
} |
| 225 |
assert!(0. < sum && sum < 100.); |
| 226 |
} |
| 227 |
|
| 228 |
#[test] |
| 229 |
fn test_distributions_map() { |
| 230 |
let dist = Uniform::new_inclusive(0, 5).map(|val| val + 15); |
| 231 |
|
| 232 |
let mut rng = crate::test::rng(212); |
| 233 |
let val = dist.sample(&mut rng); |
| 234 |
assert!((15..=20).contains(&val)); |
| 235 |
} |
| 236 |
|
| 237 |
#[test] |
| 238 |
fn test_make_an_iter() { |
| 239 |
fn ten_dice_rolls_other_than_five<R: Rng>( |
| 240 |
rng: &mut R, |
| 241 |
) -> impl Iterator<Item = i32> + '_ { |
| 242 |
Uniform::new_inclusive(1, 6) |
| 243 |
.sample_iter(rng) |
| 244 |
.filter(|x| *x != 5) |
| 245 |
.take(10) |
| 246 |
} |
| 247 |
|
| 248 |
let mut rng = crate::test::rng(211); |
| 249 |
let mut count = 0; |
| 250 |
for val in ten_dice_rolls_other_than_five(&mut rng) { |
| 251 |
assert!((1..=6).contains(&val) && val != 5); |
| 252 |
count += 1; |
| 253 |
} |
| 254 |
assert_eq!(count, 10); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
#[cfg(feature = "alloc")] |
| 259 |
fn test_dist_string() { |
| 260 |
use core::str; |
| 261 |
use crate::distributions::{Alphanumeric, DistString, Standard}; |
| 262 |
let mut rng = crate::test::rng(213); |
| 263 |
|
| 264 |
let s1 = Alphanumeric.sample_string(&mut rng, 20); |
| 265 |
assert_eq!(s1.len(), 20); |
| 266 |
assert_eq!(str::from_utf8(s1.as_bytes()), Ok(s1.as_str())); |
| 267 |
|
| 268 |
let s2 = Standard.sample_string(&mut rng, 20); |
| 269 |
assert_eq!(s2.chars().count(), 20); |
| 270 |
assert_eq!(str::from_utf8(s2.as_bytes()), Ok(s2.as_str())); |
| 271 |
} |
| 272 |
} |