Skip to content

Commit d0bd5c3

Browse files
committed
cargo fmt
1 parent c3bf89a commit d0bd5c3

1 file changed

Lines changed: 52 additions & 22 deletions

File tree

src/lib.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
134134
/// ~~~
135135
#[inline]
136136
fn extended_gcd(&self, other: &Self) -> ExtendedGcd<Self>
137-
where Self: Clone + SubAssign<Self> {
137+
where
138+
Self: Clone + SubAssign<Self>, {
138139
let mut s = (Self::zero(), Self::one());
139140
let mut t = (Self::one(), Self::zero());
140141
let mut r = (other.clone(), self.clone());
@@ -150,15 +151,26 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
150151
f(&mut t);
151152
}
152153

153-
if r.1 >= Self::zero() { ExtendedGcd { gcd: r.1, coeffs: [s.1, t.1], _hidden: () } }
154-
else { ExtendedGcd { gcd: Self::zero() - r.1,
155-
coeffs: [Self::zero() - s.1, Self::zero() - t.1], _hidden: () } }
154+
if r.1 >= Self::zero() {
155+
ExtendedGcd {
156+
gcd: r.1,
157+
coeffs: [s.1, t.1],
158+
_hidden: (),
159+
}
160+
} else {
161+
ExtendedGcd {
162+
gcd: Self::zero() - r.1,
163+
coeffs: [Self::zero() - s.1, Self::zero() - t.1],
164+
_hidden: (),
165+
}
166+
}
156167
}
157168

158169
/// Greatest common divisor, least common multiple, and Bézout coefficients.
159170
#[inline]
160171
fn extended_gcd_lcm(&self, other: &Self) -> (ExtendedGcd<Self>, Self)
161-
where Self: Clone + SubAssign<Self> {
172+
where
173+
Self: Clone + SubAssign<Self>, {
162174
(self.extended_gcd(other), self.lcm(other))
163175
}
164176

@@ -387,23 +399,26 @@ macro_rules! impl_integer_for_isize {
387399
fn extended_gcd_lcm(&self, other: &Self) -> (ExtendedGcd<Self>, Self) {
388400
let egcd = self.extended_gcd(other);
389401
// should not have to recalculate abs
390-
let lcm = if egcd.gcd.is_zero() { Self::zero() }
391-
else { (*self * (*other / egcd.gcd)).abs() };
402+
let lcm = if egcd.gcd.is_zero() {
403+
Self::zero()
404+
} else {
405+
(*self * (*other / egcd.gcd)).abs()
406+
};
392407
(egcd, lcm)
393408
}
394409

395410
/// Calculates the Lowest Common Multiple (LCM) of the number and
396411
/// `other`.
397412
#[inline]
398-
fn lcm(&self, other: &Self) -> Self {
399-
self.gcd_lcm(other).1
400-
}
413+
fn lcm(&self, other: &Self) -> Self { self.gcd_lcm(other).1 }
401414

402415
/// Calculates the Greatest Common Divisor (GCD) and
403416
/// Lowest Common Multiple (LCM) of the number and `other`.
404417
#[inline]
405418
fn gcd_lcm(&self, other: &Self) -> (Self, Self) {
406-
if self.is_zero() && other.is_zero() { return (Self::zero(), Self::zero()) }
419+
if self.is_zero() && other.is_zero() {
420+
return (Self::zero(), Self::zero());
421+
}
407422
let gcd = self.gcd(other);
408423
// should not have to recalculate abs
409424
let lcm = (*self * (*other / gcd)).abs();
@@ -595,26 +610,38 @@ macro_rules! impl_integer_for_isize {
595610
#[test]
596611
fn test_gcd_lcm() {
597612
use core::iter::once;
598-
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
599-
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
613+
for i in once(0)
614+
.chain((1..).take(127).flat_map(|a| once(a).chain(once(-a))))
615+
.chain(once(-128))
616+
{
617+
for j in once(0)
618+
.chain((1..).take(127).flat_map(|a| once(a).chain(once(-a))))
619+
.chain(once(-128))
620+
{
600621
assert_eq!(i.gcd_lcm(&j), (i.gcd(&j), i.lcm(&j)));
601622
}
602623
}
603624
}
604625

605626
#[test]
606627
fn test_extended_gcd_lcm() {
607-
use ExtendedGcd;
608628
use traits::NumAssign;
629+
use ExtendedGcd;
609630

610631
fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
611632
let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
612633
gcd == coeffs[0] * a + coeffs[1] * b
613634
}
614635

615636
use core::iter::once;
616-
for i in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
617-
for j in once(0).chain((1..).take(127).flat_map(|a| once(a).chain(once(-a)))).chain(once(-128)) {
637+
for i in once(0)
638+
.chain((1..).take(127).flat_map(|a| once(a).chain(once(-a))))
639+
.chain(once(-128))
640+
{
641+
for j in once(0)
642+
.chain((1..).take(127).flat_map(|a| once(a).chain(once(-a))))
643+
.chain(once(-128))
644+
{
618645
check(i, j);
619646
let (ExtendedGcd { gcd, .. }, lcm) = i.extended_gcd_lcm(&j);
620647
assert_eq!((gcd, lcm), (i.gcd(&j), i.lcm(&j)));
@@ -707,22 +734,25 @@ macro_rules! impl_integer_for_usize {
707734
fn extended_gcd_lcm(&self, other: &Self) -> (ExtendedGcd<Self>, Self) {
708735
let egcd = self.extended_gcd(other);
709736
// should not have to recalculate abs
710-
let lcm = if egcd.gcd.is_zero() { Self::zero() }
711-
else { *self * (*other / egcd.gcd) };
737+
let lcm = if egcd.gcd.is_zero() {
738+
Self::zero()
739+
} else {
740+
*self * (*other / egcd.gcd)
741+
};
712742
(egcd, lcm)
713743
}
714744

715745
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
716746
#[inline]
717-
fn lcm(&self, other: &Self) -> Self {
718-
self.gcd_lcm(other).1
719-
}
747+
fn lcm(&self, other: &Self) -> Self { self.gcd_lcm(other).1 }
720748

721749
/// Calculates the Greatest Common Divisor (GCD) and
722750
/// Lowest Common Multiple (LCM) of the number and `other`.
723751
#[inline]
724752
fn gcd_lcm(&self, other: &Self) -> (Self, Self) {
725-
if self.is_zero() && other.is_zero() { return (Self::zero(), Self::zero()) }
753+
if self.is_zero() && other.is_zero() {
754+
return (Self::zero(), Self::zero());
755+
}
726756
let gcd = self.gcd(other);
727757
let lcm = *self * (*other / gcd);
728758
(gcd, lcm)

0 commit comments

Comments
 (0)