Skip to content

Commit 558a652

Browse files
Merge branch 'feature/segtree' into feature/lazysegtree
2 parents 4c4516f + 32dbc68 commit 558a652

File tree

2 files changed

+89
-20
lines changed

2 files changed

+89
-20
lines changed

src/internal_type_traits.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,37 +52,65 @@ pub trait Integral:
5252
+ fmt::Debug
5353
+ fmt::Binary
5454
+ fmt::Octal
55+
+ Zero
56+
+ One
57+
+ BoundedBelow
58+
+ BoundedAbove
5559
{
60+
}
61+
62+
/// Class that has additive identity element
63+
pub trait Zero {
64+
/// The additive identity element
5665
fn zero() -> Self;
66+
}
67+
68+
/// Class that has multiplicative identity element
69+
pub trait One {
70+
/// The multiplicative identity element
5771
fn one() -> Self;
72+
}
73+
74+
pub trait BoundedBelow {
5875
fn min_value() -> Self;
76+
}
77+
78+
pub trait BoundedAbove {
5979
fn max_value() -> Self;
6080
}
6181

6282
macro_rules! impl_integral {
6383
($($ty:ty),*) => {
6484
$(
65-
impl Integral for $ty {
85+
impl Zero for $ty {
6686
#[inline]
6787
fn zero() -> Self {
6888
0
6989
}
90+
}
7091

92+
impl One for $ty {
7193
#[inline]
7294
fn one() -> Self {
7395
1
7496
}
97+
}
7598

99+
impl BoundedBelow for $ty {
76100
#[inline]
77101
fn min_value() -> Self {
78102
Self::min_value()
79103
}
104+
}
80105

106+
impl BoundedAbove for $ty {
81107
#[inline]
82108
fn max_value() -> Self {
83109
Self::max_value()
84110
}
85111
}
112+
113+
impl Integral for $ty {}
86114
)*
87115
};
88116
}

src/segtree.rs

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,70 @@
11
use crate::internal_bit::ceil_pow2;
2-
use crate::internal_type_traits::Integral;
3-
use std::cmp::max;
2+
use crate::internal_type_traits::{BoundedAbove, BoundedBelow, One, Zero};
3+
use std::cmp::{max, min};
4+
use std::convert::Infallible;
45
use std::marker::PhantomData;
6+
use std::ops::{Add, Mul};
57

68
// TODO Should I split monoid-related traits to another module?
79
pub trait Monoid {
810
type S: Clone;
911
fn identity() -> Self::S;
10-
fn binary_operation(a: Self::S, b: Self::S) -> Self::S;
12+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S;
1113
}
1214

13-
pub struct Max<S>(PhantomData<fn() -> S>);
14-
// TODO We should not restrict to integral
15+
pub struct Max<S>(Infallible, PhantomData<fn() -> S>);
1516
impl<S> Monoid for Max<S>
1617
where
17-
S: Integral,
18+
S: Copy + Ord + BoundedBelow,
1819
{
1920
type S = S;
20-
2121
fn identity() -> Self::S {
2222
S::min_value()
2323
}
24+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
25+
max(*a, *b)
26+
}
27+
}
28+
29+
pub struct Min<S>(Infallible, PhantomData<fn() -> S>);
30+
impl<S> Monoid for Min<S>
31+
where
32+
S: Copy + Ord + BoundedAbove,
33+
{
34+
type S = S;
35+
fn identity() -> Self::S {
36+
S::max_value()
37+
}
38+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
39+
min(*a, *b)
40+
}
41+
}
42+
43+
pub struct Sum<S>(Infallible, PhantomData<fn() -> S>);
44+
impl<S> Monoid for Sum<S>
45+
where
46+
S: Copy + Add<Output = S> + Zero,
47+
{
48+
type S = S;
49+
fn identity() -> Self::S {
50+
S::zero()
51+
}
52+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
53+
*a + *b
54+
}
55+
}
2456

25-
fn binary_operation(a: Self::S, b: Self::S) -> Self::S {
26-
max(a, b)
57+
pub struct Product<S>(Infallible, PhantomData<fn() -> S>);
58+
impl<S> Monoid for Product<S>
59+
where
60+
S: Copy + Mul<Output = S> + One,
61+
{
62+
type S = S;
63+
fn identity() -> Self::S {
64+
S::one()
65+
}
66+
fn binary_operation(a: &Self::S, b: &Self::S) -> Self::S {
67+
*a * *b
2768
}
2869
}
2970

@@ -75,18 +116,18 @@ impl<M: Monoid> Segtree<M> {
75116

76117
while l < r {
77118
if l & 1 != 0 {
78-
sml = M::binary_operation(sml, self.d[l].clone());
119+
sml = M::binary_operation(&sml, &self.d[l]);
79120
l += 1;
80121
}
81122
if r & 1 != 0 {
82123
r -= 1;
83-
smr = M::binary_operation(self.d[r].clone(), smr);
124+
smr = M::binary_operation(&self.d[r], &smr);
84125
}
85126
l >>= 1;
86127
r >>= 1;
87128
}
88129

89-
M::binary_operation(sml, smr)
130+
M::binary_operation(&sml, &smr)
90131
}
91132

92133
pub fn all_prod(&self) -> M::S {
@@ -109,18 +150,18 @@ impl<M: Monoid> Segtree<M> {
109150
while l % 2 == 0 {
110151
l >>= 1;
111152
}
112-
if !f(M::binary_operation(sm.clone(), self.d[l].clone())) {
153+
if !f(M::binary_operation(&sm, &self.d[l])) {
113154
while l < self.size {
114155
l *= 2;
115-
let res = M::binary_operation(sm.clone(), self.d[l].clone());
156+
let res = M::binary_operation(&sm, &self.d[l]);
116157
if f(res.clone()) {
117158
sm = res;
118159
l += 1;
119160
}
120161
}
121162
return l - self.size;
122163
}
123-
sm = M::binary_operation(sm.clone(), self.d[l].clone());
164+
sm = M::binary_operation(&sm, &self.d[l]);
124165
l += 1;
125166
// while
126167
{
@@ -148,18 +189,18 @@ impl<M: Monoid> Segtree<M> {
148189
while r > 1 && r % 2 == 1 {
149190
r >>= 1;
150191
}
151-
if !f(M::binary_operation(self.d[r].clone(), sm.clone())) {
192+
if !f(M::binary_operation(&self.d[r], &sm)) {
152193
while r < self.size {
153194
r = 2 * r + 1;
154-
let res = M::binary_operation(self.d[r].clone(), sm.clone());
195+
let res = M::binary_operation(&self.d[r], &sm);
155196
if f(res.clone()) {
156197
sm = res;
157198
r -= 1;
158199
}
159200
}
160201
return r + 1 - self.size;
161202
}
162-
sm = M::binary_operation(self.d[r].clone(), sm.clone());
203+
sm = M::binary_operation(&self.d[r], &sm);
163204
// while
164205
{
165206
let r = r as isize;
@@ -170,7 +211,7 @@ impl<M: Monoid> Segtree<M> {
170211
}
171212

172213
fn update(&mut self, k: usize) {
173-
self.d[k] = M::binary_operation(self.d[2 * k].clone(), self.d[2 * k + 1].clone());
214+
self.d[k] = M::binary_operation(&self.d[2 * k], &self.d[2 * k + 1]);
174215
}
175216
}
176217

0 commit comments

Comments
 (0)