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.
This commit is contained in:
2025-09-04 11:02:54 +02:00
parent e557933590
commit a066fdb552

14
main.cc
View File

@@ -17,7 +17,7 @@ using size_t = std::size_t;
namespace { namespace {
/** (x, y) pair storing a position. */ /** (x, y) pair storing a position. */
using Pos = std::pair<size_t, size_t>; using Pos = std::size_t;
/** A square - consisting of position of closest corner to origin, and side-length. /** A square - consisting of position of closest corner to origin, and side-length.
*/ */
@@ -83,8 +83,8 @@ namespace {
s[x + y * length_] = c; s[x + y * length_] = c;
} }
static auto sq_x(Square const& sq) noexcept -> size_t { return sq.pos().first; } auto sq_x(Square const& sq) const noexcept -> size_t { return sq.pos() % length_; }
static auto sq_y(Square const& sq) noexcept -> size_t { return sq.pos().second; } 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 { auto prettify_sq(std::string& s, Square const &sq) const noexcept -> void {
switch (sq.length()) { switch (sq.length()) {
@@ -146,8 +146,8 @@ namespace {
/** Get grid length */ /** Get grid length */
auto length() const noexcept -> size_t { return length_; } auto length() const noexcept -> size_t { return length_; }
auto pos_x(Pos const& pos) const noexcept -> size_t { return pos.first; } 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.second; } auto pos_y(Pos const& pos) const noexcept -> size_t { return pos / length_; }
/** Add a square to the grid. */ /** Add a square to the grid. */
auto add(Square const &sq) noexcept -> void { 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 b = grid_.begin() + pos_x(pos) + n + pos_y(pos) * length_;
auto const p = std::find(b, grid_.end(), empty); auto const p = std::find(b, grid_.end(), empty);
auto const v = p - grid_.begin(); auto const v = p - grid_.begin();
return std::make_pair(v % length_, v / length_); return v;
} }
private: private:
@@ -262,7 +262,7 @@ namespace {
sqs.reserve(length); sqs.reserve(length);
// Start at the origin with a square of longest side length. // Start at the origin with a square of longest side length.
Pos pos{0, 0}; Pos pos = 0;
size_t idx = n; size_t idx = n;
while (true) { while (true) {