haar_lib/algebra/
sum.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 Sum<T>(PhantomData<T>);
10impl<T> Sum<T> {
11    /// [`Sum<T>`]を返す。
12    pub fn new() -> Self {
13        Self(PhantomData)
14    }
15}
16
17macro_rules! implement {
18    (signed; $($t:ty),*) => {
19        $(impl_algebra!(Sum<$t>; set: $t; op: |_, a: $t, b: $t| a + b;
20                        id: |_| 0 as $t; inv: |_, a: $t| -a; commu; assoc;);)*
21        $(impl Additive for Sum<$t> { fn times(&self, a: $t, n: u64) -> $t { a * <$t>::try_from(n).unwrap() }})*
22        $(impl Ordered for Sum<$t>{})*
23    };
24    (unsigned; $($t:ty),*) => {
25        $(impl_algebra!(Sum<$t>; set: $t; op: |_, a: $t, b: $t| a + b;
26                        id: |_| 0 as $t; commu; assoc;);)*
27        $(impl Additive for Sum<$t> { fn times(&self, a: $t, n: u64) -> $t { a * <$t>::try_from(n).unwrap() }})*
28        $(impl Ordered for Sum<$t>{})*
29    };
30    (float; $($t:ty),*) => {
31        $(impl_algebra!(Sum<$t>; set: $t; op: |_, a: $t, b: $t| a + b;
32                        id: |_| 0 as $t; inv: |_, a: $t| -a; commu; assoc;);)*
33        $(impl Additive for Sum<$t> { fn times(&self, a: $t, n: u64) -> $t { a * n as $t }})*
34    }
35}
36
37implement!(signed; i8, i16, i32, i64, i128, isize);
38implement!(unsigned; u8, u16, u32, u64, u128, usize);
39implement!(float; f32, f64);