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

View File

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