use core::cmp::Ordering;
pub trait Score: Copy + Ord {
#[must_use]
fn negate(self) -> Self;
#[must_use]
fn next_up(self) -> Self;
#[must_use]
fn next_down(self) -> Self;
}
macro_rules! integer_score {
($($ty:ty),*) => {
$(
impl Score for $ty {
fn negate(self) -> Self {
!self
}
fn next_up(self) -> Self {
self.saturating_add(1)
}
fn next_down(self) -> Self {
self.saturating_sub(1)
}
}
)*
};
}
integer_score!(i8, i16, i32, i64, i128, isize);
macro_rules! total_float {
($(#[$doc:meta])* $name:ident, $float:ty, $bits:ty, $narrow:path) => {
$(#[$doc])*
#[derive(Debug, Clone, Copy, Default)]
pub struct $name($float);
impl $name {
#[must_use]
pub fn new(value: $float) -> Option<Self> {
(!value.is_nan()).then_some(Self(value))
}
#[must_use]
pub const fn get(self) -> $float {
self.0
}
}
impl PartialEq for $name {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for $name {}
impl PartialOrd for $name {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for $name {
fn cmp(&self, other: &Self) -> Ordering {
self.0.total_cmp(&other.0)
}
}
impl Score for $name {
fn negate(self) -> Self {
Self(-self.0)
}
fn next_up(self) -> Self {
let bits = self.0.to_bits();
let up = if self.0 == <$float>::INFINITY {
bits
} else if self.0.is_sign_positive() {
bits + 1
} else if self.0 == 0.0 {
0
} else {
bits - 1
};
Self(<$float>::from_bits(up))
}
fn next_down(self) -> Self {
self.negate().next_up().negate()
}
}
impl Expectation for $name {
fn expectation(outcomes: &[(Self, f64)]) -> Self {
let largest = outcomes.iter().map(|&(_, weight)| weight).fold(0.0, f64::max);
let total: f64 = outcomes.iter().map(|&(_, weight)| weight / largest).sum();
let mean: f64 = outcomes
.iter()
.map(|&(score, weight)| f64::from(score.0) * (weight / largest / total))
.sum();
Self::new($narrow(mean)).unwrap_or_default()
}
}
};
}
pub trait Expectation: Score {
#[must_use]
fn expectation(outcomes: &[(Self, f64)]) -> Self;
}
total_float!(
TotalF32,
f32,
u32,
narrow_to_f32
);
total_float!(
TotalF64,
f64,
u64,
core::convert::identity
);
#[expect(
clippy::cast_possible_truncation,
reason = "a mean of f32 scores fits an f32 again"
)]
const fn narrow_to_f32(value: f64) -> f32 {
value as f32
}
#[cfg(test)]
mod tests {
use super::{Score, TotalF32, TotalF64};
#[test]
fn integer_negation_mirrors_the_whole_range() {
assert_eq!(3_i32.negate(), -4);
assert_eq!(i8::MIN.negate(), i8::MAX);
assert_eq!(i8::MAX.negate(), i8::MIN);
for s in i8::MIN..i8::MAX {
assert!((s + 1).negate() < s.negate(), "{s}");
assert_eq!(s.negate().negate(), s, "{s}");
}
}
#[test]
fn floats_order_totally() {
let f = |value| TotalF64::new(value).expect("not NaN");
assert!(f(f64::NEG_INFINITY) < f(-1.0));
assert!(f(-0.0) < f(0.0));
assert!(f(1.0) < f(f64::INFINITY));
assert_eq!(f(2.0).negate(), f(-2.0));
}
#[test]
fn integer_steps_saturate() {
assert_eq!((3_i32.next_up(), 3_i32.next_down()), (4, 2));
assert_eq!((i8::MAX.next_up(), i8::MIN.next_down()), (i8::MAX, i8::MIN));
}
#[test]
fn float_steps_walk_the_total_order() {
let f = |value| TotalF32::new(value).expect("not NaN");
let tiny = f32::from_bits(1);
assert_eq!(f(-0.0).next_up().get().to_bits(), 0.0_f32.to_bits());
assert_eq!(f(0.0).next_up().get(), tiny);
assert_eq!(f(0.0).next_down().get().to_bits(), (-0.0_f32).to_bits());
assert_eq!(f(-0.0).next_down().get(), -tiny);
assert_eq!(f(f32::MAX).next_up().get(), f32::INFINITY);
assert_eq!(f(f32::INFINITY).next_up().get(), f32::INFINITY);
assert_eq!(f(f32::NEG_INFINITY).next_down().get(), f32::NEG_INFINITY);
assert_eq!(f(f32::NEG_INFINITY).next_up().get(), -f32::MAX);
assert!(f(1.0).next_up() > f(1.0) && f(1.0).next_down() < f(1.0));
assert!(f(-1.0).next_up() > f(-1.0));
assert_eq!(
TotalF64::new(0.0).map(|zero| zero.next_up().get()),
Some(f64::from_bits(1))
);
}
#[test]
fn expectations_are_weighted_means() {
use super::Expectation;
let f = |value| TotalF64::new(value).expect("not NaN");
assert_eq!(
TotalF64::expectation(&[(f(3.0), 1.0), (f(-1.0), 3.0)]).get(),
0.0
);
assert_eq!(TotalF64::expectation(&[(f(2.0), 0.5)]).get(), 2.0);
let opposite = [(f(f64::INFINITY), 1.0), (f(f64::NEG_INFINITY), 1.0)];
assert_eq!(TotalF64::expectation(&opposite).get(), 0.0);
let huge = [(f(0.001), 1e308), (f(0.002), 1e308)];
assert!((TotalF64::expectation(&huge).get() - 0.0015).abs() < 1e-15);
assert_eq!(TotalF64::expectation(&[(f(f64::MAX), 2.0)]).get(), f64::MAX);
let single = TotalF32::new(1.5).expect("not NaN");
assert_eq!(TotalF32::expectation(&[(single, 2.0)]).get(), 1.5);
}
#[test]
fn nan_is_rejected() {
assert!(TotalF32::new(f32::NAN).is_none());
assert_eq!(TotalF32::new(1.5).map(TotalF32::get), Some(1.5));
}
}