ordering.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 {
  20. __less = -1,
  21. __equiv = 0,
  22. __greater = 1
  23. };
  24. enum class _NCmpResult : signed char {
  25. __unordered = -127
  26. };
  27. class partial_ordering;
  28. class weak_ordering;
  29. class strong_ordering;
  30. template<class _Tp, class... _Args>
  31. inline constexpr bool __one_of_v = (is_same_v<_Tp, _Args> || ...);
  32. struct _CmpUnspecifiedParam {
  33. _LIBCPP_HIDE_FROM_ABI constexpr
  34. _CmpUnspecifiedParam(int _CmpUnspecifiedParam::*) noexcept {}
  35. template<class _Tp, class = enable_if_t<!__one_of_v<_Tp, int, partial_ordering, weak_ordering, strong_ordering>>>
  36. _CmpUnspecifiedParam(_Tp) = delete;
  37. };
  38. class partial_ordering {
  39. using _ValueT = signed char;
  40. _LIBCPP_HIDE_FROM_ABI
  41. explicit constexpr partial_ordering(_OrdResult __v) noexcept
  42. : __value_(_ValueT(__v)) {}
  43. _LIBCPP_HIDE_FROM_ABI
  44. explicit constexpr partial_ordering(_NCmpResult __v) noexcept
  45. : __value_(_ValueT(__v)) {}
  46. _LIBCPP_HIDE_FROM_ABI
  47. constexpr bool __is_ordered() const noexcept {
  48. return __value_ != _ValueT(_NCmpResult::__unordered);
  49. }
  50. public:
  51. // valid values
  52. static const partial_ordering less;
  53. static const partial_ordering equivalent;
  54. static const partial_ordering greater;
  55. static const partial_ordering unordered;
  56. // comparisons
  57. _LIBCPP_HIDE_FROM_ABI
  58. friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default;
  59. _LIBCPP_HIDE_FROM_ABI
  60. friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  61. return __v.__is_ordered() && __v.__value_ == 0;
  62. }
  63. _LIBCPP_HIDE_FROM_ABI
  64. friend constexpr bool operator< (partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  65. return __v.__is_ordered() && __v.__value_ < 0;
  66. }
  67. _LIBCPP_HIDE_FROM_ABI
  68. friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  69. return __v.__is_ordered() && __v.__value_ <= 0;
  70. }
  71. _LIBCPP_HIDE_FROM_ABI
  72. friend constexpr bool operator> (partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  73. return __v.__is_ordered() && __v.__value_ > 0;
  74. }
  75. _LIBCPP_HIDE_FROM_ABI
  76. friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  77. return __v.__is_ordered() && __v.__value_ >= 0;
  78. }
  79. _LIBCPP_HIDE_FROM_ABI
  80. friend constexpr bool operator< (_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  81. return __v.__is_ordered() && 0 < __v.__value_;
  82. }
  83. _LIBCPP_HIDE_FROM_ABI
  84. friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  85. return __v.__is_ordered() && 0 <= __v.__value_;
  86. }
  87. _LIBCPP_HIDE_FROM_ABI
  88. friend constexpr bool operator> (_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  89. return __v.__is_ordered() && 0 > __v.__value_;
  90. }
  91. _LIBCPP_HIDE_FROM_ABI
  92. friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  93. return __v.__is_ordered() && 0 >= __v.__value_;
  94. }
  95. _LIBCPP_HIDE_FROM_ABI
  96. friend constexpr partial_ordering operator<=>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
  97. return __v;
  98. }
  99. _LIBCPP_HIDE_FROM_ABI
  100. friend constexpr partial_ordering operator<=>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
  101. return __v < 0 ? partial_ordering::greater : (__v > 0 ? partial_ordering::less : __v);
  102. }
  103. private:
  104. _ValueT __value_;
  105. };
  106. inline constexpr partial_ordering partial_ordering::less(_OrdResult::__less);
  107. inline constexpr partial_ordering partial_ordering::equivalent(_OrdResult::__equiv);
  108. inline constexpr partial_ordering partial_ordering::greater(_OrdResult::__greater);
  109. inline constexpr partial_ordering partial_ordering::unordered(_NCmpResult ::__unordered);
  110. class weak_ordering {
  111. using _ValueT = signed char;
  112. _LIBCPP_HIDE_FROM_ABI
  113. explicit constexpr weak_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
  114. public:
  115. static const weak_ordering less;
  116. static const weak_ordering equivalent;
  117. static const weak_ordering greater;
  118. _LIBCPP_HIDE_FROM_ABI
  119. constexpr operator partial_ordering() const noexcept {
  120. return __value_ == 0 ? partial_ordering::equivalent
  121. : (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);
  122. }
  123. // comparisons
  124. _LIBCPP_HIDE_FROM_ABI
  125. friend constexpr bool operator==(weak_ordering, weak_ordering) noexcept = default;
  126. _LIBCPP_HIDE_FROM_ABI
  127. friend constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  128. return __v.__value_ == 0;
  129. }
  130. _LIBCPP_HIDE_FROM_ABI
  131. friend constexpr bool operator< (weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  132. return __v.__value_ < 0;
  133. }
  134. _LIBCPP_HIDE_FROM_ABI
  135. friend constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  136. return __v.__value_ <= 0;
  137. }
  138. _LIBCPP_HIDE_FROM_ABI
  139. friend constexpr bool operator> (weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  140. return __v.__value_ > 0;
  141. }
  142. _LIBCPP_HIDE_FROM_ABI
  143. friend constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  144. return __v.__value_ >= 0;
  145. }
  146. _LIBCPP_HIDE_FROM_ABI
  147. friend constexpr bool operator< (_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  148. return 0 < __v.__value_;
  149. }
  150. _LIBCPP_HIDE_FROM_ABI
  151. friend constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  152. return 0 <= __v.__value_;
  153. }
  154. _LIBCPP_HIDE_FROM_ABI
  155. friend constexpr bool operator> (_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  156. return 0 > __v.__value_;
  157. }
  158. _LIBCPP_HIDE_FROM_ABI
  159. friend constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  160. return 0 >= __v.__value_;
  161. }
  162. _LIBCPP_HIDE_FROM_ABI
  163. friend constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
  164. return __v;
  165. }
  166. _LIBCPP_HIDE_FROM_ABI
  167. friend constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
  168. return __v < 0 ? weak_ordering::greater : (__v > 0 ? weak_ordering::less : __v);
  169. }
  170. private:
  171. _ValueT __value_;
  172. };
  173. inline constexpr weak_ordering weak_ordering::less(_OrdResult::__less);
  174. inline constexpr weak_ordering weak_ordering::equivalent(_OrdResult::__equiv);
  175. inline constexpr weak_ordering weak_ordering::greater(_OrdResult::__greater);
  176. class strong_ordering {
  177. using _ValueT = signed char;
  178. _LIBCPP_HIDE_FROM_ABI
  179. explicit constexpr strong_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
  180. public:
  181. static const strong_ordering less;
  182. static const strong_ordering equal;
  183. static const strong_ordering equivalent;
  184. static const strong_ordering greater;
  185. // conversions
  186. _LIBCPP_HIDE_FROM_ABI
  187. constexpr operator partial_ordering() const noexcept {
  188. return __value_ == 0 ? partial_ordering::equivalent
  189. : (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);
  190. }
  191. _LIBCPP_HIDE_FROM_ABI
  192. constexpr operator weak_ordering() const noexcept {
  193. return __value_ == 0 ? weak_ordering::equivalent
  194. : (__value_ < 0 ? weak_ordering::less : weak_ordering::greater);
  195. }
  196. // comparisons
  197. _LIBCPP_HIDE_FROM_ABI
  198. friend constexpr bool operator==(strong_ordering, strong_ordering) noexcept = default;
  199. _LIBCPP_HIDE_FROM_ABI
  200. friend constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  201. return __v.__value_ == 0;
  202. }
  203. _LIBCPP_HIDE_FROM_ABI
  204. friend constexpr bool operator< (strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  205. return __v.__value_ < 0;
  206. }
  207. _LIBCPP_HIDE_FROM_ABI
  208. friend constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  209. return __v.__value_ <= 0;
  210. }
  211. _LIBCPP_HIDE_FROM_ABI
  212. friend constexpr bool operator> (strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  213. return __v.__value_ > 0;
  214. }
  215. _LIBCPP_HIDE_FROM_ABI
  216. friend constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  217. return __v.__value_ >= 0;
  218. }
  219. _LIBCPP_HIDE_FROM_ABI
  220. friend constexpr bool operator< (_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  221. return 0 < __v.__value_;
  222. }
  223. _LIBCPP_HIDE_FROM_ABI
  224. friend constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  225. return 0 <= __v.__value_;
  226. }
  227. _LIBCPP_HIDE_FROM_ABI
  228. friend constexpr bool operator> (_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  229. return 0 > __v.__value_;
  230. }
  231. _LIBCPP_HIDE_FROM_ABI
  232. friend constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  233. return 0 >= __v.__value_;
  234. }
  235. _LIBCPP_HIDE_FROM_ABI
  236. friend constexpr strong_ordering operator<=>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
  237. return __v;
  238. }
  239. _LIBCPP_HIDE_FROM_ABI
  240. friend constexpr strong_ordering operator<=>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
  241. return __v < 0 ? strong_ordering::greater : (__v > 0 ? strong_ordering::less : __v);
  242. }
  243. private:
  244. _ValueT __value_;
  245. };
  246. inline constexpr strong_ordering strong_ordering::less(_OrdResult::__less);
  247. inline constexpr strong_ordering strong_ordering::equal(_OrdResult::__equiv);
  248. inline constexpr strong_ordering strong_ordering::equivalent(_OrdResult::__equiv);
  249. inline constexpr strong_ordering strong_ordering::greater(_OrdResult::__greater);
  250. /// [cmp.categories.pre]/1
  251. /// The types partial_ordering, weak_ordering, and strong_ordering are
  252. /// collectively termed the comparison category types.
  253. template <class _Tp>
  254. concept __comparison_category = __one_of_v<_Tp, partial_ordering, weak_ordering, strong_ordering>;
  255. #endif // _LIBCPP_STD_VER >= 20
  256. _LIBCPP_END_NAMESPACE_STD
  257. #endif // _LIBCPP___COMPARE_ORDERING_H