Skip to content

Commit

Permalink
Optimize imports (#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelOwenDyer committed Apr 5, 2024
1 parent 21a1ce1 commit bca078e
Show file tree
Hide file tree
Showing 25 changed files with 137 additions and 139 deletions.
46 changes: 23 additions & 23 deletions benches/distributions.rs
Expand Up @@ -101,7 +101,7 @@ macro_rules! distr_duration {
let x: Duration = distr.sample(&mut rng);
accum = accum
.checked_add(x)
.unwrap_or(Duration::new(u64::max_value(), 999_999_999));
.unwrap_or(Duration::new(u64::MAX, 999_999_999));
}
accum
});
Expand Down Expand Up @@ -145,10 +145,10 @@ distr_int!(distr_uniform_isize, isize, Uniform::new(-1060478432isize, 1858574057
distr_float!(distr_uniform_f32, f32, Uniform::new(2.26f32, 2.319).unwrap());
distr_float!(distr_uniform_f64, f64, Uniform::new(2.26f64, 2.319).unwrap());

const LARGE_SEC: u64 = u64::max_value() / 1000;
const LARGE_SEC: u64 = u64::MAX / 1000;

distr_duration!(distr_uniform_duration_largest,
Uniform::new_inclusive(Duration::new(0, 0), Duration::new(u64::max_value(), 999_999_999)).unwrap()
Uniform::new_inclusive(Duration::new(0, 0), Duration::new(u64::MAX, 999_999_999)).unwrap()
);
distr_duration!(distr_uniform_duration_large,
Uniform::new(Duration::new(0, 0), Duration::new(LARGE_SEC, 1_000_000_000 / 2)).unwrap()
Expand Down Expand Up @@ -332,26 +332,26 @@ macro_rules! uniform_single {
// (32769) will only reject 32769 / 4294967296 samples.
const HALF_16_BIT_UNSIGNED: u16 = 1 << 15;

uniform_sample!(uniform_u16x1_allm1_new, u16, 0, u16::max_value(), 1);
uniform_sample!(uniform_u16x1_allm1_new, u16, 0, u16::MAX, 1);
uniform_sample!(uniform_u16x1_halfp1_new, u16, 0, HALF_16_BIT_UNSIGNED + 1, 1);
uniform_sample!(uniform_u16x1_half_new, u16, 0, HALF_16_BIT_UNSIGNED, 1);
uniform_sample!(uniform_u16x1_halfm1_new, u16, 0, HALF_16_BIT_UNSIGNED - 1, 1);
uniform_sample!(uniform_u16x1_6_new, u16, 0, 6u16, 1);

uniform_single!(uniform_u16x1_allm1_single, u16, 0, u16::max_value(), 1);
uniform_single!(uniform_u16x1_allm1_single, u16, 0, u16::MAX, 1);
uniform_single!(uniform_u16x1_halfp1_single, u16, 0, HALF_16_BIT_UNSIGNED + 1, 1);
uniform_single!(uniform_u16x1_half_single, u16, 0, HALF_16_BIT_UNSIGNED, 1);
uniform_single!(uniform_u16x1_halfm1_single, u16, 0, HALF_16_BIT_UNSIGNED - 1, 1);
uniform_single!(uniform_u16x1_6_single, u16, 0, 6u16, 1);

uniform_inclusive!(uniform_u16x10_all_new_inclusive, u16, 0, u16::max_value(), 10);
uniform_sample!(uniform_u16x10_allm1_new, u16, 0, u16::max_value(), 10);
uniform_inclusive!(uniform_u16x10_all_new_inclusive, u16, 0, u16::MAX, 10);
uniform_sample!(uniform_u16x10_allm1_new, u16, 0, u16::MAX, 10);
uniform_sample!(uniform_u16x10_halfp1_new, u16, 0, HALF_16_BIT_UNSIGNED + 1, 10);
uniform_sample!(uniform_u16x10_half_new, u16, 0, HALF_16_BIT_UNSIGNED, 10);
uniform_sample!(uniform_u16x10_halfm1_new, u16, 0, HALF_16_BIT_UNSIGNED - 1, 10);
uniform_sample!(uniform_u16x10_6_new, u16, 0, 6u16, 10);

uniform_single!(uniform_u16x10_allm1_single, u16, 0, u16::max_value(), 10);
uniform_single!(uniform_u16x10_allm1_single, u16, 0, u16::MAX, 10);
uniform_single!(uniform_u16x10_halfp1_single, u16, 0, HALF_16_BIT_UNSIGNED + 1, 10);
uniform_single!(uniform_u16x10_half_single, u16, 0, HALF_16_BIT_UNSIGNED, 10);
uniform_single!(uniform_u16x10_halfm1_single, u16, 0, HALF_16_BIT_UNSIGNED - 1, 10);
Expand All @@ -360,80 +360,80 @@ uniform_single!(uniform_u16x10_6_single, u16, 0, 6u16, 10);

const HALF_32_BIT_UNSIGNED: u32 = 1 << 31;

uniform_sample!(uniform_u32x1_allm1_new, u32, 0, u32::max_value(), 1);
uniform_sample!(uniform_u32x1_allm1_new, u32, 0, u32::MAX, 1);
uniform_sample!(uniform_u32x1_halfp1_new, u32, 0, HALF_32_BIT_UNSIGNED + 1, 1);
uniform_sample!(uniform_u32x1_half_new, u32, 0, HALF_32_BIT_UNSIGNED, 1);
uniform_sample!(uniform_u32x1_halfm1_new, u32, 0, HALF_32_BIT_UNSIGNED - 1, 1);
uniform_sample!(uniform_u32x1_6_new, u32, 0, 6u32, 1);

uniform_single!(uniform_u32x1_allm1_single, u32, 0, u32::max_value(), 1);
uniform_single!(uniform_u32x1_allm1_single, u32, 0, u32::MAX, 1);
uniform_single!(uniform_u32x1_halfp1_single, u32, 0, HALF_32_BIT_UNSIGNED + 1, 1);
uniform_single!(uniform_u32x1_half_single, u32, 0, HALF_32_BIT_UNSIGNED, 1);
uniform_single!(uniform_u32x1_halfm1_single, u32, 0, HALF_32_BIT_UNSIGNED - 1, 1);
uniform_single!(uniform_u32x1_6_single, u32, 0, 6u32, 1);

uniform_inclusive!(uniform_u32x10_all_new_inclusive, u32, 0, u32::max_value(), 10);
uniform_sample!(uniform_u32x10_allm1_new, u32, 0, u32::max_value(), 10);
uniform_inclusive!(uniform_u32x10_all_new_inclusive, u32, 0, u32::MAX, 10);
uniform_sample!(uniform_u32x10_allm1_new, u32, 0, u32::MAX, 10);
uniform_sample!(uniform_u32x10_halfp1_new, u32, 0, HALF_32_BIT_UNSIGNED + 1, 10);
uniform_sample!(uniform_u32x10_half_new, u32, 0, HALF_32_BIT_UNSIGNED, 10);
uniform_sample!(uniform_u32x10_halfm1_new, u32, 0, HALF_32_BIT_UNSIGNED - 1, 10);
uniform_sample!(uniform_u32x10_6_new, u32, 0, 6u32, 10);

uniform_single!(uniform_u32x10_allm1_single, u32, 0, u32::max_value(), 10);
uniform_single!(uniform_u32x10_allm1_single, u32, 0, u32::MAX, 10);
uniform_single!(uniform_u32x10_halfp1_single, u32, 0, HALF_32_BIT_UNSIGNED + 1, 10);
uniform_single!(uniform_u32x10_half_single, u32, 0, HALF_32_BIT_UNSIGNED, 10);
uniform_single!(uniform_u32x10_halfm1_single, u32, 0, HALF_32_BIT_UNSIGNED - 1, 10);
uniform_single!(uniform_u32x10_6_single, u32, 0, 6u32, 10);

const HALF_64_BIT_UNSIGNED: u64 = 1 << 63;

uniform_sample!(uniform_u64x1_allm1_new, u64, 0, u64::max_value(), 1);
uniform_sample!(uniform_u64x1_allm1_new, u64, 0, u64::MAX, 1);
uniform_sample!(uniform_u64x1_halfp1_new, u64, 0, HALF_64_BIT_UNSIGNED + 1, 1);
uniform_sample!(uniform_u64x1_half_new, u64, 0, HALF_64_BIT_UNSIGNED, 1);
uniform_sample!(uniform_u64x1_halfm1_new, u64, 0, HALF_64_BIT_UNSIGNED - 1, 1);
uniform_sample!(uniform_u64x1_6_new, u64, 0, 6u64, 1);

uniform_single!(uniform_u64x1_allm1_single, u64, 0, u64::max_value(), 1);
uniform_single!(uniform_u64x1_allm1_single, u64, 0, u64::MAX, 1);
uniform_single!(uniform_u64x1_halfp1_single, u64, 0, HALF_64_BIT_UNSIGNED + 1, 1);
uniform_single!(uniform_u64x1_half_single, u64, 0, HALF_64_BIT_UNSIGNED, 1);
uniform_single!(uniform_u64x1_halfm1_single, u64, 0, HALF_64_BIT_UNSIGNED - 1, 1);
uniform_single!(uniform_u64x1_6_single, u64, 0, 6u64, 1);

uniform_inclusive!(uniform_u64x10_all_new_inclusive, u64, 0, u64::max_value(), 10);
uniform_sample!(uniform_u64x10_allm1_new, u64, 0, u64::max_value(), 10);
uniform_inclusive!(uniform_u64x10_all_new_inclusive, u64, 0, u64::MAX, 10);
uniform_sample!(uniform_u64x10_allm1_new, u64, 0, u64::MAX, 10);
uniform_sample!(uniform_u64x10_halfp1_new, u64, 0, HALF_64_BIT_UNSIGNED + 1, 10);
uniform_sample!(uniform_u64x10_half_new, u64, 0, HALF_64_BIT_UNSIGNED, 10);
uniform_sample!(uniform_u64x10_halfm1_new, u64, 0, HALF_64_BIT_UNSIGNED - 1, 10);
uniform_sample!(uniform_u64x10_6_new, u64, 0, 6u64, 10);

uniform_single!(uniform_u64x10_allm1_single, u64, 0, u64::max_value(), 10);
uniform_single!(uniform_u64x10_allm1_single, u64, 0, u64::MAX, 10);
uniform_single!(uniform_u64x10_halfp1_single, u64, 0, HALF_64_BIT_UNSIGNED + 1, 10);
uniform_single!(uniform_u64x10_half_single, u64, 0, HALF_64_BIT_UNSIGNED, 10);
uniform_single!(uniform_u64x10_halfm1_single, u64, 0, HALF_64_BIT_UNSIGNED - 1, 10);
uniform_single!(uniform_u64x10_6_single, u64, 0, 6u64, 10);

const HALF_128_BIT_UNSIGNED: u128 = 1 << 127;

uniform_sample!(uniform_u128x1_allm1_new, u128, 0, u128::max_value(), 1);
uniform_sample!(uniform_u128x1_allm1_new, u128, 0, u128::MAX, 1);
uniform_sample!(uniform_u128x1_halfp1_new, u128, 0, HALF_128_BIT_UNSIGNED + 1, 1);
uniform_sample!(uniform_u128x1_half_new, u128, 0, HALF_128_BIT_UNSIGNED, 1);
uniform_sample!(uniform_u128x1_halfm1_new, u128, 0, HALF_128_BIT_UNSIGNED - 1, 1);
uniform_sample!(uniform_u128x1_6_new, u128, 0, 6u128, 1);

uniform_single!(uniform_u128x1_allm1_single, u128, 0, u128::max_value(), 1);
uniform_single!(uniform_u128x1_allm1_single, u128, 0, u128::MAX, 1);
uniform_single!(uniform_u128x1_halfp1_single, u128, 0, HALF_128_BIT_UNSIGNED + 1, 1);
uniform_single!(uniform_u128x1_half_single, u128, 0, HALF_128_BIT_UNSIGNED, 1);
uniform_single!(uniform_u128x1_halfm1_single, u128, 0, HALF_128_BIT_UNSIGNED - 1, 1);
uniform_single!(uniform_u128x1_6_single, u128, 0, 6u128, 1);

uniform_inclusive!(uniform_u128x10_all_new_inclusive, u128, 0, u128::max_value(), 10);
uniform_sample!(uniform_u128x10_allm1_new, u128, 0, u128::max_value(), 10);
uniform_inclusive!(uniform_u128x10_all_new_inclusive, u128, 0, u128::MAX, 10);
uniform_sample!(uniform_u128x10_allm1_new, u128, 0, u128::MAX, 10);
uniform_sample!(uniform_u128x10_halfp1_new, u128, 0, HALF_128_BIT_UNSIGNED + 1, 10);
uniform_sample!(uniform_u128x10_half_new, u128, 0, HALF_128_BIT_UNSIGNED, 10);
uniform_sample!(uniform_u128x10_halfm1_new, u128, 0, HALF_128_BIT_UNSIGNED - 1, 10);
uniform_sample!(uniform_u128x10_6_new, u128, 0, 6u128, 10);

uniform_single!(uniform_u128x10_allm1_single, u128, 0, u128::max_value(), 10);
uniform_single!(uniform_u128x10_allm1_single, u128, 0, u128::MAX, 10);
uniform_single!(uniform_u128x10_halfp1_single, u128, 0, HALF_128_BIT_UNSIGNED + 1, 10);
uniform_single!(uniform_u128x10_half_single, u128, 0, HALF_128_BIT_UNSIGNED, 10);
uniform_single!(uniform_u128x10_halfm1_single, u128, 0, HALF_128_BIT_UNSIGNED - 1, 10);
Expand Down
14 changes: 7 additions & 7 deletions benches/misc.rs
Expand Up @@ -23,7 +23,7 @@ fn misc_gen_bool_const(b: &mut Bencher) {
let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
for _ in 0..crate::RAND_BENCH_N {
for _ in 0..RAND_BENCH_N {
accum ^= rng.gen_bool(0.18);
}
accum
Expand All @@ -36,7 +36,7 @@ fn misc_gen_bool_var(b: &mut Bencher) {
b.iter(|| {
let mut accum = true;
let mut p = 0.18;
for _ in 0..crate::RAND_BENCH_N {
for _ in 0..RAND_BENCH_N {
accum ^= rng.gen_bool(p);
p += 0.0001;
}
Expand All @@ -49,7 +49,7 @@ fn misc_gen_ratio_const(b: &mut Bencher) {
let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
for _ in 0..crate::RAND_BENCH_N {
for _ in 0..RAND_BENCH_N {
accum ^= rng.gen_ratio(2, 3);
}
accum
Expand All @@ -61,7 +61,7 @@ fn misc_gen_ratio_var(b: &mut Bencher) {
let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let mut accum = true;
for i in 2..(crate::RAND_BENCH_N as u32 + 2) {
for i in 2..(RAND_BENCH_N as u32 + 2) {
accum ^= rng.gen_ratio(i, i + 1);
}
accum
Expand All @@ -72,9 +72,9 @@ fn misc_gen_ratio_var(b: &mut Bencher) {
fn misc_bernoulli_const(b: &mut Bencher) {
let mut rng = Pcg32::from_rng(&mut thread_rng()).unwrap();
b.iter(|| {
let d = rand::distributions::Bernoulli::new(0.18).unwrap();
let d = Bernoulli::new(0.18).unwrap();
let mut accum = true;
for _ in 0..crate::RAND_BENCH_N {
for _ in 0..RAND_BENCH_N {
accum ^= rng.sample(d);
}
accum
Expand All @@ -87,7 +87,7 @@ fn misc_bernoulli_var(b: &mut Bencher) {
b.iter(|| {
let mut accum = true;
let mut p = 0.18;
for _ in 0..crate::RAND_BENCH_N {
for _ in 0..RAND_BENCH_N {
let d = Bernoulli::new(p).unwrap();
accum ^= rng.sample(d);
p += 0.0001;
Expand Down
2 changes: 1 addition & 1 deletion benches/seq.rs
Expand Up @@ -47,7 +47,7 @@ fn seq_slice_choose_1_of_1000(b: &mut Bencher) {
}
s
});
b.bytes = size_of::<usize>() as u64 * crate::RAND_BENCH_N;
b.bytes = size_of::<usize>() as u64 * RAND_BENCH_N;
}

macro_rules! seq_slice_choose_multiple {
Expand Down
4 changes: 2 additions & 2 deletions rand_distr/src/binomial.rs
Expand Up @@ -77,7 +77,7 @@ impl Binomial {

/// Convert a `f64` to an `i64`, panicking on overflow.
fn f64_to_i64(x: f64) -> i64 {
assert!(x < (core::i64::MAX as f64));
assert!(x < (i64::MAX as f64));
x as i64
}

Expand Down Expand Up @@ -117,7 +117,7 @@ impl Distribution<u64> for Binomial {
// When n*p < 10, so is n*p*q which is the variance, so a result > 110 would be 100 / sqrt(10) = 31 standard deviations away.
const BINV_MAX_X : u64 = 110;

if (self.n as f64) * p < BINV_THRESHOLD && self.n <= (core::i32::MAX as u64) {
if (self.n as f64) * p < BINV_THRESHOLD && self.n <= (i32::MAX as u64) {
// Use the BINV algorithm.
let s = p / q;
let a = ((self.n + 1) as f64) * s;
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/src/cauchy.rs
Expand Up @@ -137,7 +137,7 @@ mod test {

#[test]
fn value_stability() {
fn gen_samples<F: Float + FloatConst + core::fmt::Debug>(m: F, s: F, buf: &mut [F])
fn gen_samples<F: Float + FloatConst + fmt::Debug>(m: F, s: F, buf: &mut [F])
where Standard: Distribution<F> {
let distr = Cauchy::new(m, s).unwrap();
let mut rng = crate::test::rng(353);
Expand Down
12 changes: 6 additions & 6 deletions rand_distr/src/frechet.rs
Expand Up @@ -112,13 +112,13 @@ mod tests {
#[test]
#[should_panic]
fn test_infinite_scale() {
Frechet::new(0.0, core::f64::INFINITY, 1.0).unwrap();
Frechet::new(0.0, f64::INFINITY, 1.0).unwrap();
}

#[test]
#[should_panic]
fn test_nan_scale() {
Frechet::new(0.0, core::f64::NAN, 1.0).unwrap();
Frechet::new(0.0, f64::NAN, 1.0).unwrap();
}

#[test]
Expand All @@ -130,25 +130,25 @@ mod tests {
#[test]
#[should_panic]
fn test_infinite_shape() {
Frechet::new(0.0, 1.0, core::f64::INFINITY).unwrap();
Frechet::new(0.0, 1.0, f64::INFINITY).unwrap();
}

#[test]
#[should_panic]
fn test_nan_shape() {
Frechet::new(0.0, 1.0, core::f64::NAN).unwrap();
Frechet::new(0.0, 1.0, f64::NAN).unwrap();
}

#[test]
#[should_panic]
fn test_infinite_location() {
Frechet::new(core::f64::INFINITY, 1.0, 1.0).unwrap();
Frechet::new(f64::INFINITY, 1.0, 1.0).unwrap();
}

#[test]
#[should_panic]
fn test_nan_location() {
Frechet::new(core::f64::NAN, 1.0, 1.0).unwrap();
Frechet::new(f64::NAN, 1.0, 1.0).unwrap();
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions rand_distr/src/geometric.rs
Expand Up @@ -94,7 +94,7 @@ impl Distribution<u64> for Geometric
return failures;
}

if self.p == 0.0 { return core::u64::MAX; }
if self.p == 0.0 { return u64::MAX; }

let Geometric { p, pi, k } = *self;

Expand All @@ -121,7 +121,7 @@ impl Distribution<u64> for Geometric
// fewer iterations on average. ~ October 28, 2020
let m = loop {
let m = rng.gen::<u64>() & ((1 << k) - 1);
let p_reject = if m <= core::i32::MAX as u64 {
let p_reject = if m <= i32::MAX as u64 {
(1.0 - p).powi(m as i32)
} else {
(1.0 - p).powf(m as f64)
Expand Down Expand Up @@ -176,9 +176,9 @@ mod test {

#[test]
fn test_geo_invalid_p() {
assert!(Geometric::new(core::f64::NAN).is_err());
assert!(Geometric::new(core::f64::INFINITY).is_err());
assert!(Geometric::new(core::f64::NEG_INFINITY).is_err());
assert!(Geometric::new(f64::NAN).is_err());
assert!(Geometric::new(f64::INFINITY).is_err());
assert!(Geometric::new(f64::NEG_INFINITY).is_err());

assert!(Geometric::new(-0.5).is_err());
assert!(Geometric::new(0.0).is_ok());
Expand Down
8 changes: 4 additions & 4 deletions rand_distr/src/gumbel.rs
Expand Up @@ -101,25 +101,25 @@ mod tests {
#[test]
#[should_panic]
fn test_infinite_scale() {
Gumbel::new(0.0, core::f64::INFINITY).unwrap();
Gumbel::new(0.0, f64::INFINITY).unwrap();
}

#[test]
#[should_panic]
fn test_nan_scale() {
Gumbel::new(0.0, core::f64::NAN).unwrap();
Gumbel::new(0.0, f64::NAN).unwrap();
}

#[test]
#[should_panic]
fn test_infinite_location() {
Gumbel::new(core::f64::INFINITY, 1.0).unwrap();
Gumbel::new(f64::INFINITY, 1.0).unwrap();
}

#[test]
#[should_panic]
fn test_nan_location() {
Gumbel::new(core::f64::NAN, 1.0).unwrap();
Gumbel::new(f64::NAN, 1.0).unwrap();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion rand_distr/src/normal.rs
Expand Up @@ -348,7 +348,7 @@ mod tests {
#[test]
fn test_log_normal_cv() {
let lnorm = LogNormal::from_mean_cv(0.0, 0.0).unwrap();
assert_eq!((lnorm.norm.mean, lnorm.norm.std_dev), (-core::f64::INFINITY, 0.0));
assert_eq!((lnorm.norm.mean, lnorm.norm.std_dev), (f64::NEG_INFINITY, 0.0));

let lnorm = LogNormal::from_mean_cv(1.0, 0.0).unwrap();
assert_eq!((lnorm.norm.mean, lnorm.norm.std_dev), (0.0, 0.0));
Expand Down

0 comments on commit bca078e

Please sign in to comment.