weak_order.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <__utility/forward.h>
  15. #include <__utility/priority_tag.h>
  16. #include <cmath>
  17. #include <type_traits>
  18. #ifndef _LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER
  19. # pragma GCC system_header
  20. #endif
  21. _LIBCPP_BEGIN_NAMESPACE_STD
  22. #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
  23. // [cmp.alg]
  24. namespace __weak_order {
  25. struct __fn {
  26. template<class _Tp, class _Up>
  27. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  28. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  29. __go(_Tp&& __t, _Up&& __u, __priority_tag<3>)
  30. noexcept(noexcept(weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  31. -> decltype( weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  32. { return weak_ordering(weak_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  33. template<class _Tp, class _Up, class _Dp = decay_t<_Tp>>
  34. requires is_same_v<_Dp, decay_t<_Up>> && is_floating_point_v<_Dp>
  35. _LIBCPP_HIDE_FROM_ABI static constexpr weak_ordering
  36. __go(_Tp&& __t, _Up&& __u, __priority_tag<2>) noexcept
  37. {
  38. partial_ordering __po = (__t <=> __u);
  39. if (__po == partial_ordering::less) {
  40. return weak_ordering::less;
  41. } else if (__po == partial_ordering::equivalent) {
  42. return weak_ordering::equivalent;
  43. } else if (__po == partial_ordering::greater) {
  44. return weak_ordering::greater;
  45. } else {
  46. // Otherwise, at least one of them is a NaN.
  47. bool __t_is_nan = _VSTD::isnan(__t);
  48. bool __u_is_nan = _VSTD::isnan(__u);
  49. bool __t_is_negative = _VSTD::signbit(__t);
  50. bool __u_is_negative = _VSTD::signbit(__u);
  51. if (__t_is_nan && __u_is_nan) {
  52. return (__u_is_negative <=> __t_is_negative);
  53. } else if (__t_is_nan) {
  54. return __t_is_negative ? weak_ordering::less : weak_ordering::greater;
  55. } else {
  56. return __u_is_negative ? weak_ordering::greater : weak_ordering::less;
  57. }
  58. }
  59. }
  60. template<class _Tp, class _Up>
  61. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  62. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  63. __go(_Tp&& __t, _Up&& __u, __priority_tag<1>)
  64. noexcept(noexcept(weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  65. -> decltype( weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  66. { return weak_ordering(compare_three_way()(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  67. template<class _Tp, class _Up>
  68. requires is_same_v<decay_t<_Tp>, decay_t<_Up>>
  69. _LIBCPP_HIDE_FROM_ABI static constexpr auto
  70. __go(_Tp&& __t, _Up&& __u, __priority_tag<0>)
  71. noexcept(noexcept(weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u)))))
  72. -> decltype( weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))))
  73. { return weak_ordering(_VSTD::strong_order(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u))); }
  74. template<class _Tp, class _Up>
  75. _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t, _Up&& __u) const
  76. noexcept(noexcept(__go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>())))
  77. -> decltype( __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()))
  78. { return __go(_VSTD::forward<_Tp>(__t), _VSTD::forward<_Up>(__u), __priority_tag<3>()); }
  79. };
  80. } // namespace __weak_order
  81. inline namespace __cpo {
  82. inline constexpr auto weak_order = __weak_order::__fn{};
  83. } // namespace __cpo
  84. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
  85. _LIBCPP_END_NAMESPACE_STD
  86. #endif // _LIBCPP___COMPARE_WEAK_ORDER