refactor(miscellaneous): replace TSynthThreeWay with the function version

This commit is contained in:
_Redstone_c_ 2022-04-30 19:27:25 +08:00
parent 38902eb779
commit 7aadebba70
3 changed files with 18 additions and 77 deletions

View File

@ -205,27 +205,18 @@ void TestCompare()
always_check((TIsSame<TCompareThreeWayResult<FTestWeakOrdering >::Type, weak_ordering >::Value));
always_check((TIsSame<TCompareThreeWayResult<FTestStrongOrdering >::Type, strong_ordering >::Value));
always_check((TCompareThreeWay<int32>()(0, 0) == strong_ordering::equal));
always_check((TCompareThreeWay<void>() (0, 0.0) == strong_ordering::equal));
always_check((SynthThreeWayCompare(0, 0) == strong_ordering::equal));
always_check((SynthThreeWayCompare(0, 0.0) == strong_ordering::equal));
always_check(TSynthThreeWay{}(FTestPartialOrdering(-1), FTestPartialOrdering( 0)) == partial_ordering::less);
always_check(TSynthThreeWay{}(FTestPartialOrdering( 0), FTestPartialOrdering( 0)) == partial_ordering::equivalent);
always_check(TSynthThreeWay{}(FTestPartialOrdering( 0), FTestPartialOrdering(-1)) == partial_ordering::greater);
always_check(SynthThreeWayCompare(FTestPartialOrdering(-1), FTestPartialOrdering( 0)) == partial_ordering::less);
always_check(SynthThreeWayCompare(FTestPartialOrdering( 0), FTestPartialOrdering( 0)) == partial_ordering::equivalent);
always_check(SynthThreeWayCompare(FTestPartialOrdering( 0), FTestPartialOrdering(-1)) == partial_ordering::greater);
always_check(TSynthThreeWay{}(FTestPartialOrdering( 0, true), FTestPartialOrdering( 0, false)) == partial_ordering::unordered);
always_check(SynthThreeWayCompare(FTestPartialOrdering( 0, true), FTestPartialOrdering( 0, false)) == partial_ordering::unordered);
always_check(TSynthThreeWay{}(FTestSynth(-1), FTestSynth( 0)) == weak_ordering::less);
always_check(TSynthThreeWay{}(FTestSynth( 0), FTestSynth( 0)) == weak_ordering::equivalent);
always_check(TSynthThreeWay{}(FTestSynth( 0), FTestSynth(-1)) == weak_ordering::greater);
always_check((StrongOrder(0, 0) == strong_ordering::equal));
always_check((WeakOrder(0, 0) == strong_ordering::equal));
always_check((PartialOrder(0, 0) == strong_ordering::equal));
always_check((CompareStrongOrderFallback(0, 0) == strong_ordering::equal));
always_check((CompareWeakOrderFallback(0, 0) == strong_ordering::equal));
always_check((ComparePartialOrderFallback(0, 0) == strong_ordering::equal));
always_check(SynthThreeWayCompare(FTestSynth(-1), FTestSynth( 0)) == weak_ordering::less);
always_check(SynthThreeWayCompare(FTestSynth( 0), FTestSynth( 0)) == weak_ordering::equivalent);
always_check(SynthThreeWayCompare(FTestSynth( 0), FTestSynth(-1)) == weak_ordering::greater);
}
NAMESPACE_UNNAMED_BEGIN

View File

