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