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

Implement num_traits::Zero, One, and Pow for Modint #105

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ publish = false
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
num-traits = { version = "0.2.15", optional = true }

[dev-dependencies]
proconio = "=0.3.6"
Expand Down
33 changes: 33 additions & 0 deletions src/modint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,39 @@ macro_rules! impl_basic_traits {
}
}

#[cfg(feature = "num-traits")]
impl<$generic_param: $generic_param_bound> num_traits::Zero for $self {
#[inline]
fn zero() -> Self {
Self::new(0)
}
#[inline]
fn is_zero(&self) -> bool {
self == &Self::zero()
}
}

#[cfg(feature = "num-traits")]
impl<$generic_param: $generic_param_bound> num_traits::One for $self {
#[inline]
fn one() -> Self {
Self::new(1)
}
#[inline]
fn is_one(&self) -> bool {
self == &Self::one()
}
Comment on lines +919 to +922
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[inline]
fn is_one(&self) -> bool {
self == &Self::one()
}

This part can be omitted (unlike Zero, somehow).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, that's quite surprising :) However, I took a look at the doc to realize that it actually recommends to implement this. So shall we just leave this as it is?

}

#[cfg(feature = "num-traits")]
impl<$generic_param: $generic_param_bound> num_traits::Pow<u64> for $self {
type Output=$self;
#[inline]
fn pow(self, rhs: u64) -> Self::Output {
self.pow(rhs)
}
}

impl_basic_traits!($($rest)*);
};
}
Expand Down