score_set/
dyn_score_set.rs1use alloc::{boxed::Box, vec::Vec};
2use core::marker::PhantomData;
3
4use crate::traits::{EvalF32, EvalF64};
5
6pub struct DynScoreSet64<Ctx: ?Sized> {
8 metrics: Box<[Box<dyn EvalF64<Ctx> + 'static>]>,
9}
10
11impl<Ctx: ?Sized> DynScoreSet64<Ctx> {
12 pub fn builder() -> DynScoreSet64Builder<Ctx> {
14 DynScoreSet64Builder::default()
15 }
16
17 pub fn len(&self) -> usize {
19 self.metrics.len()
20 }
21
22 pub fn is_empty(&self) -> bool {
24 self.metrics.is_empty()
25 }
26}
27
28impl<Ctx: ?Sized> EvalF64<Ctx> for DynScoreSet64<Ctx> {
29 #[inline]
30 fn eval(&self, ctx: &Ctx) -> f64 {
31 self.metrics
32 .iter()
33 .fold(0.0, |sum, metric| sum + metric.eval(ctx))
34 }
35}
36
37pub struct DynScoreSet64Builder<Ctx: ?Sized> {
39 metrics: Vec<Box<dyn EvalF64<Ctx> + 'static>>,
40 marker: PhantomData<fn(&Ctx)>,
41}
42
43impl<Ctx: ?Sized> Default for DynScoreSet64Builder<Ctx> {
44 fn default() -> Self {
45 Self {
46 metrics: Vec::new(),
47 marker: PhantomData,
48 }
49 }
50}
51
52impl<Ctx: ?Sized> DynScoreSet64Builder<Ctx> {
53 pub fn push<E>(&mut self, eval: E)
55 where
56 E: EvalF64<Ctx> + 'static,
57 {
58 self.metrics.push(Box::new(eval));
59 }
60
61 pub fn append<E>(mut self, eval: E) -> Self
63 where
64 E: EvalF64<Ctx> + 'static,
65 {
66 self.push(eval);
67 self
68 }
69
70 pub fn build(self) -> DynScoreSet64<Ctx> {
72 DynScoreSet64 {
73 metrics: self.metrics.into_boxed_slice(),
74 }
75 }
76}
77
78pub struct DynScoreSet32<Ctx: ?Sized> {
80 metrics: Box<[Box<dyn EvalF32<Ctx> + 'static>]>,
81}
82
83impl<Ctx: ?Sized> DynScoreSet32<Ctx> {
84 pub fn builder() -> DynScoreSet32Builder<Ctx> {
86 DynScoreSet32Builder::default()
87 }
88
89 pub fn len(&self) -> usize {
91 self.metrics.len()
92 }
93
94 pub fn is_empty(&self) -> bool {
96 self.metrics.is_empty()
97 }
98}
99
100impl<Ctx: ?Sized> EvalF32<Ctx> for DynScoreSet32<Ctx> {
101 #[inline]
102 fn eval(&self, ctx: &Ctx) -> f32 {
103 self.metrics
104 .iter()
105 .fold(0.0, |sum, metric| sum + metric.eval(ctx))
106 }
107}
108
109pub struct DynScoreSet32Builder<Ctx: ?Sized> {
111 metrics: Vec<Box<dyn EvalF32<Ctx> + 'static>>,
112 marker: PhantomData<fn(&Ctx)>,
113}
114
115impl<Ctx: ?Sized> Default for DynScoreSet32Builder<Ctx> {
116 fn default() -> Self {
117 Self {
118 metrics: Vec::new(),
119 marker: PhantomData,
120 }
121 }
122}
123
124impl<Ctx: ?Sized> DynScoreSet32Builder<Ctx> {
125 pub fn push<E>(&mut self, eval: E)
127 where
128 E: EvalF32<Ctx> + 'static,
129 {
130 self.metrics.push(Box::new(eval));
131 }
132
133 pub fn append<E>(mut self, eval: E) -> Self
135 where
136 E: EvalF32<Ctx> + 'static,
137 {
138 self.push(eval);
139 self
140 }
141
142 pub fn build(self) -> DynScoreSet32<Ctx> {
144 DynScoreSet32 {
145 metrics: self.metrics.into_boxed_slice(),
146 }
147 }
148}