score-set 0.2.0

A Rust library for building static weighted scoring operator sets
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// ===========================================================================
// finite_metric! — generate a zero-vtable finite metric enum (Layer 2)
// ===========================================================================

/// Declare a finite enum of metric types with static-dispatch `eval`.
///
/// This macro generates an enum whose variants each wrap a concrete metric
/// type. The generated [`Scorable`](crate::Scorable) implementation uses
/// `match` + static dispatch — zero vtable overhead.
///
/// # Syntax — concrete form
///
/// `float` and `subject` are concrete types of the scoring domain:
///
/// ```ignore
/// finite_metric! {
///     metric     => DnaKind,
///     float      => f64,
///     subject    => DnaContext<'static>,
///     dimensions =>
///         Gc(GcRatio),
///         Len(SeqLen),
/// }
/// ```
///
/// # Syntax — generic form
///
/// `T, I` in angle brackets on the metric name declare type parameters.
/// No separate `float`/`subject` keys needed — the names appear in `<T, I>`:
///
/// ```ignore
/// finite_metric! {
///     pub metric => MetricKind<T, I>,
///     dimensions =>
///         Gc(GcRatio<T, I>),
///         Tm(TmScore<T, I>),
///     }
/// }
/// ```
///
/// # Generated items
///
/// - An enum with the listed variants.
/// - A [`Scorable`](crate::Scorable) implementation with
///   static-dispatch `eval` and `name` methods.
///
/// # Requirements on variant types
///
/// Each variant's inner type must provide:
/// - `fn eval(&self, input: &I) -> Witnessed<T, Value01>`
/// - `fn name(&self) -> &str`
///
/// Both [`Metric`](crate::Metric) and `Box<dyn Scorable<T, I>>` satisfy
/// this contract.
#[macro_export]
macro_rules! finite_metric {
    // ---- concrete: metric => Name, float => f64, subject => DnaContext ----
    (
        $(#[$attr:meta])*
        $vis:vis
        metric => $name:ident,
        float => $T:ty,
        subject => $I:ty,
        dimensions => $($variant:ident($ty:ty)),+ $(,)?
    ) => {
        $(#[$attr])*
        #[allow(clippy::pub_enum_variant_fields)]
        $vis enum $name {
            $($variant($ty),)+
        }

        impl $crate::Scorable<$T, $I> for $name {
            #[inline]
            fn eval(&self, input: &$I) -> $crate::Witnessed<$T, $crate::Value01> {
                match self {
                    $(Self::$variant(m) => m.eval(input)),+
                }
            }

            #[inline]
            fn name(&self) -> &str {
                match self {
                    $(Self::$variant(m) => m.name()),+
                }
            }
        }
    };

    // ---- generic: metric => Name<T, I>, dimensions => ... ----
    (
        $(#[$attr:meta])*
        $vis:vis
        metric => $name:ident<$T:ident, $I:ident>,
        dimensions => $($variant:ident($ty:ty)),+ $(,)?
    ) => {
        $(#[$attr])*
        #[allow(clippy::pub_enum_variant_fields)]
        $vis enum $name<$T: $crate::Float, $I> {
            $($variant($ty)),+
        }

        impl<$T: $crate::Float, $I> $crate::Scorable<$T, $I> for $name<$T, $I> {
            #[inline]
            fn eval(&self, input: &$I) -> $crate::Witnessed<$T, $crate::Value01> {
                match self {
                    $(Self::$variant(m) => m.eval(input)),+
                }
            }

            #[inline]
            fn name(&self) -> &str {
                match self {
                    $(Self::$variant(m) => m.name()),+
                }
            }
        }
    };
}

// ===========================================================================
// FiniteScoreSet — weighted set with finite-enum dispatch (Layer 2)
// ===========================================================================

use crate::breakdown::Breakdown;
use crate::dynamic::Scorable;
use crate::float::Float;
use crate::value::{GtZero, NormalizedContainer, NormalizedWeight, Value01};
use core::marker::PhantomData;
use witnessed::{WitnessExt, Witnessed};

// ---------------------------------------------------------------------------
// FiniteMember — a single weighted metric in a FiniteScoreSet
// ---------------------------------------------------------------------------

/// A member of a [`FiniteScoreSet`]: a normalized weight paired with a metric
/// enum variant.
///
/// See [`Member`](crate::Member) for the Layer-1 equivalent.
pub struct FiniteMember<T: Float, E> {
    /// The normalized weight.
    pub weight: Witnessed<T, NormalizedWeight>,
    /// The metric enum variant.
    pub metric: E,
}

impl<T: Float, E> FiniteMember<T, E> {
    /// Compute the weighted contribution of a metric score.
    ///
    /// `contribute(score) = score × normalized_weight`
    #[inline]
    pub fn contribute(&self, value: Witnessed<T, Value01>) -> T {
        value.into_inner() * self.weight.into_inner()
    }

    /// Return a reference to the metric.
    #[inline]
    pub fn metric(&self) -> &E {
        &self.metric
    }
}

// ---------------------------------------------------------------------------
// FiniteScoreSet — weighted set with enum-based static dispatch (Layer 2)
// ---------------------------------------------------------------------------

/// A weighted set of scoring operators using enum-based static dispatch.
///
/// `FiniteScoreSet` stores a `Vec` of [`FiniteMember`]s, each wrapping a
/// variant of a user-declared metric enum. At evaluation time, the enum's
/// `eval` method dispatches via `match` — zero vtable overhead for all
/// non-`Custom` variants.
///
/// Construct via [`finite_score_set!`](crate::finite_score_set!), the
/// [`FiniteScoreSetBuilder`], or call [`.score()`](FiniteScoreSet::score)
/// directly.
///
/// # Type parameters
///
/// - `T: Float` — the floating-point type (`f32` or `f64`).
/// - `I` — the input type passed to each metric.
/// - `E: Scorable<T, I>` — the metric enum generated by
///   [`finite_metric!`](crate::finite_metric!).
///
/// # Example
///
/// ```ignore
/// let set = FiniteScoreSet::<f64, &str, TestKind<f64, &str>>::normalize(vec![
///     (2.0, TestKind::AlwaysZero(ConstMetric::new("zero", 0.0))),
///     (3.0, TestKind::AlwaysOne(ConstMetric::new("one", 1.0))),
/// ])?;
///
/// let total = set.sum(&"input");
/// // total = 0.4 * 0 + 0.6 * 1 = 0.6
/// ```
pub struct FiniteScoreSet<T: Float, I, E> {
    members: Vec<FiniteMember<T, E>>,
    _phantom: PhantomData<I>,
}

impl<T: Float, I, E: Scorable<T, I>> FiniteScoreSet<T, I, E> {
    /// Normalize raw weights and validate the resulting set.
    ///
    /// Each weight must be finite and strictly positive. Weights are normalized
    /// to sum to 1.
    #[doc(hidden)]
    pub fn normalize(entries: Vec<(T, E)>) -> Result<Self, &'static str> {
        if entries.is_empty() {
            return Err("FiniteScoreSet: must have at least one member");
        }

        // Validate all weights are > 0
        for (w, _) in &entries {
            GtZero::witness(*w)?;
        }

        let sum: T = entries.iter().fold(T::zero(), |acc, (w, _)| acc + *w);

        let mut normalized: Vec<T> = entries.iter().map(|(w, _)| *w / sum).collect();

        // Sort a copy for binary search in NormalizedWeight
        let mut sorted = normalized.clone();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(core::cmp::Ordering::Equal));
        let container = NormalizedContainer::witness(sorted)?;

        let members: Vec<FiniteMember<T, E>> = entries
            .into_iter()
            .zip(normalized.drain(..))
            .map(|((_, metric), nw)| {
                let weight = nw
                    .witness()
                    .by(|v| NormalizedWeight::from_normalized_container(*v, &container))?;
                Ok(FiniteMember { weight, metric })
            })
            .collect::<Result<Vec<_>, &'static str>>()?;

        Ok(FiniteScoreSet {
            members,
            _phantom: PhantomData,
        })
    }

    /// Evaluate all metrics against `input` and sum their weighted contributions.
    ///
    /// This is the most common aggregation: each metric is evaluated, multiplied
    /// by its normalized weight, and summed. Zero-allocation convenience.
    ///
    /// For custom aggregation, use [`.score()`](Self::score) instead.
    #[inline]
    pub fn sum(&self, input: &I) -> T {
        self.members
            .iter()
            .fold(T::zero(), |acc, m| acc + m.contribute(m.metric.eval(input)))
    }

    /// Enter the scoring stage, returning a reference to all members.
    ///
    /// Use [`.by()`](FiniteScoreStage::by) on the returned stage to apply a
    /// custom aggregation, or [`.sum()`](Self::sum) for the standard
    /// weighted-sum shortcut.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let total = set.score().by(|members| {
    ///     members.iter().fold(0.0, |acc, m| {
    ///         acc + m.contribute(m.metric().eval(&input))
    ///     })
    /// });
    /// ```
    #[inline]
    pub fn score(&self) -> FiniteScoreStage<'_, T, I, E> {
        FiniteScoreStage {
            members: &self.members,
            _phantom: PhantomData,
        }
    }

    /// Return the number of members in this set.
    #[inline]
    pub fn len(&self) -> usize {
        self.members.len()
    }

    /// Return `true` if the set has no members.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.members.is_empty()
    }

    /// Iterate over the members.
    #[inline]
    pub fn iter(&self) -> impl Iterator<Item = &FiniteMember<T, E>> {
        self.members.iter()
    }

    /// Evaluate all metrics against `input` and return a per-metric breakdown.
    ///
    /// Unlike [`.sum()`](Self::sum) which returns only the aggregate,
    /// `breakdown` returns one [`Breakdown`] row per member with the metric's
    /// name, raw score, normalized weight, and weighted contribution.
    #[inline]
    pub fn breakdown(&self, input: &I) -> Vec<Breakdown<'_, T>> {
        self.members
            .iter()
            .map(|m| {
                let score_witness = m.metric.eval(input);
                let score_val: T = *score_witness;
                Breakdown {
                    name: m.metric.name(),
                    score: score_val,
                    weight: m.weight.into_inner(),
                    contribution: m.contribute(score_witness),
                }
            })
            .collect()
    }

    /// Create a builder for incremental construction of a `FiniteScoreSet`.
    ///
    /// Use this when members are not known up front — push them one by one,
    /// then call [`.build()`](FiniteScoreSetBuilder::build) to finalize.
    #[inline]
    pub fn builder() -> FiniteScoreSetBuilder<T, I, E> {
        FiniteScoreSetBuilder {
            entries: Vec::new(),
            _phantom: PhantomData,
        }
    }
}

