@@ -120,12 +120,13 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
120
120
/// # Examples
121
121
///
122
122
/// ~~~
123
+ /// # extern crate num_integer;
123
124
/// # extern crate num_traits;
124
125
/// # use num_integer::{ExtendedGcd, Integer};
125
126
/// # use num_traits::NumAssign;
126
- /// fn check<A: Clone + Integer + NumAssign>(a: A, b: A) -> bool {
127
- /// let ExtendedGcd { gcd, coeffs: [x, y] , .. } = a.extended_gcd(&b);
128
- /// gcd == x * a + y * b
127
+ /// 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
129
130
/// }
130
131
/// assert!(check(10isize, 4isize));
131
132
/// assert!(check(8isize, 9isize));
@@ -239,13 +240,20 @@ pub trait Integer: Sized + Num + PartialOrd + Ord + Eq {
239
240
}
240
241
241
242
/// Greatest common divisor and Bézout coefficients
242
- #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
243
+ #[ derive( Debug , Copy , PartialEq , Eq ) ]
244
+ #[ cfg_attr( array_clone, derive( Clone ) ) ]
243
245
pub struct ExtendedGcd < A > {
244
246
pub gcd : A ,
245
247
pub coeffs : [ A ; 2 ] ,
246
248
_hidden : ( ) ,
247
249
}
248
250
251
+ #[ cfg( not( array_clone) ) ]
252
+ impl < A : Copy > Clone for ExtendedGcd < A > {
253
+ #[ inline]
254
+ fn clone ( & self ) -> Self { * self }
255
+ }
256
+
249
257
/// Simultaneous integer division and modulus
250
258
#[ inline]
251
259
pub fn div_rem < T : Integer > ( x : T , y : T ) -> ( T , T ) {
@@ -577,9 +585,8 @@ macro_rules! impl_integer_for_isize {
577
585
#[ test]
578
586
fn test_gcd_lcm( ) {
579
587
use core:: iter:: once;
580
- let r = once( 0 ) . chain( ( 1 ..) . take( 127 ) . flat_map( |a| once( a) . chain( once( -a) ) ) ) . chain( once( -128 ) ) ;
581
- for i in r. clone( ) {
582
- for j in r. clone( ) {
588
+ for i in once( 0 ) . chain( ( 1 ..) . take( 127 ) . flat_map( |a| once( a) . chain( once( -a) ) ) ) . chain( once( -128 ) ) {
589
+ for j in once( 0 ) . chain( ( 1 ..) . take( 127 ) . flat_map( |a| once( a) . chain( once( -a) ) ) ) . chain( once( -128 ) ) {
583
590
assert_eq!( i. gcd_lcm( & j) , ( i. gcd( & j) , i. lcm( & j) ) ) ;
584
591
}
585
592
}
@@ -590,15 +597,14 @@ macro_rules! impl_integer_for_isize {
590
597
use ExtendedGcd ;
591
598
use traits:: NumAssign ;
592
599
593
- fn check<A : Clone + Integer + NumAssign >( a: A , b: A ) -> bool {
594
- let ExtendedGcd { gcd, coeffs: [ x , y ] , .. } = a. extended_gcd( & b) ;
595
- gcd == x * a + y * b
600
+ fn check<A : Copy + Integer + NumAssign >( a: A , b: A ) -> bool {
601
+ let ExtendedGcd { gcd, coeffs, .. } = a. extended_gcd( & b) ;
602
+ gcd == coeffs [ 0 ] * a + coeffs [ 1 ] * b
596
603
}
597
604
598
605
use core:: iter:: once;
599
- let r = once( 0 ) . chain( ( 1 ..) . take( 127 ) . flat_map( |a| once( a) . chain( once( -a) ) ) ) . chain( once( -128 ) ) ;
600
- for i in r. clone( ) {
601
- for j in r. clone( ) {
606
+ for i in once( 0 ) . chain( ( 1 ..) . take( 127 ) . flat_map( |a| once( a) . chain( once( -a) ) ) ) . chain( once( -128 ) ) {
607
+ for j in once( 0 ) . chain( ( 1 ..) . take( 127 ) . flat_map( |a| once( a) . chain( once( -a) ) ) ) . chain( once( -128 ) ) {
602
608
check( i, j) ;
603
609
let ( ExtendedGcd { gcd, .. } , lcm) = i. extended_gcd_lcm( & j) ;
604
610
assert_eq!( ( gcd, lcm) , ( i. gcd( & j) , i. lcm( & j) ) ) ;
0 commit comments