From bd10becb6540543c94f902ff1fe1dff8f35dd9eb Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 8 Apr 2023 16:20:50 +0200 Subject: [PATCH] Use lexicographical comparison in ESM3ExteriorCellRefId::operator< --- apps/openmw_test_suite/esm/testrefid.cpp | 8 ++++++++ components/esm/esm3exteriorcellrefid.hpp | 9 +++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/apps/openmw_test_suite/esm/testrefid.cpp b/apps/openmw_test_suite/esm/testrefid.cpp index 19aa0d5d13..65fe27605c 100644 --- a/apps/openmw_test_suite/esm/testrefid.cpp +++ b/apps/openmw_test_suite/esm/testrefid.cpp @@ -212,6 +212,14 @@ namespace ESM EXPECT_NE(hash(a), hash(b)); } + TEST(ESMRefIdTest, esm3ExteriorCellHasLexicographicalOrder) + { + const RefId a = RefId::esm3ExteriorCell(0, 0); + const RefId b = RefId::esm3ExteriorCell(1, 0); + EXPECT_LT(a, b); + EXPECT_TRUE(!(b < a)); + } + struct ESMRefIdToStringTest : TestWithParam> { }; diff --git a/components/esm/esm3exteriorcellrefid.hpp b/components/esm/esm3exteriorcellrefid.hpp index 9bb9bf17d8..5fca8dc597 100644 --- a/components/esm/esm3exteriorcellrefid.hpp +++ b/components/esm/esm3exteriorcellrefid.hpp @@ -28,9 +28,14 @@ namespace ESM int32_t getX() const { return mX; } int32_t getY() const { return mY; } - constexpr bool operator==(ESM3ExteriorCellRefId rhs) const noexcept { return mX == rhs.mX && mY == rhs.mY; } + friend inline constexpr auto tie(const ESM3ExteriorCellRefId& value) noexcept + { + return std::tie(value.mX, value.mY); + } + + constexpr bool operator==(ESM3ExteriorCellRefId rhs) const noexcept { return tie(*this) == tie(rhs); } - constexpr bool operator<(ESM3ExteriorCellRefId rhs) const noexcept { return mX < rhs.mX && mY < rhs.mY; } + constexpr bool operator<(ESM3ExteriorCellRefId rhs) const noexcept { return tie(*this) < tie(rhs); } friend std::ostream& operator<<(std::ostream& stream, ESM3ExteriorCellRefId value);