From 372d67f2c1ae3b885e4ca592140a15ee4761891f Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 1 Nov 2012 16:17:38 +1000 Subject: [PATCH] Implement MinMax for all numeric types --- src/math.rs | 85 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 63 insertions(+), 22 deletions(-) diff --git a/src/math.rs b/src/math.rs index 6a205d4..4fa5992 100644 --- a/src/math.rs +++ b/src/math.rs @@ -20,29 +20,72 @@ trait MinMax { #[inline(always)] pure fn min(a: &T, b: &T) -> T { a.min(b) } #[inline(always)] pure fn max(a: &T, b: &T) -> T { a.max(b) } +impl u8: MinMax { + #[inline(always)] pure fn min(other: &u8) -> u8 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &u8) -> u8 { if self > *other { self } else { *other } } +} + +impl u16: MinMax { + #[inline(always)] pure fn min(other: &u16) -> u16 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &u16) -> u16 { if self > *other { self } else { *other } } +} + +impl u32: MinMax { + #[inline(always)] pure fn min(other: &u32) -> u32 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &u32) -> u32 { if self > *other { self } else { *other } } +} + +impl u64: MinMax { + #[inline(always)] pure fn min(other: &u64) -> u64 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &u64) -> u64 { if self > *other { self } else { *other } } +} + +impl uint: MinMax { + #[inline(always)] pure fn min(other: &uint) -> uint { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &uint) -> uint { if self > *other { self } else { *other } } +} + +impl i8: MinMax { + #[inline(always)] pure fn min(other: &i8) -> i8 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &i8) -> i8 { if self > *other { self } else { *other } } +} + +impl i16: MinMax { + #[inline(always)] pure fn min(other: &i16) -> i16 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &i16) -> i16 { if self > *other { self } else { *other } } +} + +impl i32: MinMax { + #[inline(always)] pure fn min(other: &i32) -> i32 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &i32) -> i32 { if self > *other { self } else { *other } } +} + +impl i64: MinMax { + #[inline(always)] pure fn min(other: &i64) -> i64 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &i64) -> i64 { if self > *other { self } else { *other } } +} + +impl int: MinMax { + #[inline(always)] pure fn min(other: &int) -> int { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &int) -> int { if self > *other { self } else { *other } } +} + +impl f32: MinMax { + #[inline(always)] pure fn min(other: &f32) -> f32 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &f32) -> f32 { if self > *other { self } else { *other } } +} + +impl f64: MinMax { + #[inline(always)] pure fn min(other: &f64) -> f64 { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &f64) -> f64 { if self > *other { self } else { *other } } +} + impl float: MinMax { - pure fn min(other: &float) -> float { - if self < *other { self } - else { *other } - } - - pure fn max(other: &float) -> float { - if self > *other { self } - else { *other } - } - + #[inline(always)] pure fn min(other: &float) -> float { if self < *other { self } else { *other } } + #[inline(always)] pure fn max(other: &float) -> float { if self > *other { self } else { *other } } } - -// #[inline(always)] -// pure fn abs(x: &T) -> T { -// if x >= &from_int(0) { *x } else {-x } -// } - -// -// Abs -// trait Abs { pure fn abs() -> self; } @@ -116,9 +159,7 @@ impl float: Abs { } } -// -// Sqrt -// + trait Sqrt { pure fn sqrt() -> self; }