Skip to content

Commit f54acaf

Browse files
Change arguments from value to reference
1 parent 558a652 commit f54acaf

File tree

3 files changed

+25
-25
lines changed

3 files changed

+25
-25
lines changed

examples/practice2_k_range_affine_range_sum.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Monoid for Sum {
1010
(0.into(), 0)
1111
}
1212

13-
fn binary_operation((a, n): Self::S, (b, m): Self::S) -> Self::S {
13+
fn binary_operation(&(a, n): &Self::S, &(b, m): &Self::S) -> Self::S {
1414
(a + b, n + m)
1515
}
1616
}
@@ -23,12 +23,12 @@ impl MapMonoid for Affine {
2323
(1.into(), 0.into())
2424
}
2525

26-
fn mapping((a, b): Self::F, (x, n): <Self::M as Monoid>::S) -> <Self::M as Monoid>::S {
26+
fn mapping(&(a, b): &Self::F, &(x, n): &<Self::M as Monoid>::S) -> <Self::M as Monoid>::S {
2727
(a * x + b * Mint::new(n), n)
2828
}
2929

3030
// a(cx + d) + b = (ac)x + (ad+b)
31-
fn composition((a, b): Self::F, (c, d): Self::F) -> Self::F {
31+
fn composition(&(a, b): &Self::F, &(c, d): &Self::F) -> Self::F {
3232
(a * c, a * d + b)
3333
}
3434
}

examples/practice2_l_lazy_segment_tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ impl Monoid for M {
99
fn identity() -> Self::S {
1010
(0, 0, 0)
1111
}
12-
fn binary_operation((a, b, c): Self::S, (d, e, f): Self::S) -> Self::S {
12+
fn binary_operation(&(a, b, c): &Self::S, &(d, e, f): &Self::S) -> Self::S {
1313
(a + d, b + e, c + f + b * d)
1414
}
1515
}
@@ -21,7 +21,7 @@ impl MapMonoid for F {
2121
fn identity_map() -> Self::F {
2222
false
2323
}
24-
fn mapping(f: Self::F, (a, b, c): <M as Monoid>::S) -> <M as Monoid>::S {
24+
fn mapping(&f: &Self::F, &(a, b, c): &<M as Monoid>::S) -> <M as Monoid>::S {
2525
if f {
2626
// (a + b) * (a + b - 1) / 2 - a * (a - 1) / 2 - b * (b - 1) / 2 - c
2727
// = a * b - c
@@ -30,7 +30,7 @@ impl MapMonoid for F {
3030
(a, b, c)
3131
}
3232
}
33-
fn composition(f: Self::F, g: Self::F) -> Self::F {
33+
fn composition(&f: &Self::F, &g: &Self::F) -> Self::F {
3434
f ^ g
3535
}
3636
}

src/lazysegtree.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ pub trait MapMonoid {
99
Self::M::identity()
1010
}
1111
fn binary_operation(
12-
a: <Self::M as Monoid>::S,
13-
b: <Self::M as Monoid>::S,
12+
a: &<Self::M as Monoid>::S,
13+
b: &<Self::M as Monoid>::S,
1414
) -> <Self::M as Monoid>::S {
1515
Self::M::binary_operation(a, b)
1616
}
1717
fn identity_map() -> Self::F;
18-
fn mapping(f: Self::F, x: <Self::M as Monoid>::S) -> <Self::M as Monoid>::S;
19-
fn composition(f: Self::F, g: Self::F) -> Self::F;
18+
fn mapping(f: &Self::F, x: &<Self::M as Monoid>::S) -> <Self::M as Monoid>::S;
19+
fn composition(f: &Self::F, g: &Self::F) -> Self::F;
2020
}
2121

2222
impl<F: MapMonoid> Default for LazySegtree<F> {
@@ -95,18 +95,18 @@ impl<F: MapMonoid> LazySegtree<F> {
9595
let mut smr = F::identity_element();
9696
while l < r {
9797
if l & 1 != 0 {
98-
sml = F::binary_operation(sml, self.d[l].clone());
98+
sml = F::binary_operation(&sml, &self.d[l]);
9999
l += 1;
100100
}
101101
if r & 1 != 0 {
102102
r -= 1;
103-
smr = F::binary_operation(self.d[r].clone(), smr);
103+
smr = F::binary_operation(&self.d[r], &smr);
104104
}
105105
l >>= 1;
106106
r >>= 1;
107107
}
108108

109-
F::binary_operation(sml, smr)
109+
F::binary_operation(&sml, &smr)
110110
}
111111

112112
pub fn all_prod(&self) -> <F::M as Monoid>::S {
@@ -119,7 +119,7 @@ impl<F: MapMonoid> LazySegtree<F> {
119119
for i in (1..=self.log).rev() {
120120
self.push(p >> i);
121121
}
122-
self.d[p] = F::mapping(f, self.d[p].clone());
122+
self.d[p] = F::mapping(&f, &self.d[p]);
123123
for i in 1..=self.log {
124124
self.update(p >> i);
125125
}
@@ -190,19 +190,19 @@ impl<F: MapMonoid> LazySegtree<F> {
190190
while l % 2 == 0 {
191191
l >>= 1;
192192
}
193-
if !g(F::binary_operation(sm.clone(), self.d[l].clone())) {
193+
if !g(F::binary_operation(&sm, &self.d[l])) {
194194
while l < self.size {
195195
self.push(l);
196196
l *= 2;
197-
let res = F::binary_operation(sm.clone(), self.d[l].clone());
197+
let res = F::binary_operation(&sm, &self.d[l]);
198198
if g(res.clone()) {
199199
sm = res;
200200
l += 1;
201201
}
202202
}
203203
return l - self.size;
204204
}
205-
sm = F::binary_operation(sm, self.d[l].clone());
205+
sm = F::binary_operation(&sm, &self.d[l]);
206206
l += 1;
207207
//while
208208
{
@@ -233,19 +233,19 @@ impl<F: MapMonoid> LazySegtree<F> {
233233
while r > 1 && r % 2 != 0 {
234234
r >>= 1;
235235
}
236-
if !g(F::binary_operation(self.d[r].clone(), sm.clone())) {
236+
if !g(F::binary_operation(&self.d[r], &sm)) {
237237
while r < self.size {
238238
self.push(r);
239239
r = 2 * r + 1;
240-
let res = F::binary_operation(self.d[r].clone(), sm.clone());
240+
let res = F::binary_operation(&self.d[r], &sm);
241241
if g(res.clone()) {
242242
sm = res;
243243
r -= 1;
244244
}
245245
}
246246
return r + 1 - self.size;
247247
}
248-
sm = F::binary_operation(self.d[r].clone(), sm);
248+
sm = F::binary_operation(&self.d[r], &sm);
249249
// while
250250
{
251251
let r = r as isize;
@@ -271,12 +271,12 @@ where
271271
F: MapMonoid,
272272
{
273273
fn update(&mut self, k: usize) {
274-
self.d[k] = F::binary_operation(self.d[2 * k].clone(), self.d[2 * k + 1].clone());
274+
self.d[k] = F::binary_operation(&self.d[2 * k], &self.d[2 * k + 1]);
275275
}
276276
fn all_apply(&mut self, k: usize, f: F::F) {
277-
self.d[k] = F::mapping(f.clone(), self.d[k].clone());
277+
self.d[k] = F::mapping(&f, &self.d[k]);
278278
if k < self.size {
279-
self.lz[k] = F::composition(f, self.lz[k].clone());
279+
self.lz[k] = F::composition(&f, &self.lz[k]);
280280
}
281281
}
282282
fn push(&mut self, k: usize) {
@@ -325,11 +325,11 @@ mod tests {
325325
0
326326
}
327327

328-
fn mapping(f: i32, x: i32) -> i32 {
328+
fn mapping(&f: &i32, &x: &i32) -> i32 {
329329
f + x
330330
}
331331

332-
fn composition(f: i32, g: i32) -> i32 {
332+
fn composition(&f: &i32, &g: &i32) -> i32 {
333333
f + g
334334
}
335335
}

0 commit comments

Comments
 (0)