@ -68,25 +68,6 @@ struct TCompareThreeWayResult<T, U>
using Type = decltype(DeclVal<const typename TRemoveReference<T>::Type&>() <=> DeclVal<const typename TRemoveReference<U>::Type&>());
};
template <typename T = void> requires (CSameAs<T, void> || CThreeWayComparable<T>)
struct TCompareThreeWay
{
constexpr auto operator()(T&& LHS, T&& RHS) const
{
return Forward<T>(LHS) <=> Forward<T>(RHS);
}
};
template <>
struct TCompareThreeWay<void>
{
template <typename T, typename U> requires CThreeWayComparableWith<T, U>
constexpr auto operator()(T&& LHS, U&& RHS) const
{
return Forward<T>(LHS) <=> Forward<U>(RHS);
}
};
template <typename T, typename OrderingType = partial_ordering>
concept CSynthThreeWayComparable = CThreeWayComparable<T> ||
requires(const TRemoveReference<T>::Type& A, const TRemoveReference<T>::Type& B)
@ -103,56 +84,25 @@ concept CSynthThreeWayComparableWith = CThreeWayComparableWith<T, U> ||
{ B < A } -> CBooleanTestable;
};
template <typename T = void> requires (CSameAs<T, void> || CSynthThreeWayComparable<T>)
struct TSynthThreeWay
template <typename T, typename U> requires CSynthThreeWayComparableWith<T, U>
constexpr decltype(auto) SynthThreeWayCompare(T&& LHS, U&& RHS)
{
constexpr auto operator()(T&& LHS, T&& RHS) const
if constexpr (CThreeWayComparableWith<T, U>)
{
if constexpr (CThreeWayComparable<T>)
{
return Forward<T>(LHS) <=> Forward<T>(RHS);
}
else
{
return Forward<T>(LHS) < Forward<T>(RHS) ? weak_ordering::less : Forward<T>(RHS) < Forward<T>(LHS) ? weak_ordering::greater : weak_ordering::equivalent;
}
return Forward<T>(LHS) <=> Forward<U>(RHS);
}
};
template <>
struct TSynthThreeWay<void>
{
template <typename T, typename U> requires CSynthThreeWayComparableWith<T, U>
constexpr auto operator()(T&& LHS, U&& RHS) const
else
{
if constexpr (CThreeWayComparableWith<T, U>)
{
return Forward<T>(LHS) <=> Forward<U>(RHS);
}
else
{
return Forward<T>(LHS) < Forward<U>(RHS) ? weak_ordering::less : Forward<U>(RHS) < Forward<T>(LHS) ? weak_ordering::greater : weak_ordering::equivalent;
}
return Forward<T>(LHS) < Forward<U>(RHS) ? weak_ordering::less : Forward<U>(RHS) < Forward<T>(LHS) ? weak_ordering::greater : weak_ordering::equivalent;
}
};
}
template <typename T, typename U = T>
struct TSynthThreeWayResult
{
using Type = decltype(TSynthThreeWay{}(DeclVal<const typename TRemoveReference<T>::Type&>(), DeclVal<const typename TRemoveReference<U>::Type&>()));
using Type = decltype(SynthThreeWayCompare(DeclVal<const typename TRemoveReference<T>::Type&>(), DeclVal<const typename TRemoveReference<U>::Type&>()));
};
NAMESPACE_UNNAMED_BEGIN
inline constexpr decltype(NAMESPACE_STD::strong_order) StrongOrder;
inline constexpr decltype(NAMESPACE_STD::weak_order) WeakOrder;
inline constexpr decltype(NAMESPACE_STD::partial_order) PartialOrder;
inline constexpr decltype(NAMESPACE_STD::compare_strong_order_fallback) CompareStrongOrderFallback;
inline constexpr decltype(NAMESPACE_STD::compare_weak_order_fallback) CompareWeakOrderFallback;
inline constexpr decltype(NAMESPACE_STD::compare_partial_order_fallback) ComparePartialOrderFallback;
NAMESPACE_UNNAMED_END
NAMESPACE_MODULE_END(Utility)
NAMESPACE_MODULE_END(Redcraft)
NAMESPACE_REDCRAFT_END

View File

@ -501,7 +501,7 @@ struct TTupleThreeWay<R, TIndexSequence<I, Indices...>>
template <typename LHSTupleType, typename RHSTupleType>
static constexpr R F(const LHSTupleType& LHS, const RHSTupleType& RHS)
{
auto Result = TSynthThreeWay{}(LHS.template GetValue<I>(), RHS.template GetValue<I>());
auto Result = SynthThreeWayCompare(LHS.template GetValue<I>(), RHS.template GetValue<I>());
if (Result != 0) return Result;
return TTupleThreeWay<R, TIndexSequence<Indices...>>::F(LHS, RHS);
}