Make next_pos iterate over indices.

This commit is contained in:
2025-09-04 11:12:51 +02:00
parent ed71280dc2
commit f1445b7544

17
main.cc
View File

@@ -145,6 +145,7 @@ namespace {
/** Get grid length */ /** Get grid length */
auto length() const noexcept -> size_t { return length_; } auto length() const noexcept -> size_t { return length_; }
auto end() const noexcept -> size_t { return grid_.size(); }
auto pos_x(Pos const& pos) const noexcept -> size_t { return pos % length_; } 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_; } auto pos_y(Pos const& pos) const noexcept -> size_t { return pos / length_; }
@@ -195,14 +196,14 @@ namespace {
return ye - pos_y(pos); return ye - pos_y(pos);
} }
/** Get the next position to check. n is the size of the square we just /** Get the next position to check starting at pos.
* added. *
* Returns grid_.length() if no more positions avaialble.
*/ */
auto next_pos(Pos const &pos, size_t n) const noexcept -> Pos { auto next_pos(Pos const &pos) const noexcept -> Pos {
auto const b = grid_.begin() + pos_x(pos) + n + pos_y(pos) * length_; auto const b = grid_.begin() + pos;
auto const p = std::find(b, grid_.end(), empty); auto const p = std::find(b, grid_.end(), empty);
auto const v = p - grid_.begin(); return p - grid_.begin();
return v;
} }
private: private:
@@ -296,11 +297,11 @@ namespace {
grid.add(sq); grid.add(sq);
sqs.push_back(sq); sqs.push_back(sq);
pos = grid.next_pos(pos, idx); pos = grid.next_pos(pos + idx);
idx = grid.largest_square(pos, n); idx = grid.largest_square(pos, n);
// Have we reached the end? If so success! // Have we reached the end? If so success!
if (grid.pos_x(pos) == 0 && grid.pos_y(pos) == grid.length()) { break; } if (pos == grid.end()) { break; }
} }
return Results(length, sqs); return Results(length, sqs);