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 all types that implements ::num_traits::Zero implement crate::num_traits::Zero #106

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
12d8d88
Implement Zero, One, and Pow
TonalidadeHidrica Jun 13, 2022
0f0aa8b
Move Zero and One to public module
TonalidadeHidrica Jun 16, 2022
e919319
Use Zero trait in fenwick tree
TonalidadeHidrica Jun 16, 2022
d67436a
Merge branch 'num-traits' into fenwick-zero-trait-and-num-traits
TonalidadeHidrica Jun 16, 2022
9034a0d
Impl Zero & One for all num_traits counterparts
TonalidadeHidrica Jun 16, 2022
ef261ae
impl Zero and One for modint
TonalidadeHidrica Jun 16, 2022
692957b
Merge branch 'master' into fenwick-zero-trait
TonalidadeHidrica Mar 29, 2023
cfcae20
Merge branch 'master' into num-traits
TonalidadeHidrica Mar 29, 2023
fd5ce0b
Bump num-traits version to 0.2.15
TonalidadeHidrica Mar 29, 2023
cf22065
Merge branch 'fenwick-zero-trait' into fenwick-zero-trait-and-num-traits
TonalidadeHidrica Mar 29, 2023
1492646
Merge branch 'num-traits' into fenwick-zero-trait-and-num-traits
TonalidadeHidrica Mar 29, 2023
69c3cf2
expand: fenwicktree depends internal_type_traits
mizar Mar 27, 2023
142bd16
Correct dependencies descibed in expand.py
TonalidadeHidrica Mar 29, 2023
be7fcbb
Merge branch 'fenwick-zero-trait' into fenwick-zero-trait-and-num-traits
TonalidadeHidrica Mar 29, 2023
3d756db
Merge branch 'master' into fenwick-zero-trait
TonalidadeHidrica Apr 15, 2023
756cc0f
Merge branch 'master' into num-traits
TonalidadeHidrica Apr 15, 2023
1c0c3fe
Move KaTeX-in-doc configs to Cargo.toml
TonalidadeHidrica Apr 15, 2023
14b6daa
Merge branch 'fenwick-zero-trait' into fenwick-zero-trait-and-num-traits
TonalidadeHidrica Apr 15, 2023
b4d09d4
Merge branch 'num-traits' into fenwick-zero-trait-and-num-traits
TonalidadeHidrica Apr 15, 2023
fa6e8e9
Fix expand.py so that modint depends on num_traits
TonalidadeHidrica Apr 15, 2023
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
8 changes: 0 additions & 8 deletions .cargo/config.toml

This file was deleted.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["competitive"]
categories = ["algorithms", "data-structures"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--html-in-header", "./doc/katex-header.html"]

[lib]
Expand All @@ -18,6 +19,7 @@ name = "ac_library"
# 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
2 changes: 1 addition & 1 deletion examples/library-checker-static-range-sum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
lrs: [(usize, usize); q],
}

let mut fenwick = FenwickTree::new(n, 0);
let mut fenwick = FenwickTree::<u64>::new(n);
for (i, a) in r#as.into_iter().enumerate() {
fenwick.add(i, a);
}
Expand Down
10 changes: 6 additions & 4 deletions expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,21 @@
output_header = '//https://github.com/rust-lang-ja/ac-library-rs\n'
opt_list = ['help', 'all', 'output=']
output_list_all = ('convolution', 'dsu', 'fenwicktree', 'lazysegtree', 'math',
'maxflow', 'mincostflow', 'modint', 'scc', 'segtree',
'string', 'twosat',
'maxflow', 'mincostflow', 'modint', 'num_traits', 'scc',
'segtree', 'string', 'twosat',
'internal_bit', 'internal_math', 'internal_queue',
'internal_scc', 'internal_type_traits',)
dependency_list = {'convolution': ('internal_bit', 'modint',),
'fenwicktree': ('num_traits',),
'lazysegtree': ('internal_bit', 'segtree'),
'math': ('internal_math',),
'maxflow': ('internal_type_traits', 'internal_queue',),
'mincostflow': ('internal_type_traits',),
'modint': ('internal_math',),
'modint': ('internal_math', 'num_traits',),
'scc': ('internal_scc',),
'segtree': ('internal_bit', 'internal_type_traits',),
'twosat': ('internal_scc',), }
'twosat': ('internal_scc',),
'internal_type_traits': ('num_traits',), }
src_path = 'src/'
output_path = None

Expand Down
14 changes: 7 additions & 7 deletions src/fenwicktree.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std::ops::{Bound, RangeBounds};

