weak_order.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <__type_traits/decay.h>
  15. #include <__utility/forward.h>
  16. #include <__utility/priority_tag.h>
  17. #include <cmath>
  18. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. #if _LIBCPP_STD_VER > 17
  23. // [cmp.alg]
  24. namespace __weak_order {
  25. struct __fn {
  26. // NOLINTBEGIN(libcpp-robust-against-adl) weak_order should use ADL, but only here
  27. template<class _Tp, class _Up>
  28. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  29. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  30. __go(_Tp&& __t, _Up&& __u, __priority_tag<3>)
  31. noexcept(noexcept(weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  32. -> decltype( weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  33. { return weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  34. // NOLINTEND(libcpp-robust-against-adl)
  35. template<class _Tp, class _Up, class _Dp = decay_t<_Tp>>
  36. requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
  37. _LIBCPP_HIDE_FROM_ABI static constexpr weak_ordering
  38. __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept
  39. {
  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 = _VSTD::isnan(__t);
  50. bool __u_is_nan = _VSTD::isnan(__u);
  51. bool __t_is_negative = _VSTD::signbit(__t);
  52. bool __u_is_negative = _VSTD::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
  65. __go(_Tp&& __t, _Up&& __u, __priority_tag<1>)
  66. noexcept(noexcept(weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  67. -> decltype( weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  68. { return weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  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
  72. __go(_Tp&& __t, _Up&& __u, __priority_tag<0>)
  73. noexcept(noexcept(weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  74. -> decltype( weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  75. { return weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  76. template<class _Tp, class _Up>
  77. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
  78. noexcept(noexcept(__go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>())))
  79. -> decltype( __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()))
  80. { return __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()); }
  81. };
  82. } // namespace __weak_order
  83. inline namespace __cpo {
  84. inline constexpr auto weak_order = __weak_order::__fn{};
  85. } // namespace __cpo
  86. #endif // _LIBCPP_STD_VER > 17
  87. _LIBCPP_END_NAMESPACE_STD
  88. #endif // _LIBCPP___COMPARE_WEAK_ORDER