From a066fdb5524312c8efa294b97eba3abbf4d49c7a Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 4 Sep 2025 11:02:54 +0200 Subject: [PATCH] Convert Pos to an index. Pos is now just an index into the grid. This will be a slow down for the moment as we haven't worked through the changes to the algorithms yet. --- main.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/main.cc b/main.cc index 64e5ac8..3b4d80b 100644 --- a/main.cc +++ b/main.cc @@ -17,7 +17,7 @@ using size_t = std::size_t; namespace { /** (x, y) pair storing a position. */ - using Pos = std::pair; + using Pos = std::size_t; /** A square - consisting of position of closest corner to origin, and side-length. */ @@ -83,8 +83,8 @@ namespace { s[x + y * length_] = c; } - static auto sq_x(Square const& sq) noexcept -> size_t { return sq.pos().first; } - static auto sq_y(Square const& sq) noexcept -> size_t { return sq.pos().second; } + auto sq_x(Square const& sq) const noexcept -> size_t { return sq.pos() % length_; } + auto sq_y(Square const& sq) const noexcept -> size_t { return sq.pos() / length_; } auto prettify_sq(std::string& s, Square const &sq) const noexcept -> void { switch (sq.length()) { @@ -146,8 +146,8 @@ namespace { /** Get grid length */ auto length() const noexcept -> size_t { return length_; } - auto pos_x(Pos const& pos) const noexcept -> size_t { return pos.first; } - auto pos_y(Pos const& pos) const noexcept -> size_t { return pos.second; } + auto pos_x(Pos const& pos) const noexcept -> size_t { return pos % length_; } + auto pos_y(Pos const& pos) const noexcept -> size_t { return pos / length_; } /** Add a square to the grid. */ auto add(Square const &sq) noexcept -> void { @@ -200,7 +200,7 @@ namespace { auto const b = grid_.begin() + pos_x(pos) + n + pos_y(pos) * length_; auto const p = std::find(b, grid_.end(), empty); auto const v = p - grid_.begin(); - return std::make_pair(v % length_, v / length_); + return v; } private: @@ -262,7 +262,7 @@ namespace { sqs.reserve(length); // Start at the origin with a square of longest side length. - Pos pos{0, 0}; + Pos pos = 0; size_t idx = n; while (true) {