use crate::num_traits::Zero;

// Reference: https://en.wikipedia.org/wiki/Fenwick_tree
pub struct FenwickTree<T> {
n: usize,
ary: Vec<T>,
e: T,
}

impl<T: Clone + std::ops::AddAssign<T>> FenwickTree<T> {
pub fn new(n: usize, e: T) -> Self {
impl<T: Clone + std::ops::AddAssign<T> + Zero> FenwickTree<T> {
pub fn new(n: usize) -> Self {
FenwickTree {
n,
ary: vec![e.clone(); n],
e,
ary: vec![T::zero(); n],
}
}
pub fn accum(&self, mut idx: usize) -> T {
let mut sum = self.e.clone();
let mut sum = T::zero();
while idx > 0 {
sum += self.ary[idx - 1].clone();
idx &= idx - 1;
Expand Down Expand Up @@ -62,7 +62,7 @@ mod tests {

#[test]
fn fenwick_tree_works() {
let mut bit = FenwickTree::new(5, 0i64);
let mut bit = FenwickTree::<i64>::new(5);
// [1, 2, 3, 4, 5]
for i in 0..5 {
bit.add(i, i as i64 + 1);
Expand Down
30 changes: 2 additions & 28 deletions src/internal_type_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,13 @@ pub trait Integral:
+ fmt::Debug
+ fmt::Binary
+ fmt::Octal
+ Zero
+ One
+ crate::num_traits::Zero
+ crate::num_traits::One
+ BoundedBelow
+ BoundedAbove
{
}

/// Class that has additive identity element
pub trait Zero {
/// The additive identity element
fn zero() -> Self;
}

/// Class that has multiplicative identity element
pub trait One {
/// The multiplicative identity element
fn one() -> Self;
}

pub trait BoundedBelow {
fn min_value() -> Self;
}
Expand All @@ -82,20 +70,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 Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub mod segtree;
pub mod string;
pub mod twosat;

pub mod num_traits;

mod internal_bit;
mod internal_math;
mod internal_queue;
Expand Down
49 changes: 49 additions & 0 deletions src/modint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,55 @@ 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(not(feature = "num-traits"))]
impl<$generic_param: $generic_param_bound> $crate::num_traits::Zero for $self {
#[inline]
fn zero() -> Self {
Self::new(0)
}
}

#[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()
}
}

#[cfg(not(feature = "num-traits"))]
impl<$generic_param: $generic_param_bound> $crate::num_traits::One for $self {
#[inline]
fn one() -> Self {
Self::new(1)
}
}

#[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
59 changes: 59 additions & 0 deletions src/num_traits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/// A type that has an additive identity element.
pub trait Zero {
/// The additive identity element.
fn zero() -> Self;
}
#[cfg(feature = "num-traits")]
impl<T: num_traits::Zero> Zero for T {
fn zero() -> Self {
num_traits::Zero::zero()
}
}

/// A type that has a multiplicative identity element.
pub trait One {
/// The multiplicative identity element.
fn one() -> Self;
}
#[cfg(feature = "num-traits")]
impl<T: num_traits::One> One for T {
fn one() -> Self {
num_traits::One::one()
}
}

macro_rules! impl_zero {
($zero: literal, $one: literal: $($t: ty),*) => {
$(
#[cfg(not(feature="num-traits"))]
impl Zero for $t {
fn zero() -> Self {
$zero
}
}

#[cfg(not(feature="num-traits"))]
impl One for $t {
fn one() -> Self {
$one
}
}
)*
};
}
impl_zero!(0, 1: usize, u8, u16, u32, u64, u128);
impl_zero!(0, 1: isize, i8, i16, i32, i64, i128);
impl_zero!(0.0, 1.0: f32, f64);

#[cfg(not(feature = "num-traits"))]
impl<T: Zero> Zero for core::num::Wrapping<T> {
fn zero() -> Self {
Self(T::zero())
}
}
#[cfg(not(feature = "num-traits"))]
impl<T: One> One for core::num::Wrapping<T> {
fn one() -> Self {
Self(T::one())
}
}
3 changes: 2 additions & 1 deletion src/segtree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::internal_bit::ceil_pow2;
use crate::internal_type_traits::{BoundedAbove, BoundedBelow, One, Zero};
use crate::internal_type_traits::{BoundedAbove, BoundedBelow};
use crate::num_traits::{One, Zero};
use std::cmp::{max, min};
use std::convert::Infallible;
use std::marker::PhantomData;
Expand Down