From 9fb67f812508ed59808e043330f543cead7af38a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Tue, 18 Feb 2014 12:48:52 +0100 Subject: [PATCH] Add more comparison default methods to the `PartialOrd` trait. This allows for some optimization. --- src/structs/vec_macros.rs | 20 ++++++++++++++++++++ src/traits/operations.rs | 24 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/structs/vec_macros.rs b/src/structs/vec_macros.rs index 11eac72b..27dbca56 100644 --- a/src/structs/vec_macros.rs +++ b/src/structs/vec_macros.rs @@ -83,6 +83,26 @@ macro_rules! ord_impl( } } } + + #[inline] + fn partial_lt(a: &$t, b: &$t) -> bool { + a.$comp0 < b.$comp0 $(&& a.$compN < b.$compN)* + } + + #[inline] + fn partial_le(a: &$t, b: &$t) -> bool { + a.$comp0 <= b.$comp0 $(&& a.$compN <= b.$compN)* + } + + #[inline] + fn partial_gt(a: &$t, b: &$t) -> bool { + a.$comp0 > b.$comp0 $(&& a.$compN > b.$compN)* + } + + #[inline] + fn partial_ge(a: &$t, b: &$t) -> bool { + a.$comp0 >= b.$comp0 $(&& a.$compN >= b.$compN)* + } } ) ) diff --git a/src/traits/operations.rs b/src/traits/operations.rs index 7f090281..68af19cb 100644 --- a/src/traits/operations.rs +++ b/src/traits/operations.rs @@ -80,6 +80,30 @@ pub trait PartialOrd { /// Compare `a` and `b` using a partial ordering relation. 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. #[inline] fn partial_min<'a>(a: &'a Self, b: &'a Self) -> Option<&'a Self> {