Skip to main content

score_set/
metric.rs

1use crate::traits::{EvalF32, EvalF64, Map01F32, Map01F64, Measure};
2
3/// A weighted `f64` metric built from a measure and a normalization map.
4pub struct Metric64<M, G> {
5    measure: M,
6    map: G,
7    weight: f64,
8}
9
10impl<M, G> Metric64<M, G> {
11    /// Creates a new `Metric64`.
12    pub fn new(measure: M, map: G, weight: f64) -> Self {
13        Self {
14            measure,
15            map,
16            weight,
17        }
18    }
19}
20
21impl<Ctx, M, G> EvalF64<Ctx> for Metric64<M, G>
22where
23    Ctx: ?Sized,
24    M: Measure<Ctx>,
25    G: Map01F64<Input = M::Output>,
26{
27    #[inline]
28    fn eval(&self, ctx: &Ctx) -> f64 {
29        self.weight * *self.map.map(self.measure.measure(ctx))
30    }
31}
32
33/// A weighted `f32` metric built from a measure and a normalization map.
34pub struct Metric32<M, G> {
35    measure: M,
36    map: G,
37    weight: f32,
38}
39
40impl<M, G> Metric32<M, G> {
41    /// Creates a new `Metric32`.
42    pub fn new(measure: M, map: G, weight: f32) -> Self {
43        Self {
44            measure,
45            map,
46            weight,
47        }
48    }
49}
50
51impl<Ctx, M, G> EvalF32<Ctx> for Metric32<M, G>
52where
53    Ctx: ?Sized,
54    M: Measure<Ctx>,
55    G: Map01F32<Input = M::Output>,
56{
57    #[inline]
58    fn eval(&self, ctx: &Ctx) -> f32 {
59        self.weight * *self.map.map(self.measure.measure(ctx))
60    }
61}