diff options
| author | bors <bors@rust-lang.org> | 2013-04-23 22:12:47 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-04-23 22:12:47 -0700 |
| commit | c8ac057545f7b2edf1e488aa4562138a6ed7a096 (patch) | |
| tree | 4c67cea5512e5f0cceeda17b3f2b905c4202bd36 /src/libstd | |
| parent | 706096b31960143fb1eb957a882f170ae4a8b4e9 (diff) | |
| parent | ab8068c9f2cbcce4411020b04dafe2055044f96a (diff) | |
| download | rust-c8ac057545f7b2edf1e488aa4562138a6ed7a096.tar.gz rust-c8ac057545f7b2edf1e488aa4562138a6ed7a096.zip | |
auto merge of #6041 : bjz/rust/numeric-traits, r=brson
As part of the numeric trait reform (see issue #4819), I have added the following traits to `core::num` and implemented them for the appropriate types:
~~~rust
pub trait Signed: Num
+ Neg<Self> {
fn abs(&self) -> Self;
fn signum(&self) -> Self;
fn is_positive(&self) -> bool;
fn is_negative(&self) -> bool;
}
pub trait Unsigned: Num {}
pub trait Natural: Num
+ Ord
+ Quot<Self,Self>
+ Rem<Self,Self> {
fn div(&self, other: Self) -> Self;
fn modulo(&self, other: Self) -> Self;
fn div_mod(&self, other: Self) -> (Self,Self);
fn quot_rem(&self, other: Self) -> (Self,Self);
fn gcd(&self, other: Self) -> Self;
fn lcm(&self, other: Self) -> Self;
fn divisible_by(&self, other: Self) -> bool;
fn is_even(&self) -> bool;
fn is_odd(&self) -> bool;
}
~~~
I have not implemented `Natural` for `BigInt` and `BigUInt` because they're a little over my head. Help with this would be most appreciated.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/num/bigint.rs | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs index ee9749af532..5f0fd76640a 100644 --- a/src/libstd/num/bigint.rs +++ b/src/libstd/num/bigint.rs @@ -154,6 +154,8 @@ impl One for BigUint { pub fn one() -> BigUint { BigUint::new(~[1]) } } +impl Unsigned for BigUint {} + impl Add<BigUint, BigUint> for BigUint { fn add(&self, other: &BigUint) -> BigUint { let new_len = uint::max(self.data.len(), other.data.len()); @@ -469,11 +471,8 @@ pub impl BigUint { } fn is_zero(&self) -> bool { self.data.is_empty() } + fn is_not_zero(&self) -> bool { !self.data.is_empty() } - fn is_positive(&self) -> bool { self.is_not_zero() } - fn is_negative(&self) -> bool { false } - fn is_nonpositive(&self) -> bool { self.is_zero() } - fn is_nonnegative(&self) -> bool { true } fn to_uint(&self) -> uint { match self.data.len() { @@ -693,6 +692,27 @@ impl One for BigInt { } } +impl Signed for BigInt { + fn abs(&self) -> BigInt { + match self.sign { + Plus | Zero => copy *self, + Minus => BigInt::from_biguint(Plus, copy self.data) + } + } + + fn signum(&self) -> BigInt { + match self.sign { + Plus => BigInt::from_biguint(Plus, One::one()), + Minus => BigInt::from_biguint(Minus, One::one()), + Zero => Zero::zero(), + } + } + + fn is_positive(&self) -> bool { self.sign == Plus } + + fn is_negative(&self) -> bool { self.sign == Minus } +} + impl Add<BigInt, BigInt> for BigInt { fn add(&self, other: &BigInt) -> BigInt { match (self.sign, other.sign) { @@ -888,11 +908,8 @@ pub impl BigInt { } fn is_zero(&self) -> bool { self.sign == Zero } + fn is_not_zero(&self) -> bool { self.sign != Zero } - fn is_positive(&self) -> bool { self.sign == Plus } - fn is_negative(&self) -> bool { self.sign == Minus } - fn is_nonpositive(&self) -> bool { self.sign != Plus } - fn is_nonnegative(&self) -> bool { self.sign != Minus } fn to_uint(&self) -> uint { match self.sign { |
