Merge pull request #45 from gifnksm/master

update to rust mater
This commit is contained in:
Alex Crichton 2015-01-04 12:57:33 -06:00
commit c1ad386c08
7 changed files with 218 additions and 91 deletions

View File

@ -189,7 +189,9 @@ impl Num for BigUint {}
macro_rules! forward_val_val_binop { macro_rules! forward_val_val_binop {
(impl $imp:ident for $res:ty, $method:ident) => { (impl $imp:ident for $res:ty, $method:ident) => {
impl $imp<$res, $res> for $res { impl $imp<$res> for $res {
type Output = $res;
#[inline] #[inline]
fn $method(self, other: $res) -> $res { fn $method(self, other: $res) -> $res {
(&self).$method(&other) (&self).$method(&other)
@ -200,7 +202,9 @@ macro_rules! forward_val_val_binop {
macro_rules! forward_ref_val_binop { macro_rules! forward_ref_val_binop {
(impl $imp:ident for $res:ty, $method:ident) => { (impl $imp:ident for $res:ty, $method:ident) => {
impl<'a> $imp<$res, $res> for &'a $res { impl<'a> $imp<$res> for &'a $res {
type Output = $res;
#[inline] #[inline]
fn $method(self, other: $res) -> $res { fn $method(self, other: $res) -> $res {
self.$method(&other) self.$method(&other)
@ -211,7 +215,9 @@ macro_rules! forward_ref_val_binop {
macro_rules! forward_val_ref_binop { macro_rules! forward_val_ref_binop {
(impl $imp:ident for $res:ty, $method:ident) => { (impl $imp:ident for $res:ty, $method:ident) => {
impl<'a> $imp<&'a $res, $res> for $res { impl<'a> $imp<&'a $res> for $res {
type Output = $res;
#[inline] #[inline]
fn $method(self, other: &$res) -> $res { fn $method(self, other: &$res) -> $res {
(&self).$method(other) (&self).$method(other)
@ -230,7 +236,9 @@ macro_rules! forward_all_binop {
forward_all_binop!(impl BitAnd for BigUint, bitand); forward_all_binop!(impl BitAnd for BigUint, bitand);
impl<'a, 'b> BitAnd<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> BitAnd<&'b BigUint> for &'a BigUint {
type Output = BigUint;
#[inline] #[inline]
fn bitand(self, other: &BigUint) -> BigUint { fn bitand(self, other: &BigUint) -> BigUint {
BigUint::new(self.data.iter().zip(other.data.iter()).map(|(ai, bi)| *ai & *bi).collect()) BigUint::new(self.data.iter().zip(other.data.iter()).map(|(ai, bi)| *ai & *bi).collect())
@ -239,7 +247,9 @@ impl<'a, 'b> BitAnd<&'b BigUint, BigUint> for &'a BigUint {
forward_all_binop!(impl BitOr for BigUint, bitor); forward_all_binop!(impl BitOr for BigUint, bitor);
impl<'a, 'b> BitOr<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> BitOr<&'b BigUint> for &'a BigUint {
type Output = BigUint;
fn bitor(self, other: &BigUint) -> BigUint { fn bitor(self, other: &BigUint) -> BigUint {
let zeros = ZERO_VEC.iter().cycle(); let zeros = ZERO_VEC.iter().cycle();
let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) };
@ -252,7 +262,9 @@ impl<'a, 'b> BitOr<&'b BigUint, BigUint> for &'a BigUint {
forward_all_binop!(impl BitXor for BigUint, bitxor); forward_all_binop!(impl BitXor for BigUint, bitxor);
impl<'a, 'b> BitXor<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> BitXor<&'b BigUint> for &'a BigUint {
type Output = BigUint;
fn bitxor(self, other: &BigUint) -> BigUint { fn bitxor(self, other: &BigUint) -> BigUint {
let zeros = ZERO_VEC.iter().cycle(); let zeros = ZERO_VEC.iter().cycle();
let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) };
@ -263,12 +275,16 @@ impl<'a, 'b> BitXor<&'b BigUint, BigUint> for &'a BigUint {
} }
} }
impl Shl<uint, BigUint> for BigUint { impl Shl<uint> for BigUint {
type Output = BigUint;
#[inline] #[inline]
fn shl(self, rhs: uint) -> BigUint { (&self) << rhs } fn shl(self, rhs: uint) -> BigUint { (&self) << rhs }
} }
impl<'a> Shl<uint, BigUint> for &'a BigUint { impl<'a> Shl<uint> for &'a BigUint {
type Output = BigUint;
#[inline] #[inline]
fn shl(self, rhs: uint) -> BigUint { fn shl(self, rhs: uint) -> BigUint {
let n_unit = rhs / BigDigit::BITS; let n_unit = rhs / BigDigit::BITS;
@ -277,12 +293,16 @@ impl<'a> Shl<uint, BigUint> for &'a BigUint {
} }
} }
impl Shr<uint, BigUint> for BigUint { impl Shr<uint> for BigUint {
type Output = BigUint;
#[inline] #[inline]
fn shr(self, rhs: uint) -> BigUint { (&self) >> rhs } fn shr(self, rhs: uint) -> BigUint { (&self) >> rhs }
} }
impl<'a> Shr<uint, BigUint> for &'a BigUint { impl<'a> Shr<uint> for &'a BigUint {
type Output = BigUint;
#[inline] #[inline]
fn shr(self, rhs: uint) -> BigUint { fn shr(self, rhs: uint) -> BigUint {
let n_unit = rhs / BigDigit::BITS; let n_unit = rhs / BigDigit::BITS;
@ -308,7 +328,9 @@ impl Unsigned for BigUint {}
forward_all_binop!(impl Add for BigUint, add); forward_all_binop!(impl Add for BigUint, add);
impl<'a, 'b> Add<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> Add<&'b BigUint> for &'a BigUint {
type Output = BigUint;
fn add(self, other: &BigUint) -> BigUint { fn add(self, other: &BigUint) -> BigUint {
let zeros = ZERO_VEC.iter().cycle(); let zeros = ZERO_VEC.iter().cycle();
let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) }; let (a, b) = if self.data.len() > other.data.len() { (self, other) } else { (other, self) };
@ -327,7 +349,9 @@ impl<'a, 'b> Add<&'b BigUint, BigUint> for &'a BigUint {
forward_all_binop!(impl Sub for BigUint, sub); forward_all_binop!(impl Sub for BigUint, sub);
impl<'a, 'b> Sub<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> Sub<&'b BigUint> for &'a BigUint {
type Output = BigUint;
fn sub(self, other: &BigUint) -> BigUint { fn sub(self, other: &BigUint) -> BigUint {
let new_len = cmp::max(self.data.len(), other.data.len()); let new_len = cmp::max(self.data.len(), other.data.len());
let zeros = ZERO_VEC.iter().cycle(); let zeros = ZERO_VEC.iter().cycle();
@ -358,7 +382,9 @@ impl<'a, 'b> Sub<&'b BigUint, BigUint> for &'a BigUint {
forward_all_binop!(impl Mul for BigUint, mul); forward_all_binop!(impl Mul for BigUint, mul);
impl<'a, 'b> Mul<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint {
type Output = BigUint;
fn mul(self, other: &BigUint) -> BigUint { fn mul(self, other: &BigUint) -> BigUint {
if self.is_zero() || other.is_zero() { return Zero::zero(); } if self.is_zero() || other.is_zero() { return Zero::zero(); }
@ -427,7 +453,9 @@ impl<'a, 'b> Mul<&'b BigUint, BigUint> for &'a BigUint {
forward_all_binop!(impl Div for BigUint, div); forward_all_binop!(impl Div for BigUint, div);
impl<'a, 'b> Div<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> Div<&'b BigUint> for &'a BigUint {
type Output = BigUint;
#[inline] #[inline]
fn div(self, other: &BigUint) -> BigUint { fn div(self, other: &BigUint) -> BigUint {
let (q, _) = self.div_rem(other); let (q, _) = self.div_rem(other);
@ -437,7 +465,9 @@ impl<'a, 'b> Div<&'b BigUint, BigUint> for &'a BigUint {
forward_all_binop!(impl Rem for BigUint, rem); forward_all_binop!(impl Rem for BigUint, rem);
impl<'a, 'b> Rem<&'b BigUint, BigUint> for &'a BigUint { impl<'a, 'b> Rem<&'b BigUint> for &'a BigUint {
type Output = BigUint;
#[inline] #[inline]
fn rem(self, other: &BigUint) -> BigUint { fn rem(self, other: &BigUint) -> BigUint {
let (_, r) = self.div_rem(other); let (_, r) = self.div_rem(other);
@ -445,12 +475,16 @@ impl<'a, 'b> Rem<&'b BigUint, BigUint> for &'a BigUint {
} }
} }
impl Neg<BigUint> for BigUint { impl Neg for BigUint {
type Output = BigUint;
#[inline] #[inline]
fn neg(self) -> BigUint { panic!() } fn neg(self) -> BigUint { panic!() }
} }
impl<'a> Neg<BigUint> for &'a BigUint { impl<'a> Neg for &'a BigUint {
type Output = BigUint;
#[inline] #[inline]
fn neg(self) -> BigUint { panic!() } fn neg(self) -> BigUint { panic!() }
} }
@ -818,14 +852,14 @@ impl FromStrRadix for BigUint {
} }
} }
impl<T: Iterator<BigUint>> AdditiveIterator<BigUint> for T { impl<T: Iterator<Item = BigUint>> AdditiveIterator<BigUint> for T {
fn sum(self) -> BigUint { fn sum(self) -> BigUint {
let init: BigUint = Zero::zero(); let init: BigUint = Zero::zero();
self.fold(init, |acc, x| acc + x) self.fold(init, |acc, x| acc + x)
} }
} }
impl<T: Iterator<BigUint>> MultiplicativeIterator<BigUint> for T { impl<T: Iterator<Item = BigUint>> MultiplicativeIterator<BigUint> for T {
fn product(self) -> BigUint { fn product(self) -> BigUint {
let init: BigUint = One::one(); let init: BigUint = One::one();
self.fold(init, |acc, x| acc * x) self.fold(init, |acc, x| acc * x)
@ -939,7 +973,9 @@ fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) {
#[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Show, RustcEncodable, RustcDecodable)] #[derive(PartialEq, PartialOrd, Eq, Ord, Copy, Clone, Show, RustcEncodable, RustcDecodable)]
pub enum Sign { Minus, NoSign, Plus } pub enum Sign { Minus, NoSign, Plus }
impl Neg<Sign> for Sign { impl Neg for Sign {
type Output = Sign;
/// Negate Sign value. /// Negate Sign value.
#[inline] #[inline]
fn neg(self) -> Sign { fn neg(self) -> Sign {
@ -1015,24 +1051,32 @@ impl FromStr for BigInt {
impl Num for BigInt {} impl Num for BigInt {}
impl Shl<uint, BigInt> for BigInt { impl Shl<uint> for BigInt {
type Output = BigInt;
#[inline] #[inline]
fn shl(self, rhs: uint) -> BigInt { (&self) << rhs } fn shl(self, rhs: uint) -> BigInt { (&self) << rhs }
} }
impl<'a> Shl<uint, BigInt> for &'a BigInt { impl<'a> Shl<uint> for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn shl(self, rhs: uint) -> BigInt { fn shl(self, rhs: uint) -> BigInt {
BigInt::from_biguint(self.sign, &self.data << rhs) BigInt::from_biguint(self.sign, &self.data << rhs)
} }
} }
impl Shr<uint, BigInt> for BigInt { impl Shr<uint> for BigInt {
type Output = BigInt;
#[inline] #[inline]
fn shr(self, rhs: uint) -> BigInt { (&self) >> rhs } fn shr(self, rhs: uint) -> BigInt { (&self) >> rhs }
} }
impl<'a> Shr<uint, BigInt> for &'a BigInt { impl<'a> Shr<uint> for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn shr(self, rhs: uint) -> BigInt { fn shr(self, rhs: uint) -> BigInt {
BigInt::from_biguint(self.sign, &self.data >> rhs) BigInt::from_biguint(self.sign, &self.data >> rhs)
@ -1088,7 +1132,9 @@ impl Signed for BigInt {
forward_all_binop!(impl Add for BigInt, add); forward_all_binop!(impl Add for BigInt, add);
impl<'a, 'b> Add<&'b BigInt, BigInt> for &'a BigInt { impl<'a, 'b> Add<&'b BigInt> for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn add(self, other: &BigInt) -> BigInt { fn add(self, other: &BigInt) -> BigInt {
match (self.sign, other.sign) { match (self.sign, other.sign) {
@ -1104,7 +1150,9 @@ impl<'a, 'b> Add<&'b BigInt, BigInt> for &'a BigInt {
forward_all_binop!(impl Sub for BigInt, sub); forward_all_binop!(impl Sub for BigInt, sub);
impl<'a, 'b> Sub<&'b BigInt, BigInt> for &'a BigInt { impl<'a, 'b> Sub<&'b BigInt> for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn sub(self, other: &BigInt) -> BigInt { fn sub(self, other: &BigInt) -> BigInt {
match (self.sign, other.sign) { match (self.sign, other.sign) {
@ -1124,7 +1172,9 @@ impl<'a, 'b> Sub<&'b BigInt, BigInt> for &'a BigInt {
forward_all_binop!(impl Mul for BigInt, mul); forward_all_binop!(impl Mul for BigInt, mul);
impl<'a, 'b> Mul<&'b BigInt, BigInt> for &'a BigInt { impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn mul(self, other: &BigInt) -> BigInt { fn mul(self, other: &BigInt) -> BigInt {
match (self.sign, other.sign) { match (self.sign, other.sign) {
@ -1141,7 +1191,9 @@ impl<'a, 'b> Mul<&'b BigInt, BigInt> for &'a BigInt {
forward_all_binop!(impl Div for BigInt, div); forward_all_binop!(impl Div for BigInt, div);
impl<'a, 'b> Div<&'b BigInt, BigInt> for &'a BigInt { impl<'a, 'b> Div<&'b BigInt> for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn div(self, other: &BigInt) -> BigInt { fn div(self, other: &BigInt) -> BigInt {
let (q, _) = self.div_rem(other); let (q, _) = self.div_rem(other);
@ -1151,7 +1203,9 @@ impl<'a, 'b> Div<&'b BigInt, BigInt> for &'a BigInt {
forward_all_binop!(impl Rem for BigInt, rem); forward_all_binop!(impl Rem for BigInt, rem);
impl<'a, 'b> Rem<&'b BigInt, BigInt> for &'a BigInt { impl<'a, 'b> Rem<&'b BigInt> for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn rem(self, other: &BigInt) -> BigInt { fn rem(self, other: &BigInt) -> BigInt {
let (_, r) = self.div_rem(other); let (_, r) = self.div_rem(other);
@ -1159,12 +1213,16 @@ impl<'a, 'b> Rem<&'b BigInt, BigInt> for &'a BigInt {
} }
} }
impl Neg<BigInt> for BigInt { impl Neg for BigInt {
type Output = BigInt;
#[inline] #[inline]
fn neg(self) -> BigInt { -&self } fn neg(self) -> BigInt { -&self }
} }
impl<'a> Neg<BigInt> for &'a BigInt { impl<'a> Neg for &'a BigInt {
type Output = BigInt;
#[inline] #[inline]
fn neg(self) -> BigInt { fn neg(self) -> BigInt {
BigInt::from_biguint(self.sign.neg(), self.data.clone()) BigInt::from_biguint(self.sign.neg(), self.data.clone())
@ -1496,14 +1554,14 @@ impl<R: Rng> RandBigInt for R {
} }
} }
impl<T: Iterator<BigInt>> AdditiveIterator<BigInt> for T { impl<T: Iterator<Item = BigInt>> AdditiveIterator<BigInt> for T {
fn sum(self) -> BigInt { fn sum(self) -> BigInt {
let init: BigInt = Zero::zero(); let init: BigInt = Zero::zero();
self.fold(init, |acc, x| acc + x) self.fold(init, |acc, x| acc + x)
} }
} }
impl<T: Iterator<BigInt>> MultiplicativeIterator<BigInt> for T { impl<T: Iterator<Item = BigInt>> MultiplicativeIterator<BigInt> for T {
fn product(self) -> BigInt { fn product(self) -> BigInt {
let init: BigInt = One::one(); let init: BigInt = One::one();
self.fold(init, |acc, x| acc * x) self.fold(init, |acc, x| acc * x)

View File

@ -105,7 +105,9 @@ impl<T: Clone + FloatMath + Num> Complex<T> {
macro_rules! forward_val_val_binop { macro_rules! forward_val_val_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<T: Clone + Num> $imp<Complex<T>, Complex<T>> for Complex<T> { impl<T: Clone + Num> $imp<Complex<T>> for Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn $method(self, other: Complex<T>) -> Complex<T> { fn $method(self, other: Complex<T>) -> Complex<T> {
(&self).$method(&other) (&self).$method(&other)
@ -116,7 +118,9 @@ macro_rules! forward_val_val_binop {
macro_rules! forward_ref_val_binop { macro_rules! forward_ref_val_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + Num> $imp<Complex<T>, Complex<T>> for &'a Complex<T> { impl<'a, T: Clone + Num> $imp<Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn $method(self, other: Complex<T>) -> Complex<T> { fn $method(self, other: Complex<T>) -> Complex<T> {
self.$method(&other) self.$method(&other)
@ -127,7 +131,9 @@ macro_rules! forward_ref_val_binop {
macro_rules! forward_val_ref_binop { macro_rules! forward_val_ref_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + Num> $imp<&'a Complex<T>, Complex<T>> for Complex<T> { impl<'a, T: Clone + Num> $imp<&'a Complex<T>> for Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn $method(self, other: &Complex<T>) -> Complex<T> { fn $method(self, other: &Complex<T>) -> Complex<T> {
(&self).$method(other) (&self).$method(other)
@ -148,7 +154,9 @@ macro_rules! forward_all_binop {
forward_all_binop!(impl Add, add); forward_all_binop!(impl Add, add);
// (a + i b) + (c + i d) == (a + c) + i (b + d) // (a + i b) + (c + i d) == (a + c) + i (b + d)
impl<'a, 'b, T: Clone + Num> Add<&'b Complex<T>, Complex<T>> for &'a Complex<T> { impl<'a, 'b, T: Clone + Num> Add<&'b Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn add(self, other: &Complex<T>) -> Complex<T> { fn add(self, other: &Complex<T>) -> Complex<T> {
Complex::new(self.re.clone() + other.re.clone(), Complex::new(self.re.clone() + other.re.clone(),
@ -159,7 +167,9 @@ impl<'a, 'b, T: Clone + Num> Add<&'b Complex<T>, Complex<T>> for &'a Complex<T>
forward_all_binop!(impl Sub, sub); forward_all_binop!(impl Sub, sub);
// (a + i b) - (c + i d) == (a - c) + i (b - d) // (a + i b) - (c + i d) == (a - c) + i (b - d)
impl<'a, 'b, T: Clone + Num> Sub<&'b Complex<T>, Complex<T>> for &'a Complex<T> { impl<'a, 'b, T: Clone + Num> Sub<&'b Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn sub(self, other: &Complex<T>) -> Complex<T> { fn sub(self, other: &Complex<T>) -> Complex<T> {
Complex::new(self.re.clone() - other.re.clone(), Complex::new(self.re.clone() - other.re.clone(),
@ -170,7 +180,9 @@ impl<'a, 'b, T: Clone + Num> Sub<&'b Complex<T>, Complex<T>> for &'a Complex<T>
forward_all_binop!(impl Mul, mul); forward_all_binop!(impl Mul, mul);
// (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c) // (a + i b) * (c + i d) == (a*c - b*d) + i (a*d + b*c)
impl<'a, 'b, T: Clone + Num> Mul<&'b Complex<T>, Complex<T>> for &'a Complex<T> { impl<'a, 'b, T: Clone + Num> Mul<&'b Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn mul(self, other: &Complex<T>) -> Complex<T> { fn mul(self, other: &Complex<T>) -> Complex<T> {
Complex::new(self.re.clone() * other.re.clone() - self.im.clone() * other.im.clone(), Complex::new(self.re.clone() * other.re.clone() - self.im.clone() * other.im.clone(),
@ -182,7 +194,9 @@ forward_all_binop!(impl Div, div);
// (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d) // (a + i b) / (c + i d) == [(a + i b) * (c - i d)] / (c*c + d*d)
// == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)] // == [(a*c + b*d) / (c*c + d*d)] + i [(b*c - a*d) / (c*c + d*d)]
impl<'a, 'b, T: Clone + Num> Div<&'b Complex<T>, Complex<T>> for &'a Complex<T> { impl<'a, 'b, T: Clone + Num> Div<&'b Complex<T>> for &'a Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn div(self, other: &Complex<T>) -> Complex<T> { fn div(self, other: &Complex<T>) -> Complex<T> {
let norm_sqr = other.norm_sqr(); let norm_sqr = other.norm_sqr();
@ -191,12 +205,16 @@ impl<'a, 'b, T: Clone + Num> Div<&'b Complex<T>, Complex<T>> for &'a Complex<T>
} }
} }
impl<T: Clone + Num> Neg<Complex<T>> for Complex<T> { impl<T: Clone + Num> Neg for Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn neg(self) -> Complex<T> { -&self } fn neg(self) -> Complex<T> { -&self }
} }
impl<'a, T: Clone + Num> Neg<Complex<T>> for &'a Complex<T> { impl<'a, T: Clone + Num> Neg for &'a Complex<T> {
type Output = Complex<T>;
#[inline] #[inline]
fn neg(self) -> Complex<T> { fn neg(self) -> Complex<T> {
Complex::new(-self.re.clone(), -self.im.clone()) Complex::new(-self.re.clone(), -self.im.clone())
@ -234,14 +252,18 @@ impl<T: fmt::Show + Num + PartialOrd + Clone> fmt::Show for Complex<T> {
} }
} }
impl<A: Clone + Num, T: Iterator<Complex<A>>> AdditiveIterator<Complex<A>> for T { impl<A, T> AdditiveIterator<Complex<A>> for T
where A: Clone + Num, T: Iterator<Item = Complex<A>>
{
fn sum(self) -> Complex<A> { fn sum(self) -> Complex<A> {
let init: Complex<A> = Zero::zero(); let init: Complex<A> = Zero::zero();
self.fold(init, |acc, x| acc + x) self.fold(init, |acc, x| acc + x)
} }
} }
impl<A: Clone + Num, T: Iterator<Complex<A>>> MultiplicativeIterator<Complex<A>> for T { impl<A, T> MultiplicativeIterator<Complex<A>> for T
where A: Clone + Num, T: Iterator<Item = Complex<A>>
{
fn product(self) -> Complex<A> { fn product(self) -> Complex<A> {
let init: Complex<A> = One::one(); let init: Complex<A> = One::one();
self.fold(init, |acc, x| acc * x) self.fold(init, |acc, x| acc * x)

View File

@ -15,8 +15,7 @@ use std::ops::{Div, Rem};
use {Num, Signed}; use {Num, Signed};
pub trait Integer: Sized + Num + PartialOrd pub trait Integer: Sized + Num + PartialOrd
+ Div<Self, Self> + Div<Self, Output = Self> + Rem<Self, Output = Self> {
+ Rem<Self, Self> {
/// Floored integer division. /// Floored integer division.
/// ///
/// # Examples /// # Examples

View File

@ -36,12 +36,18 @@ pub struct Range<A> {
/// } /// }
/// ``` /// ```
#[inline] #[inline]
pub fn range<A: Add<A, A> + PartialOrd + Clone + One>(start: A, stop: A) -> Range<A> { pub fn range<A>(start: A, stop: A) -> Range<A>
where A: Add<A, Output = A> + PartialOrd + Clone + One
{
Range{state: start, stop: stop, one: One::one()} Range{state: start, stop: stop, one: One::one()}
} }
// FIXME: rust-lang/rust#10414: Unfortunate type bound // FIXME: rust-lang/rust#10414: Unfortunate type bound
impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for Range<A> { impl<A> Iterator for Range<A>
where A: Add<A, Output = A> + PartialOrd + Clone + ToPrimitive
{
type Item = A;
#[inline] #[inline]
fn next(&mut self) -> Option<A> { fn next(&mut self) -> Option<A> {
if self.state < self.stop { if self.state < self.stop {
@ -88,7 +94,9 @@ impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for Range<A> {
/// `Integer` is required to ensure the range will be the same regardless of /// `Integer` is required to ensure the range will be the same regardless of
/// the direction it is consumed. /// the direction it is consumed.
impl<A: Integer + PartialOrd + Clone + ToPrimitive> DoubleEndedIterator<A> for Range<A> { impl<A> DoubleEndedIterator for Range<A>
where A: Integer + PartialOrd + Clone + ToPrimitive
{
#[inline] #[inline]
fn next_back(&mut self) -> Option<A> { fn next_back(&mut self) -> Option<A> {
if self.stop > self.state { if self.stop > self.state {
@ -109,12 +117,17 @@ pub struct RangeInclusive<A> {
/// Return an iterator over the range [start, stop] /// Return an iterator over the range [start, stop]
#[inline] #[inline]
pub fn range_inclusive<A: Add<A, A> + PartialOrd + Clone + One>(start: A, stop: A) pub fn range_inclusive<A>(start: A, stop: A) -> RangeInclusive<A>
-> RangeInclusive<A> { where A: Add<A, Output = A> + PartialOrd + Clone + One
{
RangeInclusive{range: range(start, stop), done: false} RangeInclusive{range: range(start, stop), done: false}
} }
impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for RangeInclusive<A> { impl<A> Iterator for RangeInclusive<A>
where A: Add<A, Output = A> + PartialOrd + Clone + ToPrimitive
{
type Item = A;
#[inline] #[inline]
fn next(&mut self) -> Option<A> { fn next(&mut self) -> Option<A> {
match self.range.next() { match self.range.next() {
@ -146,8 +159,9 @@ impl<A: Add<A, A> + PartialOrd + Clone + ToPrimitive> Iterator<A> for RangeInclu
} }
} }
impl<A: Sub<A, A> + Integer + PartialOrd + Clone + ToPrimitive> DoubleEndedIterator<A> impl<A> DoubleEndedIterator for RangeInclusive<A>
for RangeInclusive<A> { where A: Sub<A, Output = A> + Integer + PartialOrd + Clone + ToPrimitive
{
#[inline] #[inline]
fn next_back(&mut self) -> Option<A> { fn next_back(&mut self) -> Option<A> {
if self.range.stop > self.range.state { if self.range.stop > self.range.state {
@ -174,13 +188,18 @@ pub struct RangeStep<A> {
/// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping. /// Return an iterator over the range [start, stop) by `step`. It handles overflow by stopping.
#[inline] #[inline]
pub fn range_step<A: CheckedAdd + PartialOrd + pub fn range_step<A>(start: A, stop: A, step: A) -> RangeStep<A>
Clone + Zero>(start: A, stop: A, step: A) -> RangeStep<A> { where A: CheckedAdd + PartialOrd + Clone + Zero
{
let rev = step < Zero::zero(); let rev = step < Zero::zero();
RangeStep{state: start, stop: stop, step: step, rev: rev} RangeStep{state: start, stop: stop, step: step, rev: rev}
} }
impl<A: CheckedAdd + PartialOrd + Clone> Iterator<A> for RangeStep<A> { impl<A> Iterator for RangeStep<A>
where A: CheckedAdd + PartialOrd + Clone
{
type Item = A;
#[inline] #[inline]
fn next(&mut self) -> Option<A> { fn next(&mut self) -> Option<A> {
if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) { if (self.rev && self.state > self.stop) || (!self.rev && self.state < self.stop) {
@ -208,13 +227,18 @@ pub struct RangeStepInclusive<A> {
/// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping. /// Return an iterator over the range [start, stop] by `step`. It handles overflow by stopping.
#[inline] #[inline]
pub fn range_step_inclusive<A: CheckedAdd + PartialOrd + Clone + Zero>(start: A, stop: A, pub fn range_step_inclusive<A>(start: A, stop: A, step: A) -> RangeStepInclusive<A>
step: A) -> RangeStepInclusive<A> { where A: CheckedAdd + PartialOrd + Clone + Zero
{
let rev = step < Zero::zero(); let rev = step < Zero::zero();
RangeStepInclusive{state: start, stop: stop, step: step, rev: rev, done: false} RangeStepInclusive{state: start, stop: stop, step: step, rev: rev, done: false}
} }
impl<A: CheckedAdd + PartialOrd + Clone + PartialEq> Iterator<A> for RangeStepInclusive<A> { impl<A> Iterator for RangeStepInclusive<A>
where A: CheckedAdd + PartialOrd + Clone + PartialEq
{
type Item = A;
#[inline] #[inline]
fn next(&mut self) -> Option<A> { fn next(&mut self) -> Option<A> {
if !self.done && ((self.rev && self.state >= self.stop) || if !self.done && ((self.rev && self.state >= self.stop) ||
@ -249,7 +273,9 @@ mod tests {
fn to_u64(&self) -> Option<u64> { None } fn to_u64(&self) -> Option<u64> { None }
} }
impl Add<Foo, Foo> for Foo { impl Add<Foo> for Foo {
type Output = Foo;
fn add(self, _: Foo) -> Foo { fn add(self, _: Foo) -> Foo {
Foo Foo
} }
@ -273,7 +299,9 @@ mod tests {
} }
} }
impl Mul<Foo, Foo> for Foo { impl Mul<Foo> for Foo {
type Output = Foo;
fn mul(self, _: Foo) -> Foo { fn mul(self, _: Foo) -> Foo {
Foo Foo
} }

View File

@ -45,6 +45,7 @@
//! //!
//! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method //! [newt]: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method
#![feature(associated_types)]
#![feature(macro_rules)] #![feature(macro_rules)]
#![feature(default_type_params)] #![feature(default_type_params)]
#![feature(slicing_syntax)] #![feature(slicing_syntax)]
@ -123,7 +124,7 @@ pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
/// assert_eq!(num::pow(2i, 4), 16); /// assert_eq!(num::pow(2i, 4), 16);
/// ``` /// ```
#[inline] #[inline]
pub fn pow<T: Clone + One + Mul<T, T>>(mut base: T, mut exp: uint) -> T { pub fn pow<T: Clone + One + Mul<T, Output = T>>(mut base: T, mut exp: uint) -> T {
if exp == 1 { base } if exp == 1 { base }
else { else {
let mut acc = one::<T>(); let mut acc = one::<T>();

View File

@ -216,7 +216,7 @@ macro_rules! cmp_impl {
}; };
// return something other than a Ratio<T> // return something other than a Ratio<T>
(impl $imp:ident, $($method:ident -> $res:ty),*) => { (impl $imp:ident, $($method:ident -> $res:ty),*) => {
impl<T: Clone + Mul<T,T> + $imp> $imp for Ratio<T> { impl<T: Clone + Mul<T, Output = T> + $imp> $imp for Ratio<T> {
$( $(
#[inline] #[inline]
fn $method(&self, other: &Ratio<T>) -> $res { fn $method(&self, other: &Ratio<T>) -> $res {
@ -234,7 +234,9 @@ cmp_impl!(impl Ord, cmp -> cmp::Ordering);
macro_rules! forward_val_val_binop { macro_rules! forward_val_val_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<T: Clone + Integer + PartialOrd> $imp<Ratio<T>, Ratio<T>> for Ratio<T> { impl<T: Clone + Integer + PartialOrd> $imp<Ratio<T>> for Ratio<T> {
type Output = Ratio<T>;
#[inline] #[inline]
fn $method(self, other: Ratio<T>) -> Ratio<T> { fn $method(self, other: Ratio<T>) -> Ratio<T> {
(&self).$method(&other) (&self).$method(&other)
@ -245,7 +247,9 @@ macro_rules! forward_val_val_binop {
macro_rules! forward_ref_val_binop { macro_rules! forward_ref_val_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + Integer + PartialOrd> $imp<Ratio<T>, Ratio<T>> for &'a Ratio<T> { impl<'a, T: Clone + Integer + PartialOrd> $imp<Ratio<T>> for &'a Ratio<T> {
type Output = Ratio<T>;
#[inline] #[inline]
fn $method(self, other: Ratio<T>) -> Ratio<T> { fn $method(self, other: Ratio<T>) -> Ratio<T> {
self.$method(&other) self.$method(&other)
@ -256,7 +260,9 @@ macro_rules! forward_ref_val_binop {
macro_rules! forward_val_ref_binop { macro_rules! forward_val_ref_binop {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
impl<'a, T: Clone + Integer + PartialOrd> $imp<&'a Ratio<T>, Ratio<T>> for Ratio<T> { impl<'a, T: Clone + Integer + PartialOrd> $imp<&'a Ratio<T>> for Ratio<T> {
type Output = Ratio<T>;
#[inline] #[inline]
fn $method(self, other: &Ratio<T>) -> Ratio<T> { fn $method(self, other: &Ratio<T>) -> Ratio<T> {
(&self).$method(other) (&self).$method(other)
@ -276,8 +282,11 @@ macro_rules! forward_all_binop {
/* Arithmetic */ /* Arithmetic */
forward_all_binop!(impl Mul, mul); forward_all_binop!(impl Mul, mul);
// a/b * c/d = (a*c)/(b*d) // a/b * c/d = (a*c)/(b*d)
impl<'a, 'b, T: Clone + Integer + PartialOrd> impl<'a, 'b, T> Mul<&'b Ratio<T>> for &'a Ratio<T>
Mul<&'b Ratio<T>, Ratio<T>> for &'a Ratio<T> { where T: Clone + Integer + PartialOrd
{
type Output = Ratio<T>;
#[inline] #[inline]
fn mul(self, rhs: &Ratio<T>) -> Ratio<T> { fn mul(self, rhs: &Ratio<T>) -> Ratio<T> {
Ratio::new(self.numer.clone() * rhs.numer.clone(), self.denom.clone() * rhs.denom.clone()) Ratio::new(self.numer.clone() * rhs.numer.clone(), self.denom.clone() * rhs.denom.clone())
@ -286,8 +295,11 @@ impl<'a, 'b, T: Clone + Integer + PartialOrd>
forward_all_binop!(impl Div, div); forward_all_binop!(impl Div, div);
// (a/b) / (c/d) = (a*d)/(b*c) // (a/b) / (c/d) = (a*d)/(b*c)
impl<'a, 'b, T: Clone + Integer + PartialOrd> impl<'a, 'b, T> Div<&'b Ratio<T>> for &'a Ratio<T>
Div<&'b Ratio<T>, Ratio<T>> for &'a Ratio<T> { where T: Clone + Integer + PartialOrd
{
type Output = Ratio<T>;
#[inline] #[inline]
fn div(self, rhs: &Ratio<T>) -> Ratio<T> { fn div(self, rhs: &Ratio<T>) -> Ratio<T> {
Ratio::new(self.numer.clone() * rhs.denom.clone(), self.denom.clone() * rhs.numer.clone()) Ratio::new(self.numer.clone() * rhs.denom.clone(), self.denom.clone() * rhs.numer.clone())
@ -299,7 +311,8 @@ macro_rules! arith_impl {
(impl $imp:ident, $method:ident) => { (impl $imp:ident, $method:ident) => {
forward_all_binop!(impl $imp, $method); forward_all_binop!(impl $imp, $method);
impl<'a, 'b, T: Clone + Integer + PartialOrd> impl<'a, 'b, T: Clone + Integer + PartialOrd>
$imp<&'b Ratio<T>,Ratio<T>> for &'a Ratio<T> { $imp<&'b Ratio<T>> for &'a Ratio<T> {
type Output = Ratio<T>;
#[inline] #[inline]
fn $method(self, rhs: &Ratio<T>) -> Ratio<T> { fn $method(self, rhs: &Ratio<T>) -> Ratio<T> {
Ratio::new((self.numer.clone() * rhs.denom.clone()).$method(self.denom.clone() * rhs.numer.clone()), Ratio::new((self.numer.clone() * rhs.denom.clone()).$method(self.denom.clone() * rhs.numer.clone()),
@ -318,14 +331,20 @@ arith_impl!(impl Sub, sub);
// a/b % c/d = (a*d % b*c)/(b*d) // a/b % c/d = (a*d % b*c)/(b*d)
arith_impl!(impl Rem, rem); arith_impl!(impl Rem, rem);
impl<T: Clone + Integer + PartialOrd> impl<T> Neg for Ratio<T>
Neg<Ratio<T>> for Ratio<T> { where T: Clone + Integer + PartialOrd
{
type Output = Ratio<T>;
#[inline] #[inline]
fn neg(self) -> Ratio<T> { -&self } fn neg(self) -> Ratio<T> { -&self }
} }
impl<'a, T: Clone + Integer + PartialOrd> impl<'a, T> Neg for &'a Ratio<T>
Neg<Ratio<T>> for &'a Ratio<T> { where T: Clone + Integer + PartialOrd
{
type Output = Ratio<T>;
#[inline] #[inline]
fn neg(self) -> Ratio<T> { fn neg(self) -> Ratio<T> {
Ratio::new_raw(-self.numer.clone(), self.denom.clone()) Ratio::new_raw(-self.numer.clone(), self.denom.clone())
@ -437,14 +456,18 @@ impl<T: FromStrRadix + Clone + Integer + PartialOrd>
} }
} }
impl<A: Clone + Integer + PartialOrd, T: Iterator<Ratio<A>>> AdditiveIterator<Ratio<A>> for T { impl<A, T> AdditiveIterator<Ratio<A>> for T
where A: Clone + Integer + PartialOrd, T: Iterator<Item = Ratio<A>>
{
fn sum(self) -> Ratio<A> { fn sum(self) -> Ratio<A> {
let init: Ratio<A> = Zero::zero(); let init: Ratio<A> = Zero::zero();
self.fold(init, |acc, x| acc + x) self.fold(init, |acc, x| acc + x)
} }
} }
impl<A: Clone + Integer + PartialOrd, T: Iterator<Ratio<A>>> MultiplicativeIterator<Ratio<A>> for T { impl<A, T> MultiplicativeIterator<Ratio<A>> for T
where A: Clone + Integer + PartialOrd, T: Iterator<Item = Ratio<A>>
{
fn product(self) -> Ratio<A> { fn product(self) -> Ratio<A> {
let init: Ratio<A> = One::one(); let init: Ratio<A> = One::one();
self.fold(init, |acc, x| acc * x) self.fold(init, |acc, x| acc * x)

View File

@ -18,12 +18,8 @@ use std::{f32, f64};
/// The base trait for numeric types /// The base trait for numeric types
pub trait Num: PartialEq + Zero + One pub trait Num: PartialEq + Zero + One
+ Neg<Self> + Neg<Output = Self> + Add<Output = Self> + Sub<Output = Self>
+ Add<Self,Self> + Mul<Output = Self> + Div<Output = Self> + Rem<Output = Self> {}
+ Sub<Self,Self>
+ Mul<Self,Self>
+ Div<Self,Self>
+ Rem<Self,Self> {}
macro_rules! trait_impl { macro_rules! trait_impl {
($name:ident for $($t:ty)*) => ($( ($name:ident for $($t:ty)*) => ($(
@ -40,7 +36,7 @@ trait_impl!(Num for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64);
/// This trait can be automatically be derived using `#[deriving(Zero)]` /// This trait can be automatically be derived using `#[deriving(Zero)]`
/// attribute. If you choose to use this, make sure that the laws outlined in /// attribute. If you choose to use this, make sure that the laws outlined in
/// the documentation for `Zero::zero` still hold. /// the documentation for `Zero::zero` still hold.
pub trait Zero: Add<Self, Self> { pub trait Zero: Add<Self, Output = Self> {
/// Returns the additive identity element of `Self`, `0`. /// Returns the additive identity element of `Self`, `0`.
/// ///
/// # Laws /// # Laws
@ -90,7 +86,7 @@ zero_impl!(f32, 0.0f32);
zero_impl!(f64, 0.0f64); zero_impl!(f64, 0.0f64);
/// Defines a multiplicative identity element for `Self`. /// Defines a multiplicative identity element for `Self`.
pub trait One: Mul<Self, Self> { pub trait One: Mul<Self, Output = Self> {
/// Returns the multiplicative identity element of `Self`, `1`. /// Returns the multiplicative identity element of `Self`, `1`.
/// ///
/// # Laws /// # Laws
@ -134,7 +130,7 @@ one_impl!(f32, 1.0f32);
one_impl!(f64, 1.0f64); one_impl!(f64, 1.0f64);
/// Useful functions for signed numbers (i.e. numbers that can be negative). /// Useful functions for signed numbers (i.e. numbers that can be negative).
pub trait Signed: Num + Neg<Self> { pub trait Signed: Num + Neg<Output = Self> {
/// Computes the absolute value. /// Computes the absolute value.
/// ///
/// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`. /// For `f32` and `f64`, `NaN` will be returned if the number is `NaN`.
@ -328,7 +324,7 @@ impl<T: CheckedAdd + CheckedSub + Zero + PartialOrd + Bounded> Saturating for T
} }
/// Performs addition that returns `None` instead of wrapping around on overflow. /// Performs addition that returns `None` instead of wrapping around on overflow.
pub trait CheckedAdd: Add<Self, Self> { pub trait CheckedAdd: Add<Self, Output = Self> {
/// Adds two numbers, checking for overflow. If overflow happens, `None` is returned. /// Adds two numbers, checking for overflow. If overflow happens, `None` is returned.
/// ///
/// # Example /// # Example
@ -389,7 +385,7 @@ checked_impl!(CheckedAdd, checked_add, i32, intrinsics::i32_add_with_overflow);
checked_impl!(CheckedAdd, checked_add, i64, intrinsics::i64_add_with_overflow); checked_impl!(CheckedAdd, checked_add, i64, intrinsics::i64_add_with_overflow);
/// Performs subtraction that returns `None` instead of wrapping around on underflow. /// Performs subtraction that returns `None` instead of wrapping around on underflow.
pub trait CheckedSub: Sub<Self, Self> { pub trait CheckedSub: Sub<Self, Output = Self> {
/// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned. /// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned.
/// ///
/// # Example /// # Example
@ -424,7 +420,7 @@ checked_impl!(CheckedSub, checked_sub, i64, intrinsics::i64_sub_with_overflow);
/// Performs multiplication that returns `None` instead of wrapping around on underflow or /// Performs multiplication that returns `None` instead of wrapping around on underflow or
/// overflow. /// overflow.
pub trait CheckedMul: Mul<Self, Self> { pub trait CheckedMul: Mul<Self, Output = Self> {
/// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow /// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow
/// happens, `None` is returned. /// happens, `None` is returned.
/// ///
@ -460,7 +456,7 @@ checked_impl!(CheckedMul, checked_mul, i64, intrinsics::i64_mul_with_overflow);
/// Performs division that returns `None` instead of panicking on division by zero and instead of /// Performs division that returns `None` instead of panicking on division by zero and instead of
/// wrapping around on underflow and overflow. /// wrapping around on underflow and overflow.
pub trait CheckedDiv: Div<Self, Self> { pub trait CheckedDiv: Div<Self, Output = Self> {
/// Divides two numbers, checking for underflow, overflow and division by zero. If any of that /// Divides two numbers, checking for underflow, overflow and division by zero. If any of that
/// happens, `None` is returned. /// happens, `None` is returned.
/// ///