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

Conversation

qryxip
Copy link
Member

@qryxip qryxip commented Apr 1, 2023

Makes Zero and One extensional traits for Sum and Product, respectively.

As I said in rust-jp.slack.com 3 years ago IIRC, we might as well regard the "zero value" and "one value" described in the documentation as identity elements.

The implementations should be still inlined.

Affects #102.

@TonalidadeHidrica
Copy link
Collaborator

Will you need to implement Sum trait (Sum::sum function) when you want to use the additive segtree for a custom type, if we adopt this PR?

@qryxip
Copy link
Member Author

qryxip commented Apr 1, 2023

Yes - instead of impling the Zero from this crate.

struct S(i32);

impl Add for S {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

impl Sum for S {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self(0), Add::add)
        //        ^^^^^^^
        //        additive identity here
    }
}
// or:
struct S(i32);

//impl Add for S {
//    type Output = Self;
//
//    fn add(self, rhs: Self) -> Self::Output {
//        Self(self.0 + rhs.0)
//    }
//}

impl Sum for S {
    // only used for `Zero` from ac-library-rs
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self(0), |_, _| unreachable!())
        //        ^^^^^^^
        //        additive identity here
    }
}

Sum and Product are free from ac-library-rs, so personal libraries for non-AtCoder competitive programming services should easily adopt.

@qryxip
Copy link
Member Author

qryxip commented Apr 1, 2023

↑ The second code example was not necessary.

@TonalidadeHidrica
Copy link
Collaborator

I don't think it is a good idea. "If you want to use + monoid, implement Zero trait." --- that's straightforward. But, "if you want to use + monoid, implement Sum trait." --- that requires an additional effort and not intuitive for me. How do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants