Skip to main content

score_set/
member.rs

1use crate::float::Float;
2use crate::value::{GtZero, NormalizedContainer, NormalizedWeight, Value01};
3use witnessed::Witnessed;
4
5// ---------------------------------------------------------------------------
6// Members trait — maps raw tuple to normalized tuple
7// ---------------------------------------------------------------------------
8
9/// Trait that maps raw member tuples to normalized member tuples.
10#[doc(hidden)]
11pub trait Members<T: Float>: Sized {
12    /// The raw (pre-normalization) member tuple.
13    type Raw;
14
15    /// Extract raw weight values from the raw member tuple.
16    fn extract_raw_weights(raw: &Self::Raw) -> Vec<T>;
17
18    /// Build the normalized member tuple from raw members and a validated
19    /// normalized container.
20    ///
21    /// Each member's credential is constructed via
22    /// [`NormalizedWeight::from_normalized_container`].
23    fn from_raw_with_weights(
24        raw: Self::Raw,
25        container: &Witnessed<Vec<T>, NormalizedContainer>,
26    ) -> Self;
27}
28
29// ---------------------------------------------------------------------------
30// RawMember — weight + metric before normalization
31// ---------------------------------------------------------------------------
32
33/// A raw member: a strictly-positive weight paired with a metric.
34#[doc(hidden)]
35#[derive(Debug, Clone, Copy)]
36pub struct RawMember<T: Float, M> {
37    pub(crate) weight: Witnessed<T, GtZero>,
38    pub(crate) metric: M,
39}
40
41impl<T: Float, M> RawMember<T, M> {
42    /// Access the raw weight value.
43    pub fn weight(&self) -> T {
44        *self.weight
45    }
46
47    /// Access the metric.
48    pub fn metric(&self) -> &M {
49        &self.metric
50    }
51}
52
53/// Construct a `RawMember`, validating that `weight` is strictly positive.
54#[doc(hidden)]
55#[inline]
56pub fn raw_member<T: Float, M>(weight: T, metric: M) -> Result<RawMember<T, M>, &'static str> {
57    let w = GtZero::witness(weight)?;
58    Ok(RawMember { weight: w, metric })
59}
60
61// ---------------------------------------------------------------------------
62// Member — normalized weight + metric
63// ---------------------------------------------------------------------------
64
65/// A member of a [`MetricSet`](crate::MetricSet): a normalized weight paired
66/// with its metric.
67#[derive(Debug, Clone, Copy)]
68pub struct Member<T: Float, M> {
69    /// The normalized weight.
70    pub weight: Witnessed<T, NormalizedWeight>,
71    /// The metric or operator.
72    pub metric: M,
73}
74
75impl<T: Float, M> Member<T, M> {
76    /// Compute the contribution of a metric score.
77    ///
78    /// `contribute(score) = score × normalized_weight`
79    #[inline]
80    pub fn contribute(&self, value: Witnessed<T, Value01>) -> T {
81        value.into_inner() * self.weight.into_inner()
82    }
83
84    /// Return a reference to the metric.
85    #[inline]
86    pub fn metric(&self) -> &M {
87        &self.metric
88    }
89}
90
91#[cfg(test)]
92mod tests_for_member;