From 958aa8a9e704f239e10a342a45aabdbdc475de16 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 22 Dec 2024 11:23:19 +0800 Subject: [PATCH] feat: add speed up support --- src/core/group/group.h | 4 +++ src/core_ffi/python_ffi/CMakeLists.txt | 1 + src/core_ffi/python_ffi/binder/klsk_code.cc | 20 ++++++++--- .../python_ffi/include/py_ffi/short_code.h | 5 --- .../python_ffi/include/py_ffi/speed_up.h | 26 ++++++++++++++ src/core_ffi/python_ffi/wrapper/speed_up.cc | 35 +++++++++++++++++++ 6 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 src/core_ffi/python_ffi/include/py_ffi/speed_up.h create mode 100644 src/core_ffi/python_ffi/wrapper/speed_up.cc diff --git a/src/core/group/group.h b/src/core/group/group.h index e3139b8..671ac84 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -457,6 +457,10 @@ public: /// Execute the build process without blocking. static void build_async(Executor &&executor, Notifier &&callback); + static bool is_fast_mode() { + return fast_; + } + // ------------------------------------------------------------------------------------- // /// Get the CommonCode from CaseInfo. diff --git a/src/core_ffi/python_ffi/CMakeLists.txt b/src/core_ffi/python_ffi/CMakeLists.txt index 18ebc0e..c15f75f 100644 --- a/src/core_ffi/python_ffi/CMakeLists.txt +++ b/src/core_ffi/python_ffi/CMakeLists.txt @@ -18,6 +18,7 @@ set(KLSK_PYTHON_FFI_SRC wrapper/group_union.cc wrapper/group.cc wrapper/fast_cal.cc + wrapper/speed_up.cc ) pybind11_add_module(klotski_py ${KLSK_PYTHON_FFI_SRC}) diff --git a/src/core_ffi/python_ffi/binder/klsk_code.cc b/src/core_ffi/python_ffi/binder/klsk_code.cc index cdb65c4..4b3e1f5 100644 --- a/src/core_ffi/python_ffi/binder/klsk_code.cc +++ b/src/core_ffi/python_ffi/binder/klsk_code.cc @@ -1,5 +1,6 @@ #include #include +#include #include "binder.h" #include "py_ffi/layout.h" @@ -7,8 +8,19 @@ using klotski::ffi::PyBlock; using klotski::ffi::PyLayout; +using klotski::ffi::PySpeedUp; using klotski::ffi::PyShortCode; +static void bind_speed_up(const py::module_ &mod) { + py::class_(mod, "SpeedUp") + .def_static("build_stage_0", &PySpeedUp::stage_0) + .def_static("build_stage_1", &PySpeedUp::stage_1) + .def_static("build_stage_2", &PySpeedUp::stage_2) + .def_property_readonly_static("is_stage_0", [](const py::object&) { return PySpeedUp::is_stage_0(); }) + .def_property_readonly_static("is_stage_1", [](const py::object&) { return PySpeedUp::is_stage_1(); }) + .def_property_readonly_static("is_stage_2", [](const py::object&) { return PySpeedUp::is_stage_2(); }); +} + static void bind_layout(const py::module_ &mod) { py::enum_(mod, "Block") .value("SPACE", PyBlock::SPACE) @@ -16,8 +28,7 @@ static void bind_layout(const py::module_ &mod) { .value("B_1x2", PyBlock::B_1x2) .value("B_2x1", PyBlock::B_2x1) .value("B_2x2", PyBlock::B_2x2) - .value("FILL", PyBlock::FILL) - .export_values(); + .value("FILL", PyBlock::FILL); py::class_(mod, "Layout") .def(py::init()) @@ -73,12 +84,11 @@ static void bind_short_code(const py::module_ &mod) { .def("to_layout", &PyShortCode::layout) .def_static("check", py::overload_cast(&PyShortCode::check)) - .def_static("check", py::overload_cast(&PyShortCode::check)) - - .def_static("speed_up", &PyShortCode::speed_up, py::arg("fast_mode") = false); + .def_static("check", py::overload_cast(&PyShortCode::check)); } void bind_klsk_code(const py::module_ &mod) { bind_layout(mod); bind_short_code(mod); + bind_speed_up(mod); } diff --git a/src/core_ffi/python_ffi/include/py_ffi/short_code.h b/src/core_ffi/python_ffi/include/py_ffi/short_code.h index d33217e..925ab41 100644 --- a/src/core_ffi/python_ffi/include/py_ffi/short_code.h +++ b/src/core_ffi/python_ffi/include/py_ffi/short_code.h @@ -49,11 +49,6 @@ public: // ------------------------------------------------------------------------------------- // - /// Build conversion index for ShortCode. - static void speed_up(const bool fast_mode) { // TODO: move to `SpeedUp` - codec::ShortCode::speed_up(fast_mode); - } - private: codec::ShortCode code_; }; diff --git a/src/core_ffi/python_ffi/include/py_ffi/speed_up.h b/src/core_ffi/python_ffi/include/py_ffi/speed_up.h new file mode 100644 index 0000000..d3cec35 --- /dev/null +++ b/src/core_ffi/python_ffi/include/py_ffi/speed_up.h @@ -0,0 +1,26 @@ +/// Klotski Engine Python FFI by Dnomd343 @2024 + +#pragma once + +namespace klotski::ffi { + +// TODO: add multi-thread support + +// TODO: add `__await__` interface support + +class PySpeedUp { +public: + static void stage_0(); + + static void stage_1(); + + static void stage_2(); + + static bool is_stage_0(); + + static bool is_stage_1(); + + static bool is_stage_2(); +}; + +} // namespace klotski::ffi diff --git a/src/core_ffi/python_ffi/wrapper/speed_up.cc b/src/core_ffi/python_ffi/wrapper/speed_up.cc new file mode 100644 index 0000000..0979207 --- /dev/null +++ b/src/core_ffi/python_ffi/wrapper/speed_up.cc @@ -0,0 +1,35 @@ +#include "py_ffi/speed_up.h" + +#include +#include + +using klotski::ffi::PySpeedUp; + +using klotski::cases::AllCases; +using klotski::cases::BasicRanges; + +using klotski::group::GroupCases; + +void PySpeedUp::stage_0() { + BasicRanges::instance().build(); +} + +void PySpeedUp::stage_1() { + AllCases::instance().build(); +} + +void PySpeedUp::stage_2() { + GroupCases::build(); +} + +bool PySpeedUp::is_stage_0() { + return BasicRanges::instance().is_available(); +} + +bool PySpeedUp::is_stage_1() { + return AllCases::instance().is_available(); +} + +bool PySpeedUp::is_stage_2() { + return GroupCases::is_fast_mode(); +}