Add more comparison default methods to the `PartialOrd` trait.

This allows for some optimization.
This commit is contained in:
Sébastien Crozet 2014-02-18 12:48:52 +01:00
parent becb77843e
commit 9fb67f8125
2 changed files with 44 additions and 0 deletions

View File

@ -83,6 +83,26 @@ macro_rules! ord_impl(
} }
} }
} }
#[inline]
fn partial_lt(a: &$t<N>, b: &$t<N>) -> bool {
a.$comp0 < b.$comp0 $(&& a.$compN < b.$compN)*
}
#[inline]
fn partial_le(a: &$t<N>, b: &$t<N>) -> bool {
a.$comp0 <= b.$comp0 $(&& a.$compN <= b.$compN)*
}
#[inline]
fn partial_gt(a: &$t<N>, b: &$t<N>) -> bool {
a.$comp0 > b.$comp0 $(&& a.$compN > b.$compN)*
}
#[inline]
fn partial_ge(a: &$t<N>, b: &$t<N>) -> bool {
a.$comp0 >= b.$comp0 $(&& a.$compN >= b.$compN)*
}
} }
) )
) )

View File

@ -80,6 +80,30 @@ pub trait PartialOrd {
/// Compare `a` and `b` using a partial ordering relation. /// Compare `a` and `b` using a partial ordering relation.
fn partial_cmp(a: &Self, b: &Self) -> PartialOrdering; fn partial_cmp(a: &Self, b: &Self) -> PartialOrdering;
/// Returns `true` iff `a` and `b` are comparable and `a <= b`.
#[inline]
fn partial_le(a: &Self, b: &Self) -> bool {
PartialOrd::partial_cmp(a, b).is_le()
}
/// Returns `true` iff `a` and `b` are comparable and `a < b`.
#[inline]
fn partial_lt(a: &Self, b: &Self) -> bool {
PartialOrd::partial_cmp(a, b).is_lt()
}
/// Returns `true` iff `a` and `b` are comparable and `a >= b`.
#[inline]
fn partial_ge(a: &Self, b: &Self) -> bool {
PartialOrd::partial_cmp(a, b).is_ge()
}
/// Returns `true` iff `a` and `b` are comparable and `a > b`.
#[inline]
fn partial_gt(a: &Self, b: &Self) -> bool {
PartialOrd::partial_cmp(a, b).is_gt()
}
/// Return the minimum of `a` and `b` if they are comparable. /// Return the minimum of `a` and `b` if they are comparable.
#[inline] #[inline]
fn partial_min<'a>(a: &'a Self, b: &'a Self) -> Option<&'a Self> { fn partial_min<'a>(a: &'a Self, b: &'a Self) -> Option<&'a Self> {