integer.rs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // http://rust-lang.org/COPYRIGHT.
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //! Integer trait and functions.
  11. pub trait Integer: Num + PartialOrd
  12. + Div<Self, Self>
  13. + Rem<Self, Self> {
  14. /// Floored integer division.
  15. ///
  16. /// # Examples
  17. ///
  18. /// ~~~
  19. /// # use num::Integer;
  20. /// assert!(( 8i).div_floor(& 3) == 2);
  21. /// assert!(( 8i).div_floor(&-3) == -3);
  22. /// assert!((-8i).div_floor(& 3) == -3);
  23. /// assert!((-8i).div_floor(&-3) == 2);
  24. ///
  25. /// assert!(( 1i).div_floor(& 2) == 0);
  26. /// assert!(( 1i).div_floor(&-2) == -1);
  27. /// assert!((-1i).div_floor(& 2) == -1);
  28. /// assert!((-1i).div_floor(&-2) == 0);
  29. /// ~~~
  30. fn div_floor(&self, other: &Self) -> Self;
  31. /// Floored integer modulo, satisfying:
  32. ///
  33. /// ~~~
  34. /// # use num::Integer;
  35. /// # let n = 1i; let d = 1i;
  36. /// assert!(n.div_floor(&d) * d + n.mod_floor(&d) == n)
  37. /// ~~~
  38. ///
  39. /// # Examples
  40. ///
  41. /// ~~~
  42. /// # use num::Integer;
  43. /// assert!(( 8i).mod_floor(& 3) == 2);
  44. /// assert!(( 8i).mod_floor(&-3) == -1);
  45. /// assert!((-8i).mod_floor(& 3) == 1);
  46. /// assert!((-8i).mod_floor(&-3) == -2);
  47. ///
  48. /// assert!(( 1i).mod_floor(& 2) == 1);
  49. /// assert!(( 1i).mod_floor(&-2) == -1);
  50. /// assert!((-1i).mod_floor(& 2) == 1);
  51. /// assert!((-1i).mod_floor(&-2) == -1);
  52. /// ~~~
  53. fn mod_floor(&self, other: &Self) -> Self;
  54. /// Greatest Common Divisor (GCD).
  55. ///
  56. /// # Examples
  57. ///
  58. /// ~~~
  59. /// # use num::Integer;
  60. /// assert_eq!(6i.gcd(&8), 2);
  61. /// assert_eq!(7i.gcd(&3), 1);
  62. /// ~~~
  63. fn gcd(&self, other: &Self) -> Self;
  64. /// Lowest Common Multiple (LCM).
  65. ///
  66. /// # Examples
  67. ///
  68. /// ~~~
  69. /// # use num::Integer;
  70. /// assert_eq!(7i.lcm(&3), 21);
  71. /// assert_eq!(2i.lcm(&4), 4);
  72. /// ~~~
  73. fn lcm(&self, other: &Self) -> Self;
  74. /// Deprecated, use `is_multiple_of` instead.
  75. #[deprecated = "function renamed to `is_multiple_of`"]
  76. fn divides(&self, other: &Self) -> bool;
  77. /// Returns `true` if `other` is a multiple of `self`.
  78. ///
  79. /// # Examples
  80. ///
  81. /// ~~~
  82. /// # use num::Integer;
  83. /// assert_eq!(9i.is_multiple_of(&3), true);
  84. /// assert_eq!(3i.is_multiple_of(&9), false);
  85. /// ~~~
  86. fn is_multiple_of(&self, other: &Self) -> bool;
  87. /// Returns `true` if the number is even.
  88. ///
  89. /// # Examples
  90. ///
  91. /// ~~~
  92. /// # use num::Integer;
  93. /// assert_eq!(3i.is_even(), false);
  94. /// assert_eq!(4i.is_even(), true);
  95. /// ~~~
  96. fn is_even(&self) -> bool;
  97. /// Returns `true` if the number is odd.
  98. ///
  99. /// # Examples
  100. ///
  101. /// ~~~
  102. /// # use num::Integer;
  103. /// assert_eq!(3i.is_odd(), true);
  104. /// assert_eq!(4i.is_odd(), false);
  105. /// ~~~
  106. fn is_odd(&self) -> bool;
  107. /// Simultaneous truncated integer division and modulus.
  108. /// Returns `(quotient, remainder)`.
  109. ///
  110. /// # Examples
  111. ///
  112. /// ~~~
  113. /// # use num::Integer;
  114. /// assert_eq!(( 8i).div_rem( &3), ( 2, 2));
  115. /// assert_eq!(( 8i).div_rem(&-3), (-2, 2));
  116. /// assert_eq!((-8i).div_rem( &3), (-2, -2));
  117. /// assert_eq!((-8i).div_rem(&-3), ( 2, -2));
  118. ///
  119. /// assert_eq!(( 1i).div_rem( &2), ( 0, 1));
  120. /// assert_eq!(( 1i).div_rem(&-2), ( 0, 1));
  121. /// assert_eq!((-1i).div_rem( &2), ( 0, -1));
  122. /// assert_eq!((-1i).div_rem(&-2), ( 0, -1));
  123. /// ~~~
  124. #[inline]
  125. fn div_rem(&self, other: &Self) -> (Self, Self) {
  126. (*self / *other, *self % *other)
  127. }
  128. /// Simultaneous floored integer division and modulus.
  129. /// Returns `(quotient, remainder)`.
  130. ///
  131. /// # Examples
  132. ///
  133. /// ~~~
  134. /// # use num::Integer;
  135. /// assert_eq!(( 8i).div_mod_floor( &3), ( 2, 2));
  136. /// assert_eq!(( 8i).div_mod_floor(&-3), (-3, -1));
  137. /// assert_eq!((-8i).div_mod_floor( &3), (-3, 1));
  138. /// assert_eq!((-8i).div_mod_floor(&-3), ( 2, -2));
  139. ///
  140. /// assert_eq!(( 1i).div_mod_floor( &2), ( 0, 1));
  141. /// assert_eq!(( 1i).div_mod_floor(&-2), (-1, -1));
  142. /// assert_eq!((-1i).div_mod_floor( &2), (-1, 1));
  143. /// assert_eq!((-1i).div_mod_floor(&-2), ( 0, -1));
  144. /// ~~~
  145. fn div_mod_floor(&self, other: &Self) -> (Self, Self) {
  146. (self.div_floor(other), self.mod_floor(other))
  147. }
  148. }
  149. /// Simultaneous integer division and modulus
  150. #[inline] pub fn div_rem<T: Integer>(x: T, y: T) -> (T, T) { x.div_rem(&y) }
  151. /// Floored integer division
  152. #[inline] pub fn div_floor<T: Integer>(x: T, y: T) -> T { x.div_floor(&y) }
  153. /// Floored integer modulus
  154. #[inline] pub fn mod_floor<T: Integer>(x: T, y: T) -> T { x.mod_floor(&y) }
  155. /// Simultaneous floored integer division and modulus
  156. #[inline] pub fn div_mod_floor<T: Integer>(x: T, y: T) -> (T, T) { x.div_mod_floor(&y) }
  157. /// Calculates the Greatest Common Divisor (GCD) of the number and `other`. The
  158. /// result is always positive.
  159. #[inline(always)] pub fn gcd<T: Integer>(x: T, y: T) -> T { x.gcd(&y) }
  160. /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
  161. #[inline(always)] pub fn lcm<T: Integer>(x: T, y: T) -> T { x.lcm(&y) }
  162. macro_rules! impl_integer_for_int {
  163. ($T:ty, $test_mod:ident) => (
  164. impl Integer for $T {
  165. /// Floored integer division
  166. #[inline]
  167. fn div_floor(&self, other: &$T) -> $T {
  168. // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
  169. // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
  170. match self.div_rem(other) {
  171. (d, r) if (r > 0 && *other < 0)
  172. || (r < 0 && *other > 0) => d - 1,
  173. (d, _) => d,
  174. }
  175. }
  176. /// Floored integer modulo
  177. #[inline]
  178. fn mod_floor(&self, other: &$T) -> $T {
  179. // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
  180. // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
  181. match *self % *other {
  182. r if (r > 0 && *other < 0)
  183. || (r < 0 && *other > 0) => r + *other,
  184. r => r,
  185. }
  186. }
  187. /// Calculates `div_floor` and `mod_floor` simultaneously
  188. #[inline]
  189. fn div_mod_floor(&self, other: &$T) -> ($T,$T) {
  190. // Algorithm from [Daan Leijen. _Division and Modulus for Computer Scientists_,
  191. // December 2001](http://research.microsoft.com/pubs/151917/divmodnote-letter.pdf)
  192. match self.div_rem(other) {
  193. (d, r) if (r > 0 && *other < 0)
  194. || (r < 0 && *other > 0) => (d - 1, r + *other),
  195. (d, r) => (d, r),
  196. }
  197. }
  198. /// Calculates the Greatest Common Divisor (GCD) of the number and
  199. /// `other`. The result is always positive.
  200. #[inline]
  201. fn gcd(&self, other: &$T) -> $T {
  202. // Use Euclid's algorithm
  203. let mut m = *self;
  204. let mut n = *other;
  205. while m != 0 {
  206. let temp = m;
  207. m = n % temp;
  208. n = temp;
  209. }
  210. n.abs()
  211. }
  212. /// Calculates the Lowest Common Multiple (LCM) of the number and
  213. /// `other`.
  214. #[inline]
  215. fn lcm(&self, other: &$T) -> $T {
  216. // should not have to recalculate abs
  217. ((*self * *other) / self.gcd(other)).abs()
  218. }
  219. /// Deprecated, use `is_multiple_of` instead.
  220. #[deprecated = "function renamed to `is_multiple_of`"]
  221. #[inline]
  222. fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
  223. /// Returns `true` if the number is a multiple of `other`.
  224. #[inline]
  225. fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
  226. /// Returns `true` if the number is divisible by `2`
  227. #[inline]
  228. fn is_even(&self) -> bool { self & 1 == 0 }
  229. /// Returns `true` if the number is not divisible by `2`
  230. #[inline]
  231. fn is_odd(&self) -> bool { !self.is_even() }
  232. }
  233. #[cfg(test)]
  234. mod $test_mod {
  235. use Integer;
  236. /// Checks that the division rule holds for:
  237. ///
  238. /// - `n`: numerator (dividend)
  239. /// - `d`: denominator (divisor)
  240. /// - `qr`: quotient and remainder
  241. #[cfg(test)]
  242. fn test_division_rule((n,d): ($T,$T), (q,r): ($T,$T)) {
  243. assert_eq!(d * q + r, n);
  244. }
  245. #[test]
  246. fn test_div_rem() {
  247. fn test_nd_dr(nd: ($T,$T), qr: ($T,$T)) {
  248. let (n,d) = nd;
  249. let separate_div_rem = (n / d, n % d);
  250. let combined_div_rem = n.div_rem(&d);
  251. assert_eq!(separate_div_rem, qr);
  252. assert_eq!(combined_div_rem, qr);
  253. test_division_rule(nd, separate_div_rem);
  254. test_division_rule(nd, combined_div_rem);
  255. }
  256. test_nd_dr(( 8, 3), ( 2, 2));
  257. test_nd_dr(( 8, -3), (-2, 2));
  258. test_nd_dr((-8, 3), (-2, -2));
  259. test_nd_dr((-8, -3), ( 2, -2));
  260. test_nd_dr(( 1, 2), ( 0, 1));
  261. test_nd_dr(( 1, -2), ( 0, 1));
  262. test_nd_dr((-1, 2), ( 0, -1));
  263. test_nd_dr((-1, -2), ( 0, -1));
  264. }
  265. #[test]
  266. fn test_div_mod_floor() {
  267. fn test_nd_dm(nd: ($T,$T), dm: ($T,$T)) {
  268. let (n,d) = nd;
  269. let separate_div_mod_floor = (n.div_floor(&d), n.mod_floor(&d));
  270. let combined_div_mod_floor = n.div_mod_floor(&d);
  271. assert_eq!(separate_div_mod_floor, dm);
  272. assert_eq!(combined_div_mod_floor, dm);
  273. test_division_rule(nd, separate_div_mod_floor);
  274. test_division_rule(nd, combined_div_mod_floor);
  275. }
  276. test_nd_dm(( 8, 3), ( 2, 2));
  277. test_nd_dm(( 8, -3), (-3, -1));
  278. test_nd_dm((-8, 3), (-3, 1));
  279. test_nd_dm((-8, -3), ( 2, -2));
  280. test_nd_dm(( 1, 2), ( 0, 1));
  281. test_nd_dm(( 1, -2), (-1, -1));
  282. test_nd_dm((-1, 2), (-1, 1));
  283. test_nd_dm((-1, -2), ( 0, -1));
  284. }
  285. #[test]
  286. fn test_gcd() {
  287. assert_eq!((10 as $T).gcd(&2), 2 as $T);
  288. assert_eq!((10 as $T).gcd(&3), 1 as $T);
  289. assert_eq!((0 as $T).gcd(&3), 3 as $T);
  290. assert_eq!((3 as $T).gcd(&3), 3 as $T);
  291. assert_eq!((56 as $T).gcd(&42), 14 as $T);
  292. assert_eq!((3 as $T).gcd(&-3), 3 as $T);
  293. assert_eq!((-6 as $T).gcd(&3), 3 as $T);
  294. assert_eq!((-4 as $T).gcd(&-2), 2 as $T);
  295. }
  296. #[test]
  297. fn test_lcm() {
  298. assert_eq!((1 as $T).lcm(&0), 0 as $T);
  299. assert_eq!((0 as $T).lcm(&1), 0 as $T);
  300. assert_eq!((1 as $T).lcm(&1), 1 as $T);
  301. assert_eq!((-1 as $T).lcm(&1), 1 as $T);
  302. assert_eq!((1 as $T).lcm(&-1), 1 as $T);
  303. assert_eq!((-1 as $T).lcm(&-1), 1 as $T);
  304. assert_eq!((8 as $T).lcm(&9), 72 as $T);
  305. assert_eq!((11 as $T).lcm(&5), 55 as $T);
  306. }
  307. #[test]
  308. fn test_even() {
  309. assert_eq!((-4 as $T).is_even(), true);
  310. assert_eq!((-3 as $T).is_even(), false);
  311. assert_eq!((-2 as $T).is_even(), true);
  312. assert_eq!((-1 as $T).is_even(), false);
  313. assert_eq!((0 as $T).is_even(), true);
  314. assert_eq!((1 as $T).is_even(), false);
  315. assert_eq!((2 as $T).is_even(), true);
  316. assert_eq!((3 as $T).is_even(), false);
  317. assert_eq!((4 as $T).is_even(), true);
  318. }
  319. #[test]
  320. fn test_odd() {
  321. assert_eq!((-4 as $T).is_odd(), false);
  322. assert_eq!((-3 as $T).is_odd(), true);
  323. assert_eq!((-2 as $T).is_odd(), false);
  324. assert_eq!((-1 as $T).is_odd(), true);
  325. assert_eq!((0 as $T).is_odd(), false);
  326. assert_eq!((1 as $T).is_odd(), true);
  327. assert_eq!((2 as $T).is_odd(), false);
  328. assert_eq!((3 as $T).is_odd(), true);
  329. assert_eq!((4 as $T).is_odd(), false);
  330. }
  331. }
  332. )
  333. }
  334. impl_integer_for_int!(i8, test_integer_i8)
  335. impl_integer_for_int!(i16, test_integer_i16)
  336. impl_integer_for_int!(i32, test_integer_i32)
  337. impl_integer_for_int!(i64, test_integer_i64)
  338. impl_integer_for_int!(int, test_integer_int)
  339. macro_rules! impl_integer_for_uint {
  340. ($T:ty, $test_mod:ident) => (
  341. impl Integer for $T {
  342. /// Unsigned integer division. Returns the same result as `div` (`/`).
  343. #[inline]
  344. fn div_floor(&self, other: &$T) -> $T { *self / *other }
  345. /// Unsigned integer modulo operation. Returns the same result as `rem` (`%`).
  346. #[inline]
  347. fn mod_floor(&self, other: &$T) -> $T { *self % *other }
  348. /// Calculates the Greatest Common Divisor (GCD) of the number and `other`
  349. #[inline]
  350. fn gcd(&self, other: &$T) -> $T {
  351. // Use Euclid's algorithm
  352. let mut m = *self;
  353. let mut n = *other;
  354. while m != 0 {
  355. let temp = m;
  356. m = n % temp;
  357. n = temp;
  358. }
  359. n
  360. }
  361. /// Calculates the Lowest Common Multiple (LCM) of the number and `other`.
  362. #[inline]
  363. fn lcm(&self, other: &$T) -> $T {
  364. (*self * *other) / self.gcd(other)
  365. }
  366. /// Deprecated, use `is_multiple_of` instead.
  367. #[deprecated = "function renamed to `is_multiple_of`"]
  368. #[inline]
  369. fn divides(&self, other: &$T) -> bool { return self.is_multiple_of(other); }
  370. /// Returns `true` if the number is a multiple of `other`.
  371. #[inline]
  372. fn is_multiple_of(&self, other: &$T) -> bool { *self % *other == 0 }
  373. /// Returns `true` if the number is divisible by `2`.
  374. #[inline]
  375. fn is_even(&self) -> bool { self & 1 == 0 }
  376. /// Returns `true` if the number is not divisible by `2`.
  377. #[inline]
  378. fn is_odd(&self) -> bool { !self.is_even() }
  379. }
  380. #[cfg(test)]
  381. mod $test_mod {
  382. use Integer;
  383. #[test]
  384. fn test_div_mod_floor() {
  385. assert_eq!((10 as $T).div_floor(&(3 as $T)), 3 as $T);
  386. assert_eq!((10 as $T).mod_floor(&(3 as $T)), 1 as $T);
  387. assert_eq!((10 as $T).div_mod_floor(&(3 as $T)), (3 as $T, 1 as $T));
  388. assert_eq!((5 as $T).div_floor(&(5 as $T)), 1 as $T);
  389. assert_eq!((5 as $T).mod_floor(&(5 as $T)), 0 as $T);
  390. assert_eq!((5 as $T).div_mod_floor(&(5 as $T)), (1 as $T, 0 as $T));
  391. assert_eq!((3 as $T).div_floor(&(7 as $T)), 0 as $T);
  392. assert_eq!((3 as $T).mod_floor(&(7 as $T)), 3 as $T);
  393. assert_eq!((3 as $T).div_mod_floor(&(7 as $T)), (0 as $T, 3 as $T));
  394. }
  395. #[test]
  396. fn test_gcd() {
  397. assert_eq!((10 as $T).gcd(&2), 2 as $T);
  398. assert_eq!((10 as $T).gcd(&3), 1 as $T);
  399. assert_eq!((0 as $T).gcd(&3), 3 as $T);
  400. assert_eq!((3 as $T).gcd(&3), 3 as $T);
  401. assert_eq!((56 as $T).gcd(&42), 14 as $T);
  402. }
  403. #[test]
  404. fn test_lcm() {
  405. assert_eq!((1 as $T).lcm(&0), 0 as $T);
  406. assert_eq!((0 as $T).lcm(&1), 0 as $T);
  407. assert_eq!((1 as $T).lcm(&1), 1 as $T);
  408. assert_eq!((8 as $T).lcm(&9), 72 as $T);
  409. assert_eq!((11 as $T).lcm(&5), 55 as $T);
  410. assert_eq!((99 as $T).lcm(&17), 1683 as $T);
  411. }
  412. #[test]
  413. fn test_is_multiple_of() {
  414. assert!((6 as $T).is_multiple_of(&(6 as $T)));
  415. assert!((6 as $T).is_multiple_of(&(3 as $T)));
  416. assert!((6 as $T).is_multiple_of(&(1 as $T)));
  417. }
  418. #[test]
  419. fn test_even() {
  420. assert_eq!((0 as $T).is_even(), true);
  421. assert_eq!((1 as $T).is_even(), false);
  422. assert_eq!((2 as $T).is_even(), true);
  423. assert_eq!((3 as $T).is_even(), false);
  424. assert_eq!((4 as $T).is_even(), true);
  425. }
  426. #[test]
  427. fn test_odd() {
  428. assert_eq!((0 as $T).is_odd(), false);
  429. assert_eq!((1 as $T).is_odd(), true);
  430. assert_eq!((2 as $T).is_odd(), false);
  431. assert_eq!((3 as $T).is_odd(), true);
  432. assert_eq!((4 as $T).is_odd(), false);
  433. }
  434. }
  435. )
  436. }
  437. impl_integer_for_uint!(u8, test_integer_u8)
  438. impl_integer_for_uint!(u16, test_integer_u16)
  439. impl_integer_for_uint!(u32, test_integer_u32)
  440. impl_integer_for_uint!(u64, test_integer_u64)
  441. impl_integer_for_uint!(uint, test_integer_uint)