weak_order.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef _LIBCPP___COMPARE_WEAK_ORDER
  9. #define _LIBCPP___COMPARE_WEAK_ORDER
  10. #include <__compare/compare_three_way.h>
  11. #include <__compare/ordering.h>
  12. #include <__compare/strong_order.h>
  13. #include <__config>
  14. #include <__math/traits.h>
  15. #include <__type_traits/decay.h>
  16. #include <__type_traits/is_floating_point.h>
  17. #include <__type_traits/is_same.h>
  18. #include <__utility/forward.h>
  19. #include <__utility/priority_tag.h>
  20. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_BEGIN_NAMESPACE_STD
  24. #if _LIBCPP_STD_VER >= 20
  25. // [cmp.alg]
  26. namespace __weak_order {
  27. void weak_order() = delete;
  28. struct __fn {
  29. // NOLINTBEGIN(libcpp-robust-against-adl) weak_order should use ADL, but only here
  30. template <class _Tp, class _Up>
  31. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  32. _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<3>) noexcept(
  33. noexcept(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
  34. -> decltype(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
  35. return weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
  36. }
  37. // NOLINTEND(libcpp-robust-against-adl)
  38. template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
  39. requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
  40. _LIBCPP_HIDE_FROM_ABI static constexpr weak_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept {
  41. partial_ordering __po = (__t <=> __u);
  42. if (__po == partial_ordering::less) {
  43. return weak_ordering::less;
  44. } else if (__po == partial_ordering::equivalent) {
  45. return weak_ordering::equivalent;
  46. } else if (__po == partial_ordering::greater) {
  47. return weak_ordering::greater;
  48. } else {
  49. // Otherwise, at least one of them is a NaN.
  50. bool __t_is_nan = __math::isnan(__t);
  51. bool __u_is_nan = __math::isnan(__u);
  52. bool __t_is_negative = __math::signbit(__t);
  53. bool __u_is_negative = __math::signbit(__u);
  54. if (__t_is_nan && __u_is_nan) {
  55. return (__u_is_negative <=> __t_is_negative);
  56. } else if (__t_is_nan) {
  57. return __t_is_negative ? weak_ordering::less : weak_ordering::greater;
  58. } else {
  59. return __u_is_negative ? weak_ordering::greater : weak_ordering::less;
  60. }
  61. }
  62. }
  63. template <class _Tp, class _Up>
  64. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  65. _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept(
  66. noexcept(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
  67. -> decltype(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
  68. return weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
  69. }
  70. template <class _Tp, class _Up>
  71. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  72. _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(
  73. noexcept(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
  74. -> decltype(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
  75. return weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
  76. }
  77. template <class _Tp, class _Up>
  78. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
  79. noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())))
  80. -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())) {
  81. return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>());
  82. }
  83. };
  84. } // namespace __weak_order
  85. inline namespace __cpo {
  86. inline constexpr auto weak_order = __weak_order::__fn{};
  87. } // namespace __cpo
  88. #endif // _LIBCPP_STD_VER >= 20
  89. _LIBCPP_END_NAMESPACE_STD
  90. #endif // _LIBCPP___COMPARE_WEAK_ORDER