Skip to content

Commit 54685c4

Browse files
committed
Extract rational
1 parent 2176b70 commit 54685c4

File tree

13 files changed

+1277
-142
lines changed

13 files changed

+1277
-142
lines changed

.travis.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,9 @@ rust:
55
- nightly
66
sudo: false
77
script:
8-
- cargo build --verbose
9-
- cargo test --verbose
10-
- .travis/test_features.sh
8+
- make test
119
- |
12-
[ $TRAVIS_RUST_VERSION != nightly ] ||
13-
.travis/test_nightly.sh
10+
[ $TRAVIS_RUST_VERSION != nightly ] || .travis/test_nightly.sh
1411
- cargo doc
1512
after_success: |
1613
[ $TRAVIS_BRANCH = master ] &&

.travis/test_features.sh

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
set -ex
44

55
for feature in '' bigint rational complex; do
6-
cargo build --verbose --no-default-features --features="$feature"
76
cargo test --verbose --no-default-features --features="$feature"
87
done
9-

Cargo.toml

+7-3
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ name = "shootout-pidigits"
1919
[dependencies]
2020

2121
[dependencies.num-bigint]
22-
optional = false
22+
optional = true
2323
path = "bigint"
2424

2525
[dependencies.num-integer]
2626
path = "./integer"
2727

28+
[dependencies.num-rational]
29+
optional = true
30+
path = "rational"
31+
2832
[dependencies.num-traits]
2933
path = "./traits"
3034

@@ -46,7 +50,7 @@ version = "^0.7.0"
4650
version = "0.3.8"
4751

4852
[features]
49-
bigint = []
53+
bigint = ["num-bigint"]
5054
complex = []
5155
default = ["bigint", "complex", "rand", "rational", "rustc-serialize"]
52-
rational = []
56+
rational = ["num-rational"]

Makefile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
CARGO_CMD ?= cargo
2+
3+
packages = bigint integer rational traits
4+
5+
test:
6+
$(MAKE) run-all TASK="test --no-fail-fast"
7+
8+
run-all: $(packages)
9+
$(CARGO_CMD) $(TASK)
10+
11+
$(packages):
12+
$(CARGO_CMD) $(TASK) --manifest-path $@/Cargo.toml
13+
14+
.PHONY: $(packages) test

bigint/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ version = "0.1.0"
66
[dependencies]
77

88
[dependencies.num-integer]
9-
optional = false
109
path = "../integer"
1110

1211
[dependencies.num-traits]
13-
optional = false
1412
path = "../traits"
1513

1614
[dependencies.rand]
@@ -20,3 +18,6 @@ version = "0.3.14"
2018
[dependencies.serde]
2119
optional = true
2220
version = "0.7.0"
21+
22+
[features]
23+
default = ["rand"]

bigint/src/lib.rs

+43-31
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
//!
1919
//! ## Example
2020
//!
21-
//! ```rust
22-
//! use num::{BigUint, Zero, One};
21+
//! ```rust,ignore
22+
//! use num_bigint::BigUint;
23+
//! use num_traits::{Zero, One};
2324
//! use std::mem::replace;
2425
//!
2526
//! // Calculate large fibonacci numbers.
@@ -42,11 +43,11 @@
4243
//!
4344
//! ```rust
4445
//! extern crate rand;
45-
//! extern crate num;
46+
//! extern crate num_bigint as bigint;
4647
//!
4748
//! # #[cfg(feature = "rand")]
4849
//! # fn main() {
49-
//! use num::bigint::{ToBigInt, RandBigInt};
50+
//! use bigint::{ToBigInt, RandBigInt};
5051
//!
5152
//! let mut rng = rand::thread_rng();
5253
//! let a = rng.gen_bigint(1000);
@@ -64,6 +65,9 @@
6465
//! # }
6566
//! ```
6667
68+
#[cfg(any(feature = "rand", test))]
69+
extern crate rand;
70+
6771
extern crate num_integer as integer;
6872
extern crate num_traits as traits;
6973

