Skip to content

Commit

Permalink
implement all min/max fns in terms of </is_lt
Browse files Browse the repository at this point in the history
`<` seems to be the "lucky one" for llvm
  • Loading branch information
WaffleLapkin committed Jan 31, 2025
1 parent b20307b commit c5835cd
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions library/core/src/cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
where
Self: Sized,
{
max_by(self, other, Ord::cmp)
if other < self { self } else { other }
}

/// Compares and returns the minimum of two values.
Expand Down Expand Up @@ -1038,7 +1038,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
where
Self: Sized,
{
min_by(self, other, Ord::cmp)
if other < self { other } else { self }
}

/// Restrict a value to a certain interval.
Expand Down Expand Up @@ -1500,10 +1500,7 @@ pub fn min<T: Ord>(v1: T, v2: T) -> T {
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
match compare(&v1, &v2) {
Ordering::Less | Ordering::Equal => v1,
Ordering::Greater => v2,
}
if compare(&v2, &v1).is_lt() { v2 } else { v1 }
}

/// Returns the element that gives the minimum value from the specified function.
Expand All @@ -1528,7 +1525,7 @@ pub fn min_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
pub fn min_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
min_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
if f(&v2) < f(&v1) { v2 } else { v1 }
}

/// Compares and returns the maximum of two values.
Expand Down Expand Up @@ -1595,10 +1592,7 @@ pub fn max<T: Ord>(v1: T, v2: T) -> T {
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
match compare(&v1, &v2) {
Ordering::Less | Ordering::Equal => v2,
Ordering::Greater => v1,
}
if compare(&v2, &v1).is_lt() { v1 } else { v2 }
}

/// Returns the element that gives the maximum value from the specified function.
Expand All @@ -1623,7 +1617,7 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T {
#[must_use]
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T {
max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
if f(&v2) < f(&v1) { v1 } else { v2 }
}

/// Compares and sorts two values, returning minimum and maximum.
Expand Down Expand Up @@ -1670,7 +1664,7 @@ pub fn minmax<T>(v1: T, v2: T) -> [T; 2]
where
T: Ord,
{
if v1 <= v2 { [v1, v2] } else { [v2, v1] }
if v2 < v1 { [v2, v1] } else { [v1, v2] }
}

/// Returns minimum and maximum values with respect to the specified comparison function.
Expand Down Expand Up @@ -1701,7 +1695,7 @@ pub fn minmax_by<T, F>(v1: T, v2: T, compare: F) -> [T; 2]
where
F: FnOnce(&T, &T) -> Ordering,
{
if compare(&v1, &v2).is_le() { [v1, v2] } else { [v2, v1] }
if compare(&v2, &v1).is_lt() { [v2, v1] } else { [v1, v2] }
}

/// Returns minimum and maximum values with respect to the specified key function.
Expand Down Expand Up @@ -1730,7 +1724,7 @@ where
F: FnMut(&T) -> K,
K: Ord,
{
minmax_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2)))
if f(&v2) < f(&v1) { [v2, v1] } else { [v1, v2] }
}

// Implementation of PartialEq, Eq, PartialOrd and Ord for primitive types
Expand Down

0 comments on commit c5835cd

Please sign in to comment.