doasku

Sudoku solver
git clone git://git.dimitrijedobrota.com/doasku.git
Log | Files | Refs | README | LICENSE

commit 2fb993f0a6bc697bcbbd0f97cebba384a9053b54
parent bcd759724a4653673d65a047a8c56b1d3b026a7f
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu,  4 Apr 2024 14:52:04 +0200

Rething the types width

Diffstat:
Mmain.cpp | 32++++++++++++++++----------------
1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/main.cpp b/main.cpp @@ -12,7 +12,7 @@ static constexpr const std::int64_t mask_field = (1 << 9) - 1; static constexpr const std::int64_t mask_value = 0x201008040201; -using change_t = std::tuple<uint8_t, uint8_t>; +using change_t = std::tuple<uint16_t, uint16_t>; using changes_t = std::vector<change_t>; class row_col_t; @@ -87,8 +87,8 @@ class Row { void set_field(uint8_t field, uint8_t value) { clear(field), set(field, value); } - uint64_t get(uint8_t field) const { return (val >> 9 * field) & mask_field; } - uint64_t get(uint8_t field, uint8_t value) const { return (val >> 9 * field) & (1 << value); } + uint16_t get(uint8_t field) const { return (val >> 9 * field) & mask_field; } + uint16_t get(uint8_t field, uint8_t value) const { return (val >> 9 * field) & (1 << value); } void set(uint8_t field) { val |= mask_field << 9 * field; } void toggle(uint8_t field) { val ^= mask_field << 9 * field; } @@ -101,7 +101,7 @@ class Row { void toggle_all(uint8_t value) { val ^= mask_value << value; } void clear_all(uint8_t value) { val &= ~(mask_value << value); } - void mask(uint8_t field, uint64_t mask) { val &= ~(mask << 9 * field); } + void mask(uint8_t field, uint16_t mask) { val &= ~(uint64_t(mask) << 9 * field); } friend std::ostream &operator<<(std::ostream &os, Row r) { for (int i = 0; i < 6; i++) { @@ -118,8 +118,8 @@ class Subgrid { public: Subgrid() {} - uint64_t get(row_col_t rc) const { return rows[rc.row].get(rc.col); } - uint64_t get(row_col_rt rc) const { return rows[rc.row].get(rc.col + 3); } + uint16_t get(row_col_t rc) const { return rows[rc.row].get(rc.col); } + uint16_t get(row_col_rt rc) const { return rows[rc.row].get(rc.col + 3); } uint16_t get_ref(row_col_t rc) const { return ref.get(rc); } @@ -189,12 +189,12 @@ class Subgrid { Row o012 = rows[0] | rows[1] | rows[2]; uint64_t mask = { // clang-format off - (o012.get(1) | o012.get(2)) << 0 | - (o012.get(0) | o012.get(2)) << 9 | - (o012.get(0) | o012.get(1)) << 18 | - (o012.get(4) | o012.get(5)) << 27 | - (o012.get(3) | o012.get(5)) << 36 | - (o012.get(3) | o012.get(4)) << 45 + uint64_t(o012.get(1) | o012.get(2)) << 0 | + uint64_t(o012.get(0) | o012.get(2)) << 9 | + uint64_t(o012.get(0) | o012.get(1)) << 18 | + uint64_t(o012.get(4) | o012.get(5)) << 27 | + uint64_t(o012.get(3) | o012.get(5)) << 36 | + uint64_t(o012.get(3) | o012.get(4)) << 45 // clang-format on }; @@ -206,7 +206,7 @@ class Subgrid { std::array<changes_t, 2> res; for (uint8_t i = 0; i < 3; i++) { - uint64_t val = shooting.get(i); + uint16_t val = shooting.get(i); while (val) { uint8_t idx = std::countr_zero(val); res[0].emplace_back(i, idx); @@ -215,7 +215,7 @@ class Subgrid { } for (uint8_t i = 0; i < 3; i++) { - uint64_t val = shooting.get(i + 3); + uint16_t val = shooting.get(i + 3); while (val) { uint8_t idx = std::countr_zero(val); res[1].emplace_back(i, idx); @@ -230,7 +230,7 @@ class Subgrid { if (is_finished()) return false; static uint8_t count[512]; - static uint64_t value[9]; + static uint16_t value[9]; for (uint8_t i = 0; i < 3; i++) { for (uint8_t j = 0; j < 3; j++) { @@ -266,7 +266,7 @@ class Subgrid { res = true; // remove cleared from reference - uint64_t tvalue = value[field]; + uint16_t tvalue = value[field]; while (tvalue) { uint16_t idx = std::countr_zero(tvalue); ref.remove(nfield, idx);