2022-03-09 15:17:54 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "CoreTypes.h"
|
2022-03-30 04:07:48 +00:00
|
|
|
#include "Templates/Utility.h"
|
2022-03-09 15:17:54 +00:00
|
|
|
#include "TypeTraits/TypeTraits.h"
|
|
|
|
|
|
|
|
#include <compare>
|
|
|
|
|
|
|
|
NAMESPACE_REDCRAFT_BEGIN
|
|
|
|
NAMESPACE_MODULE_BEGIN(Redcraft)
|
|
|
|
NAMESPACE_MODULE_BEGIN(Utility)
|
|
|
|
|
2022-05-29 15:18:20 +00:00
|
|
|
// The result of the three-way comparison operator is the built-in type of the compiler, which is directly introduced here
|
2022-03-09 15:17:54 +00:00
|
|
|
|
|
|
|
typedef NAMESPACE_STD::partial_ordering partial_ordering;
|
|
|
|
typedef NAMESPACE_STD::weak_ordering weak_ordering;
|
|
|
|
typedef NAMESPACE_STD::strong_ordering strong_ordering;
|
|
|
|
|
|
|
|
NAMESPACE_PRIVATE_BEGIN
|
|
|
|
|
2022-06-16 15:37:29 +00:00
|
|
|
template<int32> struct TCommonComparisonCategoryBasic { };
|
|
|
|
template<> struct TCommonComparisonCategoryBasic<0> { using Type = strong_ordering; };
|
|
|
|
template<> struct TCommonComparisonCategoryBasic<2> { using Type = partial_ordering; };
|
|
|
|
template<> struct TCommonComparisonCategoryBasic<4> { using Type = weak_ordering; };
|
|
|
|
template<> struct TCommonComparisonCategoryBasic<6> { using Type = partial_ordering; };
|
2022-03-09 15:17:54 +00:00
|
|
|
|
|
|
|
template <typename... Types>
|
2022-06-16 15:37:29 +00:00
|
|
|
struct TCommonComparisonCategoryImpl
|
|
|
|
: TCommonComparisonCategoryBasic <(0u | ... |
|
2022-03-09 15:17:54 +00:00
|
|
|
(
|
2022-05-16 15:09:04 +00:00
|
|
|
CSameAs<Types, strong_ordering > ? 0u :
|
|
|
|
CSameAs<Types, weak_ordering > ? 4u :
|
|
|
|
CSameAs<Types, partial_ordering> ? 2u : 1u
|
2022-03-09 15:17:54 +00:00
|
|
|
)
|
|
|
|
)>
|
|
|
|
{ };
|
|
|
|
|
2022-06-16 15:37:29 +00:00
|
|
|
NAMESPACE_PRIVATE_END
|
|
|
|
|
|
|
|
template <typename... Types>
|
|
|
|
using TCommonComparisonCategory = typename NAMESPACE_PRIVATE::TCommonComparisonCategoryImpl<Types...>::Type;
|
|
|
|
|
2022-03-09 15:17:54 +00:00
|
|
|
template <typename T, typename OrderingType>
|
2022-06-16 15:37:29 +00:00
|
|
|
concept CThreeWayComparesAs = CSameAs<TCommonComparisonCategory<T, OrderingType>, OrderingType>;
|
2022-03-09 15:17:54 +00:00
|
|
|
|
2022-05-21 14:39:22 +00:00
|
|
|
template <typename T, typename U = T, typename OrderingType = partial_ordering>
|
|
|
|
concept CThreeWayComparable = CWeaklyEqualityComparable<T, U> && CPartiallyOrdered<T, U>
|
2022-05-22 14:52:47 +00:00
|
|
|
&& CCommonReference<const TRemoveReference<T>&, const TRemoveReference<U>&>
|
|
|
|
&& requires(const TRemoveReference<T>& A, const TRemoveReference<U>& B,
|
2022-05-29 15:18:20 +00:00
|
|
|
const TRemoveReference<TCommonReference<const TRemoveReference<T>&, const TRemoveReference<U>&>>& C)
|
2022-05-21 14:39:22 +00:00
|
|
|
{
|
|
|
|
{ A <=> A } -> CThreeWayComparesAs<OrderingType>;
|
|
|
|
{ B <=> B } -> CThreeWayComparesAs<OrderingType>;
|
|
|
|
{ A <=> B } -> CThreeWayComparesAs<OrderingType>;
|
|
|
|
{ B <=> A } -> CThreeWayComparesAs<OrderingType>;
|
|
|
|
{ C <=> C } -> CThreeWayComparesAs<OrderingType>;
|
|
|
|
};
|
2022-03-09 15:17:54 +00:00
|
|
|
|
2022-05-22 14:52:47 +00:00
|
|
|
template <typename T, typename U = T> requires CThreeWayComparable<T, U>
|
2022-06-16 15:37:29 +00:00
|
|
|
using TCompareThreeWayResult = decltype(DeclVal<const TRemoveReference<T>&>() <=> DeclVal<const TRemoveReference<U>&>());
|
2022-03-09 15:17:54 +00:00
|
|
|
|
2022-05-21 14:39:22 +00:00
|
|
|
template <typename T, typename U = T, typename OrderingType = partial_ordering>
|
|
|
|
concept CSynthThreeWayComparable = CThreeWayComparable<T, U> || CTotallyOrdered<T, U>;
|
2022-03-30 04:07:48 +00:00
|
|
|
|
2022-05-21 14:39:22 +00:00
|
|
|
template <typename T, typename U = T> requires CSynthThreeWayComparable<T, U>
|
2022-04-30 11:27:25 +00:00
|
|
|
constexpr decltype(auto) SynthThreeWayCompare(T&& LHS, U&& RHS)
|
2022-03-30 04:07:48 +00:00
|
|
|
{
|
2022-05-21 14:39:22 +00:00
|
|
|
if constexpr (CThreeWayComparable<T, U>)
|
2022-03-30 04:07:48 +00:00
|
|
|
{
|
2022-04-30 11:27:25 +00:00
|
|
|
return Forward<T>(LHS) <=> Forward<U>(RHS);
|
2022-03-30 04:07:48 +00:00
|
|
|
}
|
2022-04-30 11:27:25 +00:00
|
|
|
else
|
2022-03-30 04:07:48 +00:00
|
|
|
{
|
2022-04-30 11:27:25 +00:00
|
|
|
return Forward<T>(LHS) < Forward<U>(RHS) ? weak_ordering::less : Forward<U>(RHS) < Forward<T>(LHS) ? weak_ordering::greater : weak_ordering::equivalent;
|
2022-03-30 04:07:48 +00:00
|
|
|
}
|
2022-04-30 11:27:25 +00:00
|
|
|
}
|
2022-03-30 04:07:48 +00:00
|
|
|
|
2022-06-16 15:37:29 +00:00
|
|
|
template <typename T, typename U = T> requires CSynthThreeWayComparable<T, U>
|
|
|
|
using TSynthThreeWayResult = decltype(SynthThreeWayCompare(DeclVal<const TRemoveReference<T>&>(), DeclVal<const TRemoveReference<U>&>()));
|
2022-03-30 04:07:48 +00:00
|
|
|
|
2022-03-09 15:17:54 +00:00
|
|
|
NAMESPACE_MODULE_END(Utility)
|
|
|
|
NAMESPACE_MODULE_END(Redcraft)
|
|
|
|
NAMESPACE_REDCRAFT_END
|