diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/global-coords.cpp | 34 | ||||
-rw-r--r-- | src/global-coords.hpp | 28 |
2 files changed, 62 insertions, 0 deletions
diff --git a/src/global-coords.cpp b/src/global-coords.cpp index 17b8d7fd..694447c1 100644 --- a/src/global-coords.cpp +++ b/src/global-coords.cpp @@ -1,4 +1,7 @@ #include "global-coords.hpp" +#include "compat/defs.hpp" +#include <array> +#include <algorithm> namespace floormat { @@ -33,6 +36,37 @@ static_assert(g1 - g2 == Vector2i(0, 1)); static_assert((g1 + Vector2i(0, -1)).chunk() == g2.chunk()); static_assert(g1 + Vector2i(0, -1) == g2); +constexpr bool test_comparison1() +{ + auto a = point{{2, 8, 2}, {0, 0}}; + auto b = point{{1, 1, 2}, {9, 1}}; + auto c = point{{1, 0, 2}, {1, 2}}; + auto d = point{{1, 9, 1}, {1, 9}}; + auto e = point{{3, 1, 2}, {0, 0}}; + auto f = point{{9, 8, 0}, {9, 9}}; + auto g = point{{1, 9, 1}, {9, 0}}; + + const auto sorted = std::array{ + f, g, d, c, b, e, a, + }; + auto array1 = std::array{ + a, b, c, d, e, f, g, + }; + auto array2 = std::array { + a, c, e, g, b, d, f, + }; + + std::sort(array1.begin(), array1.end()); + fm_assert(array1 == sorted); + + std::sort(array2.begin(), array2.end()); + fm_assert(array2 == sorted); + + return true; +} + +static_assert(test_comparison1()); + } // namespace } // namespace floormat diff --git a/src/global-coords.hpp b/src/global-coords.hpp index 4d420534..31481ba0 100644 --- a/src/global-coords.hpp +++ b/src/global-coords.hpp @@ -1,6 +1,7 @@ #pragma once #include "local-coords.hpp" #include "compat/assert.hpp" +#include <compare> #include <concepts> #include <Magnum/Magnum.h> #include <Magnum/Math/Vector2.h> @@ -115,6 +116,15 @@ public: constexpr Vector2i operator-(global_coords other) const noexcept; }; +struct point +{ + global_coords coord; + Vector2b offset; + + constexpr bool operator==(const point&) const = default; + friend constexpr std::strong_ordering operator<=>(const point& a, const point& b) noexcept; +}; + constexpr local_coords global_coords::local() const noexcept { return { uint8_t(x & 0x0f), uint8_t(y & 0x0f), }; @@ -177,4 +187,22 @@ constexpr Vector2i global_coords::operator-(global_coords other) const noexcept return to_signed() - other.to_signed(); } +constexpr std::strong_ordering operator<=>(const point& p1, const point& p2) noexcept +{ + auto c1 = p1.coord.to_signed3(), c2 = p2.coord.to_signed3(); + + if (auto val = c1.z() <=> c2.z(); val != std::strong_ordering::equal) + return val; + if (auto val = c1.y() <=> c2.y(); val != std::strong_ordering::equal) + return val; + if (auto val = c1.x() <=> c2.x(); val != std::strong_ordering::equal) + return val; + if (auto val = p1.offset.y() <=> p2.offset.y(); val != std::strong_ordering::equal) + return val; + if (auto val = p1.offset.x() <=> p2.offset.x(); val != std::strong_ordering::equal) + return val; + + return std::strong_ordering::equal; +} + } // namespace floormat |