Skip to main content

nekolib/traits/
multiplicative.rs

1//! 乗法に関するトレイトたちです。
2//!
3//! これを実装したクラスは `OpMul` によって積を求められます。
4//! 区間和を求めるデータ構造などに使います。
5
6use std::ops::Mul;
7
8/// 乗法の単位元 $1$ を定義する。
9pub trait One: Mul<Output = Self> + Sized {
10    /// 乗法の単位元 $1$ を返す。
11    fn one() -> Self;
12}
13/// 乗法の逆元を定義する。
14pub trait MulRecip {
15    /// 返り値の型。
16    type Output;
17    /// 乗法における $x$ の逆元 $x^{-1}$ を返す。
18    fn mul_recip(self) -> Self::Output;
19}
20/// 乗法が結合法則を満たすことを示す。
21///
22/// $$ x, y, z \\in S \\implies (x \\times y) \\times z = x \\times (y \\times z). $$
23pub trait MulAssoc: Mul<Output = Self> + Sized {}
24/// 乗法が交換法則を満たすことを示す。
25///
26/// $$ x, y \\in S \\implies x \\times y = y \\times x. $$
27pub trait MulComm: Mul<Output = Self> + Sized {}
28
29macro_rules! impl_trait {
30    (
31        $( impl ($T:ty) for { $( $U:ty ),* } $S:tt )*
32    ) => {
33        $( $( impl $T for $U $S )* )*
34    };
35}
36
37impl_trait! {
38    impl (One) for {i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize} {
39        fn one() -> Self { 1 }
40    }
41    impl (MulAssoc) for {i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize} {}
42    impl (MulComm) for {i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize} {}
43}