[go: up one dir, main page]

average/
minmax.rs

1#[cfg(feature = "serde")]
2use serde::{Deserialize, Serialize};
3
4use super::{Estimate, Merge};
5
6/// Calculate the minimum of `a` and `b`.
7fn min(a: f64, b: f64) -> f64 {
8    a.min(b)
9}
10
11/// Calculate the maximum of `a` and `b`.
12fn max(a: f64, b: f64) -> f64 {
13    a.max(b)
14}
15
16/// Estimate the minimum of a sequence of numbers ("population").
17///
18///
19/// ## Example
20///
21/// ```
22/// use average::Min;
23///
24/// let a: Min = (1..6).map(f64::from).collect();
25/// println!("The minimum is {}.", a.min());
26/// ```
27#[derive(Debug, Clone)]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
29pub struct Min {
30    x: f64,
31}
32
33impl Min {
34    /// Create a new minimum estimator from a given value.
35    #[inline]
36    pub fn from_value(x: f64) -> Min {
37        Min { x }
38    }
39
40    /// Create a new minimum estimator.
41    #[inline]
42    pub fn new() -> Min {
43        Min::from_value(f64::INFINITY)
44    }
45
46    /// Estimate the minimum of the population.
47    /// 
48    /// Returns `f64::INFINITY` for an empty sample.
49    #[inline]
50    pub fn min(&self) -> f64 {
51        self.x
52    }
53}
54
55impl core::default::Default for Min {
56    fn default() -> Min {
57        Min::new()
58    }
59}
60
61impl_from_iterator!(Min);
62impl_from_par_iterator!(Min);
63impl_extend!(Min);
64
65impl Estimate for Min {
66    #[inline]
67    fn add(&mut self, x: f64) {
68        self.x = min(self.x, x);
69    }
70
71    #[inline]
72    fn estimate(&self) -> f64 {
73        self.min()
74    }
75}
76
77impl Merge for Min {
78    /// Merge another sample into this one.
79    ///
80    ///
81    /// ## Example
82    ///
83    /// ```
84    /// use average::{Min, Merge};
85    ///
86    /// let sequence: &[f64] = &[1., 2., 3., 4., 5., 6., 7., 8., 9.];
87    /// let (left, right) = sequence.split_at(3);
88    /// let min_total: Min = sequence.iter().collect();
89    /// let mut min_left: Min = left.iter().collect();
90    /// let min_right: Min = right.iter().collect();
91    /// min_left.merge(&min_right);
92    /// assert_eq!(min_total.min(), min_left.min());
93    /// ```
94    #[inline]
95    fn merge(&mut self, other: &Min) {
96        self.add(other.x);
97    }
98}
99
100/// Estimate the maximum of a sequence of numbers ("population").
101///
102///
103/// ## Example
104///
105/// ```
106/// use average::Max;
107///
108/// let a: Max = (1..6).map(f64::from).collect();
109/// assert_eq!(a.max(), 5.);
110/// ```
111#[derive(Debug, Clone)]
112#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
113pub struct Max {
114    x: f64,
115}
116
117impl Max {
118    /// Create a new maximum estimator from a given value.
119    #[inline]
120    pub fn from_value(x: f64) -> Max {
121        Max { x }
122    }
123
124    /// Create a new maximum estimator.
125    #[inline]
126    pub fn new() -> Max {
127        Max::from_value(f64::NEG_INFINITY)
128    }
129
130    /// Estimate the maximum of the population.
131    /// 
132    /// Returns `f64::NEG_INFINITY` for an empty sample.
133    #[inline]
134    pub fn max(&self) -> f64 {
135        self.x
136    }
137}
138
139impl core::default::Default for Max {
140    fn default() -> Max {
141        Max::new()
142    }
143}
144
145impl_from_iterator!(Max);
146impl_from_par_iterator!(Max);
147
148impl Estimate for Max {
149    #[inline]
150    fn add(&mut self, x: f64) {
151        self.x = max(self.x, x);
152    }
153
154    #[inline]
155    fn estimate(&self) -> f64 {
156        self.max()
157    }
158}
159
160impl Merge for Max {
161    /// Merge another sample into this one.
162    ///
163    ///
164    /// ## Example
165    ///
166    /// ```
167    /// use average::{Max, Merge};
168    ///
169    /// let sequence: &[f64] = &[1., 2., 3., 4., 5., 6., 7., 8., 9.];
170    /// let (left, right) = sequence.split_at(3);
171    /// let max_total: Max = sequence.iter().collect();
172    /// let mut max_left: Max = left.iter().collect();
173    /// let max_right: Max = right.iter().collect();
174    /// max_left.merge(&max_right);
175    /// assert_eq!(max_total.max(), max_left.max());
176    /// ```
177    #[inline]
178    fn merge(&mut self, other: &Max) {
179        self.add(other.x);
180    }
181}