Skip to content

Commit

Permalink
segtree: monoid BitwiseAnd/BitwiseOr/BitwiseXor
Browse files Browse the repository at this point in the history
  • Loading branch information
mizar committed Apr 16, 2023
1 parent 71c3474 commit a04c1ef
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ pub use modint::{
ModInt1000000007, ModInt998244353, Modulus, RemEuclidU32, StaticModInt,
};
pub use scc::SccGraph;
pub use segtree::{Additive, Max, Min, Monoid, Multiplicative, Segtree};
pub use segtree::{
Additive, BitwiseAnd, BitwiseOr, BitwiseXor, Max, Min, Monoid, Multiplicative, Segtree,
};
pub use string::{
lcp_array, lcp_array_arbitrary, suffix_array, suffix_array_arbitrary, suffix_array_manual,
z_algorithm, z_algorithm_arbitrary,
Expand Down
44 changes: 43 additions & 1 deletion src/segtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::internal_type_traits::{BoundedAbove, BoundedBelow, One, Zero};
use std::cmp::{max, min};
use std::convert::Infallible;
use std::marker::PhantomData;
use std::ops::{Add, Bound, Mul, RangeBounds};
use std::ops::{Add, BitAnd, BitOr, BitXor, Bound, Mul, Not, RangeBounds};

// TODO Should I split monoid-related traits to another module?
pub trait Monoid {
Expand Down Expand Up @@ -68,6 +68,48 @@ where
}
}

pub struct BitwiseOr<S>(Infallible, PhantomData<fn() -> S>);
impl<S> Monoid for BitwiseOr<S>
where
S: Copy + BitOr<Output = S> + Zero,
{
type S = S;
fn identity() -> Self::S {
S::zero()
}
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
*a | *b
}
}

pub struct BitwiseAnd<S>(Infallible, PhantomData<fn() -> S>);
impl<S> Monoid for BitwiseAnd<S>
where
S: Copy + BitAnd<Output = S> + Not<Output = S> + Zero,
{
type S = S;
fn identity() -> Self::S {
!S::zero()
}
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
*a & *b
}
}

pub struct BitwiseXor<S>(Infallible, PhantomData<fn() -> S>);
impl<S> Monoid for BitwiseXor<S>
where
S: Copy + BitXor<Output = S> + Zero,
{
type S = S;
fn identity() -> Self::S {
S::zero()
}
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
*a ^ *b
}
}

impl<M: Monoid> Default for Segtree<M> {
fn default() -> Self {
Segtree::new(0)
Expand Down

0 comments on commit a04c1ef

Please sign in to comment.