// ---------------------------------------------------------------------------
// FiniteScoreStage — member reference for custom aggregation (Layer 2)
// ---------------------------------------------------------------------------

/// The scoring stage for a [`FiniteScoreSet`], created by
/// [`FiniteScoreSet::score`].
///
/// Holds a reference to the set's members. Call
/// [`.by()`](FiniteScoreStage::by) to apply a custom aggregation over the
/// member slice. For the standard weighted-sum shortcut, use
/// [`FiniteScoreSet::sum`] instead.
///
/// # Examples
///
/// ```ignore
/// // Standard weighted sum via the stage:
/// let total = set.score().by(|members| {
///     members.iter().fold(0.0, |acc, m| {
///         acc + m.contribute(m.metric().eval(&input))
///     })
/// });
///
/// // Custom: use only the worst contribution
/// let worst = set.score().by(|members| {
///     members.iter().map(|m| {
///         m.contribute(m.metric().eval(&input))
///     }).fold(f64::INFINITY, f64::min)
/// });
/// ```
pub struct FiniteScoreStage<'a, T: Float, I, E> {
    members: &'a [FiniteMember<T, E>],
    _phantom: PhantomData<I>,
}

impl<'a, T: Float, I, E: Scorable<T, I>> FiniteScoreStage<'a, T, I, E> {
    /// Apply a custom aggregation to the members.
    ///
    /// The closure receives a `&[FiniteMember<T, E>]` — one entry per member
    /// in insertion order. Each [`FiniteMember`] provides
    /// [`.metric()`](FiniteMember::metric) for evaluation and
    /// [`.contribute()`](FiniteMember::contribute) for weighting. The closure
    /// may return any type `R`.
    #[inline]
    pub fn by<F, R>(self, f: F) -> R
    where
        F: FnOnce(&[FiniteMember<T, E>]) -> R,
    {
        f(self.members)
    }
}

// ---------------------------------------------------------------------------
// FiniteScoreSetBuilder — incremental builder for FiniteScoreSet
// ---------------------------------------------------------------------------

/// Incremental builder for [`FiniteScoreSet`].
///
/// Accumulates raw `(weight, variant)` pairs via [`.push()`](Self::push), then
/// normalizes them into a [`FiniteScoreSet`] via [`.build()`](Self::build).
///
/// Each weight is validated on push (must be finite and > 0). Normalization
/// happens once at build time.
///
/// # Examples
///
/// Chain construction:
///
/// ```ignore
/// let set = FiniteScoreSet::<f64, &str, TestKind<f64, &str>>::builder()
///     .push(2.0, TestKind::AlwaysZero(const_metric("zero", 0.0)))?
///     .push(3.0, TestKind::AlwaysOne(const_metric("one", 1.0)))?
///     .build()?;
/// ```
///
/// Conditional construction:
///
/// ```ignore
/// let mut builder = FiniteScoreSet::<f64, &str, TestKind<f64, &str>>::builder();
/// builder = builder.push(2.0, baseline_variant)?;
/// if enable_extra {
///     builder = builder.push(1.0, extra_variant)?;
/// }
/// let set = builder.build()?;
/// ```
pub struct FiniteScoreSetBuilder<T: Float, I, E> {
    entries: Vec<(T, E)>,
    _phantom: PhantomData<I>,
}

