compare 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_COMPARE
  10. #define _LIBCPP_COMPARE
  11. /*
  12. compare synopsis
  13. namespace std {
  14. // [cmp.categories], comparison category types
  15. class partial_ordering;
  16. class weak_ordering;
  17. class strong_ordering;
  18. // named comparison functions
  19. constexpr bool is_eq (partial_ordering cmp) noexcept { return cmp == 0; }
  20. constexpr bool is_neq (partial_ordering cmp) noexcept { return cmp != 0; }
  21. constexpr bool is_lt (partial_ordering cmp) noexcept { return cmp < 0; }
  22. constexpr bool is_lteq(partial_ordering cmp) noexcept { return cmp <= 0; }
  23. constexpr bool is_gt (partial_ordering cmp) noexcept { return cmp > 0; }
  24. constexpr bool is_gteq(partial_ordering cmp) noexcept { return cmp >= 0; }
  25. // [cmp.common], common comparison category type
  26. template<class... Ts>
  27. struct common_comparison_category {
  28. using type = see below;
  29. };
  30. template<class... Ts>
  31. using common_comparison_category_t = typename common_comparison_category<Ts...>::type;
  32. // [cmp.concept], concept three_way_comparable
  33. template<class T, class Cat = partial_ordering>
  34. concept three_way_comparable = see below;
  35. template<class T, class U, class Cat = partial_ordering>
  36. concept three_way_comparable_with = see below;
  37. // [cmp.result], result of three-way comparison
  38. template<class T, class U = T> struct compare_three_way_result;
  39. template<class T, class U = T>
  40. using compare_three_way_result_t = typename compare_three_way_result<T, U>::type;
  41. // [comparisons.three.way], class compare_three_way
  42. struct compare_three_way; // C++20
  43. // [cmp.alg], comparison algorithms
  44. inline namespace unspecified {
  45. inline constexpr unspecified strong_order = unspecified;
  46. inline constexpr unspecified weak_order = unspecified;
  47. inline constexpr unspecified partial_order = unspecified;
  48. inline constexpr unspecified compare_strong_order_fallback = unspecified;
  49. inline constexpr unspecified compare_weak_order_fallback = unspecified;
  50. inline constexpr unspecified compare_partial_order_fallback = unspecified;
  51. }
  52. // [cmp.partialord], Class partial_ordering
  53. class partial_ordering {
  54. public:
  55. // valid values
  56. static const partial_ordering less;
  57. static const partial_ordering equivalent;
  58. static const partial_ordering greater;
  59. static const partial_ordering unordered;
  60. // comparisons
  61. friend constexpr bool operator==(partial_ordering v, unspecified) noexcept;
  62. friend constexpr bool operator==(partial_ordering v, partial_ordering w) noexcept = default;
  63. friend constexpr bool operator< (partial_ordering v, unspecified) noexcept;
  64. friend constexpr bool operator> (partial_ordering v, unspecified) noexcept;
  65. friend constexpr bool operator<=(partial_ordering v, unspecified) noexcept;
  66. friend constexpr bool operator>=(partial_ordering v, unspecified) noexcept;
  67. friend constexpr bool operator< (unspecified, partial_ordering v) noexcept;
  68. friend constexpr bool operator> (unspecified, partial_ordering v) noexcept;
  69. friend constexpr bool operator<=(unspecified, partial_ordering v) noexcept;
  70. friend constexpr bool operator>=(unspecified, partial_ordering v) noexcept;
  71. friend constexpr partial_ordering operator<=>(partial_ordering v, unspecified) noexcept;
  72. friend constexpr partial_ordering operator<=>(unspecified, partial_ordering v) noexcept;
  73. };
  74. // [cmp.weakord], Class weak_ordering
  75. class weak_ordering {
  76. public:
  77. // valid values
  78. static const weak_ordering less;
  79. static const weak_ordering equivalent;
  80. static const weak_ordering greater;
  81. // conversions
  82. constexpr operator partial_ordering() const noexcept;
  83. // comparisons
  84. friend constexpr bool operator==(weak_ordering v, unspecified) noexcept;
  85. friend constexpr bool operator==(weak_ordering v, weak_ordering w) noexcept = default;
  86. friend constexpr bool operator< (weak_ordering v, unspecified) noexcept;
  87. friend constexpr bool operator> (weak_ordering v, unspecified) noexcept;
  88. friend constexpr bool operator<=(weak_ordering v, unspecified) noexcept;
  89. friend constexpr bool operator>=(weak_ordering v, unspecified) noexcept;
  90. friend constexpr bool operator< (unspecified, weak_ordering v) noexcept;
  91. friend constexpr bool operator> (unspecified, weak_ordering v) noexcept;
  92. friend constexpr bool operator<=(unspecified, weak_ordering v) noexcept;
  93. friend constexpr bool operator>=(unspecified, weak_ordering v) noexcept;
  94. friend constexpr weak_ordering operator<=>(weak_ordering v, unspecified) noexcept;
  95. friend constexpr weak_ordering operator<=>(unspecified, weak_ordering v) noexcept;
  96. };
  97. // [cmp.strongord], Class strong_ordering
  98. class strong_ordering {
  99. public:
  100. // valid values
  101. static const strong_ordering less;
  102. static const strong_ordering equal;
  103. static const strong_ordering equivalent;
  104. static const strong_ordering greater;
  105. // conversions
  106. constexpr operator partial_ordering() const noexcept;
  107. constexpr operator weak_ordering() const noexcept;
  108. // comparisons
  109. friend constexpr bool operator==(strong_ordering v, unspecified) noexcept;
  110. friend constexpr bool operator==(strong_ordering v, strong_ordering w) noexcept = default;
  111. friend constexpr bool operator< (strong_ordering v, unspecified) noexcept;
  112. friend constexpr bool operator> (strong_ordering v, unspecified) noexcept;
  113. friend constexpr bool operator<=(strong_ordering v, unspecified) noexcept;
  114. friend constexpr bool operator>=(strong_ordering v, unspecified) noexcept;
  115. friend constexpr bool operator< (unspecified, strong_ordering v) noexcept;
  116. friend constexpr bool operator> (unspecified, strong_ordering v) noexcept;
  117. friend constexpr bool operator<=(unspecified, strong_ordering v) noexcept;
  118. friend constexpr bool operator>=(unspecified, strong_ordering v) noexcept;
  119. friend constexpr strong_ordering operator<=>(strong_ordering v, unspecified) noexcept;
  120. friend constexpr strong_ordering operator<=>(unspecified, strong_ordering v) noexcept;
  121. };
  122. }
  123. */
  124. #include <__assert> // all public C++ headers provide the assertion handler
  125. #include <__compare/common_comparison_category.h>
  126. #include <__compare/compare_partial_order_fallback.h>
  127. #include <__compare/compare_strong_order_fallback.h>
  128. #include <__compare/compare_three_way.h>
  129. #include <__compare/compare_three_way_result.h>
  130. #include <__compare/compare_weak_order_fallback.h>
  131. #include <__compare/is_eq.h>
  132. #include <__compare/ordering.h>
  133. #include <__compare/partial_order.h>
  134. #include <__compare/strong_order.h>
  135. #include <__compare/three_way_comparable.h>
  136. #include <__compare/weak_order.h>
  137. #include <__config>
  138. #include <version>
  139. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  140. # pragma GCC system_header
  141. #endif
  142. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  143. # include <type_traits>
  144. #endif
  145. #endif // _LIBCPP_COMPARE