Skip to main content

nekolib/utils/
op_add.rs

1//! 加法に関する wrapper クラス。
2
3use super::super::traits::additive;
4use super::super::traits::binop;
5
6use std::fmt::Debug;
7
8use additive::{AddAssoc, AddComm, Zero};
9use binop::{Associative, Commutative, Identity, Magma, PartialRecip, Recip};
10
11/// 和を返す演算を持つ。
12///
13/// [`std::ops::Add`](https://doc.rust-lang.org/std/ops/trait.Add.html) により定義される。
14/// 単位元は [`Zero`]、逆元は [`std::ops::Neg`](https://doc.rust-lang.org/std/ops/trait.Neg.html) で定義する。
15/// 結合法則を満たすときは [`AddAssoc`]、交換法則を満たすときは [`AddComm`] を実装することで示す。
16///
17/// [`Zero`]: ../../traits/additive/trait.Zero.html
18/// [`AddAssoc`]: ../../traits/additive/trait.AddAssoc.html
19/// [`AddComm`]: ../../traits/additive/trait.AddComm.html
20#[derive(Clone, Copy, Debug, Eq, PartialEq)]
21pub enum OpAdd<T> {
22    OpAddV,
23    _Marker(T),
24}
25pub use OpAdd::OpAddV;
26
27impl<T> Default for OpAdd<T> {
28    fn default() -> Self { OpAddV }
29}
30
31use std::ops::{Add, Neg};
32
33impl<T> Magma for OpAdd<T>
34where
35    T: Add<Output = T> + Eq + Sized,
36{
37    type Set = T;
38    fn op(&self, x: Self::Set, y: Self::Set) -> Self::Set { x + y }
39}
40impl<T> Identity for OpAdd<T>
41where
42    T: Add<Output = T> + Eq + Sized + Zero,
43{
44    fn id(&self) -> Self::Set { T::zero() }
45}
46impl<T> PartialRecip for OpAdd<T>
47where
48    T: Add<Output = T> + Eq + Sized + Neg<Output = T>,
49{
50    fn partial_recip(&self, x: Self::Set) -> Option<Self::Set> { Some(-x) }
51}
52impl<T> Recip for OpAdd<T>
53where
54    T: Add<Output = T> + Eq + Sized + Neg<Output = T>,
55{
56    fn recip(&self, x: Self::Set) -> Self::Set { -x }
57}
58impl<T> Associative for OpAdd<T> where T: Add<Output = T> + Eq + Sized + AddAssoc
59{}
60impl<T> Commutative for OpAdd<T> where T: Add<Output = T> + Eq + Sized + AddComm {}