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; 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>) NODISCARD friend auto operator<=>(const TArray& LHS, const TArray& RHS) requires (CSynthThreeWayComparable<ElementType>)
{ {
using OrderingType = TSynthThreeWayResult<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 LHSIter = LHS.Begin();
ConstIterator RHSIter = RHS.Begin(); ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End()) while (LHSIter != LHS.End() || RHSIter != RHS.End())
{ {
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter); TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter);
@ -325,9 +322,7 @@ public:
++RHSIter; ++RHSIter;
} }
check(RHSIter == RHS.End()); return LHS.Num() <=> RHS.Num();
return OrderingType::equivalent;
} }
/** Inserts 'InValue' before 'Iter' in the container. */ /** Inserts 'InValue' before 'Iter' in the container. */

View File

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