about summary refs log tree commit diff
path: root/src/libstd/num
diff options
context:
space:
mode:
authorgifnksm <makoto.nksm+github@gmail.com>2013-05-14 21:20:27 +0900
committergifnksm <makoto.nksm+github@gmail.com>2013-05-14 21:20:27 +0900
commite3695468b742ff307da3cf29684128ed0785ee03 (patch)
tree45304cfa311f809eea963ed13eb830a4779e3317 /src/libstd/num
parent5ce0795de51b5305bcb48e69a5fcf8cb2e169a30 (diff)
downloadrust-e3695468b742ff307da3cf29684128ed0785ee03.tar.gz
rust-e3695468b742ff307da3cf29684128ed0785ee03.zip
libstd: impl `Orderable` for `BigUint`/`BigInt`
Diffstat (limited to 'src/libstd/num')
-rw-r--r--src/libstd/num/bigint.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/src/libstd/num/bigint.rs b/src/libstd/num/bigint.rs
index 53054afb4d2..c35415c5331 100644
--- a/src/libstd/num/bigint.rs
+++ b/src/libstd/num/bigint.rs
@@ -17,7 +17,7 @@ A BigInt is a combination of BigUint and Sign.
 */
 
 use core::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
-use core::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix};
+use core::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix, Orderable};
 
 /**
 A BigDigit is a BigUint's composing element.
@@ -146,6 +146,24 @@ impl FromStr for BigUint {
 
 impl Num for BigUint {}
 
+impl Orderable for BigUint {
+    #[inline(always)]
+    fn min(&self, other: &BigUint) -> BigUint {
+        if self < other { self.clone() } else { other.clone() }
+    }
+
+    #[inline(always)]
+    fn max(&self, other: &BigUint) -> BigUint {
+        if self > other { self.clone() } else { other.clone() }
+    }
+
+    #[inline(always)]
+    fn clamp(&self, mn: &BigUint, mx: &BigUint) -> BigUint {
+        if self > mx { mx.clone() } else
+        if self < mn { mn.clone() } else { self.clone() }
+    }
+}
+
 impl Shl<uint, BigUint> for BigUint {
     #[inline(always)]
     fn shl(&self, rhs: &uint) -> BigUint {
@@ -792,6 +810,24 @@ impl FromStr for BigInt {
 
 impl Num for BigInt {}
 
+impl Orderable for BigInt {
+    #[inline(always)]
+    fn min(&self, other: &BigInt) -> BigInt {
+        if self < other { self.clone() } else { other.clone() }
+    }
+
+    #[inline(always)]
+    fn max(&self, other: &BigInt) -> BigInt {
+        if self > other { self.clone() } else { other.clone() }
+    }
+
+    #[inline(always)]
+    fn clamp(&self, mn: &BigInt, mx: &BigInt) -> BigInt {
+        if self > mx { mx.clone() } else
+        if self < mn { mn.clone() } else { self.clone() }
+    }
+}
+
 impl Shl<uint, BigInt> for BigInt {
     #[inline(always)]
     fn shl(&self, rhs: &uint) -> BigInt {