cp_library_rs/algebraic_structure/
monoid_with_context.rs1pub trait MonoidCtx {
13 type Val: Clone;
15 fn e(&self) -> Self::Val;
17 fn op(&self, left: &Self::Val, right: &Self::Val) -> Self::Val;
19}
20
21pub mod examples {
24 use std::ops::{Add, Mul, Rem};
25
26 use num::{One, Zero};
27
28 use crate::algebraic_structure::monoid_with_context::MonoidCtx;
29
30 pub struct AddMod<T>(pub T);
32
33 impl<T> MonoidCtx for AddMod<T>
34 where
35 T: Clone + Zero + Add + Rem<Output = T>,
36 {
37 type Val = T;
38 fn e(&self) -> Self::Val {
39 T::zero()
40 }
41 fn op(&self, left: &Self::Val, right: &Self::Val) -> Self::Val {
42 (left.clone() + right.clone()) % self.0.clone()
43 }
44 }
45
46 pub struct MulMod<T>(pub T);
48
49 impl<T> MonoidCtx for MulMod<T>
50 where
51 T: Clone + One + Mul + Rem<Output = T>,
52 {
53 type Val = T;
54 fn e(&self) -> Self::Val {
55 T::one()
56 }
57 fn op(&self, left: &Self::Val, right: &Self::Val) -> Self::Val {
58 (left.clone() * right.clone()) % self.0.clone()
59 }
60 }
61}