Skip to content

Commit c5835cd

Browse files
committed
implement all min/max fns in terms of </is_lt
`<` seems to be the "lucky one" for llvm
1 parent b20307b commit c5835cd

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

Diff for: library/core/src/cmp.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
999999
where
10001000
Self: Sized,
10011001
{
1002-
max_by(self, other, Ord::cmp)
1002+
if other < self { self } else { other }
10031003
}
10041004

10051005
/// Compares and returns the minimum of two values.
@@ -1038,7 +1038,7 @@ pub trait Ord: Eq + PartialOrd<Self> {
10381038
where
10391039
Self: Sized,
10401040
{
1041-
min_by(self, other, Ord::cmp)
1041+
if other < self { other } else { self }
10421042
}
10431043

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

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

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

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

16291623
/// Compares and sorts two values, returning minimum and maximum.
@@ -1670,7 +1664,7 @@ pub fn minmax<T>(v1: T, v2: T) -> [T; 2]
16701664
where
16711665
T: Ord,
16721666
{
1673-
if v1 <= v2 { [v1, v2] } else { [v2, v1] }
1667+
if v2 < v1 { [v2, v1] } else { [v1, v2] }
16741668
}
16751669

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

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

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

0 commit comments

Comments
 (0)