histogram: Implement AddAssign
and MulAssign
Also clarify documentation and name of `min`/`max`.
This commit is contained in:
parent
2775f78e8e
commit
c04ce8887e
@ -132,14 +132,18 @@ macro_rules! define_histogram {
|
||||
}
|
||||
|
||||
/// Return the lower range limit.
|
||||
///
|
||||
/// (The corresponding bin might be empty.)
|
||||
#[inline]
|
||||
pub fn min(&self) -> f64 {
|
||||
pub fn range_min(&self) -> f64 {
|
||||
self.range[0]
|
||||
}
|
||||
|
||||
/// Return the upper range limit.
|
||||
///
|
||||
/// (The corresponding bin might be empty.)
|
||||
#[inline]
|
||||
pub fn max(&self) -> f64 {
|
||||
pub fn range_max(&self) -> f64 {
|
||||
self.range[LEN]
|
||||
}
|
||||
}
|
||||
@ -181,5 +185,24 @@ macro_rules! define_histogram {
|
||||
&self.bin as &[u64]
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ::core::ops::AddAssign<&'a Self> for $name {
|
||||
#[inline]
|
||||
fn add_assign(&mut self, other: &Self) {
|
||||
assert_eq!(self.range, other.range);
|
||||
for (x, y) in self.bin.iter_mut().zip(other.bin.iter()) {
|
||||
*x += y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ::core::ops::MulAssign<u64> for $name {
|
||||
#[inline]
|
||||
fn mul_assign(&mut self, other: u64) {
|
||||
for x in self.bin.iter_mut() {
|
||||
*x *= other;
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -141,8 +141,43 @@ fn reset() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minmax() {
|
||||
fn range_minmax() {
|
||||
let h = Histogram10::with_const_width(0., 100.);
|
||||
assert_eq!(h.min(), 0.);
|
||||
assert_eq!(h.max(), 100.);
|
||||
assert_eq!(h.range_min(), 0.);
|
||||
assert_eq!(h.range_max(), 100.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add() {
|
||||
let mut h1 = Histogram10::with_const_width(0., 100.);
|
||||
let mut h2 = h1.clone();
|
||||
let mut expected = h1.clone();
|
||||
|
||||
for i in 0..50 {
|
||||
h1.add(f64::from(i)).unwrap();
|
||||
expected.add(f64::from(i)).unwrap();
|
||||
}
|
||||
for i in 50..100 {
|
||||
h2.add(f64::from(i)).unwrap();
|
||||
expected.add(f64::from(i)).unwrap();
|
||||
}
|
||||
h1 += &h2;
|
||||
|
||||
assert_eq!(h1.bins(), expected.bins());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mul() {
|
||||
let mut h = Histogram10::with_const_width(0., 100.);
|
||||
let mut expected = h.clone();
|
||||
|
||||
for i in 0..100 {
|
||||
h.add(f64::from(i)).unwrap();
|
||||
expected.add(f64::from(i)).unwrap();
|
||||
expected.add(f64::from(i)).unwrap();
|
||||
}
|
||||
|
||||
h *= 2;
|
||||
|
||||
assert_eq!(h.bins(), expected.bins());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user