From 7fbea0a4fb3742110ea317c58dcf7cc1da6f8d38 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sat, 1 Apr 2023 12:08:54 +0900 Subject: [PATCH 1/4] Make `Zero` and `One` extensional traits --- src/internal_type_traits.rs | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/internal_type_traits.rs b/src/internal_type_traits.rs index 88a52a9..5e96ed8 100644 --- a/src/internal_type_traits.rs +++ b/src/internal_type_traits.rs @@ -1,6 +1,6 @@ use std::{ fmt, - iter::{Product, Sum}, + iter::{self, Product, Sum}, ops::{ Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, @@ -36,8 +36,6 @@ pub trait Integral: + MulAssign + DivAssign + RemAssign - + Sum - + Product + BitOr + BitAnd + BitXor @@ -60,17 +58,27 @@ pub trait Integral: } /// Class that has additive identity element -pub trait Zero { +pub trait Zero: Sum { /// The additive identity element - fn zero() -> Self; + #[inline] + fn zero() -> Self { + iter::empty().sum() + } } +impl Zero for T {} + /// Class that has multiplicative identity element -pub trait One { +pub trait One: Product { /// The multiplicative identity element - fn one() -> Self; + #[inline] + fn one() -> Self { + iter::empty().product() + } } +impl One for T {} + pub trait BoundedBelow { fn min_value() -> Self; } @@ -82,20 +90,6 @@ pub trait BoundedAbove { macro_rules! impl_integral { ($($ty:ty),*) => { $( - impl Zero for $ty { - #[inline] - fn zero() -> Self { - 0 - } - } - - impl One for $ty { - #[inline] - fn one() -> Self { - 1 - } - } - impl BoundedBelow for $ty { #[inline] fn min_value() -> Self { From 412e3b74755d0870f0dad1afe2d005ab628d255c Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sat, 1 Apr 2023 12:36:15 +0900 Subject: [PATCH 2/4] Add tests --- src/internal_type_traits.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/internal_type_traits.rs b/src/internal_type_traits.rs index 5e96ed8..ba02263 100644 --- a/src/internal_type_traits.rs +++ b/src/internal_type_traits.rs @@ -110,3 +110,40 @@ macro_rules! impl_integral { } impl_integral!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize); + +#[cfg(test)] +mod tests { + use super::{One, Zero}; + + #[test] + fn zero() { + assert_eq!(0, i8::zero()); + assert_eq!(0, i16::zero()); + assert_eq!(0, i32::zero()); + assert_eq!(0, i64::zero()); + assert_eq!(0, i128::zero()); + assert_eq!(0, isize::zero()); + assert_eq!(0, u8::zero()); + assert_eq!(0, u16::zero()); + assert_eq!(0, u32::zero()); + assert_eq!(0, u64::zero()); + assert_eq!(0, u128::zero()); + assert_eq!(0, usize::zero()); + } + + #[test] + fn one() { + assert_eq!(1, i8::one()); + assert_eq!(1, i16::one()); + assert_eq!(1, i32::one()); + assert_eq!(1, i64::one()); + assert_eq!(1, i128::one()); + assert_eq!(1, isize::one()); + assert_eq!(1, u8::one()); + assert_eq!(1, u16::one()); + assert_eq!(1, u32::one()); + assert_eq!(1, u64::one()); + assert_eq!(1, u128::one()); + assert_eq!(1, usize::one()); + } +} From 9c7597352fc18182f5f363559af91057f7e97dd0 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sat, 8 Apr 2023 16:47:40 +0900 Subject: [PATCH 3/4] Rename `Zero` to `SumExt`, `One` to `ProductExt` --- src/internal_type_traits.rs | 14 +++++++------- src/maxflow.rs | 2 +- src/mincostflow.rs | 2 +- src/segtree.rs | 7 ++++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/internal_type_traits.rs b/src/internal_type_traits.rs index ba02263..c59c375 100644 --- a/src/internal_type_traits.rs +++ b/src/internal_type_traits.rs @@ -36,6 +36,8 @@ pub trait Integral: + MulAssign + DivAssign + RemAssign + + Sum + + Product + BitOr + BitAnd + BitXor @@ -50,15 +52,13 @@ pub trait Integral: + fmt::Debug + fmt::Binary + fmt::Octal - + Zero - + One + BoundedBelow + BoundedAbove { } /// Class that has additive identity element -pub trait Zero: Sum { +pub trait SumExt: Sum { /// The additive identity element #[inline] fn zero() -> Self { @@ -66,10 +66,10 @@ pub trait Zero: Sum { } } -impl Zero for T {} +impl SumExt for T {} /// Class that has multiplicative identity element -pub trait One: Product { +pub trait ProductExt: Product { /// The multiplicative identity element #[inline] fn one() -> Self { @@ -77,7 +77,7 @@ pub trait One: Product { } } -impl One for T {} +impl ProductExt for T {} pub trait BoundedBelow { fn min_value() -> Self; @@ -113,7 +113,7 @@ impl_integral!(i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize); #[cfg(test)] mod tests { - use super::{One, Zero}; + use super::{ProductExt as _, SumExt as _}; #[test] fn zero() { diff --git a/src/maxflow.rs b/src/maxflow.rs index 6e6e157..4459171 100644 --- a/src/maxflow.rs +++ b/src/maxflow.rs @@ -1,6 +1,6 @@ #![allow(dead_code)] use crate::internal_queue::SimpleQueue; -use crate::internal_type_traits::Integral; +use crate::internal_type_traits::{Integral, SumExt as _}; use std::cmp::min; use std::iter; diff --git a/src/mincostflow.rs b/src/mincostflow.rs index 7d708e4..5eb6bf9 100644 --- a/src/mincostflow.rs +++ b/src/mincostflow.rs @@ -1,4 +1,4 @@ -use crate::internal_type_traits::Integral; +use crate::internal_type_traits::{Integral, SumExt as _}; pub struct MinCostFlowEdge { pub from: usize, diff --git a/src/segtree.rs b/src/segtree.rs index b543aa3..9f9dbab 100644 --- a/src/segtree.rs +++ b/src/segtree.rs @@ -1,7 +1,8 @@ use crate::internal_bit::ceil_pow2; -use crate::internal_type_traits::{BoundedAbove, BoundedBelow, One, Zero}; +use crate::internal_type_traits::{BoundedAbove, BoundedBelow, ProductExt as _, SumExt as _}; use std::cmp::{max, min}; use std::convert::Infallible; +use std::iter::{Product, Sum}; use std::marker::PhantomData; use std::ops::{Add, Mul}; @@ -43,7 +44,7 @@ where pub struct Additive(Infallible, PhantomData S>); impl Monoid for Additive where - S: Copy + Add + Zero, + S: Copy + Add + Sum, { type S = S; fn identity() -> Self::S { @@ -57,7 +58,7 @@ where pub struct Multiplicative(Infallible, PhantomData S>); impl Monoid for Multiplicative where - S: Copy + Mul + One, + S: Copy + Mul + Product, { type S = S; fn identity() -> Self::S { From 135a09b5f01d60fb13b70cbb56f429be0357703d Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Sat, 8 Apr 2023 17:42:53 +0900 Subject: [PATCH 4/4] Make `pub(crate)` --- src/internal_type_traits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/internal_type_traits.rs b/src/internal_type_traits.rs index c59c375..4405c9b 100644 --- a/src/internal_type_traits.rs +++ b/src/internal_type_traits.rs @@ -58,7 +58,7 @@ pub trait Integral: } /// Class that has additive identity element -pub trait SumExt: Sum { +pub(super) trait SumExt: Sum { /// The additive identity element #[inline] fn zero() -> Self { @@ -69,7 +69,7 @@ pub trait SumExt: Sum { impl SumExt for T {} /// Class that has multiplicative identity element -pub trait ProductExt: Product { +pub(super) trait ProductExt: Product { /// The multiplicative identity element #[inline] fn one() -> Self {