ndhistogram/histogram/fill.rs
1use std::ops::AddAssign;
2
3use num_traits::One;
4
5/// Fill a histogram bin value with unit weight.
6///
7/// Values that may be stored in a [Histogram](crate::Histogram) should
8/// implement this trait to allow that histogram to be filled.
9/// A blanket implementation is provided for types that implement [One]
10/// and [AddAssign] traits. See also [FillWith].
11pub trait Fill {
12 /// Fill this value with unit weight.
13 /// For a simple number type this means simply increment by one.
14 fn fill(&mut self);
15}
16
17impl<T: One + AddAssign> Fill for T {
18 #[inline]
19 fn fill(&mut self) {
20 *self += Self::one();
21 }
22}
23
24/// Fill a histogram bin value with some data.
25///
26/// Fill a histogram with some value. This trait has a blanket implementation
27/// for [AddAssign].
28/// In the case of primitive histogram values, this is equivalent to a weighted
29/// fill.
30pub trait FillWith<D> {
31 /// Fill this value with some data.
32 /// For a simple number type means adding the weight.
33 fn fill_with(&mut self, value: D);
34}
35
36impl<D> FillWith<&D> for D
37where
38 for<'a> Self: AddAssign<&'a D>,
39{
40 #[inline]
41 fn fill_with(&mut self, data: &D) {
42 *self += data;
43 }
44}
45
46impl<D> FillWith<D> for D
47where
48 for<'a> Self: AddAssign<D>,
49{
50 #[inline]
51 fn fill_with(&mut self, data: D) {
52 *self += data;
53 }
54}
55
56/// Fill a histogram with some weighted value.
57///
58/// As [FillWith], but for instances where the value may also be weighted.
59/// For example, see [WeightedMean](crate::value::WeightedMean).
60pub trait FillWithWeighted<D, W> {
61 /// Fill a histogram with some weighted value.
62 fn fill_with_weighted(&mut self, data: D, weight: W);
63}