I would like to write code for unsigned integer (and later maybe signed integer) no matter their type. So the way to go is to use generic types and implementing the super trait “Unsigned”. But I have a problem with BigUint.
Here is a basic code for an extended euclid algorithm.
use num::Unsigned;
use num_bigint::BigUint;
fn general_extended_euclid<U>(a:U,b:U)->(U,U,U)
where
U: Unsigned + PartialOrd +Copy,
{
let mut q:U;
let mut r_temp:U;
let mut u_temp:U;
let mut v_temp:U;
let mut u_1:U=U::one();
let mut v_1:U=U::zero();
let mut u_2:U=U::zero();
let mut v_2:U=U::one();
let mut r_1:U=a;
let mut r_2:U=b;
let mut n:U=U::zero();
let two:U= U::one()+U::one();
while r_2 !=U::zero(){
q=r_1/r_2;
r_temp = r_1;
u_temp = u_1;
v_temp = v_1;
r_1=r_2;
u_1=u_2;
v_1=v_2;
if r_temp>=q*r_2{
r_2=r_temp-q*r_2;
}else{
r_2=q*r_2-r_temp;
}
u_2=u_temp+q*u_2;
v_2=v_temp+q*v_2;
n=n+U::one();
}
if n%two== U::one(){
u_1=b-u_1;
}else{
v_1=a-v_1
}
return (r_1,u_1,v_1);
}
fn main()
{
let a_1=345_u32;
let b_1=37_u32;
let a_2=345_u64;
let b_2=37_u64;
let res_1 = general_extended_euclid(a_1,b_1);
println!("r_u32:{}",res_1.0);
println!("u_u32:{}",res_1.1);
println!("v_u32:{}",res_1.2);
let res_2 = general_extended_euclid(a_2,b_2);
println!("r_u64:{}",res_2.0);
println!("u_u64:{}",res_2.1);
println!("v_u64:{}",res_2.2);
/*
let a_3:BigUint=BigUint::from_slice(&[a_1]);
let b_3:BigUint=BigUint::from_slice(&[b_1]);
let res_3 = general_extended_euclid(a_3,b_3);
println!("r_ubint:{}",res_3.0);
println!("u_ubint:{}",res_3.1);
println!("v_ubint:{}",res_3.2);
}
It works fine for any usual unsigned integer, but there is a problem with BigUint because it doesn’t “bind/bound?” to the copy trait. So I can not assign using a mere =
.
What is the solution for doing a generic code including BigUint ? Should I implement Copy for BigUint? Is it the only solution? Apparently the Copy bound is accepted for any type implementing copy, so each time you use a bound you need to be sure that all the types you will use accept that bound. Ok, but I would definitely like that the Copy bound is implemented for all the Unsigned types implementing Unsigned. Apparently there are ways to implement traits on traits but I am absolutely not confident enough to do that and not sure of the result…
I think I can do .clone() for each assignment and remove the copy binding, but is it normal practice?
I know the code would be more concise if I was using tuple.
I did not try much more than just copying.