1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//! ## モノイド
//!
//! - [`Monoid::Val`] : データの型 $`S`$
//! - [`Monoid::id`] : 単位元を返す関数 $`\varnothing \to S`$
//! - [`Monoid::op`] : 演算 $`S\times S \to S`$

macro_rules! impl_monoid_add {
    ($ty:ty, $id:expr) => {
        impl Monoid for Add<$ty> {
            type Val = $ty;
            fn id() -> Self::Val {
                $id
            }
            fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
                *left + *right
            }
        }
    };
}

use crate::{
    algebraic_structure::operation::{Add, Max, Min, Mul, Xor, GCD},
    number_theory::modint::{M107, M998},
    utils::num_traits::{Bounded, One},
};

use super::operation::MinMax;

/// モノイド
///
/// - [`Monoid::Val`] : データの型 $`S`$
/// - [`Monoid::id`] : 単位元を返す関数 $`\varnothing \to S`$
/// - [`Monoid::op`] : 演算 $`S\times S \to S`$
pub trait Monoid {
    /// データの型 ($`S`$)
    type Val: Clone;
    /// 単位元 ($`\varnothing \to S`$)
    fn id() -> Self::Val;
    /// 演算 ($`S \times S \to S`$)
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val;
}

// ========== 実装 ==========
impl Monoid for () {
    type Val = ();
    fn id() -> Self::Val {}
    fn op(_: &Self::Val, _: &Self::Val) -> Self::Val {}
}

impl<T: Ord + Bounded + Clone> Monoid for Min<T> {
    type Val = T;
    fn id() -> Self::Val {
        Self::Val::max_value()
    }
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
        left.min(right).clone()
    }
}

impl<T: Ord + Bounded + Clone> Monoid for Max<T> {
    type Val = T;
    fn id() -> Self::Val {
        Self::Val::min_value()
    }
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
        left.max(right).clone()
    }
}

impl<T: Ord + Bounded + Clone> Monoid for MinMax<T> {
    type Val = (T, T);
    fn id() -> Self::Val {
        (Min::id(), Max::id())
    }
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
        (Min::op(&left.0, &right.0), Max::op(&left.1, &right.1))
    }
}

impl Monoid for Add<usize> {
    type Val = usize;
    fn id() -> Self::Val {
        0
    }
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
        left.wrapping_add(*right)
    }
}
impl_monoid_add!(isize, 0);
impl_monoid_add!(f64, 0.0);
impl_monoid_add!(M107, M107::new(0));
impl_monoid_add!(M998, M998::new(0));

impl<T: One + Clone> Monoid for Mul<T> {
    type Val = T;
    fn id() -> Self::Val {
        T::one()
    }
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
        left.clone() * right.clone()
    }
}

impl Monoid for Xor {
    type Val = usize;
    fn id() -> Self::Val {
        0
    }
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
        left ^ right
    }
}

impl<T> Monoid for GCD<T> {
    type Val = usize;
    fn id() -> Self::Val {
        0
    }
    fn op(left: &Self::Val, right: &Self::Val) -> Self::Val {
        GCD::gcd(left, right)
    }
}