diff options
Diffstat (limited to 'qt-widgets/adapter.cpp')
| -rw-r--r-- | qt-widgets/adapter.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/qt-widgets/adapter.cpp b/qt-widgets/adapter.cpp new file mode 100644 index 0000000..c15181c --- /dev/null +++ b/qt-widgets/adapter.cpp | |||
| @@ -0,0 +1,46 @@ | |||
| 1 | #include "adapter.h" | ||
| 2 | |||
| 3 | #include <array> | ||
| 4 | #include <filesystem> | ||
| 5 | #include <fstream> | ||
| 6 | #include <sstream> | ||
| 7 | #include <QDebug> | ||
| 8 | |||
| 9 | NissyAdapter::NissyAdapter() | ||
| 10 | { | ||
| 11 | auto sid = nissy::solverinfo(defaultOptimalSolver); | ||
| 12 | auto [sz, dataid] = std::get<std::pair<size_t, std::string>>(sid); | ||
| 13 | const std::string path = "../nissy-core/tables/" + dataid; | ||
| 14 | |||
| 15 | std::filesystem::path filePath(path); | ||
| 16 | if (std::filesystem::file_size(filePath) != static_cast<uintmax_t>(sz)) | ||
| 17 | qDebug("Error in file size!"); // TODO: better handle error, gentable | ||
| 18 | |||
| 19 | optimalSolverData.value.resize(sz); | ||
| 20 | std::ifstream ifs(path, std::ios::binary); | ||
| 21 | ifs.read(reinterpret_cast<char *>(optimalSolverData.value.data()), sz); | ||
| 22 | ifs.close(); | ||
| 23 | } | ||
| 24 | |||
| 25 | NissyAdapter::~NissyAdapter() {} | ||
| 26 | |||
| 27 | void NissyAdapter::solve(SolverConfiguration cfg) | ||
| 28 | { | ||
| 29 | auto se = nissy::solve(cfg.cube, defaultOptimalSolver, | ||
| 30 | nissy::nissflag::NORMAL, cfg.minmoves, cfg.maxmoves, | ||
| 31 | cfg.maxsolutions, cfg.optimal, cfg.threads, optimalSolverData); | ||
| 32 | if (std::holds_alternative<nissy::error_t>(se)) { | ||
| 33 | auto code = std::get<nissy::error_t>(se).value; | ||
| 34 | emit solveDone(QString("Error " + code)); | ||
| 35 | return; | ||
| 36 | } | ||
| 37 | |||
| 38 | auto [sols, stats] = std::get<nissy::solve_result_t>(se); | ||
| 39 | if (sols.size() == 0) { | ||
| 40 | emit solveDone(QString("No solution found")); | ||
| 41 | } else { | ||
| 42 | auto solstr = std::accumulate( | ||
| 43 | sols.begin(), sols.end(), std::string{}); | ||
| 44 | emit solveDone(QString::fromStdString(solstr)); | ||
| 45 | } | ||
| 46 | } | ||
