ordering.h 10 KB

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