fix(containers): fix operator== and operator<=> of TArray etc

This commit is contained in:
_Redstone_c_ 2023-04-01 19:28:03 +08:00
parent 389a72444b
commit f644372642
3 changed files with 16 additions and 69 deletions

View File

@ -288,38 +288,22 @@ public:
{ {
if (LHS.Num() != RHS.Num()) return false; if (LHS.Num() != RHS.Num()) return false;
ConstIterator LHSIter = LHS.Begin(); for (size_t Index = 0; Index < LHS.Num(); ++Index)
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
{ {
if (*LHSIter != *RHSIter) return false; if (LHS[Index] != RHS[Index]) return false;
++LHSIter;
++RHSIter;
} }
check(RHSIter == RHS.End());
return true; return true;
} }
/** Compares the contents of 'LHS' and 'RHS' lexicographically. */ /** 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>; const size_t NumToCompare = LHS.Num() < RHS.Num() ? LHS.Num() : RHS.Num();
ConstIterator LHSIter = LHS.Begin(); for (size_t Index = 0; Index < NumToCompare; ++Index)
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End() || RHSIter != RHS.End())
{ {
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter); if (const auto Result = SynthThreeWayCompare(LHS[Index], RHS[Index]); Result != 0) return Result;
if (Ordering != OrderingType::equivalent) return Ordering;
++LHSIter;
++RHSIter;
} }
return LHS.Num() <=> RHS.Num(); return LHS.Num() <=> RHS.Num();

View File

@ -134,46 +134,25 @@ public:
{ {
if (LHS.Num() != RHS.Num()) return false; if (LHS.Num() != RHS.Num()) return false;
Iterator LHSIter = LHS.Begin(); for (size_t Index = 0; Index < LHS.Num(); ++Index)
Iterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
{ {
if (*LHSIter != *RHSIter) return false; if (LHS[Index] != RHS[Index]) return false;
++LHSIter;
++RHSIter;
} }
check(RHSIter == RHS.End());
return true; return true;
} }
/** Compares the contents of two array views. */ /** Compares the contents of two array views. */
NODISCARD friend constexpr auto operator<=>(TArrayView LHS, TArrayView RHS) requires (CSynthThreeWayComparable<ElementType>) NODISCARD friend constexpr auto operator<=>(TArrayView LHS, TArrayView RHS) requires (CSynthThreeWayComparable<ElementType>)
{ {
using OrderingType = TSynthThreeWayResult<ElementType>; const size_t NumToCompare = LHS.Num() < RHS.Num() ? LHS.Num() : RHS.Num();
if (LHS.Num() < RHS.Num()) return OrderingType::less; for (size_t Index = 0; Index < NumToCompare; ++Index)
if (LHS.Num() > RHS.Num()) return OrderingType::greater;
Iterator LHSIter = LHS.Begin();
Iterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
{ {
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter); if (const auto Result = SynthThreeWayCompare(LHS[Index], RHS[Index]); Result != 0) return Result;
if (Ordering != OrderingType::equivalent) return Ordering;
++LHSIter;
++RHSIter;
} }
check(RHSIter == RHS.End()); return LHS.Num() <=> RHS.Num();
return OrderingType::equivalent;
} }
/** Obtains an array view that is a view over the first 'Count' elements of this array view. */ /** Obtains an array view that is a view over the first 'Count' elements of this array view. */

View File

@ -46,38 +46,22 @@ public:
{ {
if (LHS.Num() != RHS.Num()) return false; if (LHS.Num() != RHS.Num()) return false;
ConstIterator LHSIter = LHS.Begin(); for (size_t Index = 0; Index < LHS.Num(); ++Index)
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End())
{ {
if (*LHSIter != *RHSIter) return false; if (LHS[Index] != RHS[Index]) return false;
++LHSIter;
++RHSIter;
} }
check(RHSIter == RHS.End());
return true; return true;
} }
/** Compares the contents of 'LHS' and 'RHS' lexicographically. */ /** 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>; const size_t NumToCompare = LHS.Num() < RHS.Num() ? LHS.Num() : RHS.Num();
ConstIterator LHSIter = LHS.Begin(); for (size_t Index = 0; Index < NumToCompare; ++Index)
ConstIterator RHSIter = RHS.Begin();
while (LHSIter != LHS.End() || RHSIter != RHS.End())
{ {
TSynthThreeWayResult<ElementType> Ordering = SynthThreeWayCompare(*LHSIter, *RHSIter); if (const auto Result = SynthThreeWayCompare(LHS[Index], RHS[Index]); Result != 0) return Result;
if (Ordering != OrderingType::equivalent) return Ordering;
++LHSIter;
++RHSIter;
} }
return LHS.Num() <=> RHS.Num(); return LHS.Num() <=> RHS.Num();