From b26652f2a7094fcbc0d35969dcc3e5bc127695dd Mon Sep 17 00:00:00 2001 From: Eh2406 Date: Sun, 17 Dec 2017 22:53:50 -0500 Subject: [PATCH] inline reduce to make minmax compatibal with serde --- src/lib.rs | 1 - src/minmax.rs | 21 ++++++++++---------- src/reduce.rs | 53 --------------------------------------------------- 3 files changed, 10 insertions(+), 65 deletions(-) delete mode 100644 src/reduce.rs diff --git a/src/lib.rs b/src/lib.rs index ec6b237..22d9ff2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -77,7 +77,6 @@ extern crate serde; mod moments; mod weighted_mean; mod minmax; -mod reduce; mod quantile; mod traits; diff --git a/src/minmax.rs b/src/minmax.rs index 6c27622..31b85b3 100644 --- a/src/minmax.rs +++ b/src/minmax.rs @@ -1,6 +1,5 @@ use core; -use super::reduce::Reduce; use super::{Estimate, Merge}; /// Calculate the minimum of `a` and `b`. @@ -27,7 +26,7 @@ fn max(a: f64, b: f64) -> f64 { #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Min { - r: Reduce f64>, + x: f64, } impl Min { @@ -35,7 +34,7 @@ impl Min { #[inline] pub fn from_value(x: f64) -> Min { Min { - r: Reduce::from_value_and_fn(x, min), + x: x, } } @@ -48,7 +47,7 @@ impl Min { /// Estimate the minium of the population. #[inline] pub fn min(&self) -> f64 { - self.r.reduction() + self.x } } @@ -63,7 +62,7 @@ impl_from_iterator!(Min); impl Estimate for Min { #[inline] fn add(&mut self, x: f64) { - self.r.add(x); + self.x = min(self.x, x); } #[inline] @@ -91,7 +90,7 @@ impl Merge for Min { /// ``` #[inline] fn merge(&mut self, other: &Min) { - self.r.merge(&other.r); + self.add(other.x); } } @@ -109,7 +108,7 @@ impl Merge for Min { #[derive(Debug, Clone)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] pub struct Max { - r: Reduce f64>, + x: f64, } impl Max { @@ -117,7 +116,7 @@ impl Max { #[inline] pub fn from_value(x: f64) -> Max { Max { - r: Reduce::from_value_and_fn(x, max), + x: x, } } @@ -130,7 +129,7 @@ impl Max { /// Estimate the maxium of the population. #[inline] pub fn max(&self) -> f64 { - self.r.reduction() + self.x } } @@ -145,7 +144,7 @@ impl_from_iterator!(Max); impl Estimate for Max { #[inline] fn add(&mut self, x: f64) { - self.r.add(x); + self.x = max(self.x, x); } #[inline] @@ -173,6 +172,6 @@ impl Merge for Max { /// ``` #[inline] fn merge(&mut self, other: &Max) { - self.r.merge(&other.r); + self.add(other.x); } } diff --git a/src/reduce.rs b/src/reduce.rs deleted file mode 100644 index 4511603..0000000 --- a/src/reduce.rs +++ /dev/null @@ -1,53 +0,0 @@ -use super::{Estimate, Merge}; - -/// Estimate the reduction of a sequence of numbers ("population"). -/// -/// The reduction is a given function `Fn(f64, f64) -> f64`. -/// -/// Everything is calculated iteratively using constant memory, so the sequence -/// of numbers can be an iterator. -#[derive(Debug, Clone)] -#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] -pub struct Reduce { - x: f64, - reduce: F, -} - -impl Reduce { - /// Create a new reduction estimator given an initial value and a reduction. - #[inline] - pub fn from_value_and_fn(x: f64, f: F) -> Reduce { - Reduce { x: x, reduce: f } - } - - /// Estimate the reduction of the population. - #[inline] - pub fn reduction(&self) -> f64 { - self.x - } - -} - -impl Estimate for Reduce - where F: Fn(f64, f64) -> f64, -{ - #[inline] - fn add(&mut self, x: f64) { - self.x = (self.reduce)(self.x, x); - } - - #[inline] - fn estimate(&self) -> f64 { - self.reduction() - } -} - -impl Merge for Reduce - where F: Fn(f64, f64) -> f64, -{ - /// Merge another sample into this one. - #[inline] - fn merge(&mut self, other: &Reduce) { - self.add(other.x); - } -}