Use cond! macro where appropriate

This commit is contained in:
Brendan Zabarauskas 2013-05-18 15:02:58 +10:00
parent 8badea49b0
commit a10974da2d
5 changed files with 78 additions and 6 deletions

View file

@ -248,8 +248,7 @@ impl Orderable for f32 {
if self.is_NaN() || other.is_NaN() { Float::NaN() } else { fmax(*self, *other) }
}
/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
#[cfg(stage0)]
#[inline(always)]
fn clamp(&self, mn: &f32, mx: &f32) -> f32 {
if self.is_NaN() { *self }
@ -257,6 +256,19 @@ impl Orderable for f32 {
else if !(*self >= *mn) { *mn }
else { *self }
}
/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
#[cfg(not(stage0))]
#[inline(always)]
fn clamp(&self, mn: &f32, mx: &f32) -> f32 {
cond!(
(self.is_NaN()) { *self }
(!(*self <= *mx)) { *mx }
(!(*self >= *mn)) { *mn }
_ { *self }
)
}
}
impl Zero for f32 {

View file

@ -270,8 +270,7 @@ impl Orderable for f64 {
if self.is_NaN() || other.is_NaN() { Float::NaN() } else { fmax(*self, *other) }
}
/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
#[cfg(stage0)]
#[inline(always)]
fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
if self.is_NaN() { *self }
@ -279,6 +278,19 @@ impl Orderable for f64 {
else if !(*self >= *mn) { *mn }
else { *self }
}
/// Returns the number constrained within the range `mn <= self <= mx`.
/// If any of the numbers are `NaN` then `NaN` is returned.
#[cfg(not(stage0))]
#[inline(always)]
fn clamp(&self, mn: &f64, mx: &f64) -> f64 {
cond!(
(self.is_NaN()) { *self }
(!(*self <= *mx)) { *mx }
(!(*self >= *mn)) { *mn }
_ { *self }
)
}
}
impl Zero for f64 {

View file

@ -187,11 +187,23 @@ impl Orderable for T {
if *self > *other { *self } else { *other }
}
#[cfg(stage0)]
#[inline(always)]
fn clamp(&self, mn: &T, mx: &T) -> T {
if *self > *mx { *mx } else
if *self < *mn { *mn } else { *self }
}
/// Returns the number constrained within the range `mn <= self <= mx`.
#[cfg(not(stage0))]
#[inline(always)]
fn clamp(&self, mn: &T, mx: &T) -> T {
cond!(
(*self > *mx) { *mx }
(*self < *mn) { *mn }
_ { *self }
)
}
}
impl Zero for T {

View file

@ -153,11 +153,23 @@ impl Orderable for T {
if *self > *other { *self } else { *other }
}
#[cfg(stage0)]
#[inline(always)]
fn clamp(&self, mn: &T, mx: &T) -> T {
if *self > *mx { *mx } else
if *self < *mn { *mn } else { *self }
}
/// Returns the number constrained within the range `mn <= self <= mx`.
#[cfg(not(stage0))]
#[inline(always)]
fn clamp(&self, mn: &T, mx: &T) -> T {
cond!(
(*self > *mx) { *mx }
(*self < *mn) { *mn }
_ { *self }
)
}
}
impl Zero for T {

View file

@ -14,6 +14,7 @@
pub mod general_category {
#[cfg(stage0)]
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
use cmp::{Equal, Less, Greater};
use vec::bsearch;
@ -25,6 +26,18 @@ pub mod general_category {
}) != None
}
#[cfg(not(stage0))]
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
use cmp::{Equal, Less, Greater};
use vec::bsearch;
use option::None;
(do bsearch(r) |&(lo,hi)| { cond!(
(lo <= c && c <= hi) { Equal }
(hi < c) { Less }
_ { Greater }
)}) != None
}
static Cc_table : &'static [(char,char)] = &[
('\x00', '\x1f'), ('\x7f', '\x9f')
@ -1449,8 +1462,7 @@ pub mod general_category {
}
pub mod derived_property {
#[cfg(stage0)]
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
use cmp::{Equal, Less, Greater};
use vec::bsearch;
@ -1462,6 +1474,18 @@ pub mod derived_property {
}) != None
}
#[cfg(not(stage0))]
fn bsearch_range_table(c: char, r: &'static [(char,char)]) -> bool {
use cmp::{Equal, Less, Greater};
use vec::bsearch;
use option::None;
(do bsearch(r) |&(lo,hi)| { cond!(
(lo <= c && c <= hi) { Equal }
(hi < c) { Less }
_ { Greater }
)}) != None
}
static Alphabetic_table : &'static [(char,char)] = &[
('\x41', '\x5a'), ('\x61', '\x7a'),