@@ -75,6 +79,7 @@ use std::num::ParseIntError;
7579
use std::ops::{Add, BitAnd, BitOr, BitXor, Div, Mul, Neg, Rem, Shl, Shr, Sub};
7680
use std::str::{self, FromStr};
7781
use std::fmt;
82+
use std::hash;
7883
use std::cmp::Ordering::{self, Less, Greater, Equal};
7984
use std::{f32, f64};
8085
use std::{u8, i64, u64};
@@ -1655,7 +1660,7 @@ impl BigUint {
16551660
/// # Examples
16561661
///
16571662
/// ```
1658-
/// use num::bigint::BigUint;
1663+
/// use num_bigint::BigUint;
16591664
///
16601665
/// assert_eq!(BigUint::from_bytes_be(b"A"),
16611666
/// BigUint::parse_bytes(b"65", 10).unwrap());
@@ -1694,7 +1699,7 @@ impl BigUint {
16941699
/// # Examples
16951700
///
16961701
/// ```
1697-
/// use num::bigint::BigUint;
1702+
/// use num_bigint::BigUint;
16981703
///
16991704
/// let i = BigUint::parse_bytes(b"1125", 10).unwrap();
17001705
/// assert_eq!(i.to_bytes_le(), vec![101, 4]);
@@ -1713,7 +1718,7 @@ impl BigUint {
17131718
/// # Examples
17141719
///
17151720
/// ```
1716-
/// use num::bigint::BigUint;
1721+
/// use num_bigint::BigUint;
17171722
///
17181723
/// let i = BigUint::parse_bytes(b"1125", 10).unwrap();
17191724
/// assert_eq!(i.to_bytes_be(), vec![4, 101]);
@@ -1731,7 +1736,7 @@ impl BigUint {
17311736
/// # Examples
17321737
///
17331738
/// ```
1734-
/// use num::bigint::BigUint;
1739+
/// use num_bigint::BigUint;
17351740
///
17361741
/// let i = BigUint::parse_bytes(b"ff", 16).unwrap();
17371742
/// assert_eq!(i.to_str_radix(16), "ff");
@@ -1748,7 +1753,7 @@ impl BigUint {
17481753
/// # Examples
17491754
///
17501755
/// ```
1751-
/// use num::bigint::{BigUint, ToBigUint};
1756+
/// use num_bigint::{BigUint, ToBigUint};
17521757
///
17531758
/// assert_eq!(BigUint::parse_bytes(b"1234", 10), ToBigUint::to_biguint(&1234));
17541759
/// assert_eq!(BigUint::parse_bytes(b"ABCD", 16), ToBigUint::to_biguint(&0xABCD));
@@ -2764,7 +2769,7 @@ impl BigInt {
27642769
/// # Examples
27652770
///
27662771
/// ```
2767-
/// use num::bigint::{BigInt, Sign};
2772+
/// use num_bigint::{BigInt, Sign};
27682773
///
27692774
/// assert_eq!(BigInt::from_bytes_be(Sign::Plus, b"A"),
27702775
/// BigInt::parse_bytes(b"65", 10).unwrap());
@@ -2793,7 +2798,7 @@ impl BigInt {
27932798
/// # Examples
27942799
///
27952800
/// ```
2796-
/// use num::bigint::{ToBigInt, Sign};
2801+
/// use num_bigint::{ToBigInt, Sign};
27972802
///
27982803
/// let i = -1125.to_bigint().unwrap();
27992804
/// assert_eq!(i.to_bytes_le(), (Sign::Minus, vec![101, 4]));
@@ -2808,7 +2813,7 @@ impl BigInt {
28082813
/// # Examples
28092814
///
28102815
/// ```
2811-
/// use num::bigint::{ToBigInt, Sign};
2816+
/// use num_bigint::{ToBigInt, Sign};
28122817
///
28132818
/// let i = -1125.to_bigint().unwrap();
28142819
/// assert_eq!(i.to_bytes_be(), (Sign::Minus, vec![4, 101]));
@@ -2824,7 +2829,7 @@ impl BigInt {
28242829
/// # Examples
28252830
///
28262831
/// ```
2827-
/// use num::bigint::BigInt;
2832+
/// use num_bigint::BigInt;
28282833
///
28292834
/// let i = BigInt::parse_bytes(b"ff", 16).unwrap();
28302835
/// assert_eq!(i.to_str_radix(16), "ff");
@@ -2846,7 +2851,7 @@ impl BigInt {
28462851
/// # Examples
28472852
///
28482853
/// ```
2849-
/// use num::bigint::{ToBigInt, Sign};
2854+
/// use num_bigint::{ToBigInt, Sign};
28502855
///
28512856
/// assert_eq!(ToBigInt::to_bigint(&1234).unwrap().sign(), Sign::Plus);
28522857
/// assert_eq!(ToBigInt::to_bigint(&-4321).unwrap().sign(), Sign::Minus);
@@ -2862,7 +2867,7 @@ impl BigInt {
28622867
/// # Examples
28632868
///
28642869
/// ```
2865-
/// use num::bigint::{BigInt, ToBigInt};
2870+
/// use num_bigint::{BigInt, ToBigInt};
28662871
///
28672872
/// assert_eq!(BigInt::parse_bytes(b"1234", 10), ToBigInt::to_bigint(&1234));
28682873
/// assert_eq!(BigInt::parse_bytes(b"ABCD", 16), ToBigInt::to_bigint(&0xABCD));
@@ -2935,9 +2940,17 @@ impl From<ParseIntError> for ParseBigIntError {
29352940
}
29362941
}
29372942

2943+
#[cfg(test)]
2944+
fn hash<T: hash::Hash>(x: &T) -> u64 {
2945+
use std::hash::Hasher;
2946+
let mut hasher = hash::SipHasher::new();
2947+
x.hash(&mut hasher);
2948+
hasher.finish()
2949+
}
2950+
29382951
#[cfg(test)]
29392952
mod biguint_tests {
2940-
use Integer;
2953+
use integer::Integer;
29412954
use super::{BigDigit, BigUint, ToBigUint, big_digit};
29422955
use super::{BigInt, RandBigInt, ToBigInt};
29432956
use super::Sign::Plus;
@@ -2950,9 +2963,9 @@ mod biguint_tests {
29502963
use std::{u8, u16, u32, u64, usize};
29512964

29522965
use rand::thread_rng;
2953-
use {Num, Zero, One, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv};
2954-
use {ToPrimitive, FromPrimitive};
2955-
use Float;
2966+
use traits::{Num, Zero, One, CheckedAdd, CheckedSub, CheckedMul, CheckedDiv, ToPrimitive,
2967+
FromPrimitive, Float};
2968+
29562969

29572970
/// Assert that an op works for all val/ref combinations
29582971
macro_rules! assert_op {
@@ -3083,10 +3096,10 @@ mod biguint_tests {
30833096
let c = BigUint::new(vec![1]);
30843097
let d = BigUint::new(vec![1, 0, 0, 0, 0, 0]);
30853098
let e = BigUint::new(vec![0, 0, 0, 0, 0, 1]);
3086-
assert!(::hash(&a) == ::hash(&b));
3087-
assert!(::hash(&b) != ::hash(&c));
3088-
assert!(::hash(&c) == ::hash(&d));
3089-
assert!(::hash(&d) != ::hash(&e));
3099+
assert!(super::hash(&a) == super::hash(&b));
3100+
assert!(super::hash(&b) != super::hash(&c));
3101+
assert!(super::hash(&c) == super::hash(&d));
3102+
assert!(super::hash(&d) != super::hash(&e));
30903103
}
30913104

30923105
const BIT_TESTS: &'static [(&'static [BigDigit],
@@ -4173,7 +4186,6 @@ mod biguint_tests {
41734186

41744187
#[cfg(test)]
41754188
mod bigint_tests {
4176-
use Integer;
41774189
use super::{BigDigit, BigUint, ToBigUint};
41784190
use super::{Sign, BigInt, RandBigInt, ToBigInt, big_digit};
41794191
use super::Sign::{Minus, NoSign, Plus};
@@ -4187,8 +4199,8 @@ mod bigint_tests {
41874199

41884200
use rand::thread_rng;
41894201

4190-
use {Zero, One, Signed, ToPrimitive, FromPrimitive, Num};
4191-
use Float;
4202+
use integer::Integer;
4203+
use traits::{Zero, One, Signed, ToPrimitive, FromPrimitive, Num, Float};
41924204

41934205
/// Assert that an op works for all val/ref combinations
41944206
macro_rules! assert_op {
@@ -4334,11 +4346,11 @@ mod bigint_tests {
43344346
let d = BigInt::new(Plus, vec![1, 0, 0, 0, 0, 0]);
43354347
let e = BigInt::new(Plus, vec![0, 0, 0, 0, 0, 1]);
43364348
let f = BigInt::new(Minus, vec![1]);
4337-
assert!(::hash(&a) == ::hash(&b));
4338-
assert!(::hash(&b) != ::hash(&c));
4339-
assert!(::hash(&c) == ::hash(&d));
4340-
assert!(::hash(&d) != ::hash(&e));
4341-
assert!(::hash(&c) != ::hash(&f));
4349+
assert!(super::hash(&a) == super::hash(&b));
4350+
assert!(super::hash(&b) != super::hash(&c));
4351+
assert!(super::hash(&c) == super::hash(&d));
4352+
assert!(super::hash(&d) != super::hash(&e));
4353+
assert!(super::hash(&c) != super::hash(&f));
43424354
}
43434355

43444356
#[test]

0 commit comments

Comments
 (0)