haar_lib/algebra/semiring/
add_mul.rs

1//! AddとMulの半環
2use std::marker::PhantomData;
3
4pub use crate::algebra::semiring::Semiring;
5
6/// AddとMulの半環
7#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct AddMul<T>(PhantomData<T>);
9impl<T> AddMul<T> {
10    /// [`AddMul`]を返す。
11    pub fn new() -> Self {
12        Self(PhantomData)
13    }
14}
15
16macro_rules! impl_semiring {
17    ($($t:ty),*) => {
18        $(impl Semiring for AddMul<$t> {
19            type Element = $t;
20            fn zero(&self) -> Self::Element {
21                0
22            }
23            fn one(&self) -> Self::Element {
24                1
25            }
26            fn add(&self, a: Self::Element, b: Self::Element) -> Self::Element {
27                a + b
28            }
29            fn mul(&self, a: Self::Element, b: Self::Element) -> Self::Element {
30                a * b
31            }
32            fn times(&self, a: Self::Element, n: u64) -> Self::Element {
33                a * <$t>::try_from(n).unwrap()
34            }
35        })*
36    };
37}
38
39impl_semiring!(u8, u16, u32, u64, u128, usize);