Skip to content

Commit f89453f

Browse files
committed
segtree: monoid BitOrOper/BitAndOper/BitXorOper
1 parent 0b92413 commit f89453f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ pub use modint::{
2929
ModInt1000000007, ModInt998244353, Modulus, RemEuclidU32, StaticModInt,
3030
};
3131
pub use scc::SccGraph;
32-
pub use segtree::{Additive, Max, Min, Monoid, Multiplicative, Segtree};
32+
pub use segtree::{
33+
Additive, BitAndOper, BitOrOper, BitXorOper, Max, Min, Monoid, Multiplicative, Segtree,
34+
};
3335
pub use string::{
3436
lcp_array, lcp_array_arbitrary, suffix_array, suffix_array_arbitrary, suffix_array_manual,
3537
z_algorithm, z_algorithm_arbitrary,

src/segtree.rs

+43-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::internal_type_traits::{BoundedAbove, BoundedBelow, One, Zero};
33
use std::cmp::{max, min};
44
use std::convert::Infallible;
55
use std::marker::PhantomData;
6-
use std::ops::{Add, Mul};
6+
use std::ops::{Add, BitAnd, BitOr, BitXor, Mul, Not};
77

88
// TODO Should I split monoid-related traits to another module?
99
pub trait Monoid {
@@ -68,6 +68,48 @@ where
6868
}
6969
}
7070

71+
pub struct BitOrOper<S>(Infallible, PhantomData<fn() -> S>);
72+
impl<S> Monoid for BitOrOper<S>
73+
where
74+
S: Copy + BitOr<Output = S> + Default,
75+
{
76+
type S = S;
77+
fn identity() -> Self::S {
78+
S::default()
79+
}
80+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
81+
*a | *b
82+
}
83+
}
84+
85+
pub struct BitAndOper<S>(Infallible, PhantomData<fn() -> S>);
86+
impl<S> Monoid for BitAndOper<S>
87+
where
88+
S: Copy + BitAnd<Output = S> + Not<Output = S> + Default,
89+
{
90+
type S = S;
91+
fn identity() -> Self::S {
92+
!S::default()
93+
}
94+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
95+
*a & *b
96+
}
97+
}
98+
99+
pub struct BitXorOper<S>(Infallible, PhantomData<fn() -> S>);
100+
impl<S> Monoid for BitXorOper<S>
101+
where
102+
S: Copy + BitXor<Output = S> + Default,
103+
{
104+
type S = S;
105+
fn identity() -> Self::S {
106+
S::default()
107+
}
108+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
109+
*a ^ *b
110+
}
111+
}
112+
71113
impl<M: Monoid> Default for Segtree<M> {
72114
fn default() -> Self {
73115
Segtree::new(0)

0 commit comments

Comments
 (0)