]> git.proxmox.com Git - rustc.git/blobdiff - src/librand/distributions/mod.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / librand / distributions / mod.rs
index a54c8df2352ac363b6ac519d350bb651cf2ed0b2..47967a719d397a8972d39bf4e90ac356721af550 100644 (file)
 //! internally. The `IndependentSample` trait is for generating values
 //! that do not need to record state.
 
+use core::fmt;
+
+#[cfg(not(test))] // only necessary for no_std
 use core::num::Float;
+
 use core::marker::PhantomData;
 
-use {Rng, Rand};
+use {Rand, Rng};
 
 pub use self::range::Range;
-pub use self::gamma::{Gamma, ChiSquared, FisherF, StudentT};
-pub use self::normal::{Normal, LogNormal};
+pub use self::gamma::{ChiSquared, FisherF, Gamma, StudentT};
+pub use self::normal::{LogNormal, Normal};
 pub use self::exponential::Exp;
 
 pub mod range;
@@ -49,7 +53,7 @@ pub trait Sample<Support> {
 // trait called `Sample` and the other should be `DependentSample`.
 pub trait IndependentSample<Support>: Sample<Support> {
     /// Generate a random value.
-    fn ind_sample<R: Rng>(&self, &mut R) -> Support;
+    fn ind_sample<R: Rng>(&self, _: &mut R) -> Support;
 }
 
 /// A wrapper for generating types that implement `Rand` via the
@@ -76,6 +80,12 @@ impl<Sup: Rand> IndependentSample<Sup> for RandSample<Sup> {
     }
 }
 
+impl<Sup> fmt::Debug for RandSample<Sup> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        f.pad("RandSample { .. }")
+    }
+}
+
 /// A value with a particular weight for use with `WeightedChoice`.
 pub struct Weighted<T> {
     /// The numerical weight of this item
@@ -84,6 +94,15 @@ pub struct Weighted<T> {
     pub item: T,
 }
 
+impl<T: fmt::Debug> fmt::Debug for Weighted<T> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        f.debug_struct("Weighted")
+         .field("weight", &self.weight)
+         .field("item", &self.item)
+         .finish()
+    }
+}
+
 /// A distribution that selects from a finite collection of weighted items.
 ///
 /// Each item has an associated weight that influences how likely it
@@ -130,7 +149,7 @@ impl<'a, T: Clone> WeightedChoice<'a, T> {
                 "WeightedChoice::new called with a total weight of 0");
 
         WeightedChoice {
-            items: items,
+            items,
             // we're likely to be generating numbers in this range
             // relatively often, so might as well cache it
             weight_range: Range::new(0, running_total),
@@ -187,6 +206,15 @@ impl<'a, T: Clone> IndependentSample<T> for WeightedChoice<'a, T> {
     }
 }
 
+impl<'a, T: fmt::Debug> fmt::Debug for WeightedChoice<'a, T> {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        f.debug_struct("WeightedChoice")
+         .field("items", &self.items)
+         .field("weight_range", &self.weight_range)
+         .finish()
+    }
+}
+
 mod ziggurat_tables;
 
 /// Sample a random number using the Ziggurat method (specifically the
@@ -235,18 +263,10 @@ fn ziggurat<R: Rng, P, Z>(rng: &mut R,
 
         // u is either U(-1, 1) or U(0, 1) depending on if this is a
         // symmetric distribution or not.
-        let u = if symmetric {
-            2.0 * f - 1.0
-        } else {
-            f
-        };
+        let u = if symmetric { 2.0 * f - 1.0 } else { f };
         let x = u * x_tab[i];
 
-        let test_x = if symmetric {
-            x.abs()
-        } else {
-            x
-        };
+        let test_x = if symmetric { x.abs() } else { x };
 
         // algebraically equivalent to |u| < x_tab[i+1]/x_tab[i] (or u < x_tab[i+1]/x_tab[i])
         if test_x < x_tab[i + 1] {
@@ -264,8 +284,8 @@ fn ziggurat<R: Rng, P, Z>(rng: &mut R,
 
 #[cfg(test)]
 mod tests {
-    use {Rng, Rand};
-    use super::{RandSample, WeightedChoice, Weighted, Sample, IndependentSample};
+    use {Rand, Rng};
+    use super::{IndependentSample, RandSample, Sample, Weighted, WeightedChoice};
 
     #[derive(PartialEq, Debug)]
     struct ConstRand(usize);
@@ -318,37 +338,37 @@ mod tests {
             }}
         }
 
-        t!(vec!(Weighted { weight: 1, item: 10 }),
+        t!(vec![Weighted { weight: 1, item: 10 }],
            [10]);
 
         // skip some
-        t!(vec!(Weighted { weight: 0, item: 20 },
+        t!(vec![Weighted { weight: 0, item: 20 },
                 Weighted { weight: 2, item: 21 },
                 Weighted { weight: 0, item: 22 },
-                Weighted { weight: 1, item: 23 }),
+                Weighted { weight: 1, item: 23 }],
            [21, 21, 23]);
 
         // different weights
-        t!(vec!(Weighted { weight: 4, item: 30 },
-                Weighted { weight: 3, item: 31 }),
+        t!(vec![Weighted { weight: 4, item: 30 },
+                Weighted { weight: 3, item: 31 }],
            [30, 30, 30, 30, 31, 31, 31]);
 
         // check that we're binary searching
         // correctly with some vectors of odd
         // length.
-        t!(vec!(Weighted { weight: 1, item: 40 },
+        t!(vec![Weighted { weight: 1, item: 40 },
                 Weighted { weight: 1, item: 41 },
                 Weighted { weight: 1, item: 42 },
                 Weighted { weight: 1, item: 43 },
-                Weighted { weight: 1, item: 44 }),
+                Weighted { weight: 1, item: 44 }],
            [40, 41, 42, 43, 44]);
-        t!(vec!(Weighted { weight: 1, item: 50 },
+        t!(vec![Weighted { weight: 1, item: 50 },
                 Weighted { weight: 1, item: 51 },
                 Weighted { weight: 1, item: 52 },
                 Weighted { weight: 1, item: 53 },
                 Weighted { weight: 1, item: 54 },
                 Weighted { weight: 1, item: 55 },
-                Weighted { weight: 1, item: 56 }),
+                Weighted { weight: 1, item: 56 }],
            [50, 51, 52, 53, 54, 55, 56]);
     }