haar_lib/algebra/
min_count.rs1use crate::algebra::traits::*;
3use crate::num::one_zero::Zero;
4use std::{cmp::Ordering, ops::Add};
5
6#[derive(Copy, Clone, Default, Debug, PartialEq, Eq)]
8pub struct MinCount<T, U>(pub Option<T>, pub U);
9impl<T, U> Set for MinCount<T, U> {}
10impl<T: Ord, U: Add<Output = U>> BinaryOp for MinCount<T, U> {
11 fn op(self, b: Self) -> Self {
12 match (self, b) {
13 (Self(None, ca), Self(None, cb)) => Self(None, ca + cb),
14 (Self(None, _), a) => a,
15 (b, Self(None, _)) => b,
16 (Self(Some(a), ca), Self(Some(b), cb)) => match a.cmp(&b) {
17 Ordering::Equal => Self(Some(a), ca + cb),
18 Ordering::Less => Self(Some(a), ca),
19 Ordering::Greater => Self(Some(b), cb),
20 },
21 }
22 }
23}
24impl<T, U: Zero> Identity for MinCount<T, U> {
25 fn id() -> Self {
26 Self(None, U::zero())
27 }
28}
29impl<T, U> Associative for MinCount<T, U> {}
30impl<T, U> Commutative for MinCount<T, U> {}