fix(containers): fix operator<=> of TArray and TStaticArray to compare lexicographically

This commit is contained in:
_Redstone_c_ 2023-03-13 21:53:47 +08:00
parent dd8b698bb3
commit 1daf90adee
2 changed files with 6 additions and 16 deletions

View File

@ -304,18 +304,15 @@ public:
return true;
}
/** Compares the contents of two arrays. */
/** Compares the contents of 'LHS' and 'RHS' lexicographically. */
NODISCARD friend auto operator<=>(const TArray& LHS, const TArray& RHS) requires (CSynthThreeWayComparable<ElementType>)
{
using OrderingType = TSynthThreeWayResult<ElementType>;
if (LHS.Num() < RHS.Num()) return OrderingType::less;
if (LHS.Num() > RHS.Num()) return OrderingType::greater;
ConstIterator LHSIter = LHS.Begin();
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
while (LHSIter != LHS.End() || RHSIter != RHS.End())
{
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter);
@ -325,9 +322,7 @@ public:
++RHSIter;
}
check(RHSIter == RHS.End());
return OrderingType::equivalent;
return LHS.Num() <=> RHS.Num();
}
/** Inserts 'InValue' before 'Iter' in the container. */

View File

@ -62,18 +62,15 @@ public:
return true;
}
/** Compares the contents of two arrays. */
/** Compares the contents of 'LHS' and 'RHS' lexicographically. */
NODISCARD friend constexpr auto operator<=>(const TStaticArray& LHS, const TStaticArray& RHS) requires (CSynthThreeWayComparable<ElementType>)
{
using OrderingType = TSynthThreeWayResult<ElementType>;
if (LHS.Num() < RHS.Num()) return OrderingType::less;
if (LHS.Num() > RHS.Num()) return OrderingType::greater;
ConstIterator LHSIter = LHS.Begin();
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
while (LHSIter != LHS.End() || RHSIter != RHS.End())
{
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter);
@ -83,9 +80,7 @@ public:
++RHSIter;
}
check(RHSIter == RHS.End());
return OrderingType::equivalent;
return LHS.Num() <=> RHS.Num();
}
/** @return The pointer to the underlying element storage. */