Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Zero and One extensional traits #122

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 52 additions & 21 deletions src/internal_type_traits.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -52,25 +52,33 @@ pub trait Integral:
+ fmt::Debug
+ fmt::Binary
+ fmt::Octal
+ Zero
+ One
+ BoundedBelow
+ BoundedAbove
{
}

/// Class that has additive identity element
pub trait Zero {
pub(super) trait SumExt: Sum {
/// The additive identity element
fn zero() -> Self;
#[inline]
fn zero() -> Self {
iter::empty().sum()
}
}

impl<T: Sum> SumExt for T {}

/// Class that has multiplicative identity element
pub trait One {
pub(super) trait ProductExt: Product {
/// The multiplicative identity element
fn one() -> Self;
#[inline]
fn one() -> Self {
iter::empty().product()
}
}

impl<T: Product> ProductExt for T {}

pub trait BoundedBelow {
fn min_value() -> Self;
}
Expand All @@ -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 {
Expand All @@ -116,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::{ProductExt as _, SumExt as _};

#[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());
}
}
2 changes: 1 addition & 1 deletion src/maxflow.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/mincostflow.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::internal_type_traits::Integral;
use crate::internal_type_traits::{Integral, SumExt as _};

pub struct MinCostFlowEdge<T> {
pub from: usize,
Expand Down
7 changes: 4 additions & 3 deletions src/segtree.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -43,7 +44,7 @@ where
pub struct Additive<S>(Infallible, PhantomData<fn() -> S>);
impl<S> Monoid for Additive<S>
where
S: Copy + Add<Output = S> + Zero,
S: Copy + Add<Output = S> + Sum,
{
type S = S;
fn identity() -> Self::S {
Expand All @@ -57,7 +58,7 @@ where
pub struct Multiplicative<S>(Infallible, PhantomData<fn() -> S>);
impl<S> Monoid for Multiplicative<S>
where
S: Copy + Mul<Output = S> + One,
S: Copy + Mul<Output = S> + Product,
{
type S = S;
fn identity() -> Self::S {
Expand Down