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