Skip to content

Commit f1a8085

Browse files
committed
Extract integer module
1 parent 4361521 commit f1a8085

File tree

5 files changed

+46
-679
lines changed

5 files changed

+46
-679
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ serde = { version = "^0.7.0", optional = true }
2121
[dependencies.num-traits]
2222
path = "./traits"
2323

24+
[dependencies.num-integer]
25+
path = "./integer"
26+
2427
[dev-dependencies]
2528
# Some tests of non-rand functionality still use rand because the tests
2629
# themselves are randomized.

integer/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[package]
2-
name = "integer"
2+
name = "num-integer"
33
version = "0.1.0"
44
authors = ["Łukasz Jan Niemier <[email protected]>"]
55

6-
[dependencies]
6+
[dependencies.num-traits]
7+
path = "../traits"

integer/src/lib.rs

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
//! Integer trait and functions.
1212
13-
use {Num, Signed};
13+
extern crate num_traits as traits;
14+
15+
use traits::{Num, Signed};
1416

1517
pub trait Integer
1618
: Sized
@@ -179,7 +181,7 @@ macro_rules! impl_integer_for_isize {
179181
impl Integer for $T {
180182
/// Floored integer division
181183
#[inline]
182-
fn div_floor(&self, other: &$T) -> $T {
184+
fn div_floor(&self, other: &Self) -> Self {
183185
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
184186
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
185187
match self.div_rem(other) {
@@ -191,7 +193,7 @@ macro_rules! impl_integer_for_isize {
191193

192194
/// Floored integer modulo
193195
#[inline]
194-
fn mod_floor(&self, other: &$T) -> $T {
196+
fn mod_floor(&self, other: &Self) -> Self {
195197
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
196198
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
197199
match *self % *other {
@@ -203,7 +205,7 @@ macro_rules! impl_integer_for_isize {
203205

204206
/// Calculates `div_floor` and `mod_floor` simultaneously
205207
#[inline]
206-
fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
208+
fn div_mod_floor(&self, other: &Self) -> (Self, Self) {
207209
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
208210
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
209211
match self.div_rem(other) {
@@ -216,7 +218,7 @@ macro_rules! impl_integer_for_isize {
216218
/// Calculates the Greatest Common Divisor (GCD) of the number and
217219
/// `other`. The result is always positive.
218220
#[inline]
219-
fn gcd(&self, other: &$T) -> $T {
221+
fn gcd(&self, other: &Self) -> Self {
220222
// Use Stein's algorithm
221223
let mut m = *self;
222224
let mut n = *other;
@@ -233,7 +235,7 @@ macro_rules! impl_integer_for_isize {
233235
// Assuming two's complement, the number created by the shift
234236
// is positive for all numbers except gcd = abs(min value)
235237
// The call to .abs() causes a panic in debug mode
236-
if m == <$T>::min_value() || n == <$T>::min_value() {
238+
if m == Self::min_value() || n == Self::min_value() {
237239
return (1 << shift).abs()
238240
}
239241

@@ -257,18 +259,22 @@ macro_rules! impl_integer_for_isize {
257259
/// Calculates the Lowest Common Multiple (LCM) of the number and
258260
/// `other`.
259261
#[inline]
260-
fn lcm(&self, other: &$T) -> $T {
262+
fn lcm(&self, other: &Self) -> Self {
261263
// should not have to recalculate abs
262264
((*self * *other) / self.gcd(other)).abs()
263265
}
264266

265267
/// Deprecated, use `is_multiple_of` instead.
266268
#[inline]
267-
fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
269+
fn divides(&self, other: &Self) -> bool {
270+
self.is_multiple_of(other)
271+
}
268272

269273
/// Returns `true` if the number is a multiple of `other`.
270274
#[inline]
271-
fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
275+
fn is_multiple_of(&self, other: &Self) -> bool {
276+
*self % *other == 0
277+
}
272278

273279
/// Returns `true` if the number is divisible by `2`
274280
#[inline]
@@ -280,7 +286,7 @@ macro_rules! impl_integer_for_isize {
280286

281287
/// Simultaneous truncated integer division and modulus.
282288
#[inline]
283-
fn div_rem(&self, other: &$T) -> ($T, $T) {
289+
fn div_rem(&self, other: &Self) -> (Self, Self) {
284290
(*self / *other, *self % *other)
285291
}
286292
}
@@ -295,7 +301,7 @@ macro_rules! impl_integer_for_isize {
295301
/// - `d`: denominator (divisor)
296302
/// - `qr`: quotient and remainder
297303
#[cfg(test)]
298-
fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) {
304+
fn test_division_rule((n,d): ($T, $T), (q,r): ($T, $T)) {
299305
assert_eq!(d * q + r, n);
300306
}
301307

@@ -475,15 +481,19 @@ macro_rules! impl_integer_for_usize {
475481
impl Integer for $T {
476482
/// Unsigned integer division. Returns the same result as `div` (`/`).
477483
#[inline]
478-
fn div_floor(&self, other: &$T) -> $T { *self / *other }
484+
fn div_floor(&self, other: &Self) -> Self {
485+
*self / *other
486+
}
479487

480488
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
481489
#[inline]
482-
fn mod_floor(&self, other: &$T) -> $T { *self % *other }
490+
fn mod_floor(&self, other: &Self) -> Self {
491+
*self % *other
492+
}
483493

484494
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`
485495
#[inline]
486-
fn gcd(&self, other: &$T) -> $T {
496+
fn gcd(&self, other: &Self) -> Self {
487497
// Use Stein's algorithm
488498
let mut m = *self;
489499
let mut n = *other;
@@ -507,29 +517,37 @@ macro_rules! impl_integer_for_usize {
507517

508518
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
509519
#[inline]
510-
fn lcm(&self, other: &$T) -> $T {
520+
fn lcm(&self, other: &Self) -> Self {
511521
(*self * *other) / self.gcd(other)
512522
}
513523

514524
/// Deprecated, use `is_multiple_of` instead.
515525
#[inline]
516-
fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
526+
fn divides(&self, other: &Self) -> bool {
527+
self.is_multiple_of(other)
528+
}
517529

518530
/// Returns `true` if the number is a multiple of `other`.
519531
#[inline]
520-
fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
532+
fn is_multiple_of(&self, other: &Self) -> bool {
533+
*self % *other == 0
534+
}
521535

522536
/// Returns `true` if the number is divisible by `2`.
523537
#[inline]
524-
fn is_even(&self) -> bool { (*self) & 1 == 0 }
538+
fn is_even(&self) -> bool {
539+
*self % 2 == 0
540+
}
525541

526542
/// Returns `true` if the number is not divisible by `2`.
527543
#[inline]
528-
fn is_odd(&self) -> bool { !(*self).is_even() }
544+
fn is_odd(&self) -> bool {
545+
!self.is_even()
546+
}
529547

530548
/// Simultaneous truncated integer division and modulus.
531549
#[inline]
532-
fn div_rem(&self, other: &$T) -> ($T, $T) {
550+
fn div_rem(&self, other: &Self) -> (Self, Self) {
533551
(*self / *other, *self % *other)
534552
}
535553
}

0 commit comments

Comments
 (0)