haar_lib/algebra/semiring/
xor_and.rs

1//! XorとAndの半環
2use std::marker::PhantomData;
3
4pub use crate::algebra::semiring::*;
5
6/// XorとAndの半環
7#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct XorAnd<T>(PhantomData<T>);
9impl<T> XorAnd<T> {
10    /// [`XorAnd`]を返す。
11    pub fn new() -> Self {
12        Self(PhantomData)
13    }
14}
15
16macro_rules! impl_semiring {
17    ($($t:ty),*) => {
18        $(impl Semiring for XorAnd<$t> {
19            type Element = $t;
20            fn zero(&self) -> Self::Element {
21                0
22            }
23            fn one(&self) -> Self::Element {
24                !0
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                if n % 2 == 0 { 0 } else { a }
34            }
35        })*
36
37        $(impl Ring for XorAnd<$t> {
38            fn neg(&self, a: Self::Element) -> Self::Element {
39                a
40            }
41        })*
42    };
43}
44
45impl_semiring!(u8, u16, u32, u64, u128, usize);