Skip to content

Commit 6fa054d

Browse files
committed
Use borrowed pointers for Integer methods
This brings them in line with the quot and rem traits, and is be better for large Integer types like BigInt and BigUint because they don't need to be copied unnecessarily.
1 parent 024bf2e commit 6fa054d

File tree

3 files changed

+71
-71
lines changed

3 files changed

+71
-71
lines changed

src/libcore/num/int-template.rs

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,13 @@ impl Integer for T {
298298
* ~~~
299299
*/
300300
#[inline(always)]
301-
fn div(&self, other: T) -> T {
301+
fn div(&self, other: &T) -> T {
302302
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
303303
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
304304
match self.quot_rem(other) {
305-
(q, r) if (r > 0 && other < 0)
306-
|| (r < 0 && other > 0) => q - 1,
307-
(q, _) => q,
305+
(q, r) if (r > 0 && *other < 0)
306+
|| (r < 0 && *other > 0) => q - 1,
307+
(q, _) => q,
308308
}
309309
}
310310
@@ -330,32 +330,32 @@ impl Integer for T {
330330
* ~~~
331331
*/
332332
#[inline(always)]
333-
fn modulo(&self, other: T) -> T {
333+
fn modulo(&self, other: &T) -> T {
334334
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
335335
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
336-
match *self % other {
337-
r if (r > 0 && other < 0)
338-
|| (r < 0 && other > 0) => r + other,
339-
r => r,
336+
match *self % *other {
337+
r if (r > 0 && *other < 0)
338+
|| (r < 0 && *other > 0) => r + *other,
339+
r => r,
340340
}
341341
}
342342
343343
/// Calculates `div` and `modulo` simultaneously
344344
#[inline(always)]
345-
fn div_mod(&self, other: T) -> (T,T) {
345+
fn div_mod(&self, other: &T) -> (T,T) {
346346
// Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
347347
// December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
348348
match self.quot_rem(other) {
349-
(q, r) if (r > 0 && other < 0)
350-
|| (r < 0 && other > 0) => (q - 1, r + other),
351-
(q, r) => (q, r),
349+
(q, r) if (r > 0 && *other < 0)
350+
|| (r < 0 && *other > 0) => (q - 1, r + *other),
351+
(q, r) => (q, r),
352352
}
353353
}
354354
355355
/// Calculates `quot` (`\`) and `rem` (`%`) simultaneously
356356
#[inline(always)]
357-
fn quot_rem(&self, other: T) -> (T,T) {
358-
(*self / other, *self % other)
357+
fn quot_rem(&self, other: &T) -> (T,T) {
358+
(*self / *other, *self % *other)
359359
}
360360
361361
/**
@@ -364,9 +364,9 @@ impl Integer for T {
364364
* The result is always positive
365365
*/
366366
#[inline(always)]
367-
fn gcd(&self, other: T) -> T {
367+
fn gcd(&self, other: &T) -> T {
368368
// Use Euclid's algorithm
369-
let mut m = *self, n = other;
369+
let mut m = *self, n = *other;
370370
while m != 0 {
371371
let temp = m;
372372
m = n % temp;
@@ -379,17 +379,17 @@ impl Integer for T {
379379
* Calculates the Lowest Common Multiple (LCM) of the number and `other`
380380
*/
381381
#[inline(always)]
382-
fn lcm(&self, other: T) -> T {
383-
((*self * other) / self.gcd(other)).abs() // should not have to recaluculate abs
382+
fn lcm(&self, other: &T) -> T {
383+
((*self * *other) / self.gcd(other)).abs() // should not have to recaluculate abs
384384
}
385385
386386
/// Returns `true` if the number can be divided by `other` without leaving a remainder
387387
#[inline(always)]
388-
fn divisible_by(&self, other: T) -> bool { *self % other == 0 }
388+
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
389389
390390
/// Returns `true` if the number is divisible by `2`
391391
#[inline(always)]
392-
fn is_even(&self) -> bool { self.divisible_by(2) }
392+
fn is_even(&self) -> bool { self.divisible_by(&2) }
393393
394394
/// Returns `true` if the number is not divisible by `2`
395395
#[inline(always)]
@@ -562,7 +562,7 @@ mod tests {
562562
fn test_nd_qr(nd: (T,T), qr: (T,T)) {
563563
let (n,d) = nd;
564564
let separate_quot_rem = (n / d, n % d);
565-
let combined_quot_rem = n.quot_rem(d);
565+
let combined_quot_rem = n.quot_rem(&d);
566566
567567
assert_eq!(separate_quot_rem, qr);
568568
assert_eq!(combined_quot_rem, qr);
@@ -586,8 +586,8 @@ mod tests {
586586
fn test_div_mod() {
587587
fn test_nd_dm(nd: (T,T), dm: (T,T)) {
588588
let (n,d) = nd;
589-
let separate_div_mod = (n.div(d), n.modulo(d));
590-
let combined_div_mod = n.div_mod(d);
589+
let separate_div_mod = (n.div(&d), n.modulo(&d));
590+
let combined_div_mod = n.div_mod(&d);
591591
592592
assert_eq!(separate_div_mod, dm);
593593
assert_eq!(combined_div_mod, dm);
@@ -609,26 +609,26 @@ mod tests {
609609
610610
#[test]
611611
fn test_gcd() {
612-
assert_eq!((10 as T).gcd(2), 2 as T);
613-
assert_eq!((10 as T).gcd(3), 1 as T);
614-
assert_eq!((0 as T).gcd(3), 3 as T);
615-
assert_eq!((3 as T).gcd(3), 3 as T);
616-
assert_eq!((56 as T).gcd(42), 14 as T);
617-
assert_eq!((3 as T).gcd(-3), 3 as T);
618-
assert_eq!((-6 as T).gcd(3), 3 as T);
619-
assert_eq!((-4 as T).gcd(-2), 2 as T);
612+
assert_eq!((10 as T).gcd(&2), 2 as T);
613+
assert_eq!((10 as T).gcd(&3), 1 as T);
614+
assert_eq!((0 as T).gcd(&3), 3 as T);
615+
assert_eq!((3 as T).gcd(&3), 3 as T);
616+
assert_eq!((56 as T).gcd(&42), 14 as T);
617+
assert_eq!((3 as T).gcd(&-3), 3 as T);
618+
assert_eq!((-6 as T).gcd(&3), 3 as T);
619+
assert_eq!((-4 as T).gcd(&-2), 2 as T);
620620
}
621621
622622
#[test]
623623
fn test_lcm() {
624-
assert_eq!((1 as T).lcm(0), 0 as T);
625-
assert_eq!((0 as T).lcm(1), 0 as T);
626-
assert_eq!((1 as T).lcm(1), 1 as T);
627-
assert_eq!((-1 as T).lcm(1), 1 as T);
628-
assert_eq!((1 as T).lcm(-1), 1 as T);
629-
assert_eq!((-1 as T).lcm(-1), 1 as T);
630-
assert_eq!((8 as T).lcm(9), 72 as T);
631-
assert_eq!((11 as T).lcm(5), 55 as T);
624+
assert_eq!((1 as T).lcm(&0), 0 as T);
625+
assert_eq!((0 as T).lcm(&1), 0 as T);
626+
assert_eq!((1 as T).lcm(&1), 1 as T);
627+
assert_eq!((-1 as T).lcm(&1), 1 as T);
628+
assert_eq!((1 as T).lcm(&-1), 1 as T);
629+
assert_eq!((-1 as T).lcm(&-1), 1 as T);
630+
assert_eq!((8 as T).lcm(&9), 72 as T);
631+
assert_eq!((11 as T).lcm(&5), 55 as T);
632632
}
633633
634634
#[test]

src/libcore/num/num.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ pub trait Integer: Num
6565
+ Ord
6666
+ Quot<Self,Self>
6767
+ Rem<Self,Self> {
68-
fn div(&self, other: Self) -> Self;
69-
fn modulo(&self, other: Self) -> Self;
70-
fn div_mod(&self, other: Self) -> (Self,Self);
71-
fn quot_rem(&self, other: Self) -> (Self,Self);
72-
73-
fn gcd(&self, other: Self) -> Self;
74-
fn lcm(&self, other: Self) -> Self;
75-
fn divisible_by(&self, other: Self) -> bool;
68+
fn div(&self, other: &Self) -> Self;
69+
fn modulo(&self, other: &Self) -> Self;
70+
fn div_mod(&self, other: &Self) -> (Self,Self);
71+
fn quot_rem(&self, other: &Self) -> (Self,Self);
72+
73+
fn gcd(&self, other: &Self) -> Self;
74+
fn lcm(&self, other: &Self) -> Self;
75+
fn divisible_by(&self, other: &Self) -> bool;
7676
fn is_even(&self) -> bool;
7777
fn is_odd(&self) -> bool;
7878
}

src/libcore/num/uint-template.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -179,29 +179,29 @@ impl Unsigned for T {}
179179
impl Integer for T {
180180
/// Unsigned integer division. Returns the same result as `quot` (`/`).
181181
#[inline(always)]
182-
fn div(&self, other: T) -> T { *self / other }
182+
fn div(&self, other: &T) -> T { *self / *other }
183183
184184
/// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
185185
#[inline(always)]
186-
fn modulo(&self, other: T) -> T { *self / other }
186+
fn modulo(&self, other: &T) -> T { *self / *other }
187187
188188
/// Calculates `div` and `modulo` simultaneously
189189
#[inline(always)]
190-
fn div_mod(&self, other: T) -> (T,T) {
191-
(*self / other, *self % other)
190+
fn div_mod(&self, other: &T) -> (T,T) {
191+
(*self / *other, *self % *other)
192192
}
193193
194194
/// Calculates `quot` (`\`) and `rem` (`%`) simultaneously
195195
#[inline(always)]
196-
fn quot_rem(&self, other: T) -> (T,T) {
197-
(*self / other, *self % other)
196+
fn quot_rem(&self, other: &T) -> (T,T) {
197+
(*self / *other, *self % *other)
198198
}
199199
200200
/// Calculates the Greatest Common Divisor (GCD) of the number and `other`
201201
#[inline(always)]
202-
fn gcd(&self, other: T) -> T {
202+
fn gcd(&self, other: &T) -> T {
203203
// Use Euclid's algorithm
204-
let mut m = *self, n = other;
204+
let mut m = *self, n = *other;
205205
while m != 0 {
206206
let temp = m;
207207
m = n % temp;
@@ -212,17 +212,17 @@ impl Integer for T {
212212
213213
/// Calculates the Lowest Common Multiple (LCM) of the number and `other`
214214
#[inline(always)]
215-
fn lcm(&self, other: T) -> T {
216-
(*self * other) / self.gcd(other)
215+
fn lcm(&self, other: &T) -> T {
216+
(*self * *other) / self.gcd(other)
217217
}
218218
219219
/// Returns `true` if the number can be divided by `other` without leaving a remainder
220220
#[inline(always)]
221-
fn divisible_by(&self, other: T) -> bool { *self % other == 0 }
221+
fn divisible_by(&self, other: &T) -> bool { *self % *other == 0 }
222222
223223
/// Returns `true` if the number is divisible by `2`
224224
#[inline(always)]
225-
fn is_even(&self) -> bool { self.divisible_by(2) }
225+
fn is_even(&self) -> bool { self.divisible_by(&2) }
226226
227227
/// Returns `true` if the number is not divisible by `2`
228228
#[inline(always)]
@@ -355,21 +355,21 @@ mod tests {
355355
356356
#[test]
357357
fn test_gcd() {
358-
assert_eq!((10 as T).gcd(2), 2 as T);
359-
assert_eq!((10 as T).gcd(3), 1 as T);
360-
assert_eq!((0 as T).gcd(3), 3 as T);
361-
assert_eq!((3 as T).gcd(3), 3 as T);
362-
assert_eq!((56 as T).gcd(42), 14 as T);
358+
assert_eq!((10 as T).gcd(&2), 2 as T);
359+
assert_eq!((10 as T).gcd(&3), 1 as T);
360+
assert_eq!((0 as T).gcd(&3), 3 as T);
361+
assert_eq!((3 as T).gcd(&3), 3 as T);
362+
assert_eq!((56 as T).gcd(&42), 14 as T);
363363
}
364364
365365
#[test]
366366
fn test_lcm() {
367-
assert_eq!((1 as T).lcm(0), 0 as T);
368-
assert_eq!((0 as T).lcm(1), 0 as T);
369-
assert_eq!((1 as T).lcm(1), 1 as T);
370-
assert_eq!((8 as T).lcm(9), 72 as T);
371-
assert_eq!((11 as T).lcm(5), 55 as T);
372-
assert_eq!((99 as T).lcm(17), 1683 as T);
367+
assert_eq!((1 as T).lcm(&0), 0 as T);
368+
assert_eq!((0 as T).lcm(&1), 0 as T);
369+
assert_eq!((1 as T).lcm(&1), 1 as T);
370+
assert_eq!((8 as T).lcm(&9), 72 as T);
371+
assert_eq!((11 as T).lcm(&5), 55 as T);
372+
assert_eq!((99 as T).lcm(&17), 1683 as T);
373373
}
374374
375375
#[test]

0 commit comments

Comments
 (0)