From 224d9dfc2f4b2c355d9c325964262881025a0b43 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 11 Oct 2020 17:06:08 +0900 Subject: [PATCH 1/4] impl binop for Mint --- src/modint.rs | 51 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/src/modint.rs b/src/modint.rs index 1445f5b..93b29a0 100644 --- a/src/modint.rs +++ b/src/modint.rs @@ -891,8 +891,8 @@ impl_basic_traits! { macro_rules! impl_bin_ops { () => {}; - (for<$generic_param:ident : $generic_param_bound:tt> <$lhs_ty:ty> ~ <$rhs_ty:ty> -> $output:ty { { $lhs_body:expr } ~ { $rhs_body:expr } } $($rest:tt)*) => { - impl <$generic_param: $generic_param_bound> Add<$rhs_ty> for $lhs_ty { + (for<$($generic_param:ident : $generic_param_bound:tt),*> <$lhs_ty:ty> ~ <$rhs_ty:ty> -> $output:ty { { $lhs_body:expr } ~ { $rhs_body:expr } } $($rest:tt)*) => { + impl <$($generic_param: $generic_param_bound),*> Add<$rhs_ty> for $lhs_ty { type Output = $output; #[inline] @@ -901,7 +901,7 @@ macro_rules! impl_bin_ops { } } - impl <$generic_param: $generic_param_bound> Sub<$rhs_ty> for $lhs_ty { + impl <$($generic_param: $generic_param_bound),*> Sub<$rhs_ty> for $lhs_ty { type Output = $output; #[inline] @@ -910,7 +910,7 @@ macro_rules! impl_bin_ops { } } - impl <$generic_param: $generic_param_bound> Mul<$rhs_ty> for $lhs_ty { + impl <$($generic_param: $generic_param_bound),*> Mul<$rhs_ty> for $lhs_ty { type Output = $output; #[inline] @@ -919,7 +919,7 @@ macro_rules! impl_bin_ops { } } - impl <$generic_param: $generic_param_bound> Div<$rhs_ty> for $lhs_ty { + impl <$($generic_param: $generic_param_bound),*> Div<$rhs_ty> for $lhs_ty { type Output = $output; #[inline] @@ -934,29 +934,29 @@ macro_rules! impl_bin_ops { macro_rules! impl_assign_ops { () => {}; - (for<$generic_param:ident : $generic_param_bound:tt> <$lhs_ty:ty> ~= <$rhs_ty:ty> { _ ~= { $rhs_body:expr } } $($rest:tt)*) => { - impl <$generic_param: $generic_param_bound> AddAssign<$rhs_ty> for $lhs_ty { + (for<$($generic_param:ident : $generic_param_bound:tt),*> <$lhs_ty:ty> ~= <$rhs_ty:ty> { _ ~= { $rhs_body:expr } } $($rest:tt)*) => { + impl <$($generic_param: $generic_param_bound),*> AddAssign<$rhs_ty> for $lhs_ty { #[inline] fn add_assign(&mut self, rhs: $rhs_ty) { *self = *self + apply($rhs_body, rhs); } } - impl <$generic_param: $generic_param_bound> SubAssign<$rhs_ty> for $lhs_ty { + impl <$($generic_param: $generic_param_bound),*> SubAssign<$rhs_ty> for $lhs_ty { #[inline] fn sub_assign(&mut self, rhs: $rhs_ty) { *self = *self - apply($rhs_body, rhs); } } - impl <$generic_param: $generic_param_bound> MulAssign<$rhs_ty> for $lhs_ty { + impl <$($generic_param: $generic_param_bound),*> MulAssign<$rhs_ty> for $lhs_ty { #[inline] fn mul_assign(&mut self, rhs: $rhs_ty) { *self = *self * apply($rhs_body, rhs); } } - impl <$generic_param: $generic_param_bound> DivAssign<$rhs_ty> for $lhs_ty { + impl <$($generic_param: $generic_param_bound),*> DivAssign<$rhs_ty> for $lhs_ty { #[inline] fn div_assign(&mut self, rhs: $rhs_ty) { *self = *self / apply($rhs_body, rhs); @@ -981,6 +981,9 @@ impl_bin_ops! { for > ~ <&'_ DynamicModInt> -> DynamicModInt { { |x| x } ~ { |&x| x } } for <&'_ DynamicModInt> ~ > -> DynamicModInt { { |&x| x } ~ { |x| x } } for <&'_ DynamicModInt> ~ <&'_ DynamicModInt> -> DynamicModInt { { |&x| x } ~ { |&x| x } } + + for > ~ -> StaticModInt { { |x| x } ~ { |x| StaticModInt::::new(x) } } + for > ~ -> DynamicModInt { { |x| x } ~ { |x| DynamicModInt::::new(x) } } } impl_assign_ops! { @@ -988,6 +991,9 @@ impl_assign_ops! { for > ~= <&'_ StaticModInt > { _ ~= { |&x| x } } for > ~= > { _ ~= { |x| x } } for > ~= <&'_ DynamicModInt> { _ ~= { |&x| x } } + + for > ~= { _ ~= { |x| StaticModInt::::new(x) } } + for > ~= { _ ~= { |x| DynamicModInt::::new(x) } } } macro_rules! impl_folding { @@ -1108,4 +1114,29 @@ mod tests { assert_eq!(ModInt1000000007::new(-120), product(&[-1, 2, -3, 4, -5])); } + + #[test] + fn static_modint_binop_coercion() { + let f = ModInt1000000007::new; + let a = 10293812usize; + let b = 9083240982usize; + assert_eq!(f(a) + f(b), f(a) + b); + assert_eq!(f(a) - f(b), f(a) - b); + assert_eq!(f(a) * f(b), f(a) * b); + assert_eq!(f(a) / f(b), f(a) / b); + } + + #[test] + fn static_modint_assign_coercion() { + let f = ModInt1000000007::new; + let a = f(10293812usize); + let b = 9083240982usize; + let expected = (((a + b) * b) - b) / b; + let mut c = a; + c += b; + c *= b; + c -= b; + c /= b; + assert_eq!(expected, c); + } } From e90bb7e88c20acc6a64ba04cb26221dac068a44c Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 11 Oct 2020 17:43:38 +0900 Subject: [PATCH 2/4] lint --- src/modint.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/modint.rs b/src/modint.rs index 93b29a0..ab0b07e 100644 --- a/src/modint.rs +++ b/src/modint.rs @@ -982,8 +982,8 @@ impl_bin_ops! { for <&'_ DynamicModInt> ~ > -> DynamicModInt { { |&x| x } ~ { |x| x } } for <&'_ DynamicModInt> ~ <&'_ DynamicModInt> -> DynamicModInt { { |&x| x } ~ { |&x| x } } - for > ~ -> StaticModInt { { |x| x } ~ { |x| StaticModInt::::new(x) } } - for > ~ -> DynamicModInt { { |x| x } ~ { |x| DynamicModInt::::new(x) } } + for > ~ -> StaticModInt { { |x| x } ~ { StaticModInt::::new } } + for > ~ -> DynamicModInt { { |x| x } ~ { DynamicModInt::::new } } } impl_assign_ops! { @@ -992,8 +992,8 @@ impl_assign_ops! { for > ~= > { _ ~= { |x| x } } for > ~= <&'_ DynamicModInt> { _ ~= { |&x| x } } - for > ~= { _ ~= { |x| StaticModInt::::new(x) } } - for > ~= { _ ~= { |x| DynamicModInt::::new(x) } } + for > ~= { _ ~= { StaticModInt::::new } } + for > ~= { _ ~= { DynamicModInt::::new } } } macro_rules! impl_folding { From 92a7243ca681e52bbbc8c62e380dd2735291706b Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 11 Oct 2020 17:49:06 +0900 Subject: [PATCH 3/4] clippy --- src/modint.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modint.rs b/src/modint.rs index ab0b07e..1f2162b 100644 --- a/src/modint.rs +++ b/src/modint.rs @@ -1118,8 +1118,8 @@ mod tests { #[test] fn static_modint_binop_coercion() { let f = ModInt1000000007::new; - let a = 10293812usize; - let b = 9083240982usize; + let a = 10_293_812_usize; + let b = 9_083_240_982_usize; assert_eq!(f(a) + f(b), f(a) + b); assert_eq!(f(a) - f(b), f(a) - b); assert_eq!(f(a) * f(b), f(a) * b); From 3fdf7d982e8ecb2d96d39cdd3fc7838158ad4b28 Mon Sep 17 00:00:00 2001 From: Yohei Tamura Date: Sun, 11 Oct 2020 17:51:33 +0900 Subject: [PATCH 4/4] clippy --- src/modint.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/modint.rs b/src/modint.rs index 1f2162b..ba72d48 100644 --- a/src/modint.rs +++ b/src/modint.rs @@ -1129,8 +1129,8 @@ mod tests { #[test] fn static_modint_assign_coercion() { let f = ModInt1000000007::new; - let a = f(10293812usize); - let b = 9083240982usize; + let a = f(10_293_812_usize); + let b = 9_083_240_982_usize; let expected = (((a + b) * b) - b) / b; let mut c = a; c += b;