nalgebra/src/base/helper.rs

27 lines
661 B
Rust
Raw Normal View History

2018-02-02 19:26:35 +08:00
#[cfg(feature = "arbitrary")]
use quickcheck::{Arbitrary, Gen};
2018-05-23 05:58:14 +08:00
use rand::distributions::{Distribution, Standard};
use rand::Rng;
/// Simple helper function for rejection sampling
2018-02-02 19:26:35 +08:00
#[cfg(feature = "arbitrary")]
#[doc(hidden)]
#[inline]
pub fn reject<G: Gen, F: FnMut(&T) -> bool, T: Arbitrary>(g: &mut G, f: F) -> T {
use std::iter;
2018-02-02 19:26:35 +08:00
iter::repeat(())
.map(|_| Arbitrary::arbitrary(g))
.find(f)
.unwrap()
}
#[doc(hidden)]
#[inline]
2018-05-23 05:58:14 +08:00
pub fn reject_rand<G: Rng + ?Sized, F: FnMut(&T) -> bool, T>(g: &mut G, f: F) -> T
where
Standard: Distribution<T>,
{
use std::iter;
2018-05-23 05:58:14 +08:00
iter::repeat(()).map(|_| g.gen()).find(f).unwrap()
}