weak_order.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. struct __fn {
  28. // NOLINTBEGIN(libcpp-robust-against-adl) weak_order should use ADL, but only here
  29. template <class _Tp, class _Up>
  30. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  31. _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<3>) noexcept(
  32. noexcept(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
  33. -> decltype(weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
  34. return weak_ordering(weak_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
  35. }
  36. // NOLINTEND(libcpp-robust-against-adl)
  37. template <class _Tp, class _Up, class _Dp = decay_t<_Tp>>
  38. requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
  39. _LIBCPP_HIDE_FROM_ABI static constexpr weak_ordering __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept {
  40. partial_ordering __po = (__t <=> __u);
  41. if (__po == partial_ordering::less) {
  42. return weak_ordering::less;
  43. } else if (__po == partial_ordering::equivalent) {
  44. return weak_ordering::equivalent;
  45. } else if (__po == partial_ordering::greater) {
  46. return weak_ordering::greater;
  47. } else {
  48. // Otherwise, at least one of them is a NaN.
  49. bool __t_is_nan = __math::isnan(__t);
  50. bool __u_is_nan = __math::isnan(__u);
  51. bool __t_is_negative = __math::signbit(__t);
  52. bool __u_is_negative = __math::signbit(__u);
  53. if (__t_is_nan && __u_is_nan) {
  54. return (__u_is_negative <=> __t_is_negative);
  55. } else if (__t_is_nan) {
  56. return __t_is_negative ? weak_ordering::less : weak_ordering::greater;
  57. } else {
  58. return __u_is_negative ? weak_ordering::greater : weak_ordering::less;
  59. }
  60. }
  61. }
  62. template <class _Tp, class _Up>
  63. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  64. _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<1>) noexcept(
  65. noexcept(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
  66. -> decltype(weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
  67. return weak_ordering(compare_three_way()(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
  68. }
  69. template <class _Tp, class _Up>
  70. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  71. _LIBCPP_HIDE_FROM_ABI static constexpr auto __go(_Tp&& __t, _Up&& __u, __priority_tag<0>) noexcept(
  72. noexcept(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))))
  73. -> decltype(weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)))) {
  74. return weak_ordering(std::strong_order(std::forward<_Tp>(__t), std::forward<_Up>(__u)));
  75. }
  76. template <class _Tp, class _Up>
  77. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
  78. noexcept(noexcept(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())))
  79. -> decltype(__go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>())) {
  80. return __go(std::forward<_Tp>(__t), std::forward<_Up>(__u), __priority_tag<3>());
  81. }
  82. };
  83. } // namespace __weak_order
  84. inline namespace __cpo {
  85. inline constexpr auto weak_order = __weak_order::__fn{};
  86. } // namespace __cpo
  87. #endif // _LIBCPP_STD_VER >= 20
  88. _LIBCPP_END_NAMESPACE_STD
  89. #endif // _LIBCPP___COMPARE_WEAK_ORDER