impl<T: Float, I, E: Scorable<T, I>> FiniteScoreSetBuilder<T, I, E> {
    /// Push a metric enum variant with a raw weight into the builder.
    ///
    /// The weight must be finite and strictly positive. This is validated
    /// immediately (fail-fast). Takes and returns `Self` for chaining.
    ///
    /// For incremental construction, rebind the result:
    ///
    /// ```ignore
    /// let mut builder = FiniteScoreSet::builder();
    /// builder = builder.push(2.0, variant_a)?;
    /// if some_condition {
    ///     builder = builder.push(1.0, variant_b)?;
    /// }
    /// let set = builder.build()?;
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if `weight` is zero, negative, or not finite.
    #[inline]
    pub fn push(mut self, weight: T, variant: E) -> Result<Self, &'static str> {
        GtZero::witness(weight)?;
        self.entries.push((weight, variant));
        Ok(self)
    }

    /// Consume the builder and produce a [`FiniteScoreSet`] with normalized
    /// weights.
    ///
    /// # Errors
    ///
    /// Returns an error if no members were pushed.
    #[inline]
    pub fn build(self) -> Result<FiniteScoreSet<T, I, E>, &'static str> {
        FiniteScoreSet::normalize(self.entries)
    }
}

#[cfg(test)]
mod tests_for_finite;