Skip to content

Commit e369546

Browse files
committed
libstd: impl Orderable for BigUint/BigInt
1 parent 5ce0795 commit e369546

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/libstd/num/bigint.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ A BigInt is a combination of BigUint and Sign.
1717
*/
1818

1919
use core::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater};
20-
use core::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix};
20+
use core::num::{IntConvertible, Zero, One, ToStrRadix, FromStrRadix, Orderable};
2121

2222
/**
2323
A BigDigit is a BigUint's composing element.
@@ -146,6 +146,24 @@ impl FromStr for BigUint {
146146

147147
impl Num for BigUint {}
148148

149+
impl Orderable for BigUint {
150+
#[inline(always)]
151+
fn min(&self, other: &BigUint) -> BigUint {
152+
if self < other { self.clone() } else { other.clone() }
153+
}
154+
155+
#[inline(always)]
156+
fn max(&self, other: &BigUint) -> BigUint {
157+
if self > other { self.clone() } else { other.clone() }
158+
}
159+
160+
#[inline(always)]
161+
fn clamp(&self, mn: &BigUint, mx: &BigUint) -> BigUint {
162+
if self > mx { mx.clone() } else
163+
if self < mn { mn.clone() } else { self.clone() }
164+
}
165+
}
166+
149167
impl Shl<uint, BigUint> for BigUint {
150168
#[inline(always)]
151169
fn shl(&self, rhs: &uint) -> BigUint {
@@ -792,6 +810,24 @@ impl FromStr for BigInt {
792810

793811
impl Num for BigInt {}
794812

813+
impl Orderable for BigInt {
814+
#[inline(always)]
815+
fn min(&self, other: &BigInt) -> BigInt {
816+
if self < other { self.clone() } else { other.clone() }
817+
}
818+
819+
#[inline(always)]
820+
fn max(&self, other: &BigInt) -> BigInt {
821+
if self > other { self.clone() } else { other.clone() }
822+
}
823+
824+
#[inline(always)]
825+
fn clamp(&self, mn: &BigInt, mx: &BigInt) -> BigInt {
826+
if self > mx { mx.clone() } else
827+
if self < mn { mn.clone() } else { self.clone() }
828+
}
829+
}
830+
795831
impl Shl<uint, BigInt> for BigInt {
796832
#[inline(always)]
797833
fn shl(&self, rhs: &uint) -> BigInt {

0 commit comments

Comments
 (0)