Browse Source

feat: add speed up support

master
Dnomd343 5 days ago
parent
commit
34d8d715c3
  1. 4
      src/core/group/group.h
  2. 1
      src/core_ffi/python_ffi/CMakeLists.txt
  3. 20
      src/core_ffi/python_ffi/binder/klsk_code.cc
  4. 5
      src/core_ffi/python_ffi/include/py_ffi/short_code.h
  5. 26
      src/core_ffi/python_ffi/include/py_ffi/speed_up.h
  6. 35
      src/core_ffi/python_ffi/wrapper/speed_up.cc

4
src/core/group/group.h

@ -457,6 +457,10 @@ public:
/// Execute the build process without blocking. /// Execute the build process without blocking.
static void build_async(Executor &&executor, Notifier &&callback); static void build_async(Executor &&executor, Notifier &&callback);
static bool is_fast_mode() {
return fast_;
}
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Get the CommonCode from CaseInfo. /// Get the CommonCode from CaseInfo.

1
src/core_ffi/python_ffi/CMakeLists.txt

@ -18,6 +18,7 @@ set(KLSK_PYTHON_FFI_SRC
wrapper/group_union.cc wrapper/group_union.cc
wrapper/group.cc wrapper/group.cc
wrapper/fast_cal.cc wrapper/fast_cal.cc
wrapper/speed_up.cc
) )
pybind11_add_module(klotski_py ${KLSK_PYTHON_FFI_SRC}) pybind11_add_module(klotski_py ${KLSK_PYTHON_FFI_SRC})

20
src/core_ffi/python_ffi/binder/klsk_code.cc

@ -1,5 +1,6 @@
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include <pybind11/operators.h> #include <pybind11/operators.h>
#include <py_ffi/speed_up.h>
#include "binder.h" #include "binder.h"
#include "py_ffi/layout.h" #include "py_ffi/layout.h"
@ -7,8 +8,19 @@
using klotski::ffi::PyBlock; using klotski::ffi::PyBlock;
using klotski::ffi::PyLayout; using klotski::ffi::PyLayout;
using klotski::ffi::PySpeedUp;
using klotski::ffi::PyShortCode; using klotski::ffi::PyShortCode;
static void bind_speed_up(const py::module_ &mod) {
py::class_<PySpeedUp>(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) { static void bind_layout(const py::module_ &mod) {
py::enum_<PyBlock>(mod, "Block") py::enum_<PyBlock>(mod, "Block")
.value("SPACE", PyBlock::SPACE) .value("SPACE", PyBlock::SPACE)
@ -16,8 +28,7 @@ static void bind_layout(const py::module_ &mod) {
.value("B_1x2", PyBlock::B_1x2) .value("B_1x2", PyBlock::B_1x2)
.value("B_2x1", PyBlock::B_2x1) .value("B_2x1", PyBlock::B_2x1)
.value("B_2x2", PyBlock::B_2x2) .value("B_2x2", PyBlock::B_2x2)
.value("FILL", PyBlock::FILL) .value("FILL", PyBlock::FILL);
.export_values();
py::class_<PyLayout>(mod, "Layout") py::class_<PyLayout>(mod, "Layout")
.def(py::init<uint64_t>()) .def(py::init<uint64_t>())
@ -73,12 +84,11 @@ static void bind_short_code(const py::module_ &mod) {
.def("to_layout", &PyShortCode::layout) .def("to_layout", &PyShortCode::layout)
.def_static("check", py::overload_cast<uint32_t>(&PyShortCode::check)) .def_static("check", py::overload_cast<uint32_t>(&PyShortCode::check))
.def_static("check", py::overload_cast<std::string_view>(&PyShortCode::check)) .def_static("check", py::overload_cast<std::string_view>(&PyShortCode::check));
.def_static("speed_up", &PyShortCode::speed_up, py::arg("fast_mode") = false);
} }
void bind_klsk_code(const py::module_ &mod) { void bind_klsk_code(const py::module_ &mod) {
bind_layout(mod); bind_layout(mod);
bind_short_code(mod); bind_short_code(mod);
bind_speed_up(mod);
} }

5
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: private:
codec::ShortCode code_; codec::ShortCode code_;
}; };

26
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

35
src/core_ffi/python_ffi/wrapper/speed_up.cc

@ -0,0 +1,35 @@
#include "py_ffi/speed_up.h"
#include <group/group.h>
#include <all_cases/all_cases.h>
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();
}
Loading…
Cancel
Save