Skip to content

Commit 7c9c42b

Browse files
committed
separately declare coefficients of ExtendedGcd
1 parent 1c21bc8 commit 7c9c42b

2 files changed

Lines changed: 16 additions & 19 deletions

File tree

build.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ fn main() {
88
} else if env::var_os("CARGO_FEATURE_I128").is_some() {
99
panic!("i128 support was not detected!");
1010
}
11-
12-
if probe("#[derive(Clone)] struct A; fn main() { let _ = [A; 2].clone(); }") {
13-
println!("cargo:rustc-cfg=array_clone");
14-
}
1511
}
1612

1713
/// Test if a code snippet can be compiled

src/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
125125
/// # use num_integer::{ExtendedGcd, Integer};
126126
/// # use num_traits::NumAssign;
127127
/// fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
128-
/// let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
129-
/// gcd == coeffs[0] * a + coeffs[1] * b
128+
/// let ExtendedGcd { gcd, x, y, .. } = a.extended_gcd(&b);
129+
/// gcd == x * a + y * b
130130
/// }
131131
/// assert!(check(10isize, 4isize));
132132
/// assert!(check(8isize, 9isize));
@@ -155,13 +155,15 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
155155
if r.1 >= Self::zero() {
156156
ExtendedGcd {
157157
gcd: r.1,
158-
coeffs: [s.1, t.1],
158+
x: s.1,
159+
y: t.1,
159160
_hidden: (),
160161
}
161162
} else {
162163
ExtendedGcd {
163164
gcd: Self::zero() - r.1,
164-
coeffs: [Self::zero() - s.1, Self::zero() - t.1],
165+
x: Self::zero() - s.1,
166+
y: Self::zero() - t.1,
165167
_hidden: (),
166168
}
167169
}
@@ -254,20 +256,19 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
254256
}
255257

256258
/// Greatest common divisor and Bézout coefficients
257-
#[derive(Debug, Copy, PartialEq, Eq)]
258-
#[cfg_attr(array_clone, derive(Clone))]
259+
///
260+
/// ```no_build
261+
/// let e = isize::extended_gcd(a, b);
262+
/// assert_eq!(e.gcd, e.x*a + e.y*b);
263+
/// ```
264+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
259265
pub struct ExtendedGcd<A> {
260266
pub gcd: A,
261-
pub coeffs: [A; 2],
267+
pub x: A,
268+
pub y: A,
262269
_hidden: (),
263270
}
264271

265-
#[cfg(not(array_clone))]
266-
impl<A: Copy> Clone for ExtendedGcd<A> {
267-
#[inline]
268-
fn clone(&self) -> Self { *self }
269-
}
270-
271272
/// Simultaneous integer division and modulus
272273
#[inline]
273274
pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) {
@@ -630,8 +631,8 @@ macro_rules! impl_integer_for_isize {
630631
use ExtendedGcd;
631632

632633
fn check<A: Copy + Integer + NumAssign>(a: A, b: A) -> bool {
633-
let ExtendedGcd { gcd, coeffs, .. } = a.extended_gcd(&b);
634-
gcd == coeffs[0] * a + coeffs[1] * b
634+
let ExtendedGcd { gcd, x, y, .. } = a.extended_gcd(&b);
635+
gcd == x * a + y * b
635636
}
636637

637638
use core::iter::once;

0 commit comments

Comments
 (0)