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
9#ifndef _LIBCPP___COMPARE_ORDERING_H
10#define _LIBCPP___COMPARE_ORDERING_H
11
12#include <__config>
13#include <__type_traits/enable_if.h>
14#include <__type_traits/is_same.h>
15
16#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17# pragma GCC system_header
18#endif
19
20#if _LIBCPP_STD_VER >= 20
21
22_LIBCPP_BEGIN_NAMESPACE_STD
23
24// exposition only
25enum class _OrdResult : signed char { __less = -1, __equiv = 0, __greater = 1 };
26
27enum class _PartialOrdResult : signed char {
28 __less = static_cast<signed char>(_OrdResult::__less),
29 __equiv = static_cast<signed char>(_OrdResult::__equiv),
30 __greater = static_cast<signed char>(_OrdResult::__greater),
31 __unordered = -127,
32};
33
34class partial_ordering;
35class weak_ordering;
36class strong_ordering;
37
38struct _CmpUnspecifiedParam {
39 // If anything other than a literal 0 is provided, the behavior is undefined by the Standard.
40 //
41 // The alternative to the `__enable_if__` attribute would be to use the fact that a pointer
42 // can be constructed from literal 0, but this conflicts with `-Wzero-as-null-pointer-constant`.
43 template <class _Tp, class = __enable_if_t<is_same_v<_Tp, int> > >
44 consteval _CmpUnspecifiedParam(_Tp __zero) noexcept
45# if __has_attribute(__enable_if__)
46 __attribute__((__enable_if__(
47 __zero == 0, "Only literal 0 is allowed as the operand of a comparison with one of the ordering types")))
48# endif
49 {
50 (void)__zero;
51 }
52};
53
54class partial_ordering {
55 explicit constexpr partial_ordering(_PartialOrdResult __v) noexcept : __value_(__v) {}
56
57public:
58 // valid values
59 static const partial_ordering less;
60 static const partial_ordering equivalent;
61 static const partial_ordering greater;
62 static const partial_ordering unordered;
63
64 // comparisons
65 friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default;
66
67 friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
68 return __v.__value_ == _PartialOrdResult::__equiv;
69 }
70
71 friend constexpr bool operator<(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
72 return __v.__value_ == _PartialOrdResult::__less;
73 }
74
75 friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
76 return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__less;
77 }
78
79 friend constexpr bool operator>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
80 return __v.__value_ == _PartialOrdResult::__greater;
81 }
82
83 friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
84 return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__greater;
85 }
86
87 friend constexpr bool operator<(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
88 return __v.__value_ == _PartialOrdResult::__greater;
89 }
90
91 friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
92 return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__greater;
93 }
94
95 friend constexpr bool operator>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
96 return __v.__value_ == _PartialOrdResult::__less;
97 }
98
99 friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
100 return __v.__value_ == _PartialOrdResult::__equiv || __v.__value_ == _PartialOrdResult::__less;
101 }
102
103 friend constexpr partial_ordering operator<=>(partial_ordering __v, _CmpUnspecifiedParam) noexcept { return __v; }
104
105 friend constexpr partial_ordering operator<=>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
106 return __v < 0 ? partial_ordering::greater : (__v > 0 ? partial_ordering::less : __v);
107 }
108
109private:
110 _PartialOrdResult __value_;
111};
112
113inline constexpr partial_ordering partial_ordering::less(_PartialOrdResult::__less);
114inline constexpr partial_ordering partial_ordering::equivalent(_PartialOrdResult::__equiv);
115inline constexpr partial_ordering partial_ordering::greater(_PartialOrdResult::__greater);
116inline constexpr partial_ordering partial_ordering::unordered(_PartialOrdResult::__unordered);
117
118class weak_ordering {
119 using _ValueT _LIBCPP_NODEBUG = signed char;
120
121 explicit constexpr weak_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
122
123public:
124 static const weak_ordering less;
125 static const weak_ordering equivalent;
126 static const weak_ordering greater;
127
128 constexpr operator partial_ordering() const noexcept {
129 return __value_ == 0 ? partial_ordering::equivalent
130 : (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);
131 }
132
133 // comparisons
134 friend constexpr bool operator==(weak_ordering, weak_ordering) noexcept = default;
135
136 friend constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ == 0; }
137
138 friend constexpr bool operator<(weak_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ < 0; }
139
140 friend constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ <= 0; }
141
142 friend constexpr bool operator>(weak_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ > 0; }
143
144 friend constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ >= 0; }
145
146 friend constexpr bool operator<(_CmpUnspecifiedParam, weak_ordering __v) noexcept { return 0 < __v.__value_; }
147
148 friend constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept { return 0 <= __v.__value_; }
149
150 friend constexpr bool operator>(_CmpUnspecifiedParam, weak_ordering __v) noexcept { return 0 > __v.__value_; }
151
152 friend constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept { return 0 >= __v.__value_; }
153
154 friend constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) noexcept { return __v; }
155
156 friend constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
157 return __v < 0 ? weak_ordering::greater : (__v > 0 ? weak_ordering::less : __v);
158 }
159
160private:
161 _ValueT __value_;
162};
163
164inline constexpr weak_ordering weak_ordering::less(_OrdResult::__less);
165inline constexpr weak_ordering weak_ordering::equivalent(_OrdResult::__equiv);
166inline constexpr weak_ordering weak_ordering::greater(_OrdResult::__greater);
167
168class strong_ordering {
169 using _ValueT _LIBCPP_NODEBUG = signed char;
170
171 explicit constexpr strong_ordering(_OrdResult __v) noexcept : __value_(_ValueT(__v)) {}
172
173public:
174 static const strong_ordering less;
175 static const strong_ordering equal;
176 static const strong_ordering equivalent;
177 static const strong_ordering greater;
178
179 // conversions
180 constexpr operator partial_ordering() const noexcept {
181 return __value_ == 0 ? partial_ordering::equivalent
182 : (__value_ < 0 ? partial_ordering::less : partial_ordering::greater);
183 }
184
185 constexpr operator weak_ordering() const noexcept {
186 return __value_ == 0 ? weak_ordering::equivalent : (__value_ < 0 ? weak_ordering::less : weak_ordering::greater);
187 }
188
189 // comparisons
190 friend constexpr bool operator==(strong_ordering, strong_ordering) noexcept = default;
191
192 friend constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ == 0; }
193
194 friend constexpr bool operator<(strong_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ < 0; }
195
196 friend constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ <= 0; }
197
198 friend constexpr bool operator>(strong_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ > 0; }
199
200 friend constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept { return __v.__value_ >= 0; }
201
202 friend constexpr bool operator<(_CmpUnspecifiedParam, strong_ordering __v) noexcept { return 0 < __v.__value_; }
203
204 friend constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept { return 0 <= __v.__value_; }
205
206 friend constexpr bool operator>(_CmpUnspecifiedParam, strong_ordering __v) noexcept { return 0 > __v.__value_; }
207
208 friend constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept { return 0 >= __v.__value_; }
209
210 friend constexpr strong_ordering operator<=>(strong_ordering __v, _CmpUnspecifiedParam) noexcept { return __v; }
211
212 friend constexpr strong_ordering operator<=>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
213 return __v < 0 ? strong_ordering::greater : (__v > 0 ? strong_ordering::less : __v);
214 }
215
216private:
217 _ValueT __value_;
218};
219
220inline constexpr strong_ordering strong_ordering::less(_OrdResult::__less);
221inline constexpr strong_ordering strong_ordering::equal(_OrdResult::__equiv);
222inline constexpr strong_ordering strong_ordering::equivalent(_OrdResult::__equiv);
223inline constexpr strong_ordering strong_ordering::greater(_OrdResult::__greater);
224
225/// [cmp.categories.pre]/1
226/// The types partial_ordering, weak_ordering, and strong_ordering are
227/// collectively termed the comparison category types.
228template <class _Tp>
229concept __comparison_category =
230 is_same_v<_Tp, partial_ordering> || is_same_v<_Tp, weak_ordering> || is_same_v<_Tp, strong_ordering>;
231
232_LIBCPP_END_NAMESPACE_STD
233
234#endif // _LIBCPP_STD_VER >= 20
235
236#endif // _LIBCPP___COMPARE_ORDERING_H
237