123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
- // file at the top-level directory of this distribution and at
- // http://rust-lang.org/COPYRIGHT.
- //
- // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
- // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
- // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
- // option. This file may not be copied, modified, or distributed
- // except according to those terms.
- //! Integer trait and functions.
- pub trait Integer: Num + PartialOrd
- + Div<Self, Self>
- + Rem<Self, Self> {
- /// Floored integer division.
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert!(( 8i).div_floor(& 3) == 2);
- /// assert!(( 8i).div_floor(&-3) == -3);
- /// assert!((-8i).div_floor(& 3) == -3);
- /// assert!((-8i).div_floor(&-3) == 2);
- ///
- /// assert!(( 1i).div_floor(& 2) == 0);
- /// assert!(( 1i).div_floor(&-2) == -1);
- /// assert!((-1i).div_floor(& 2) == -1);
- /// assert!((-1i).div_floor(&-2) == 0);
- /// ~~~
- fn div_floor(&self, other: &Self) -> Self;
- /// Floored integer modulo, satisfying:
- ///
- /// ~~~
- /// # use num::Integer;
- /// # let n = 1i; let d = 1i;
- /// assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n)
- /// ~~~
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert!(( 8i).mod_floor(& 3) == 2);
- /// assert!(( 8i).mod_floor(&-3) == -1);
- /// assert!((-8i).mod_floor(& 3) == 1);
- /// assert!((-8i).mod_floor(&-3) == -2);
- ///
- /// assert!(( 1i).mod_floor(& 2) == 1);
- /// assert!(( 1i).mod_floor(&-2) == -1);
- /// assert!((-1i).mod_floor(& 2) == 1);
- /// assert!((-1i).mod_floor(&-2) == -1);
- /// ~~~
- fn mod_floor(&self, other: &Self) -> Self;
- /// Greatest Common Divisor (GCD).
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert_eq!(6i.gcd(&8), 2);
- /// assert_eq!(7i.gcd(&3), 1);
- /// ~~~
- fn gcd(&self, other: &Self) -> Self;
- /// Lowest Common Multiple (LCM).
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert_eq!(7i.lcm(&3), 21);
- /// assert_eq!(2i.lcm(&4), 4);
- /// ~~~
- fn lcm(&self, other: &Self) -> Self;
- /// Deprecated, use `is_multiple_of` instead.
- #[deprecated = "function renamed to `is_multiple_of`"]
- fn divides(&self, other: &Self) -> bool;
- /// Returns `true` if `other` is a multiple of `self`.
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert_eq!(9i.is_multiple_of(&3), true);
- /// assert_eq!(3i.is_multiple_of(&9), false);
- /// ~~~
- fn is_multiple_of(&self, other: &Self) -> bool;
- /// Returns `true` if the number is even.
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert_eq!(3i.is_even(), false);
- /// assert_eq!(4i.is_even(), true);
- /// ~~~
- fn is_even(&self) -> bool;
- /// Returns `true` if the number is odd.
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert_eq!(3i.is_odd(), true);
- /// assert_eq!(4i.is_odd(), false);
- /// ~~~
- fn is_odd(&self) -> bool;
- /// Simultaneous truncated integer division and modulus.
- /// Returns `(quotient, remainder)`.
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert_eq!(( 8i).div_rem( &3), ( 2, 2));
- /// assert_eq!(( 8i).div_rem(&-3), (-2, 2));
- /// assert_eq!((-8i).div_rem( &3), (-2, -2));
- /// assert_eq!((-8i).div_rem(&-3), ( 2, -2));
- ///
- /// assert_eq!(( 1i).div_rem( &2), ( 0, 1));
- /// assert_eq!(( 1i).div_rem(&-2), ( 0, 1));
- /// assert_eq!((-1i).div_rem( &2), ( 0, -1));
- /// assert_eq!((-1i).div_rem(&-2), ( 0, -1));
- /// ~~~
- #[inline]
- fn div_rem(&self, other: &Self) -> (Self, Self) {
- (*self / *other, *self % *other)
- }
- /// Simultaneous floored integer division and modulus.
- /// Returns `(quotient, remainder)`.
- ///
- /// # Examples
- ///
- /// ~~~
- /// # use num::Integer;
- /// assert_eq!(( 8i).div_mod_floor( &3), ( 2, 2));
- /// assert_eq!(( 8i).div_mod_floor(&-3), (-3, -1));
- /// assert_eq!((-8i).div_mod_floor( &3), (-3, 1));
- /// assert_eq!((-8i).div_mod_floor(&-3), ( 2, -2));
- ///
- /// assert_eq!(( 1i).div_mod_floor( &2), ( 0, 1));
- /// assert_eq!(( 1i).div_mod_floor(&-2), (-1, -1));
- /// assert_eq!((-1i).div_mod_floor( &2), (-1, 1));
- /// assert_eq!((-1i).div_mod_floor(&-2), ( 0, -1));
- /// ~~~
- fn div_mod_floor(&self, other: &Self) -> (Self, Self) {
- (self.div_floor(other), self.mod_floor(other))
- }
- }
- /// Simultaneous integer division and modulus
- #[inline] pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) { x.div_rem(&y) }
- /// Floored integer division
- #[inline] pub fn div_floor<T: Integer>(x: T, y: T) -> T { x.div_floor(&y) }
- /// Floored integer modulus
- #[inline] pub fn mod_floor<T: Integer>(x: T, y: T) -> T { x.mod_floor(&y) }
- /// Simultaneous floored integer division and modulus
- #[inline] pub fn div_mod_floor<T: Integer>(x: T, y: T) -> (T, T) { x.div_mod_floor(&y) }
- /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. The
- /// result is always positive.
- #[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
- /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
- #[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
- macro_rules! impl_integer_for_int {
- ($T:ty, $test_mod:ident) => (
- impl Integer for $T {
- /// Floored integer division
- #[inline]
- fn div_floor(&self, other: &$T) -> $T {
- // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
- // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
- match self.div_rem(other) {
- (d, r) if (r > 0 && *other < 0)
- || (r < 0 && *other > 0) => d - 1,
- (d, _) => d,
- }
- }
- /// Floored integer modulo
- #[inline]
- fn mod_floor(&self, other: &$T) -> $T {
- // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
- // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
- match *self % *other {
- r if (r > 0 && *other < 0)
- || (r < 0 && *other > 0) => r + *other,
- r => r,
- }
- }
- /// Calculates `div_floor` and `mod_floor` simultaneously
- #[inline]
- fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
- // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
- // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
- match self.div_rem(other) {
- (d, r) if (r > 0 && *other < 0)
- || (r < 0 && *other > 0) => (d - 1, r + *other),
- (d, r) => (d, r),
- }
- }
- /// Calculates the Greatest Common Divisor (GCD) of the number and
- /// `other`. The result is always positive.
- #[inline]
- fn gcd(&self, other: &$T) -> $T {
- // Use Euclid's algorithm
- let mut m = *self;
- let mut n = *other;
- while m != 0 {
- let temp = m;
- m = n % temp;
- n = temp;
- }
- n.abs()
- }
- /// Calculates the Lowest Common Multiple (LCM) of the number and
- /// `other`.
- #[inline]
- fn lcm(&self, other: &$T) -> $T {
- // should not have to recalculate abs
- ((*self * *other) / self.gcd(other)).abs()
- }
- /// Deprecated, use `is_multiple_of` instead.
- #[deprecated = "function renamed to `is_multiple_of`"]
- #[inline]
- fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
- /// Returns `true` if the number is a multiple of `other`.
- #[inline]
- fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
- /// Returns `true` if the number is divisible by `2`
- #[inline]
- fn is_even(&self) -> bool { self & 1 == 0 }
- /// Returns `true` if the number is not divisible by `2`
- #[inline]
- fn is_odd(&self) -> bool { !self.is_even() }
- }
- #[cfg(test)]
- mod $test_mod {
- use Integer;
- /// Checks that the division rule holds for:
- ///
- /// - `n`: numerator (dividend)
- /// - `d`: denominator (divisor)
- /// - `qr`: quotient and remainder
- #[cfg(test)]
- fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) {
- assert_eq!(d * q + r, n);
- }
- #[test]
- fn test_div_rem() {
- fn test_nd_dr(nd: ($T,$T), qr: ($T,$T)) {
- let (n,d) = nd;
- let separate_div_rem = (n / d, n % d);
- let combined_div_rem = n.div_rem(&d);
- assert_eq!(separate_div_rem, qr);
- assert_eq!(combined_div_rem, qr);
- test_division_rule(nd, separate_div_rem);
- test_division_rule(nd, combined_div_rem);
- }
- test_nd_dr(( 8, 3), ( 2, 2));
- test_nd_dr(( 8, -3), (-2, 2));
- test_nd_dr((-8, 3), (-2, -2));
- test_nd_dr((-8, -3), ( 2, -2));
- test_nd_dr(( 1, 2), ( 0, 1));
- test_nd_dr(( 1, -2), ( 0, 1));
- test_nd_dr((-1, 2), ( 0, -1));
- test_nd_dr((-1, -2), ( 0, -1));
- }
- #[test]
- fn test_div_mod_floor() {
- fn test_nd_dm(nd: ($T,$T), dm: ($T,$T)) {
- let (n,d) = nd;
- let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d));
- let combined_div_mod_floor = n.div_mod_floor(&d);
- assert_eq!(separate_div_mod_floor, dm);
- assert_eq!(combined_div_mod_floor, dm);
- test_division_rule(nd, separate_div_mod_floor);
- test_division_rule(nd, combined_div_mod_floor);
- }
- test_nd_dm(( 8, 3), ( 2, 2));
- test_nd_dm(( 8, -3), (-3, -1));
- test_nd_dm((-8, 3), (-3, 1));
- test_nd_dm((-8, -3), ( 2, -2));
- test_nd_dm(( 1, 2), ( 0, 1));
- test_nd_dm(( 1, -2), (-1, -1));
- test_nd_dm((-1, 2), (-1, 1));
- test_nd_dm((-1, -2), ( 0, -1));
- }
- #[test]
- fn test_gcd() {
- assert_eq!((10 as $T).gcd(&2), 2 as $T);
- assert_eq!((10 as $T).gcd(&3), 1 as $T);
- assert_eq!((0 as $T).gcd(&3), 3 as $T);
- assert_eq!((3 as $T).gcd(&3), 3 as $T);
- assert_eq!((56 as $T).gcd(&42), 14 as $T);
- assert_eq!((3 as $T).gcd(&-3), 3 as $T);
- assert_eq!((-6 as $T).gcd(&3), 3 as $T);
- assert_eq!((-4 as $T).gcd(&-2), 2 as $T);
- }
- #[test]
- fn test_lcm() {
- assert_eq!((1 as $T).lcm(&0), 0 as $T);
- assert_eq!((0 as $T).lcm(&1), 0 as $T);
- assert_eq!((1 as $T).lcm(&1), 1 as $T);
- assert_eq!((-1 as $T).lcm(&1), 1 as $T);
- assert_eq!((1 as $T).lcm(&-1), 1 as $T);
- assert_eq!((-1 as $T).lcm(&-1), 1 as $T);
- assert_eq!((8 as $T).lcm(&9), 72 as $T);
- assert_eq!((11 as $T).lcm(&5), 55 as $T);
- }
- #[test]
- fn test_even() {
- assert_eq!((-4 as $T).is_even(), true);
- assert_eq!((-3 as $T).is_even(), false);
- assert_eq!((-2 as $T).is_even(), true);
- assert_eq!((-1 as $T).is_even(), false);
- assert_eq!((0 as $T).is_even(), true);
- assert_eq!((1 as $T).is_even(), false);
- assert_eq!((2 as $T).is_even(), true);
- assert_eq!((3 as $T).is_even(), false);
- assert_eq!((4 as $T).is_even(), true);
- }
- #[test]
- fn test_odd() {
- assert_eq!((-4 as $T).is_odd(), false);
- assert_eq!((-3 as $T).is_odd(), true);
- assert_eq!((-2 as $T).is_odd(), false);
- assert_eq!((-1 as $T).is_odd(), true);
- assert_eq!((0 as $T).is_odd(), false);
- assert_eq!((1 as $T).is_odd(), true);
- assert_eq!((2 as $T).is_odd(), false);
- assert_eq!((3 as $T).is_odd(), true);
- assert_eq!((4 as $T).is_odd(), false);
- }
- }
- )
- }
- 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)
- macro_rules! impl_integer_for_uint {
- ($T:ty, $test_mod:ident) => (
- impl Integer for $T {
- /// Unsigned integer division. Returns the same result as `div` (`/`).
- #[inline]
- fn div_floor(&self, other: &$T) -> $T { *self / *other }
- /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
- #[inline]
- fn mod_floor(&self, other: &$T) -> $T { *self % *other }
- /// Calculates the Greatest Common Divisor (GCD) of the number and `other`
- #[inline]
- fn gcd(&self, other: &$T) -> $T {
- // Use Euclid's algorithm
- let mut m = *self;
- let mut n = *other;
- while m != 0 {
- let temp = m;
- m = n % temp;
- n = temp;
- }
- n
- }
- /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
- #[inline]
- fn lcm(&self, other: &$T) -> $T {
- (*self * *other) / self.gcd(other)
- }
- /// Deprecated, use `is_multiple_of` instead.
- #[deprecated = "function renamed to `is_multiple_of`"]
- #[inline]
- fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
- /// Returns `true` if the number is a multiple of `other`.
- #[inline]
- fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
- /// Returns `true` if the number is divisible by `2`.
- #[inline]
- fn is_even(&self) -> bool { self & 1 == 0 }
- /// Returns `true` if the number is not divisible by `2`.
- #[inline]
- fn is_odd(&self) -> bool { !self.is_even() }
- }
- #[cfg(test)]
- mod $test_mod {
- use Integer;
- #[test]
- fn test_div_mod_floor() {
- assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T);
- assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T);
- assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T));
- assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T);
- assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T);
- assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T));
- assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T);
- assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T);
- assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T));
- }
- #[test]
- fn test_gcd() {
- assert_eq!((10 as $T).gcd(&2), 2 as $T);
- assert_eq!((10 as $T).gcd(&3), 1 as $T);
- assert_eq!((0 as $T).gcd(&3), 3 as $T);
- assert_eq!((3 as $T).gcd(&3), 3 as $T);
- assert_eq!((56 as $T).gcd(&42), 14 as $T);
- }
- #[test]
- fn test_lcm() {
- assert_eq!((1 as $T).lcm(&0), 0 as $T);
- assert_eq!((0 as $T).lcm(&1), 0 as $T);
- assert_eq!((1 as $T).lcm(&1), 1 as $T);
- assert_eq!((8 as $T).lcm(&9), 72 as $T);
- assert_eq!((11 as $T).lcm(&5), 55 as $T);
- assert_eq!((99 as $T).lcm(&17), 1683 as $T);
- }
- #[test]
- fn test_is_multiple_of() {
- assert!((6 as $T).is_multiple_of(&(6 as $T)));
- assert!((6 as $T).is_multiple_of(&(3 as $T)));
- assert!((6 as $T).is_multiple_of(&(1 as $T)));
- }
- #[test]
- fn test_even() {
- assert_eq!((0 as $T).is_even(), true);
- assert_eq!((1 as $T).is_even(), false);
- assert_eq!((2 as $T).is_even(), true);
- assert_eq!((3 as $T).is_even(), false);
- assert_eq!((4 as $T).is_even(), true);
- }
- #[test]
- fn test_odd() {
- assert_eq!((0 as $T).is_odd(), false);
- assert_eq!((1 as $T).is_odd(), true);
- assert_eq!((2 as $T).is_odd(), false);
- assert_eq!((3 as $T).is_odd(), true);
- assert_eq!((4 as $T).is_odd(), false);
- }
- }
- )
- }
- 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)
|