Skip to content

Commit a5db245

Browse files
authored
Merge pull request #29 from jlapeyre/fewer-clone-2
Rewrite methods to allow removing clone
2 parents 4e559ef + 14e4b6b commit a5db245

File tree

6 files changed

+110
-104
lines changed

6 files changed

+110
-104
lines changed

src/diophantine.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ where
488488
ZOmega::from_int(p.clone()),
489489
);
490490
let t_conj = t.conj();
491-
if t_conj.clone() * t.clone() == p.clone() || t_conj * &t == -p.clone() {
491+
if t_conj * &t == p || t_conj * &t == -p {
492492
return Ok(Some(t));
493493
}
494494
}
@@ -500,7 +500,7 @@ where
500500
ZOmega::from_int(p.clone()),
501501
);
502502
let t_conj = t.conj();
503-
if t_conj.clone() * t.clone() == p.clone() || t_conj * &t == -p.clone() {
503+
if t_conj * &t == p || t_conj * &t == -p {
504504
return Ok(Some(t));
505505
}
506506
}
@@ -573,7 +573,7 @@ fn adj_decompose_int(
573573
}
574574
}
575575
Ok(Some(t_p)) => {
576-
t = t * t_p;
576+
t = &t * &t_p;
577577
}
578578
Err(e) => {
579579
return Err(e);
@@ -592,15 +592,15 @@ fn adj_decompose_selfassociate(
592592
return Ok(Some(ZOmega::from_int(IBig::ZERO)));
593593
}
594594
let n: IBig = (&xi.a).gcd(&xi.b).into();
595-
let r = xi / n.clone();
595+
let r = &xi / n.clone();
596596
let t1 = adj_decompose_int(n, start_time, diophantine_data);
597597
let t2 = if r % ZRootTwo::new(IBig::ZERO, IBig::ONE) == ZRootTwo::from_int(IBig::ZERO) {
598598
ZOmega::new(IBig::ZERO, IBig::ZERO, IBig::ONE, IBig::ONE)
599599
} else {
600600
ZOmega::from_int(IBig::ONE)
601601
};
602602
match t1 {
603-
Ok(Some(t1_val)) => Ok(Some(t1_val * t2)),
603+
Ok(Some(t1_val)) => Ok(Some(&t1_val * &t2)),
604604
Ok(None) => Ok(None),
605605
Err(e) => Err(e),
606606
}
@@ -627,7 +627,7 @@ pub fn decompose_relatively_zomega_prime(
627627
}
628628
let (a, k_a) = &facs[i];
629629
if ZRootTwo::sim(a.clone(), b.clone()) {
630-
u = u * (b.clone() / a.clone()).pow(&IBig::from(k_b));
630+
u = u * (&b / a).pow(&IBig::from(k_b));
631631
facs[i] = (a.clone(), k_a + k_b);
632632
break;
633633
} else {
@@ -636,12 +636,12 @@ pub fn decompose_relatively_zomega_prime(
636636
i += 1;
637637
continue;
638638
} else {
639-
let partial = vec![(a.clone() / g.clone(), *k_a), (g.clone(), k_a + k_b)];
639+
let partial = vec![(a / &g, *k_a), (g.clone(), k_a + k_b)];
640640
let (u_a, mut facs_a) = decompose_relatively_zomega_prime(partial);
641641
u = u * u_a;
642642
facs[i] = facs_a.remove(0);
643643
facs.append(&mut facs_a);
644-
stack.push((b / g, k_b));
644+
stack.push((&b / &g, k_b));
645645
break;
646646
}
647647
}
@@ -762,7 +762,7 @@ pub fn adj_decompose_selfcoprime(
762762
{
763763
// &mut diophantine_data.rng) {
764764
let fac = ZRootTwo::gcd(xi.clone(), ZRootTwo::from_int(fac_n));
765-
facs.push((eta / fac.clone(), k));
765+
facs.push((&eta / &fac, k));
766766
facs.push((fac, k));
767767
let (_, new_facs) = decompose_relatively_zomega_prime(facs);
768768
facs = new_facs;
@@ -771,7 +771,7 @@ pub fn adj_decompose_selfcoprime(
771771
}
772772
}
773773
Ok(Some(t_eta)) => {
774-
t = t * t_eta;
774+
t = &t * &t_eta;
775775
}
776776
Err(e) => {
777777
return Err(e);
@@ -790,13 +790,13 @@ fn adj_decompose(
790790
return Ok(Some(ZOmega::from_int(IBig::ZERO)));
791791
}
792792
let d = ZRootTwo::gcd(xi.clone(), xi.conj_sq2());
793-
let eta = xi / d.clone();
793+
let eta = &xi / &d;
794794
let t1 = adj_decompose_selfassociate(d, start_time, diophantine_data);
795795
match t1 {
796796
Ok(Some(t1_val)) => {
797797
let t2 = adj_decompose_selfcoprime(eta, start_time, diophantine_data);
798798
match t2 {
799-
Ok(Some(t2_val)) => Ok(Some(t1_val * t2_val)),
799+
Ok(Some(t2_val)) => Ok(Some(&t1_val * &t2_val)),
800800
Ok(None) => Ok(None),
801801
Err(e) => Err(e),
802802
}
@@ -821,9 +821,9 @@ fn diophantine(
821821
match t {
822822
Ok(Some(t)) => {
823823
let xi_associate = ZRootTwo::from_zomega(t.conj() * &t);
824-
let u = xi.clone() / xi_associate;
824+
let u = xi / &xi_associate;
825825
match u.sqrt() {
826-
Some(v) => Ok(Some(ZOmega::from_zroottwo(&v) * t)),
826+
Some(v) => Ok(Some(&ZOmega::from_zroottwo(&v) * &t)),
827827
None => {
828828
warn!("Cannot find square root of u");
829829
Ok(None)
@@ -903,7 +903,7 @@ pub(crate) fn diophantine_dyadic(
903903
Ok(None) => None,
904904
Ok(Some(mut t)) => {
905905
if k_mod_2 == 1 {
906-
t = t * ZOmega::new(IBig::ZERO, IBig::NEG_ONE, IBig::ONE, IBig::ZERO);
906+
t = &t * &ZOmega::new(IBig::ZERO, IBig::NEG_ONE, IBig::ONE, IBig::ZERO);
907907
}
908908
Some(DOmega::new(t, k_div_2 + k_mod_2))
909909
}

src/odgp.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ pub fn solve_odgp(i: Interval, j: Interval) -> impl Iterator<Item = ZRootTwo> {
4040
}
4141

4242
fn solve_odgp_internal(i: Interval, j: Interval) -> Box<dyn Iterator<Item = ZRootTwo>> {
43-
if i.width() < ib_to_bf_prec(IBig::ZERO) || j.width() < ib_to_bf_prec(IBig::ZERO) {
43+
let bfzero = ib_to_bf_prec(IBig::ZERO);
44+
if i.width() < bfzero || j.width() < bfzero {
4445
return Box::new(vec![].into_iter());
45-
} else if i.width() > ib_to_bf_prec(IBig::ZERO) && j.width() <= ib_to_bf_prec(IBig::ZERO) {
46+
} else if i.width() > bfzero && j.width() <= bfzero {
4647
return Box::new(solve_odgp_internal(j, i).map(|beta| beta.conj_sq2()));
4748
}
4849

49-
let n = if j.width() <= ib_to_bf_prec(IBig::ZERO) {
50+
let n = if j.width() <= bfzero {
5051
IBig::ZERO
5152
} else {
5253
floorlog(j.width(), LAMBDA.to_real()).0

src/ring/d_omega.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl DOmega {
221221

222222
let mut new_u = self.u.clone() * (IBig::ONE << d_div_2_usize);
223223
if d_mod_2 == 1 {
224-
new_u = new_u * ZOmega::new(IBig::NEG_ONE, IBig::ZERO, IBig::ONE, IBig::ZERO);
224+
new_u = &new_u * &ZOmega::new(IBig::NEG_ONE, IBig::ZERO, IBig::ONE, IBig::ZERO);
225225
}
226226

227227
Self::new(new_u, self.k)
@@ -286,7 +286,7 @@ impl Add<DOmega> for DOmega {
286286
let k = self.k;
287287
self + rhs.renew_denomexp(k)
288288
}
289-
std::cmp::Ordering::Equal => Self::new(self.u + rhs.u, self.k),
289+
std::cmp::Ordering::Equal => Self::new(&self.u + &rhs.u, self.k),
290290
}
291291
}
292292
}
@@ -355,7 +355,7 @@ impl Mul<IBig> for DOmega {
355355
impl Mul<DOmega> for DOmega {
356356
type Output = Self;
357357
fn mul(self, rhs: Self) -> Self {
358-
Self::new(self.u * rhs.u, self.k + rhs.k)
358+
Self::new(&self.u * &rhs.u, self.k + rhs.k)
359359
}
360360
}
361361

src/ring/z_omega.rs

Lines changed: 62 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,15 @@ impl ZOmega {
4343
}
4444
}
4545

46-
pub fn coef(&self) -> [IBig; 4] {
47-
self.coef
48-
.get_or_init(|| {
49-
[
50-
self.d.clone(),
51-
self.c.clone(),
52-
self.b.clone(),
53-
self.a.clone(),
54-
]
55-
})
56-
.clone()
46+
pub fn coef(&self) -> &[IBig; 4] {
47+
self.coef.get_or_init(|| {
48+
[
49+
self.d.clone(),
50+
self.c.clone(),
51+
self.b.clone(),
52+
self.a.clone(),
53+
]
54+
})
5755
}
5856

5957
pub fn conj(&self) -> &Self {
@@ -93,8 +91,9 @@ impl ZOmega {
9391

9492
pub fn norm(&self) -> &IBig {
9593
self.norm_cache.get_or_init(|| {
96-
let s1 = &self.a * &self.a + &self.b * &self.b + &self.c * &self.c + &self.d * &self.d;
97-
let s2 = &self.a * &self.b + &self.b * &self.c + &self.c * &self.d - &self.d * &self.a;
94+
let (a, b, c, d) = (&self.a, &self.b, &self.c, &self.d);
95+
let s1 = a * a + b * b + c * c + d * d;
96+
let s2 = a * b + b * c + c * d - d * a;
9897
&s1 * &s1 - 2 * &s2 * &s2
9998
})
10099
}
@@ -155,15 +154,15 @@ impl ZOmega {
155154
pub fn divmod(&self, other: &Self) -> (Self, Self) {
156155
let p0 = self * other.conj();
157156
let p1 = other.conj().conj_sq2() * other.conj_sq2();
158-
let p = p0 * p1;
157+
let p = &p0 * &p1;
159158
let k = other.norm();
160159
let q = ZOmega::new(
161160
rounddiv(&p.a, k),
162161
rounddiv(&p.b, k),
163162
rounddiv(&p.c, k),
164163
rounddiv(&p.d, k),
165164
);
166-
let r = self.clone() - other.clone() * q.clone();
165+
let r = self - &(other * &q);
167166
(q, r)
168167
}
169168

@@ -218,43 +217,48 @@ impl ZOmega {
218217
}
219218
}
220219

221-
impl Add for ZOmega {
222-
type Output = Self;
223-
fn add(self, rhs: Self) -> Self {
224-
Self::new(
225-
self.a + rhs.a,
226-
self.b + rhs.b,
227-
self.c + rhs.c,
228-
self.d + rhs.d,
220+
// impl Add for ZOmega {
221+
// type Output = Self;
222+
// fn add(self, rhs: Self) -> Self {
223+
// Self::new(
224+
// self.a + rhs.a,
225+
// self.b + rhs.b,
226+
// self.c + rhs.c,
227+
// self.d + rhs.d,
228+
// )
229+
// }
230+
// }
231+
232+
impl Add for &ZOmega {
233+
type Output = ZOmega;
234+
fn add(self, rhs: Self) -> ZOmega {
235+
ZOmega::new(
236+
&self.a + &rhs.a,
237+
&self.b + &rhs.b,
238+
&self.c + &rhs.c,
239+
&self.d + &rhs.d,
229240
)
230241
}
231242
}
232243

233-
impl Add<IBig> for ZOmega {
234-
type Output = Self;
235-
fn add(self, rhs: IBig) -> Self {
236-
self + Self::from_int(rhs)
237-
}
238-
}
244+
// impl Add<&ZOmega> for &IBig {
245+
// type Output = ZOmega;
246+
// fn add(self, rhs: &ZOmega) -> ZOmega {
247+
// &ZOmega::from_int(self) + &rhs
248+
// }
249+
// }
239250

240251
impl Add<ZOmega> for IBig {
241252
type Output = ZOmega;
242253
fn add(self, rhs: ZOmega) -> ZOmega {
243-
ZOmega::from_int(self) + rhs
254+
&ZOmega::from_int(self) + &rhs
244255
}
245256
}
246257

247-
impl Sub for ZOmega {
248-
type Output = Self;
249-
fn sub(self, rhs: Self) -> Self {
250-
self + (-rhs)
251-
}
252-
}
253-
254-
impl<'b> Sub<&'b ZOmega> for &ZOmega {
258+
impl Sub<&ZOmega> for &ZOmega {
255259
type Output = ZOmega;
256260

257-
fn sub(self, rhs: &'b ZOmega) -> ZOmega {
261+
fn sub(self, rhs: &ZOmega) -> ZOmega {
258262
ZOmega::new(
259263
&self.a - &rhs.a,
260264
&self.b - &rhs.b,
@@ -264,19 +268,21 @@ impl<'b> Sub<&'b ZOmega> for &ZOmega {
264268
}
265269
}
266270

267-
impl Sub<IBig> for ZOmega {
268-
type Output = Self;
269-
fn sub(self, rhs: IBig) -> Self {
270-
self - Self::from_int(rhs)
271-
}
272-
}
273-
274-
impl Sub<ZOmega> for IBig {
275-
type Output = ZOmega;
276-
fn sub(self, rhs: ZOmega) -> ZOmega {
277-
ZOmega::from_int(self) - rhs
278-
}
279-
}
271+
// Never used apparently
272+
// impl Sub<IBig> for ZOmega {
273+
// type Output = Self;
274+
// fn sub(self, rhs: IBig) -> Self {
275+
// self - Self::from_int(rhs)
276+
// }
277+
// }
278+
279+
// Never used apparently
280+
// impl Sub<&ZOmega> for IBig {
281+
// type Output = ZOmega;
282+
// fn sub(self, rhs: &ZOmega) -> ZOmega {
283+
// &ZOmega::from_int(self) - rhs
284+
// }
285+
// }
280286

281287
impl Neg for ZOmega {
282288
type Output = Self;
@@ -285,10 +291,10 @@ impl Neg for ZOmega {
285291
}
286292
}
287293

288-
impl Mul for ZOmega {
289-
type Output = Self;
294+
impl Mul<&ZOmega> for &ZOmega {
295+
type Output = ZOmega;
290296

291-
fn mul(self, rhs: Self) -> Self {
297+
fn mul(self, rhs: &ZOmega) -> ZOmega {
292298
let lhs_coef = self.coef();
293299
let rhs_coef = rhs.coef();
294300

@@ -305,7 +311,7 @@ impl Mul for ZOmega {
305311
}
306312
}
307313

308-
Self::new(
314+
ZOmega::new(
309315
std::mem::take(&mut new_coef[3]),
310316
std::mem::take(&mut new_coef[2]),
311317
std::mem::take(&mut new_coef[1]),
@@ -314,14 +320,6 @@ impl Mul for ZOmega {
314320
}
315321
}
316322

317-
impl Mul<&ZOmega> for &ZOmega {
318-
type Output = ZOmega;
319-
320-
fn mul(self, rhs: &ZOmega) -> ZOmega {
321-
self.clone() * rhs.clone()
322-
}
323-
}
324-
325323
impl Mul<IBig> for ZOmega {
326324
type Output = Self;
327325
fn mul(self, rhs: IBig) -> Self {

0 commit comments

Comments
 (0)