ordering.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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_ORDERING_H
  9. #define _LIBCPP___COMPARE_ORDERING_H
  10. #include <__config>
  11. #include <__type_traits/enable_if.h>
  12. #include <__type_traits/is_same.h>
  13. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  14. # pragma GCC system_header
  15. #endif
  16. _LIBCPP_BEGIN_NAMESPACE_STD
  17. #if _LIBCPP_STD_VER >= 20
  18. // exposition only
  19. enum class _OrdResult : signed char { __less = -1, __equiv = 0, __greater = 1 };
  20. enum class _NCmpResult : signed char { __unordered = -127 };
  21. class partial_ordering;
  22. class weak_ordering;
  23. class strong_ordering;
  24. template <class _Tp, class... _Args>
  25. inline constexpr bool __one_of_v = (is_same_v<_Tp, _Args> || ...);
  26. struct _CmpUnspecifiedParam {
  27. _LIBCPP_HIDE_FROM_ABI constexpr _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
  28. template <class _Tp, class = enable_if_t<!__one_of_v<_Tp, int, partial_ordering, weak_ordering, strong_ordering>>>
  29. _CmpUnspecifiedParam(_Tp) = delete;
  30. };
  31. class partial_ordering {
  32. using _ValueT = signed char;
  33. _LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
  34. _LIBCPP_HIDE_FROM_ABI explicit constexpr partial_ordering(_NCmpResult __v) noexcept : __value_(_ValueT(__v)) {}
  35. _LIBCPP_HIDE_FROM_ABI constexpr bool __is_ordered() const noexcept {
  36. return __value_ != _ValueT(_NCmpResult::__unordered);
  37. }
  38. public:
  39. // valid values
  40. static const partial_ordering less;
  41. static const partial_ordering equivalent;
  42. static const partial_ordering greater;
  43. static const partial_ordering unordered;
  44. // comparisons
  45. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default;
  46. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  47. return __v.__is_ordered() && __v.__value_ == 0;
  48. }
  49. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  50. return __v.__is_ordered() && __v.__value_ < 0;
  51. }
  52. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  53. return __v.__is_ordered() && __v.__value_ <= 0;
  54. }
  55. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  56. return __v.__is_ordered() && __v.__value_ > 0;
  57. }
  58. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  59. return __v.__is_ordered() && __v.__value_ >= 0;
  60. }
  61. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  62. return __v.__is_ordered() && 0 < __v.__value_;
  63. }
  64. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  65. return __v.__is_ordered() && 0 <= __v.__value_;
  66. }
  67. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  68. return __v.__is_ordered() && 0 > __v.__value_;
  69. }
  70. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  71. return __v.__is_ordered() && 0 >= __v.__value_;
  72. }
  73. _LIBCPP_HIDE_FROM_ABI friend constexpr partial_ordering
  74. operator<=>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  75. return __v;
  76. }
  77. _LIBCPP_HIDE_FROM_ABI friend constexpr partial_ordering
  78. operator<=>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  79. return __v < 0 ? partial_ordering::greater : (__v > 0 ? partial_ordering::less : __v);
  80. }
  81. private:
  82. _ValueT __value_;
  83. };
  84. inline constexpr partial_ordering partial_ordering::less(_OrdResult::__less);
  85. inline constexpr partial_ordering partial_ordering::equivalent(_OrdResult::__equiv);
  86. inline constexpr partial_ordering partial_ordering::greater(_OrdResult::__greater);
  87. inline constexpr partial_ordering partial_ordering::unordered(_NCmpResult ::__unordered);
  88. class weak_ordering {
  89. using _ValueT = signed char;
  90. _LIBCPP_HIDE_FROM_ABI explicit constexpr weak_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
  91. public:
  92. static const weak_ordering less;
  93. static const weak_ordering equivalent;
  94. static const weak_ordering greater;
  95. _LIBCPP_HIDE_FROM_ABI constexpr operator partial_ordering() const noexcept {
  96. return __value_ == 0 ? partial_ordering::equivalent
  97. : (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);
  98. }
  99. // comparisons
  100. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(weak_ordering, weak_ordering) noexcept = default;
  101. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  102. return __v.__value_ == 0;
  103. }
  104. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  105. return __v.__value_ < 0;
  106. }
  107. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  108. return __v.__value_ <= 0;
  109. }
  110. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  111. return __v.__value_ > 0;
  112. }
  113. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  114. return __v.__value_ >= 0;
  115. }
  116. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  117. return 0 < __v.__value_;
  118. }
  119. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  120. return 0 <= __v.__value_;
  121. }
  122. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  123. return 0 > __v.__value_;
  124. }
  125. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  126. return 0 >= __v.__value_;
  127. }
  128. _LIBCPP_HIDE_FROM_ABI friend constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  129. return __v;
  130. }
  131. _LIBCPP_HIDE_FROM_ABI friend constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  132. return __v < 0 ? weak_ordering::greater : (__v > 0 ? weak_ordering::less : __v);
  133. }
  134. private:
  135. _ValueT __value_;
  136. };
  137. inline constexpr weak_ordering weak_ordering::less(_OrdResult::__less);
  138. inline constexpr weak_ordering weak_ordering::equivalent(_OrdResult::__equiv);
  139. inline constexpr weak_ordering weak_ordering::greater(_OrdResult::__greater);
  140. class strong_ordering {
  141. using _ValueT = signed char;
  142. _LIBCPP_HIDE_FROM_ABI explicit constexpr strong_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
  143. public:
  144. static const strong_ordering less;
  145. static const strong_ordering equal;
  146. static const strong_ordering equivalent;
  147. static const strong_ordering greater;
  148. // conversions
  149. _LIBCPP_HIDE_FROM_ABI constexpr operator partial_ordering() const noexcept {
  150. return __value_ == 0 ? partial_ordering::equivalent
  151. : (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);
  152. }
  153. _LIBCPP_HIDE_FROM_ABI constexpr operator weak_ordering() const noexcept {
  154. return __value_ == 0 ? weak_ordering::equivalent : (__value_ < 0 ? weak_ordering::less : weak_ordering::greater);
  155. }
  156. // comparisons
  157. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(strong_ordering, strong_ordering) noexcept = default;
  158. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  159. return __v.__value_ == 0;
  160. }
  161. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  162. return __v.__value_ < 0;
  163. }
  164. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  165. return __v.__value_ <= 0;
  166. }
  167. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  168. return __v.__value_ > 0;
  169. }
  170. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  171. return __v.__value_ >= 0;
  172. }
  173. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  174. return 0 < __v.__value_;
  175. }
  176. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  177. return 0 <= __v.__value_;
  178. }
  179. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  180. return 0 > __v.__value_;
  181. }
  182. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  183. return 0 >= __v.__value_;
  184. }
  185. _LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering
  186. operator<=>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  187. return __v;
  188. }
  189. _LIBCPP_HIDE_FROM_ABI friend constexpr strong_ordering
  190. operator<=>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  191. return __v < 0 ? strong_ordering::greater : (__v > 0 ? strong_ordering::less : __v);
  192. }
  193. private:
  194. _ValueT __value_;
  195. };
  196. inline constexpr strong_ordering strong_ordering::less(_OrdResult::__less);
  197. inline constexpr strong_ordering strong_ordering::equal(_OrdResult::__equiv);
  198. inline constexpr strong_ordering strong_ordering::equivalent(_OrdResult::__equiv);
  199. inline constexpr strong_ordering strong_ordering::greater(_OrdResult::__greater);
  200. /// [cmp.categories.pre]/1
  201. /// The types partial_ordering, weak_ordering, and strong_ordering are
  202. /// collectively termed the comparison category types.
  203. template <class _Tp>
  204. concept __comparison_category = __one_of_v<_Tp, partial_ordering, weak_ordering, strong_ordering>;
  205. #endif // _LIBCPP_STD_VER >= 20
  206. _LIBCPP_END_NAMESPACE_STD
  207. #endif // _LIBCPP___COMPARE_ORDERING_H