Permalink
Browse files
Move clamp into Ord and use RangeInclusive
- Loading branch information...
Showing
with
67 additions
and
76 deletions.
-
+31
−0
src/libcore/cmp.rs
-
+0
−50
src/libcore/num/mod.rs
-
+19
−13
src/libstd/f32.rs
-
+17
−13
src/libstd/f64.rs
|
|
@@ -652,6 +652,37 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T { |
|
|
|
if v2 >= v1 { v2 } else { v1 } |
|
|
|
} |
|
|
|
|
|
|
|
use ops::RangeInclusive; |
|
|
|
/// Returns the upper bound of the range if input is greater than the range, and the lower bound of |
|
|
|
/// the range if input is less than the range. Otherwise this will return input. |
|
|
|
/// |
|
|
|
/// #Examples |
|
|
|
/// |
|
|
|
/// ``` |
|
|
|
/// assert!((-3).clamp(-2...1) == -2); |
|
|
|
/// assert!((0).clamp(-2...1) == 0); |
|
|
|
/// assert!((2).clamp(-2, 1) == 1); |
|
|
|
/// ``` |
|
|
|
#[inline] |
|
|
|
#[unstable(feature = "clamp", reason = "Recently added.")] |
|
|
|
pub fn clamp<T: Ord>(input: T, range: RangeInclusive<T>) -> T { |
|
|
|
if let RangeInclusive::NonEmpty{start, end} = range { |
|
|
|
if input < start { |
|
|
|
return start; |
|
|
|
} |
|
|
|
else if input > end { |
|
|
|
return end; |
|
|
|
} |
|
|
|
else { |
|
|
|
return input; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
// This should never be executed. |
|
|
|
return input; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types |
|
|
|
mod impls { |
|
|
|
use cmp::Ordering::{self, Less, Greater, Equal}; |
|
|
|
|
|
@@ -1303,31 +1303,6 @@ macro_rules! int_impl { |
|
|
|
#[stable(feature = "rust1", since = "1.0.0")] |
|
|
|
#[inline] |
|
|
|
pub fn is_negative(self) -> bool { self < 0 } |
|
|
|
|
|
|
|
/// Returns max if self is greater than max, and min if self is less than min. |
|
|
|
/// Otherwise this returns self. |
|
|
|
/// |
|
|
|
/// # Examples |
|
|
|
/// |
|
|
|
/// Basic usage: |
|
|
|
/// |
|
|
|
/// ``` |
|
|
|
/// assert!((-3i8).clamp(-2i8, 1i8) == -2i8); |
|
|
|
/// assert!((0i8).clamp(-2i8, 1i8) == 0i8); |
|
|
|
/// assert!((2i8).clamp(-2i8, 1i8) == 1i8); |
|
|
|
/// ``` |
|
|
|
#[inline] |
|
|
|
pub fn clamp(self, min: Self, max: Self) -> Self { |
|
|
|
if self < min { |
|
|
|
min |
|
|
|
} |
|
|
|
else if self > max { |
|
|
|
max |
|
|
|
} |
|
|
|
else { |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@@ -2393,31 +2368,6 @@ macro_rules! uint_impl { |
|
|
|
None |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// Returns max if self is greater than max, and min if self is less than min. |
|
|
|
/// Otherwise this returns self. |
|
|
|
/// |
|
|
|
/// # Examples |
|
|
|
/// |
|
|
|
/// Basic usage: |
|
|
|
/// |
|
|
|
/// ``` |
|
|
|
/// assert!((0u8).clamp(1u8, 3u8) == 1u8); |
|
|
|
/// assert!((2u8).clamp(1u8, 3u8) == 2u8); |
|
|
|
/// assert!((4u8).clamp(1u8, 3u8) == 3u8); |
|
|
|
/// ``` |
|
|
|
#[inline] |
|
|
|
pub fn clamp(self, min: Self, max: Self) -> Self { |
|
|
|
if self < min { |
|
|
|
min |
|
|
|
} |
|
|
|
else if self > max { |
|
|
|
max |
|
|
|
} |
|
|
|
else { |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
@@ -23,6 +23,8 @@ use intrinsics; |
|
|
|
use libc::c_int; |
|
|
|
#[cfg(not(test))] |
|
|
|
use num::FpCategory; |
|
|
|
#[cfg(not(test))] |
|
|
|
use ops::RangeInclusive; |
|
|
|
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")] |
|
|
@@ -1225,28 +1227,32 @@ impl f32 { |
|
|
|
0.5 * ((2.0 * self) / (1.0 - self)).ln_1p() |
|
|
|
} |
|
|
|
|
|
|
|
/// Returns max if self is greater than max, and min if self is less than min. |
|
|
|
/// Returns the upper bound if self is greater than the bound, and the lower bound if self is less than the bound. |
|
|
|
/// Otherwise this returns self. |
|
|
|
/// |
|
|
|
/// # Examples |
|
|
|
/// |
|
|
|
/// Basic usage: |
|
|
|
/// |
|
|
|
/// ``` |
|
|
|
/// assert!((-3.0f32).clamp(-2.0f32, 1.0f32) == -2.0f32); |
|
|
|
/// assert!((0.0f32).clamp(-2.0f32, 1.0f32) == 0.0f32); |
|
|
|
/// assert!((2.0f32).clamp(-2.0f32, 1.0f32) == 1.0f32); |
|
|
|
/// assert!((-3.0f32).clamp(-2.0f32...1.0f32) == -2.0f32); |
|
|
|
/// assert!((0.0f32).clamp(-2.0f32...1.0f32) == 0.0f32); |
|
|
|
/// assert!((2.0f32).clamp(-2.0f32...1.0f32) == 1.0f32); |
|
|
|
/// ``` |
|
|
|
#[unstable(feature = "clamp", reason = "Recently added.")] |
|
|
|
#[inline] |
|
|
|
pub fn clamp(self, min: f32, max: f32) -> f32 { |
|
|
|
if self < min { |
|
|
|
min |
|
|
|
} |
|
|
|
else if self > max { |
|
|
|
max |
|
|
|
pub fn clamp(self, range: RangeInclusive<f32>) -> f32 { |
|
|
|
if let NonEmpty{start, end} = range { |
|
|
|
if self < start { |
|
|
|
return start; |
|
|
|
} |
|
|
|
else if self > max { |
|
|
|
return max; |
|
|
|
} |
|
|
|
else { |
|
|
|
return self; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
self |
|
|
|
return NAN; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
@@ -1117,28 +1117,32 @@ impl f64 { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// Returns max if self is greater than max, and min if self is less than min. |
|
|
|
/// Returns the upper bound if self is greater than the bound, and the lower bound if self is less than the bound. |
|
|
|
/// Otherwise this returns self. |
|
|
|
/// |
|
|
|
/// # Examples |
|
|
|
/// |
|
|
|
/// Basic usage: |
|
|
|
/// |
|
|
|
/// ``` |
|
|
|
/// assert!((-3.0f64).clamp(-2.0f64, 1.0f64) == -2.0f64); |
|
|
|
/// assert!((0.0f64).clamp(-2.0f64, 1.0f64) == 0.0f64); |
|
|
|
/// assert!((2.0f64).clamp(-2.0f64, 1.0f64) == 1.0f64); |
|
|
|
/// assert!((-3.0f64).clamp(-2.0f64...1.0f64) == -2.0f64); |
|
|
|
/// assert!((0.0f64).clamp(-2.0f64...1.0f64) == 0.0f64); |
|
|
|
/// assert!((2.0f64).clamp(-2.0f64...1.0f64) == 1.0f64); |
|
|
|
/// ``` |
|
|
|
#[unstable(feature = "clamp", reason = "Recently added.")] |
|
|
|
#[inline] |
|
|
|
pub fn clamp(self, min: f64, max: f64) -> f64 { |
|
|
|
if self < min { |
|
|
|
min |
|
|
|
} |
|
|
|
else if self > max { |
|
|
|
max |
|
|
|
pub fn clamp(self, range: RangeInclusive<f64>) -> f64 { |
|
|
|
if let NonEmpty{start, end} = range { |
|
|
|
if self < start { |
|
|
|
return start; |
|
|
|
} |
|
|
|
else if self > max { |
|
|
|
return max; |
|
|
|
} |
|
|
|
else { |
|
|
|
return self; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
self |
|
|
|
return NAN; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
0 comments on commit
e1f9051