From 1daf90adeec63cdd09829b0472d3be5ca8838df4 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Mon, 13 Mar 2023 21:53:47 +0800 Subject: [PATCH] fix(containers): fix operator<=> of TArray and TStaticArray to compare lexicographically --- Redcraft.Utility/Source/Public/Containers/Array.h | 11 +++-------- .../Source/Public/Containers/StaticArray.h | 11 +++-------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/Redcraft.Utility/Source/Public/Containers/Array.h b/Redcraft.Utility/Source/Public/Containers/Array.h index 26418b0..ddabc08 100644 --- a/Redcraft.Utility/Source/Public/Containers/Array.h +++ b/Redcraft.Utility/Source/Public/Containers/Array.h @@ -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) { using OrderingType = TSynthThreeWayResult; - 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 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. */ diff --git a/Redcraft.Utility/Source/Public/Containers/StaticArray.h b/Redcraft.Utility/Source/Public/Containers/StaticArray.h index 90daa86..01f0195 100644 --- a/Redcraft.Utility/Source/Public/Containers/StaticArray.h +++ b/Redcraft.Utility/Source/Public/Containers/StaticArray.h @@ -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) { using OrderingType = TSynthThreeWayResult; - 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 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. */