-
Notifications
You must be signed in to change notification settings - Fork 169
Introduce trait for (forced) total ordering of numbers #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fogti
wants to merge
1
commit into
rust-num:main
Choose a base branch
from
fogti:total-ord
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+181
−83
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ pub mod inv; | |
| pub mod mul_add; | ||
| pub mod overflowing; | ||
| pub mod saturating; | ||
| pub mod total_order; | ||
| pub mod wrapping; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| #![forbid(unconditional_recursion)] | ||
|
|
||
| use core::{ | ||
| cmp::Ordering, | ||
| num::{NonZero, self}, | ||
| }; | ||
|
|
||
| /// Returns the ordering between `self` and `other`. | ||
| /// | ||
| /// For some types (particularly floating-point types), there exists a way to | ||
| /// induce a canonical ordering on them, but as it might behave in unexpected | ||
| /// ways, it isn't the default. | ||
| /// | ||
| /// # Integers | ||
| /// | ||
| /// For integers, this just forwards to the normal [`Ord`] implementation. | ||
| /// | ||
| /// # Floating point numbers | ||
| /// | ||
| /// For floating point numbers this provides an implementation | ||
| /// of the `totalOrder` predicate as defined in the IEEE 754 (2008 revision) | ||
| /// floating point standard. | ||
| pub trait TotalOrder { | ||
| /// Return the ordering between `self` and `other`. | ||
| /// | ||
| /// # Floating point numbers | ||
| /// | ||
| /// Unlike the standard partial comparison between floating point numbers, | ||
| /// this comparison always produces an ordering in accordance to | ||
| /// the `totalOrder` predicate as defined in the IEEE 754 (2008 revision) | ||
| /// floating point standard. The values are ordered in the following sequence: | ||
| /// | ||
| /// - negative quiet NaN | ||
| /// - negative signaling NaN | ||
| /// - negative infinity | ||
| /// - negative numbers | ||
| /// - negative subnormal numbers | ||
| /// - negative zero | ||
| /// - positive zero | ||
| /// - positive subnormal numbers | ||
| /// - positive numbers | ||
| /// - positive infinity | ||
| /// - positive signaling NaN | ||
| /// - positive quiet NaN. | ||
| /// | ||
| /// The ordering established by this function does not always agree with the | ||
| /// [`PartialOrd`] and [`PartialEq`] implementations. For example, | ||
| /// they consider negative and positive zero equal, while `total_cmp` | ||
| /// doesn't. | ||
| /// | ||
| /// The interpretation of the signaling NaN bit follows the definition in | ||
| /// the IEEE 754 standard, which may not match the interpretation by some of | ||
| /// the older, non-conformant (e.g. MIPS) hardware implementations. | ||
| /// | ||
| /// # Examples | ||
| /// ``` | ||
| /// use num_traits::ops::total_order::TotalOrder; | ||
| /// use std::cmp::Ordering; | ||
| /// use std::{f32, f64}; | ||
| /// | ||
| /// fn check_eq<T: TotalOrder>(x: T, y: T) { | ||
| /// assert_eq!(x.total_cmp(&y), Ordering::Equal); | ||
| /// } | ||
| /// | ||
| /// check_eq(f64::NAN, f64::NAN); | ||
| /// check_eq(f32::NAN, f32::NAN); | ||
| /// | ||
| /// fn check_lt<T: TotalOrder>(x: T, y: T) { | ||
| /// assert_eq!(x.total_cmp(&y), Ordering::Less); | ||
| /// } | ||
| /// | ||
| /// check_lt(-f64::NAN, f64::NAN); | ||
| /// check_lt(f64::INFINITY, f64::NAN); | ||
| /// check_lt(-0.0_f64, 0.0_f64); | ||
| /// ``` | ||
| fn total_cmp(&self, other: &Self) -> Ordering; | ||
| } | ||
|
|
||
| impl<T: TotalOrder + ?Sized> TotalOrder for &T { | ||
| #[inline] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| (*self).total_cmp(*other) | ||
| } | ||
| } | ||
|
|
||
| impl<T: TotalOrder + ?Sized> TotalOrder for &mut T { | ||
| #[inline] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| (**self).total_cmp(&**other) | ||
| } | ||
| } | ||
|
|
||
| impl<T: TotalOrder> TotalOrder for Option<T> { | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| // Implemented the same as `Ord for Option<_>` | ||
| match (self, other) { | ||
| (Some(l), Some(r)) => l.total_cmp(r), | ||
| (Some(_), None) => Ordering::Greater, | ||
| (None, Some(_)) => Ordering::Less, | ||
| (None, None) => Ordering::Equal, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(has_num_saturating)] | ||
| impl<T: TotalOrder> TotalOrder for num::Saturating<T> { | ||
| #[inline] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| self.0.total_cmp(&other.0) | ||
| } | ||
| } | ||
|
|
||
| impl<T: TotalOrder> TotalOrder for num::Wrapping<T> { | ||
| #[inline] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| self.0.total_cmp(&other.0) | ||
| } | ||
| } | ||
|
|
||
| macro_rules! totalorder_float_impl { | ||
| ($T:ident, $I:ident, $U:ident, $bits:expr) => { | ||
| impl TotalOrder for $T { | ||
| #[inline] | ||
| #[cfg(has_total_cmp)] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| // Forward to the core implementation | ||
| Self::total_cmp(&self, other) | ||
| } | ||
| #[inline] | ||
| #[cfg(not(has_total_cmp))] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| // Backport the core implementation (since 1.62) | ||
| let mut left = self.to_bits() as $I; | ||
| let mut right = other.to_bits() as $I; | ||
|
|
||
| left ^= (((left >> ($bits - 1)) as $U) >> 1) as $I; | ||
| right ^= (((right >> ($bits - 1)) as $U) >> 1) as $I; | ||
|
|
||
| left.cmp(&right) | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| totalorder_float_impl!(f64, i64, u64, 64); | ||
| totalorder_float_impl!(f32, i32, u32, 32); | ||
|
|
||
| macro_rules! totalorder_impl_via_ord_core { | ||
| ($($t:ty),* $(,)?) => { | ||
| $( | ||
| impl TotalOrder for $t { | ||
| #[inline] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| Self::cmp(self, other) | ||
| } | ||
| } | ||
| )* | ||
| } | ||
| } | ||
|
|
||
| totalorder_impl_via_ord_core!((), bool, Ordering); | ||
|
|
||
| macro_rules! totalorder_impl_zeroable_via_ord_core { | ||
| ($($t:ty),* $(,)?) => { | ||
| totalorder_impl_via_ord_core!($($t,)*); | ||
| $( | ||
| impl TotalOrder for NonZero<$t> { | ||
| #[inline] | ||
| fn total_cmp(&self, other: &Self) -> Ordering { | ||
| <$t>::cmp(&self.get(), &other.get()) | ||
| } | ||
| } | ||
| )* | ||
| } | ||
| } | ||
|
|
||
| totalorder_impl_zeroable_via_ord_core!( | ||
| char, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weirdly enough |
||
| ); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, only remaining problem afaik is that
NonZerorequires a bump of MSRV to 1.79