From 139cf8cf66825cc5d239ea61abf40dfed7c74c53 Mon Sep 17 00:00:00 2001 From: Huon Wilson Date: Fri, 9 Jan 2015 22:36:03 +1100 Subject: [PATCH] Bump to 0.1.7. --- Cargo.toml | 2 +- src/bigint.rs | 279 +++++++++++++++++++++++++----------------------- src/complex.rs | 20 +++- src/integer.rs | 24 ++--- src/iter.rs | 70 ++++++------ src/lib.rs | 14 ++- src/rational.rs | 45 ++++---- src/traits.rs | 75 +++++++------ 8 files changed, 284 insertions(+), 245 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6904e4b..02d524a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "num" -version = "0.1.6" +version = "0.1.7" authors = ["The Rust Project Developers"] license = "MIT/Apache-2.0" homepage = "https://github.com/rust-lang/num" diff --git a/src/bigint.rs b/src/bigint.rs index e847588..bdeb342 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -23,10 +23,10 @@ //! use std::mem::replace; //! //! // Calculate large fibonacci numbers. -//! fn fib(n: uint) -> BigUint { +//! fn fib(n: usize) -> BigUint { //! let mut f0: BigUint = Zero::zero(); //! let mut f1: BigUint = One::one(); -//! for _ in range(0, n) { +//! for _ in (0..n) { //! let f2 = f0 + &f1; //! // This is a low cost way of swapping f0 with f1 and f1 with f2. //! f0 = replace(&mut f1, f2); @@ -41,14 +41,15 @@ //! It's easy to generate large random numbers: //! //! ```rust +//! # #![allow(unstable)] //! use num::bigint::{ToBigInt, RandBigInt}; //! use std::rand; //! //! let mut rng = rand::thread_rng(); -//! let a = rng.gen_bigint(1000u); +//! let a = rng.gen_bigint(1000); //! -//! let low = -10000i.to_bigint().unwrap(); -//! let high = 10000i.to_bigint().unwrap(); +//! let low = -10000.to_bigint().unwrap(); +//! let high = 10000.to_bigint().unwrap(); //! let b = rng.gen_bigint_range(&low, &high); //! //! // Probably an even larger number. @@ -88,7 +89,7 @@ pub mod BigDigit { use super::DoubleBigDigit; // `DoubleBigDigit` size dependent - pub const BITS: uint = 32; + pub const BITS: usize = 32; pub const BASE: DoubleBigDigit = 1 << BITS; const LO_MASK: DoubleBigDigit = (-1 as DoubleBigDigit) >> BITS; @@ -155,7 +156,7 @@ impl Default for BigUint { fn default() -> BigUint { Zero::zero() } } -impl hash::Hash for BigUint { +impl hash::Hash for BigUint { fn hash(&self, state: &mut S) { // hash 0 in case it's all 0's 0u32.hash(state); @@ -176,6 +177,11 @@ impl fmt::Show for BigUint { write!(f, "{}", to_str_radix(self, 10)) } } +impl fmt::String for BigUint { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", to_str_radix(self, 10)) + } +} impl FromStr for BigUint { #[inline] @@ -274,36 +280,36 @@ impl<'a, 'b> BitXor<&'b BigUint> for &'a BigUint { } } -impl Shl for BigUint { +impl Shl for BigUint { type Output = BigUint; #[inline] - fn shl(self, rhs: uint) -> BigUint { (&self) << rhs } + fn shl(self, rhs: usize) -> BigUint { (&self) << rhs } } -impl<'a> Shl for &'a BigUint { +impl<'a> Shl for &'a BigUint { type Output = BigUint; #[inline] - fn shl(self, rhs: uint) -> BigUint { + fn shl(self, rhs: usize) -> BigUint { let n_unit = rhs / BigDigit::BITS; let n_bits = rhs % BigDigit::BITS; return self.shl_unit(n_unit).shl_bits(n_bits); } } -impl Shr for BigUint { +impl Shr for BigUint { type Output = BigUint; #[inline] - fn shr(self, rhs: uint) -> BigUint { (&self) >> rhs } + fn shr(self, rhs: usize) -> BigUint { (&self) >> rhs } } -impl<'a> Shr for &'a BigUint { +impl<'a> Shr for &'a BigUint { type Output = BigUint; #[inline] - fn shr(self, rhs: uint) -> BigUint { + fn shr(self, rhs: usize) -> BigUint { let n_unit = rhs / BigDigit::BITS; let n_bits = rhs % BigDigit::BITS; return self.shr_unit(n_unit).shr_bits(n_bits); @@ -356,7 +362,7 @@ impl<'a, 'b> Sub<&'b BigUint> for &'a BigUint { let zeros = ZERO_VEC.iter().cycle(); let (a, b) = (self.data.iter().chain(zeros.clone()), other.data.iter().chain(zeros)); - let mut borrow = 0i; + let mut borrow = 0is; let diff: Vec = a.take(new_len).zip(b).map(|(ai, bi)| { let (hi, lo) = BigDigit::from_doublebigdigit( BigDigit::BASE @@ -432,10 +438,10 @@ impl<'a, 'b> Mul<&'b BigUint> for &'a BigUint { } #[inline] - fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) { + fn cut_at(a: &BigUint, n: usize) -> (BigUint, BigUint) { let mid = cmp::min(a.data.len(), n); - (BigUint::from_slice(a.data[mid ..]), - BigUint::from_slice(a.data[.. mid])) + (BigUint::from_slice(&a.data[mid ..]), + BigUint::from_slice(&a.data[.. mid])) } #[inline] @@ -594,13 +600,13 @@ impl Integer for BigUint { } - fn div_estimate(a: &BigUint, b: &BigUint, n: uint) + fn div_estimate(a: &BigUint, b: &BigUint, n: usize) -> (BigUint, BigUint, BigUint) { if a.data.len() < n { return (Zero::zero(), Zero::zero(), (*a).clone()); } - let an = a.data[a.data.len() - n ..]; + let an = &a.data[a.data.len() - n ..]; let bn = *b.data.last().unwrap(); let mut d = Vec::with_capacity(an.len()); let mut carry = 0; @@ -721,12 +727,12 @@ impl FromPrimitive for BigUint { /// A generic trait for converting a value to a `BigUint`. pub trait ToBigUint { /// Converts the value of `self` to a `BigUint`. - fn to_biguint(&self) -> Option; + fn to_bigusize(&self) -> Option; } impl ToBigUint for BigInt { #[inline] - fn to_biguint(&self) -> Option { + fn to_bigusize(&self) -> Option { if self.sign == Plus { Some(self.data.clone()) } else if self.sign == NoSign { @@ -739,43 +745,43 @@ impl ToBigUint for BigInt { impl ToBigUint for BigUint { #[inline] - fn to_biguint(&self) -> Option { + fn to_bigusize(&self) -> Option { Some(self.clone()) } } -macro_rules! impl_to_biguint { +macro_rules! impl_to_bigusize { ($T:ty, $from_ty:path) => { impl ToBigUint for $T { #[inline] - fn to_biguint(&self) -> Option { + fn to_bigusize(&self) -> Option { $from_ty(*self) } } } } -impl_to_biguint!(int, FromPrimitive::from_int); -impl_to_biguint!(i8, FromPrimitive::from_i8); -impl_to_biguint!(i16, FromPrimitive::from_i16); -impl_to_biguint!(i32, FromPrimitive::from_i32); -impl_to_biguint!(i64, FromPrimitive::from_i64); -impl_to_biguint!(uint, FromPrimitive::from_uint); -impl_to_biguint!(u8, FromPrimitive::from_u8); -impl_to_biguint!(u16, FromPrimitive::from_u16); -impl_to_biguint!(u32, FromPrimitive::from_u32); -impl_to_biguint!(u64, FromPrimitive::from_u64); +impl_to_bigusize!(isize, FromPrimitive::from_int); +impl_to_bigusize!(i8, FromPrimitive::from_i8); +impl_to_bigusize!(i16, FromPrimitive::from_i16); +impl_to_bigusize!(i32, FromPrimitive::from_i32); +impl_to_bigusize!(i64, FromPrimitive::from_i64); +impl_to_bigusize!(usize, FromPrimitive::from_uint); +impl_to_bigusize!(u8, FromPrimitive::from_u8); +impl_to_bigusize!(u16, FromPrimitive::from_u16); +impl_to_bigusize!(u32, FromPrimitive::from_u32); +impl_to_bigusize!(u64, FromPrimitive::from_u64); -fn to_str_radix(me: &BigUint, radix: uint) -> String { +fn to_str_radix(me: &BigUint, radix: usize) -> String { assert!(1 < radix && radix <= 16, "The radix must be within (1, 16]"); let (base, max_len) = get_radix_base(radix); if base == BigDigit::BASE { - return fill_concat(me.data[], radix, max_len) + return fill_concat(&me.data[], radix, max_len) } - return fill_concat(convert_base(me, base)[], radix, max_len); + return fill_concat(&convert_base(me, base)[], radix, max_len); fn convert_base(n: &BigUint, base: DoubleBigDigit) -> Vec { - let divider = base.to_biguint().unwrap(); + let divider = base.to_bigusize().unwrap(); let mut result = Vec::new(); let mut m = n.clone(); while m >= divider { @@ -789,21 +795,21 @@ fn to_str_radix(me: &BigUint, radix: uint) -> String { return result; } - fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> String { + fn fill_concat(v: &[BigDigit], radix: usize, l: usize) -> String { if v.is_empty() { return "0".to_string() } let mut s = String::with_capacity(v.len() * l); for n in v.iter().rev() { - let ss = fmt::radix(*n as uint, radix as u8).to_string(); + let ss = fmt::radix(*n as usize, radix as u8).to_string(); s.extend(repeat("0").take(l - ss.len())); - s.push_str(ss[]); + s.push_str(&ss[]); } s.trim_left_matches('0').to_string() } } -fn to_str_radix_signed(me: &BigInt, radix: uint) -> String { +fn to_str_radix_signed(me: &BigInt, radix: usize) -> String { match me.sign { Plus => to_str_radix(&me.data, radix), NoSign => "0".to_string(), @@ -814,9 +820,9 @@ fn to_str_radix_signed(me: &BigInt, radix: uint) -> String { impl FromStrRadix for BigUint { /// Creates and initializes a `BigUint`. #[inline] - fn from_str_radix(s: &str, radix: uint) -> Option { + fn from_str_radix(s: &str, radix: usize) -> Option { let (base, unit_len) = get_radix_base(radix); - let base_num = match base.to_biguint() { + let base_num = match base.to_bigusize() { Some(base_num) => base_num, None => { return None; } }; @@ -826,7 +832,7 @@ impl FromStrRadix for BigUint { let mut power: BigUint = One::one(); loop { let start = cmp::max(end, unit_len) - unit_len; - match FromStrRadix::from_str_radix(s[start .. end], radix) { + match FromStrRadix::from_str_radix(&s[start .. end], radix) { Some(d) => { let d: Option = FromPrimitive::from_uint(d); match d { @@ -873,21 +879,21 @@ impl BigUint { /// Creates and initializes a `BigUint`. #[inline] - pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { + pub fn parse_bytes(buf: &[u8], radix: usize) -> Option { str::from_utf8(buf).ok().and_then(|s| FromStrRadix::from_str_radix(s, radix)) } #[inline] - fn shl_unit(&self, n_unit: uint) -> BigUint { + fn shl_unit(&self, n_unit: usize) -> BigUint { if n_unit == 0 || self.is_zero() { return (*self).clone(); } let mut v = repeat(ZERO_BIG_DIGIT).take(n_unit).collect::>(); - v.push_all(self.data[]); + v.push_all(&self.data[]); BigUint::new(v) } #[inline] - fn shl_bits(&self, n_bits: uint) -> BigUint { + fn shl_bits(&self, n_bits: usize) -> BigUint { if n_bits == 0 || self.is_zero() { return (*self).clone(); } let mut carry = 0; @@ -903,14 +909,14 @@ impl BigUint { } #[inline] - fn shr_unit(&self, n_unit: uint) -> BigUint { + fn shr_unit(&self, n_unit: usize) -> BigUint { if n_unit == 0 { return (*self).clone(); } if self.data.len() < n_unit { return Zero::zero(); } - BigUint::from_slice(self.data[n_unit ..]) + BigUint::from_slice(&self.data[n_unit ..]) } #[inline] - fn shr_bits(&self, n_bits: uint) -> BigUint { + fn shr_bits(&self, n_bits: usize) -> BigUint { if n_bits == 0 || self.data.is_empty() { return (*self).clone(); } let mut borrow = 0; @@ -924,7 +930,7 @@ impl BigUint { } /// Determines the fewest bits necessary to express the `BigUint`. - pub fn bits(&self) -> uint { + pub fn bits(&self) -> usize { if self.is_zero() { return 0; } let zeros = self.data.last().unwrap().leading_zeros(); return self.data.len()*BigDigit::BITS - zeros; @@ -933,7 +939,7 @@ impl BigUint { // `DoubleBigDigit` size dependent #[inline] -fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) { +fn get_radix_base(radix: usize) -> (DoubleBigDigit, usize) { match radix { 2 => (4294967296, 32), 3 => (3486784401, 20), @@ -1019,8 +1025,13 @@ impl fmt::Show for BigInt { write!(f, "{}", to_str_radix_signed(self, 10)) } } +impl fmt::String for BigInt { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", to_str_radix_signed(self, 10)) + } +} -impl hash::Hash for BigInt { +impl hash::Hash for BigInt { fn hash(&self, state: &mut S) { (self.sign == Plus).hash(state); self.data.hash(state); @@ -1036,34 +1047,34 @@ impl FromStr for BigInt { impl Num for BigInt {} -impl Shl for BigInt { +impl Shl for BigInt { type Output = BigInt; #[inline] - fn shl(self, rhs: uint) -> BigInt { (&self) << rhs } + fn shl(self, rhs: usize) -> BigInt { (&self) << rhs } } -impl<'a> Shl for &'a BigInt { +impl<'a> Shl for &'a BigInt { type Output = BigInt; #[inline] - fn shl(self, rhs: uint) -> BigInt { + fn shl(self, rhs: usize) -> BigInt { BigInt::from_biguint(self.sign, &self.data << rhs) } } -impl Shr for BigInt { +impl Shr for BigInt { type Output = BigInt; #[inline] - fn shr(self, rhs: uint) -> BigInt { (&self) >> rhs } + fn shr(self, rhs: usize) -> BigInt { (&self) >> rhs } } -impl<'a> Shr for &'a BigInt { +impl<'a> Shr for &'a BigInt { type Output = BigInt; #[inline] - fn shr(self, rhs: uint) -> BigInt { + fn shr(self, rhs: usize) -> BigInt { BigInt::from_biguint(self.sign, &self.data >> rhs) } } @@ -1427,12 +1438,12 @@ macro_rules! impl_to_bigint { } } -impl_to_bigint!(int, FromPrimitive::from_int); +impl_to_bigint!(isize, FromPrimitive::from_int); impl_to_bigint!(i8, FromPrimitive::from_i8); impl_to_bigint!(i16, FromPrimitive::from_i16); impl_to_bigint!(i32, FromPrimitive::from_i32); impl_to_bigint!(i64, FromPrimitive::from_i64); -impl_to_bigint!(uint, FromPrimitive::from_uint); +impl_to_bigint!(usize, FromPrimitive::from_uint); impl_to_bigint!(u8, FromPrimitive::from_u8); impl_to_bigint!(u16, FromPrimitive::from_u16); impl_to_bigint!(u32, FromPrimitive::from_u32); @@ -1441,7 +1452,7 @@ impl_to_bigint!(u64, FromPrimitive::from_u64); impl FromStrRadix for BigInt { /// Creates and initializes a BigInt. #[inline] - fn from_str_radix(s: &str, radix: uint) -> Option { + fn from_str_radix(s: &str, radix: usize) -> Option { if s.is_empty() { return None; } let mut sign = Plus; let mut start = 0; @@ -1449,26 +1460,26 @@ impl FromStrRadix for BigInt { sign = Minus; start = 1; } - FromStrRadix::from_str_radix(s[start ..], radix) + FromStrRadix::from_str_radix(&s[start ..], radix) .map(|bu| BigInt::from_biguint(sign, bu)) } } pub trait RandBigInt { /// Generate a random `BigUint` of the given bit size. - fn gen_biguint(&mut self, bit_size: uint) -> BigUint; + fn gen_bigusize(&mut self, bit_size: usize) -> BigUint; /// Generate a random BigInt of the given bit size. - fn gen_bigint(&mut self, bit_size: uint) -> BigInt; + fn gen_bigint(&mut self, bit_size: usize) -> BigInt; /// Generate a random `BigUint` less than the given bound. Fails /// when the bound is zero. - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint; + fn gen_bigusize_below(&mut self, bound: &BigUint) -> BigUint; /// Generate a random `BigUint` within the given range. The lower /// bound is inclusive; the upper bound is exclusive. Fails when /// the upper bound is not greater than the lower bound. - fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; + fn gen_bigusize_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; /// Generate a random `BigInt` within the given range. The lower /// bound is inclusive; the upper bound is exclusive. Fails when @@ -1477,7 +1488,7 @@ pub trait RandBigInt { } impl RandBigInt for R { - fn gen_biguint(&mut self, bit_size: uint) -> BigUint { + fn gen_bigusize(&mut self, bit_size: usize) -> BigUint { let (digits, rem) = bit_size.div_rem(&BigDigit::BITS); let mut data = Vec::with_capacity(digits+1); for _ in range(0, digits) { @@ -1490,11 +1501,11 @@ impl RandBigInt for R { BigUint::new(data) } - fn gen_bigint(&mut self, bit_size: uint) -> BigInt { + fn gen_bigint(&mut self, bit_size: usize) -> BigInt { // Generate a random BigUint... - let biguint = self.gen_biguint(bit_size); + let bigusize = self.gen_bigusize(bit_size); // ...and then randomly assign it a Sign... - let sign = if biguint.is_zero() { + let sign = if bigusize.is_zero() { // ...except that if the BigUint is zero, we need to try // again with probability 0.5. This is because otherwise, // the probability of generating a zero BigInt would be @@ -1509,24 +1520,24 @@ impl RandBigInt for R { } else { Minus }; - BigInt::from_biguint(sign, biguint) + BigInt::from_biguint(sign, bigusize) } - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint { + fn gen_bigusize_below(&mut self, bound: &BigUint) -> BigUint { assert!(!bound.is_zero()); let bits = bound.bits(); loop { - let n = self.gen_biguint(bits); + let n = self.gen_bigusize(bits); if n < *bound { return n; } } } - fn gen_biguint_range(&mut self, + fn gen_bigusize_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint { assert!(*lbound < *ubound); - return lbound + self.gen_biguint_below(&(ubound - lbound)); + return lbound + self.gen_bigusize_below(&(ubound - lbound)); } fn gen_bigint_range(&mut self, @@ -1534,8 +1545,8 @@ impl RandBigInt for R { ubound: &BigInt) -> BigInt { assert!(*lbound < *ubound); - let delta = (ubound - lbound).to_biguint().unwrap(); - return lbound + self.gen_biguint_below(&delta).to_bigint().unwrap(); + let delta = (ubound - lbound).to_bigusize().unwrap(); + return lbound + self.gen_bigusize_below(&delta).to_bigint().unwrap(); } } @@ -1567,14 +1578,14 @@ impl BigInt { /// Creates and initializes a `BigInt`. #[inline] - pub fn parse_bytes(buf: &[u8], radix: uint) -> Option { + pub fn parse_bytes(buf: &[u8], radix: usize) -> Option { str::from_utf8(buf).ok().and_then(|s| FromStrRadix::from_str_radix(s, radix)) } /// Converts this `BigInt` into a `BigUint`, if it's not negative. #[inline] - pub fn to_biguint(&self) -> Option { + pub fn to_bigusize(&self) -> Option { match self.sign { Plus => Some(self.data.clone()), NoSign => Some(Zero::zero()), @@ -1607,14 +1618,13 @@ impl BigInt { } #[cfg(test)] -mod biguint_tests { +mod bigusize_tests { use Integer; use super::{BigDigit, BigUint, ToBigUint, to_str_radix}; use super::{BigInt, RandBigInt, ToBigInt}; use super::Sign::Plus; use std::cmp::Ordering::{Less, Equal, Greater}; - use std::hash::hash; use std::i64; use std::iter::repeat; use std::num::FromStrRadix; @@ -1682,10 +1692,10 @@ mod biguint_tests { let c = BigUint::new(vec!(1)); let d = BigUint::new(vec!(1,0,0,0,0,0)); let e = BigUint::new(vec!(0,0,0,0,0,1)); - assert!(hash(&a) == hash(&b)); - assert!(hash(&b) != hash(&c)); - assert!(hash(&c) == hash(&d)); - assert!(hash(&d) != hash(&e)); + assert!(::hash(&a) == ::hash(&b)); + assert!(::hash(&b) != ::hash(&c)); + assert!(::hash(&c) == ::hash(&d)); + assert!(::hash(&d) != ::hash(&e)); } #[test] @@ -1732,9 +1742,9 @@ mod biguint_tests { #[test] fn test_shl() { - fn check(s: &str, shift: uint, ans: &str) { - let opt_biguint: Option = FromStrRadix::from_str_radix(s, 16); - let bu = to_str_radix(&(opt_biguint.unwrap() << shift), 16); + fn check(s: &str, shift: usize, ans: &str) { + let opt_bigusize: Option = FromStrRadix::from_str_radix(s, 16); + let bu = to_str_radix(&(opt_bigusize.unwrap() << shift), 16); assert_eq!(bu.as_slice(), ans); } @@ -1853,10 +1863,10 @@ mod biguint_tests { #[test] fn test_shr() { - fn check(s: &str, shift: uint, ans: &str) { - let opt_biguint: Option = + fn check(s: &str, shift: usize, ans: &str) { + let opt_bigusize: Option = FromStrRadix::from_str_radix(s, 16); - let bu = to_str_radix(&(opt_biguint.unwrap() >> shift), 16); + let bu = to_str_radix(&(opt_bigusize.unwrap() >> shift), 16); assert_eq!(bu.as_slice(), ans); } @@ -1980,7 +1990,7 @@ mod biguint_tests { check(Zero::zero(), 0); check(One::one(), 1); - check(i64::MAX.to_biguint().unwrap(), i64::MAX); + check(i64::MAX.to_bigusize().unwrap(), i64::MAX); check(BigUint::new(vec!( )), 0); check(BigUint::new(vec!( 1 )), (1 << (0*BigDigit::BITS))); @@ -1988,7 +1998,7 @@ mod biguint_tests { check(BigUint::new(vec!( 0, 1 )), (1 << (1*BigDigit::BITS))); check(BigUint::new(vec!(-1, -1 >> 1)), i64::MAX); - assert_eq!(i64::MIN.to_biguint(), None); + assert_eq!(i64::MIN.to_bigusize(), None); assert_eq!(BigUint::new(vec!(-1, -1 )).to_i64(), None); assert_eq!(BigUint::new(vec!( 0, 0, 1)).to_i64(), None); assert_eq!(BigUint::new(vec!(-1, -1, -1)).to_i64(), None); @@ -2005,8 +2015,8 @@ mod biguint_tests { check(Zero::zero(), 0); check(One::one(), 1); - check(u64::MIN.to_biguint().unwrap(), u64::MIN); - check(u64::MAX.to_biguint().unwrap(), u64::MAX); + check(u64::MIN.to_bigusize().unwrap(), u64::MIN); + check(u64::MAX.to_bigusize().unwrap(), u64::MAX); check(BigUint::new(vec!( )), 0); check(BigUint::new(vec!( 1 )), (1 << (0*BigDigit::BITS))); @@ -2022,7 +2032,7 @@ mod biguint_tests { fn test_convert_to_bigint() { fn check(n: BigUint, ans: BigInt) { assert_eq!(n.to_bigint().unwrap(), ans); - assert_eq!(n.to_bigint().unwrap().to_biguint().unwrap(), n); + assert_eq!(n.to_bigint().unwrap().to_bigusize().unwrap(), n); } check(Zero::zero(), Zero::zero()); check(BigUint::new(vec!(1,2,3)), @@ -2243,7 +2253,7 @@ mod biguint_tests { #[test] fn test_gcd() { - fn check(a: uint, b: uint, c: uint) { + fn check(a: usize, b: usize, c: usize) { let big_a: BigUint = FromPrimitive::from_uint(a).unwrap(); let big_b: BigUint = FromPrimitive::from_uint(b).unwrap(); let big_c: BigUint = FromPrimitive::from_uint(c).unwrap(); @@ -2260,7 +2270,7 @@ mod biguint_tests { #[test] fn test_lcm() { - fn check(a: uint, b: uint, c: uint) { + fn check(a: usize, b: usize, c: usize) { let big_a: BigUint = FromPrimitive::from_uint(a).unwrap(); let big_b: BigUint = FromPrimitive::from_uint(b).unwrap(); let big_c: BigUint = FromPrimitive::from_uint(c).unwrap(); @@ -2292,7 +2302,7 @@ mod biguint_tests { assert!(((&one << 64) + one).is_odd()); } - fn to_str_pairs() -> Vec<(BigUint, Vec<(uint, String)>)> { + fn to_str_pairs() -> Vec<(BigUint, Vec<(usize, String)>)> { let bits = BigDigit::BITS; vec!(( Zero::zero(), vec!( (2, "0".to_string()), (3, "0".to_string()) @@ -2386,7 +2396,7 @@ mod biguint_tests { #[test] fn test_factor() { - fn factor(n: uint) -> BigUint { + fn factor(n: usize) -> BigUint { let mut f: BigUint = One::one(); for i in range(2, n + 1) { // FIXME(#5992): assignment operator overloads @@ -2397,7 +2407,7 @@ mod biguint_tests { return f; } - fn check(n: uint, s: &str) { + fn check(n: usize, s: &str) { let n = factor(n); let ans = match FromStrRadix::from_str_radix(s, 10) { Some(x) => x, None => panic!() @@ -2429,15 +2439,15 @@ mod biguint_tests { #[test] fn test_rand() { let mut rng = thread_rng(); - let _n: BigUint = rng.gen_biguint(137); - assert!(rng.gen_biguint(0).is_zero()); + let _n: BigUint = rng.gen_bigusize(137); + assert!(rng.gen_bigusize(0).is_zero()); } #[test] fn test_rand_range() { let mut rng = thread_rng(); - for _ in range(0u, 10) { + for _ in range(0, 10) { assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_uint(236).unwrap(), &FromPrimitive::from_uint(237).unwrap()), FromPrimitive::from_uint(236).unwrap()); @@ -2445,11 +2455,11 @@ mod biguint_tests { let l = FromPrimitive::from_uint(403469000 + 2352).unwrap(); let u = FromPrimitive::from_uint(403469000 + 3513).unwrap(); - for _ in range(0u, 1000) { - let n: BigUint = rng.gen_biguint_below(&u); + for _ in range(0, 1000) { + let n: BigUint = rng.gen_bigusize_below(&u); assert!(n < u); - let n: BigUint = rng.gen_biguint_range(&l, &u); + let n: BigUint = rng.gen_bigusize_range(&l, &u); assert!(n >= l); assert!(n < u); } @@ -2458,7 +2468,7 @@ mod biguint_tests { #[test] #[should_fail] fn test_zero_rand_range() { - thread_rng().gen_biguint_range(&FromPrimitive::from_uint(54).unwrap(), + thread_rng().gen_bigusize_range(&FromPrimitive::from_uint(54).unwrap(), &FromPrimitive::from_uint(54).unwrap()); } @@ -2469,7 +2479,7 @@ mod biguint_tests { let l = FromPrimitive::from_uint(2352).unwrap(); let u = FromPrimitive::from_uint(3513).unwrap(); // Switching u and l should fail: - let _n: BigUint = rng.gen_biguint_range(&u, &l); + let _n: BigUint = rng.gen_bigusize_range(&u, &l); } } @@ -2481,7 +2491,6 @@ mod bigint_tests { use super::Sign::{Minus, NoSign, Plus}; use std::cmp::Ordering::{Less, Equal, Greater}; - use std::hash::hash; use std::i64; use std::iter::repeat; use std::num::FromStrRadix; @@ -2494,7 +2503,7 @@ mod bigint_tests { #[test] fn test_from_biguint() { - fn check(inp_s: Sign, inp_n: uint, ans_s: Sign, ans_n: uint) { + fn check(inp_s: Sign, inp_n: usize, ans_s: Sign, ans_n: usize) { let inp = BigInt::from_biguint(inp_s, FromPrimitive::from_uint(inp_n).unwrap()); let ans = BigInt { sign: ans_s, data: FromPrimitive::from_uint(ans_n).unwrap()}; assert_eq!(inp, ans); @@ -2556,11 +2565,11 @@ mod bigint_tests { let d = BigInt::new(Plus, vec!(1,0,0,0,0,0)); let e = BigInt::new(Plus, vec!(0,0,0,0,0,1)); let f = BigInt::new(Minus, vec!(1)); - assert!(hash(&a) == hash(&b)); - assert!(hash(&b) != hash(&c)); - assert!(hash(&c) == hash(&d)); - assert!(hash(&d) != hash(&e)); - assert!(hash(&c) != hash(&f)); + assert!(::hash(&a) == ::hash(&b)); + assert!(::hash(&b) != ::hash(&c)); + assert!(::hash(&c) == ::hash(&d)); + assert!(::hash(&d) != ::hash(&e)); + assert!(::hash(&c) != ::hash(&f)); } #[test] @@ -2616,10 +2625,10 @@ mod bigint_tests { } #[test] - fn test_convert_to_biguint() { + fn test_convert_to_bigusize() { fn check(n: BigInt, ans_1: BigUint) { - assert_eq!(n.to_biguint().unwrap(), ans_1); - assert_eq!(n.to_biguint().unwrap().to_bigint().unwrap(), n); + assert_eq!(n.to_bigusize().unwrap(), ans_1); + assert_eq!(n.to_bigusize().unwrap().to_bigint().unwrap(), n); } let zero: BigInt = Zero::zero(); let unsigned_zero: BigUint = Zero::zero(); @@ -2630,7 +2639,7 @@ mod bigint_tests { check(zero, unsigned_zero); check(positive, BigUint::new(vec!(1,2,3))); - assert_eq!(negative.to_biguint(), None); + assert_eq!(negative.to_bigusize(), None); } const SUM_TRIPLES: &'static [(&'static [BigDigit], @@ -2934,7 +2943,7 @@ mod bigint_tests { #[test] fn test_gcd() { - fn check(a: int, b: int, c: int) { + fn check(a: isize, b: isize, c: isize) { let big_a: BigInt = FromPrimitive::from_int(a).unwrap(); let big_b: BigInt = FromPrimitive::from_int(b).unwrap(); let big_c: BigInt = FromPrimitive::from_int(c).unwrap(); @@ -2954,7 +2963,7 @@ mod bigint_tests { #[test] fn test_lcm() { - fn check(a: int, b: int, c: int) { + fn check(a: isize, b: isize, c: isize) { let big_a: BigInt = FromPrimitive::from_int(a).unwrap(); let big_b: BigInt = FromPrimitive::from_int(b).unwrap(); let big_c: BigInt = FromPrimitive::from_int(c).unwrap(); @@ -2990,7 +2999,7 @@ mod bigint_tests { #[test] fn test_from_str_radix() { - fn check(s: &str, ans: Option) { + fn check(s: &str, ans: Option) { let ans = ans.map(|n| { let x: BigInt = FromPrimitive::from_int(n).unwrap(); x @@ -3033,7 +3042,7 @@ mod bigint_tests { fn test_rand_range() { let mut rng = thread_rng(); - for _ in range(0u, 10) { + for _ in range(0, 10) { assert_eq!(rng.gen_bigint_range(&FromPrimitive::from_uint(236).unwrap(), &FromPrimitive::from_uint(237).unwrap()), FromPrimitive::from_uint(236).unwrap()); @@ -3041,7 +3050,7 @@ mod bigint_tests { fn check(l: BigInt, u: BigInt) { let mut rng = thread_rng(); - for _ in range(0u, 1000) { + for _ in range(0, 1000) { let n: BigInt = rng.gen_bigint_range(&l, &u); assert!(n >= l); assert!(n < u); @@ -3083,7 +3092,7 @@ mod bench { use {Zero, One}; - fn factorial(n: uint) -> BigUint { + fn factorial(n: usize) -> BigUint { let mut f: BigUint = One::one(); for i in iter::range_inclusive(1, n) { let bu: BigUint = FromPrimitive::from_uint(i).unwrap(); @@ -3092,7 +3101,7 @@ mod bench { f } - fn fib(n: uint) -> BigUint { + fn fib(n: usize) -> BigUint { let mut f0: BigUint = Zero::zero(); let mut f1: BigUint = One::one(); for _ in range(0, n) { @@ -3133,7 +3142,7 @@ mod bench { let n = { let one : BigUint = One::one(); one << 1000 }; b.iter(|| { let mut m = n.clone(); - for _ in range(0u, 10) { + for _ in range(0, 10) { m = m >> 1; } }) diff --git a/src/complex.rs b/src/complex.rs index abac2bd..3eaf169 100644 --- a/src/complex.rs +++ b/src/complex.rs @@ -242,6 +242,15 @@ impl One for Complex { /* string conversions */ impl fmt::Show for Complex { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.im < Zero::zero() { + write!(f, "{:?}-{:?}i", self.re, -self.im.clone()) + } else { + write!(f, "{:?}+{:?}i", self.re, self.im) + } + } +} +impl fmt::String for Complex { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.im < Zero::zero() { write!(f, "{}-{}i", self.re, -self.im.clone()) @@ -258,7 +267,6 @@ mod test { use super::{Complex64, Complex}; use std::f64; use std::num::Float; - use std::hash::hash; use {Zero, One}; @@ -327,7 +335,7 @@ mod test { #[test] #[should_fail] fn test_divide_by_zero_natural() { - let n = Complex::new(2i, 3i); + let n = Complex::new(2, 3); let d = Complex::new(0, 0); let _x = n / d; } @@ -437,11 +445,13 @@ mod test { #[test] fn test_hash() { + + let a = Complex::new(0i32, 0i32); let b = Complex::new(1i32, 0i32); let c = Complex::new(0i32, 1i32); - assert!(hash(&a) != hash(&b)); - assert!(hash(&b) != hash(&c)); - assert!(hash(&c) != hash(&a)); + assert!(::hash(&a) != ::hash(&b)); + assert!(::hash(&b) != ::hash(&c)); + assert!(::hash(&c) != ::hash(&a)); } } diff --git a/src/integer.rs b/src/integer.rs index a44b552..078ca93 100644 --- a/src/integer.rs +++ b/src/integer.rs @@ -174,7 +174,7 @@ pub trait Integer: Sized + Num + PartialOrd /// Calculates the Lowest Common Multiple (LCM) of the number and `other`. #[inline(always)] pub fn lcm(x: T, y: T) -> T { x.lcm(&y) } -macro_rules! impl_integer_for_int { +macro_rules! impl_integer_for_isize { ($T:ty, $test_mod:ident) => ( impl Integer for $T { /// Floored integer division @@ -377,13 +377,13 @@ macro_rules! impl_integer_for_int { ) } -impl_integer_for_int!(i8, test_integer_i8); -impl_integer_for_int!(i16, test_integer_i16); -impl_integer_for_int!(i32, test_integer_i32); -impl_integer_for_int!(i64, test_integer_i64); -impl_integer_for_int!(int, test_integer_int); +impl_integer_for_isize!(i8, test_integer_i8); +impl_integer_for_isize!(i16, test_integer_i16); +impl_integer_for_isize!(i32, test_integer_i32); +impl_integer_for_isize!(i64, test_integer_i64); +impl_integer_for_isize!(isize, test_integer_isize); -macro_rules! impl_integer_for_uint { +macro_rules! impl_integer_for_usize { ($T:ty, $test_mod:ident) => ( impl Integer for $T { /// Unsigned integer division. Returns the same result as `div` (`/`). @@ -502,8 +502,8 @@ macro_rules! impl_integer_for_uint { ) } -impl_integer_for_uint!(u8, test_integer_u8); -impl_integer_for_uint!(u16, test_integer_u16); -impl_integer_for_uint!(u32, test_integer_u32); -impl_integer_for_uint!(u64, test_integer_u64); -impl_integer_for_uint!(uint, test_integer_uint); +impl_integer_for_usize!(u8, test_integer_u8); +impl_integer_for_usize!(u16, test_integer_u16); +impl_integer_for_usize!(u32, test_integer_u32); +impl_integer_for_usize!(u64, test_integer_u64); +impl_integer_for_usize!(usize, test_integer_usize); diff --git a/src/iter.rs b/src/iter.rs index ecf0fd2..dd67fc5 100644 --- a/src/iter.rs +++ b/src/iter.rs @@ -28,9 +28,11 @@ pub struct Range { /// # Example /// /// ```rust +/// use num::iter; +/// /// let array = [0, 1, 2, 3, 4]; /// -/// for i in range(0, 5u) { +/// for i in iter::range(0, 5) { /// println!("{}", i); /// assert_eq!(i, array[i]); /// } @@ -60,9 +62,9 @@ impl Iterator for Range } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { // This first checks if the elements are representable as i64. If they aren't, try u64 (to - // handle cases like range(huge, huger)). We don't use uint/int because the difference of + // handle cases like range(huge, huger)). We don't use usize/int because the difference of // the i64/u64 might lie within their range. let bound = match self.state.to_i64() { Some(a) => { @@ -144,7 +146,7 @@ impl Iterator for RangeInclusive } #[inline] - fn size_hint(&self) -> (uint, Option) { + fn size_hint(&self) -> (usize, Option) { let (lo, hi) = self.range.size_hint(); if self.done { (lo, hi) @@ -257,7 +259,7 @@ impl Iterator for RangeStepInclusive #[cfg(test)] mod tests { - use std::uint; + use std::usize; use std::num::ToPrimitive; use std::ops::{Add, Mul}; use std::cmp::Ordering; @@ -313,61 +315,61 @@ mod tests { } } - assert!(super::range(0i, 5).collect::>() == vec![0i, 1, 2, 3, 4]); - assert!(super::range(-10i, -1).collect::>() == + assert!(super::range(0, 5).collect::>() == vec![0, 1, 2, 3, 4]); + assert!(super::range(-10, -1).collect::>() == vec![-10, -9, -8, -7, -6, -5, -4, -3, -2]); - assert!(super::range(0i, 5).rev().collect::>() == vec![4, 3, 2, 1, 0]); - assert_eq!(super::range(200i, -5).count(), 0); - assert_eq!(super::range(200i, -5).rev().count(), 0); - assert_eq!(super::range(200i, 200).count(), 0); - assert_eq!(super::range(200i, 200).rev().count(), 0); + assert!(super::range(0, 5).rev().collect::>() == vec![4, 3, 2, 1, 0]); + assert_eq!(super::range(200, -5).count(), 0); + assert_eq!(super::range(200, -5).rev().count(), 0); + assert_eq!(super::range(200, 200).count(), 0); + assert_eq!(super::range(200, 200).rev().count(), 0); - assert_eq!(super::range(0i, 100).size_hint(), (100, Some(100))); - // this test is only meaningful when sizeof uint < sizeof u64 - assert_eq!(super::range(uint::MAX - 1, uint::MAX).size_hint(), (1, Some(1))); - assert_eq!(super::range(-10i, -1).size_hint(), (9, Some(9))); + assert_eq!(super::range(0, 100).size_hint(), (100, Some(100))); + // this test is only meaningful when sizeof usize < sizeof u64 + assert_eq!(super::range(usize::MAX - 1, usize::MAX).size_hint(), (1, Some(1))); + assert_eq!(super::range(-10, -1).size_hint(), (9, Some(9))); assert_eq!(super::range(Foo, Foo).size_hint(), (0, None)); } #[test] fn test_range_inclusive() { - assert!(super::range_inclusive(0i, 5).collect::>() == - vec![0i, 1, 2, 3, 4, 5]); - assert!(super::range_inclusive(0i, 5).rev().collect::>() == - vec![5i, 4, 3, 2, 1, 0]); - assert_eq!(super::range_inclusive(200i, -5).count(), 0); - assert_eq!(super::range_inclusive(200i, -5).rev().count(), 0); - assert!(super::range_inclusive(200i, 200).collect::>() == vec![200]); - assert!(super::range_inclusive(200i, 200).rev().collect::>() == vec![200]); + assert!(super::range_inclusive(0, 5).collect::>() == + vec![0, 1, 2, 3, 4, 5]); + assert!(super::range_inclusive(0, 5).rev().collect::>() == + vec![5, 4, 3, 2, 1, 0]); + assert_eq!(super::range_inclusive(200, -5).count(), 0); + assert_eq!(super::range_inclusive(200, -5).rev().count(), 0); + assert!(super::range_inclusive(200, 200).collect::>() == vec![200]); + assert!(super::range_inclusive(200, 200).rev().collect::>() == vec![200]); } #[test] fn test_range_step() { - assert!(super::range_step(0i, 20, 5).collect::>() == + assert!(super::range_step(0, 20, 5).collect::>() == vec![0, 5, 10, 15]); - assert!(super::range_step(20i, 0, -5).collect::>() == + assert!(super::range_step(20, 0, -5).collect::>() == vec![20, 15, 10, 5]); - assert!(super::range_step(20i, 0, -6).collect::>() == + assert!(super::range_step(20, 0, -6).collect::>() == vec![20, 14, 8, 2]); assert!(super::range_step(200u8, 255, 50).collect::>() == vec![200u8, 250]); - assert!(super::range_step(200i, -5, 1).collect::>() == vec![]); - assert!(super::range_step(200i, 200, 1).collect::>() == vec![]); + assert!(super::range_step(200, -5, 1).collect::>() == vec![]); + assert!(super::range_step(200, 200, 1).collect::>() == vec![]); } #[test] fn test_range_step_inclusive() { - assert!(super::range_step_inclusive(0i, 20, 5).collect::>() == + assert!(super::range_step_inclusive(0, 20, 5).collect::>() == vec![0, 5, 10, 15, 20]); - assert!(super::range_step_inclusive(20i, 0, -5).collect::>() == + assert!(super::range_step_inclusive(20, 0, -5).collect::>() == vec![20, 15, 10, 5, 0]); - assert!(super::range_step_inclusive(20i, 0, -6).collect::>() == + assert!(super::range_step_inclusive(20, 0, -6).collect::>() == vec![20, 14, 8, 2]); assert!(super::range_step_inclusive(200u8, 255, 50).collect::>() == vec![200u8, 250]); - assert!(super::range_step_inclusive(200i, -5, 1).collect::>() == + assert!(super::range_step_inclusive(200, -5, 1).collect::>() == vec![]); - assert!(super::range_step_inclusive(200i, 200, 1).collect::>() == + assert!(super::range_step_inclusive(200, 200, 1).collect::>() == vec![200]); } } diff --git a/src/lib.rs b/src/lib.rs index f83caa0..5f5ad45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,13 +18,14 @@ //! approximate a square root to arbitrary precision: //! //! ``` +//! # #![allow(unstable)] //! extern crate num; //! //! use std::num::FromPrimitive; //! use num::bigint::BigInt; //! use num::rational::{Ratio, BigRational}; //! -//! fn approx_sqrt(number: u64, iterations: uint) -> BigRational { +//! fn approx_sqrt(number: u64, iterations: usize) -> BigRational { //! let start: Ratio = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap()); //! let mut approx = start.clone(); //! @@ -50,6 +51,8 @@ html_root_url = "http://doc.rust-lang.org/num/", html_playground_url = "http://play.rust-lang.org/")] +#![allow(unstable)] + extern crate "rustc-serialize" as rustc_serialize; pub use bigint::{BigInt, BigUint}; @@ -60,6 +63,8 @@ pub use iter::{range, range_inclusive, range_step, range_step_inclusive}; pub use traits::{Num, Zero, One, Signed, Unsigned, Bounded, Saturating, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv}; +#[cfg(test)] use std::hash; + use std::ops::{Mul}; pub mod bigint; @@ -119,7 +124,7 @@ pub fn abs_sub(x: T, y: T) -> T { /// assert_eq!(num::pow(2i, 4), 16); /// ``` #[inline] -pub fn pow>(mut base: T, mut exp: uint) -> T { +pub fn pow>(mut base: T, mut exp: usize) -> T { if exp == 1 { base } else { let mut acc = one::(); @@ -133,3 +138,8 @@ pub fn pow>(mut base: T, mut exp: uint) -> T acc } } + +#[cfg(test)] +fn hash>(x: &T) -> u64 { + hash::hash::<_, hash::SipHasher>(x) +} diff --git a/src/rational.rs b/src/rational.rs index 7226152..ea20c50 100644 --- a/src/rational.rs +++ b/src/rational.rs @@ -30,7 +30,7 @@ pub struct Ratio { } /// Alias for a `Ratio` of machine-sized integers. -pub type Rational = Ratio; +pub type Rational = Ratio; pub type Rational32 = Ratio; pub type Rational64 = Ratio; @@ -192,12 +192,12 @@ impl Ratio { let bigint_sign = if sign == 1 { Sign::Plus } else { Sign::Minus }; if exponent < 0 { let one: BigInt = One::one(); - let denom: BigInt = one << ((-exponent) as uint); + let denom: BigInt = one << ((-exponent) as usize); let numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap(); Some(Ratio::new(BigInt::from_biguint(bigint_sign, numer), denom)) } else { let mut numer: BigUint = FromPrimitive::from_u64(mantissa).unwrap(); - numer = numer << (exponent as uint); + numer = numer << (exponent as usize); Some(Ratio::from_integer(BigInt::from_biguint(bigint_sign, numer))) } } @@ -407,6 +407,16 @@ impl /* String conversions */ impl fmt::Show for Ratio { + /// Renders as `numer/denom`. If denom=1, renders as numer. + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.denom == One::one() { + write!(f, "{:?}", self.numer) + } else { + write!(f, "{:?}/{:?}", self.numer, self.denom) + } + } +} +impl fmt::String for Ratio { /// Renders as `numer/denom`. If denom=1, renders as numer. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { if self.denom == One::one() { @@ -436,7 +446,7 @@ impl impl FromStrRadix for Ratio { /// Parses `numer/denom` where the numbers are in base `radix`. - fn from_str_radix(s: &str, radix: uint) -> Option> { + fn from_str_radix(s: &str, radix: usize) -> Option> { let split: Vec<&str> = s.splitn(1, '/').collect(); if split.len() < 2 { None @@ -461,7 +471,6 @@ mod test { use super::{Ratio, Rational, BigRational}; use std::num::{FromPrimitive, Float}; use std::str::FromStr; - use std::hash::hash; use std::i32; use {Zero, One, Signed}; @@ -488,22 +497,22 @@ mod test { // check our constants are what Ratio::new etc. would make. assert_eq!(_0, Zero::zero()); assert_eq!(_1, One::one()); - assert_eq!(_2, Ratio::from_integer(2i)); - assert_eq!(_1_2, Ratio::new(1i,2i)); - assert_eq!(_3_2, Ratio::new(3i,2i)); - assert_eq!(_NEG1_2, Ratio::new(-1i,2i)); + assert_eq!(_2, Ratio::from_integer(2)); + assert_eq!(_1_2, Ratio::new(1,2)); + assert_eq!(_3_2, Ratio::new(3,2)); + assert_eq!(_NEG1_2, Ratio::new(-1,2)); } #[test] fn test_new_reduce() { - let one22 = Ratio::new(2i,2); + let one22 = Ratio::new(2,2); assert_eq!(one22, One::one()); } #[test] #[should_fail] fn test_new_zero() { - let _a = Ratio::new(1i,0); + let _a = Ratio::new(1,0); } @@ -568,7 +577,7 @@ mod test { assert_eq!(format!("{}", _2), "2".to_string()); assert_eq!(format!("{}", _1_2), "1/2".to_string()); assert_eq!(format!("{}", _0), "0".to_string()); - assert_eq!(format!("{}", Ratio::from_integer(-2i)), "-2".to_string()); + assert_eq!(format!("{}", Ratio::from_integer(-2)), "-2".to_string()); } mod arith { @@ -608,8 +617,8 @@ mod test { } test(_1, _1_2, _1_2); - test(_1_2, _3_2, Ratio::new(3i,4i)); - test(_1_2, _NEG1_2, Ratio::new(-1i, 4i)); + test(_1_2, _3_2, Ratio::new(3,4)); + test(_1_2, _NEG1_2, Ratio::new(-1, 4)); } #[test] @@ -741,7 +750,7 @@ mod test { #[test] fn test_to_from_str() { fn test(r: Rational, s: String) { - assert_eq!(FromStr::from_str(s[]), Some(r)); + assert_eq!(FromStr::from_str(&s[]), Some(r)); assert_eq!(r.to_string(), s); } test(_1, "1".to_string()); @@ -808,7 +817,7 @@ mod test { assert_eq!(_3_2.abs_sub(&_1_2), _1); assert_eq!(_1_2.abs_sub(&_3_2), Zero::zero()); assert_eq!(_1_2.signum(), One::one()); - assert_eq!(_NEG1_2.signum(), - ::one::>()); + assert_eq!(_NEG1_2.signum(), - ::one::>()); assert!(_NEG1_2.is_negative()); assert!(! _NEG1_2.is_positive()); assert!(! _1_2.is_negative()); @@ -816,7 +825,7 @@ mod test { #[test] fn test_hash() { - assert!(hash(&_0) != hash(&_1)); - assert!(hash(&_0) != hash(&_3_2)); + assert!(::hash(&_0) != ::hash(&_1)); + assert!(::hash(&_0) != ::hash(&_3_2)); } } diff --git a/src/traits.rs b/src/traits.rs index c76380a..c814b79 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -12,8 +12,8 @@ use std::intrinsics; use std::ops::{Add, Div, Mul, Neg, Rem, Sub}; -use std::{uint, u8, u16, u32, u64}; -use std::{int, i8, i16, i32, i64}; +use std::{usize, u8, u16, u32, u64}; +use std::{isize, i8, i16, i32, i64}; use std::{f32, f64}; /// The base trait for numeric types @@ -27,7 +27,7 @@ macro_rules! trait_impl { )*) } -trait_impl!(Num for uint u8 u16 u32 u64 int i8 i16 i32 i64 f32 f64); +trait_impl!(Num for usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64); /// Defines an additive identity element for `Self`. /// @@ -70,13 +70,13 @@ macro_rules! zero_impl { } } -zero_impl!(uint, 0u); +zero_impl!(usize, 0us); zero_impl!(u8, 0u8); zero_impl!(u16, 0u16); zero_impl!(u32, 0u32); zero_impl!(u64, 0u64); -zero_impl!(int, 0i); +zero_impl!(isize, 0is); zero_impl!(i8, 0i8); zero_impl!(i16, 0i16); zero_impl!(i32, 0i32); @@ -114,13 +114,13 @@ macro_rules! one_impl { } } -one_impl!(uint, 1u); +one_impl!(usize, 1us); one_impl!(u8, 1u8); one_impl!(u16, 1u16); one_impl!(u32, 1u32); one_impl!(u64, 1u64); -one_impl!(int, 1i); +one_impl!(isize, 1is); one_impl!(i8, 1i8); one_impl!(i16, 1i16); one_impl!(i32, 1i32); @@ -197,7 +197,7 @@ macro_rules! signed_impl { )*) } -signed_impl!(int i8 i16 i32 i64); +signed_impl!(isize i8 i16 i32 i64); macro_rules! signed_float_impl { ($t:ty, $nan:expr, $inf:expr, $neg_inf:expr, $fabs:path, $fcopysign:path, $fdim:ident) => { @@ -248,7 +248,7 @@ signed_float_impl!(f64, f64::NAN, f64::INFINITY, f64::NEG_INFINITY, /// A trait for values which cannot be negative pub trait Unsigned: Num {} -trait_impl!(Unsigned for uint u8 u16 u32 u64); +trait_impl!(Unsigned for usize u8 u16 u32 u64); /// Numbers which have upper and lower bounds pub trait Bounded { @@ -271,13 +271,13 @@ macro_rules! bounded_impl { } } -bounded_impl!(uint, uint::MIN, uint::MAX); +bounded_impl!(usize, usize::MIN, usize::MAX); bounded_impl!(u8, u8::MIN, u8::MAX); bounded_impl!(u16, u16::MIN, u16::MAX); bounded_impl!(u32, u32::MIN, u32::MAX); bounded_impl!(u64, u64::MIN, u64::MAX); -bounded_impl!(int, int::MIN, int::MAX); +bounded_impl!(isize, isize::MIN, isize::MAX); bounded_impl!(i8, i8::MIN, i8::MAX); bounded_impl!(i16, i16::MIN, i16::MAX); bounded_impl!(i32, i32::MIN, i32::MAX); @@ -364,20 +364,20 @@ macro_rules! checked_cast_impl { } } -#[cfg(target_word_size = "32")] -checked_cast_impl!(CheckedAdd, checked_add, uint, u32, intrinsics::u32_add_with_overflow); -#[cfg(target_word_size = "64")] -checked_cast_impl!(CheckedAdd, checked_add, uint, u64, intrinsics::u64_add_with_overflow); +#[cfg(target_pointer_width = "32")] +checked_cast_impl!(CheckedAdd, checked_add, usize, u32, intrinsics::u32_add_with_overflow); +#[cfg(target_pointer_width = "64")] +checked_cast_impl!(CheckedAdd, checked_add, usize, u64, intrinsics::u64_add_with_overflow); checked_impl!(CheckedAdd, checked_add, u8, intrinsics::u8_add_with_overflow); checked_impl!(CheckedAdd, checked_add, u16, intrinsics::u16_add_with_overflow); checked_impl!(CheckedAdd, checked_add, u32, intrinsics::u32_add_with_overflow); checked_impl!(CheckedAdd, checked_add, u64, intrinsics::u64_add_with_overflow); -#[cfg(target_word_size = "32")] -checked_cast_impl!(CheckedAdd, checked_add, int, i32, intrinsics::i32_add_with_overflow); -#[cfg(target_word_size = "64")] -checked_cast_impl!(CheckedAdd, checked_add, int, i64, intrinsics::i64_add_with_overflow); +#[cfg(target_pointer_width = "32")] +checked_cast_impl!(CheckedAdd, checked_add, isize, i32, intrinsics::i32_add_with_overflow); +#[cfg(target_pointer_width = "64")] +checked_cast_impl!(CheckedAdd, checked_add, isize, i64, intrinsics::i64_add_with_overflow); checked_impl!(CheckedAdd, checked_add, i8, intrinsics::i8_add_with_overflow); checked_impl!(CheckedAdd, checked_add, i16, intrinsics::i16_add_with_overflow); @@ -398,20 +398,20 @@ pub trait CheckedSub: Sub { fn checked_sub(&self, v: &Self) -> Option; } -#[cfg(target_word_size = "32")] -checked_cast_impl!(CheckedSub, checked_sub, uint, u32, intrinsics::u32_sub_with_overflow); -#[cfg(target_word_size = "64")] -checked_cast_impl!(CheckedSub, checked_sub, uint, u64, intrinsics::u64_sub_with_overflow); +#[cfg(target_pointer_width = "32")] +checked_cast_impl!(CheckedSub, checked_sub, usize, u32, intrinsics::u32_sub_with_overflow); +#[cfg(target_pointer_width = "64")] +checked_cast_impl!(CheckedSub, checked_sub, usize, u64, intrinsics::u64_sub_with_overflow); checked_impl!(CheckedSub, checked_sub, u8, intrinsics::u8_sub_with_overflow); checked_impl!(CheckedSub, checked_sub, u16, intrinsics::u16_sub_with_overflow); checked_impl!(CheckedSub, checked_sub, u32, intrinsics::u32_sub_with_overflow); checked_impl!(CheckedSub, checked_sub, u64, intrinsics::u64_sub_with_overflow); -#[cfg(target_word_size = "32")] -checked_cast_impl!(CheckedSub, checked_sub, int, i32, intrinsics::i32_sub_with_overflow); -#[cfg(target_word_size = "64")] -checked_cast_impl!(CheckedSub, checked_sub, int, i64, intrinsics::i64_sub_with_overflow); +#[cfg(target_pointer_width = "32")] +checked_cast_impl!(CheckedSub, checked_sub, isize, i32, intrinsics::i32_sub_with_overflow); +#[cfg(target_pointer_width = "64")] +checked_cast_impl!(CheckedSub, checked_sub, isize, i64, intrinsics::i64_sub_with_overflow); checked_impl!(CheckedSub, checked_sub, i8, intrinsics::i8_sub_with_overflow); checked_impl!(CheckedSub, checked_sub, i16, intrinsics::i16_sub_with_overflow); @@ -434,20 +434,20 @@ pub trait CheckedMul: Mul { fn checked_mul(&self, v: &Self) -> Option; } -#[cfg(target_word_size = "32")] -checked_cast_impl!(CheckedMul, checked_mul, uint, u32, intrinsics::u32_mul_with_overflow); -#[cfg(target_word_size = "64")] -checked_cast_impl!(CheckedMul, checked_mul, uint, u64, intrinsics::u64_mul_with_overflow); +#[cfg(target_pointer_width = "32")] +checked_cast_impl!(CheckedMul, checked_mul, usize, u32, intrinsics::u32_mul_with_overflow); +#[cfg(target_pointer_width = "64")] +checked_cast_impl!(CheckedMul, checked_mul, usize, u64, intrinsics::u64_mul_with_overflow); checked_impl!(CheckedMul, checked_mul, u8, intrinsics::u8_mul_with_overflow); checked_impl!(CheckedMul, checked_mul, u16, intrinsics::u16_mul_with_overflow); checked_impl!(CheckedMul, checked_mul, u32, intrinsics::u32_mul_with_overflow); checked_impl!(CheckedMul, checked_mul, u64, intrinsics::u64_mul_with_overflow); -#[cfg(target_word_size = "32")] -checked_cast_impl!(CheckedMul, checked_mul, int, i32, intrinsics::i32_mul_with_overflow); -#[cfg(target_word_size = "64")] -checked_cast_impl!(CheckedMul, checked_mul, int, i64, intrinsics::i64_mul_with_overflow); +#[cfg(target_pointer_width = "32")] +checked_cast_impl!(CheckedMul, checked_mul, isize, i32, intrinsics::i32_mul_with_overflow); +#[cfg(target_pointer_width = "64")] +checked_cast_impl!(CheckedMul, checked_mul, isize, i64, intrinsics::i64_mul_with_overflow); checked_impl!(CheckedMul, checked_mul, i8, intrinsics::i8_mul_with_overflow); checked_impl!(CheckedMul, checked_mul, i16, intrinsics::i16_mul_with_overflow); @@ -486,7 +486,7 @@ macro_rules! checkeddiv_int_impl { } } -checkeddiv_int_impl!(int, int::MIN); +checkeddiv_int_impl!(isize, isize::MIN); checkeddiv_int_impl!(i8, i8::MIN); checkeddiv_int_impl!(i16, i16::MIN); checkeddiv_int_impl!(i32, i32::MIN); @@ -507,5 +507,4 @@ macro_rules! checkeddiv_uint_impl { )*) } -checkeddiv_uint_impl!(uint u8 u16 u32 u64); - +checkeddiv_uint_impl!(usize u8 u16 u32 u64);