doasku

Human-like solver for sudoku
git clone git://git.dimitrijedobrota.com/doasku.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |

commit8f55dc256a616127fe139c92c22c6ceffc8bdd70
parent9bc87891964a37a0dfc09358e27a3d2f4ce111a1
authorDimitrije Dobrota <mail@dimitrijedobrota.com>
dateThu, 20 Jun 2024 18:53:36 +0200

Improve readability with std::invoke and auto

Diffstat:
MCMakeLists.txt|+-
Msrc/grid.cpp|++++++++++++++++++++++++++++++++++++-----------------------------------

2 files changed, 37 insertions(+), 36 deletions(-)


diff --git a/CMakeLists.txt b/CMakeLists.txt

@@ -3,7 +3,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(
doasku
VERSION 0.0.1
VERSION 0.0.2
DESCRIPTION "Sudoku madness"
HOMEPAGE_URL https://git.dimitrijedobrota.com/doasku.git
LANGUAGES CXX

diff --git a/src/grid.cpp b/src/grid.cpp

@@ -1,5 +1,6 @@

#include <algorithm>
#include <bitset>
#include <functional>
#include <iostream>
#include "grid.hpp"

@@ -35,41 +36,41 @@ Grid::Grid(const std::string &s) {

bool Grid::solve() {
// clang-format off
static const auto sub_op =
[this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) {
for(uint8_t subgrid = 0; subgrid < 9; subgrid++) {
for_each((subgrids[subgrid].*(f))(), [this, subgrid, op](const Ref::change_t ch) {
(this->*(op))(operation_t({cord_t(subgrid), cord_t(std::get<0>(ch))}, std::get<1>(ch)));
});
}
return changed;
};
static const auto row_op =
[this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) {
for(uint8_t row = 0; row < 9; row++) {
for_each((rows[row].*(f))(), [this, row, op](const Ref::change_t ch) {
(this->*(op))(operation_t({row, std::get<0>(ch)}, std::get<1>(ch)));
});
}
return changed;
};
static const auto col_op =
[this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) {
for(uint8_t col = 0; col < 9; col++) {
for_each((cols[col].*(f))(), [this, col, op](const Ref::change_t ch) {
(this->*(op))(operation_t({std::get<0>(ch), col}, std::get<1>(ch)));
});
}
return changed;
};
static const auto all_op =
[this](void (Grid::*op)(operation_t), Ref::changes_t (Ref::*f)() const) {
sub_op(op, f), row_op(op, f), col_op(op, f);
return changed;
};
static const auto sub_op =
[this](auto op, const auto f) {
for(uint8_t subgrid = 0; subgrid < 9; subgrid++) {
for_each(std::invoke(f, subgrids[subgrid]), [&](auto& ch) {
std::invoke(op, this, operation_t({cord_t(subgrid), cord_t(std::get<0>(ch))}, std::get<1>(ch)));
});
}
return changed;
};
static const auto row_op =
[this](auto op, const auto f) {
for(uint8_t row = 0; row < 9; row++) {
for_each(std::invoke(f, rows[row]), [&](auto& ch) {
std::invoke(op, this, operation_t({row, std::get<0>(ch)}, std::get<1>(ch)));
});
}
return changed;
};
static const auto col_op =
[this](auto op, const auto f) {
for(uint8_t col = 0; col < 9; col++) {
for_each(std::invoke(f, cols[col]), [&](auto &ch) {
std::invoke(op, this, operation_t({std::get<0>(ch), col}, std::get<1>(ch)));
});
}
return changed;
};
static const auto all_op =
[this](auto op, const auto f) {
sub_op(op, f), row_op(op, f), col_op(op, f);
return changed;
};
// clang-format on
changed = true;