haar_lib/algebra/
bit.rs

1//! 論理積・論理和・排他的論理和
2use std::marker::PhantomData;
3
4pub use crate::algebra::traits::*;
5use crate::impl_algebra;
6
7/// 論理積を演算とする代数的構造
8#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct BitAnd<T>(PhantomData<T>);
10impl<T> BitAnd<T> {
11    /// [`BitAnd`]を返す。
12    pub fn new() -> Self {
13        Self(PhantomData)
14    }
15}
16
17/// 論理和を演算とする代数的構造
18#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
19pub struct BitOr<T>(PhantomData<T>);
20impl<T> BitOr<T> {
21    /// [`BitOr`]を返す。
22    pub fn new() -> Self {
23        Self(PhantomData)
24    }
25}
26
27/// 排他的論理和を演算とする代数的構造
28#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
29pub struct BitXor<T>(PhantomData<T>);
30impl<T> BitXor<T> {
31    /// [`BitXor`]を返す。
32    pub fn new() -> Self {
33        Self(PhantomData)
34    }
35}
36
37macro_rules! implement {
38    ($($t:tt),*) => {
39        $(impl_algebra!(BitAnd<$t>; set: $t; op: |_, a: $t, b: $t| a & b;
40                        id: |_| !0; commu; assoc; idem;);)*
41        $(impl_algebra!(BitOr<$t>; set: $t; op: |_, a: $t, b: $t| a | b;
42                        id: |_| 0; commu; assoc; idem;);)*
43        $(impl_algebra!(BitXor<$t>; set: $t; op: |_, a: $t, b: $t| a ^ b;
44                        id: |_| 0; inv: |_, a| a; commu; assoc;);)*
45    };
46}
47
48implement!(u8, u16, u32, u64, u128, usize);