From b06a665041cb5d928b6f39ea593a1deaa20deb15 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 3 Nov 2024 12:21:19 +0800 Subject: [PATCH] refactor: file structure of py_ffi --- src/core_ffi/CMakeLists.txt | 14 +- src/core_ffi/py_ffi/CMakeLists.txt | 23 +++ src/core_ffi/py_ffi/binder.cc | 143 ------------------ src/core_ffi/py_ffi/binder/cases.cc | 10 +- src/core_ffi/py_ffi/binder/common_code.cc | 30 ++++ src/core_ffi/py_ffi/binder/group.cc | 17 +++ src/core_ffi/py_ffi/binder/group_union.cc | 23 +++ src/core_ffi/py_ffi/binder/short_code.cc | 28 ++++ src/core_ffi/py_ffi/include/binder.h | 26 ++++ .../include/{py_exception.h => exception.h} | 0 .../include/{py_cases.h => py_ffi/cases.h} | 4 +- .../common_code.h} | 2 +- .../include/{py_group.h => py_ffi/group.h} | 4 +- .../{py_short_code.h => py_ffi/short_code.h} | 2 +- src/core_ffi/py_ffi/klotski.cc | 52 +++++++ src/core_ffi/py_ffi/wrapper/cases.cc | 2 +- src/core_ffi/py_ffi/wrapper/common_codec.cc | 4 +- src/core_ffi/py_ffi/wrapper/group.cc | 2 +- src/core_ffi/py_ffi/wrapper/group_union.cc | 5 +- src/core_ffi/py_ffi/wrapper/short_code.cc | 4 +- 20 files changed, 216 insertions(+), 179 deletions(-) create mode 100644 src/core_ffi/py_ffi/CMakeLists.txt delete mode 100644 src/core_ffi/py_ffi/binder.cc create mode 100644 src/core_ffi/py_ffi/binder/common_code.cc create mode 100644 src/core_ffi/py_ffi/binder/group.cc create mode 100644 src/core_ffi/py_ffi/binder/group_union.cc create mode 100644 src/core_ffi/py_ffi/binder/short_code.cc create mode 100644 src/core_ffi/py_ffi/include/binder.h rename src/core_ffi/py_ffi/include/{py_exception.h => exception.h} (100%) rename src/core_ffi/py_ffi/include/{py_cases.h => py_ffi/cases.h} (97%) rename src/core_ffi/py_ffi/include/{py_common_code.h => py_ffi/common_code.h} (98%) rename src/core_ffi/py_ffi/include/{py_group.h => py_ffi/group.h} (95%) rename src/core_ffi/py_ffi/include/{py_short_code.h => py_ffi/short_code.h} (98%) create mode 100644 src/core_ffi/py_ffi/klotski.cc diff --git a/src/core_ffi/CMakeLists.txt b/src/core_ffi/CMakeLists.txt index df93ee1..9c112b4 100644 --- a/src/core_ffi/CMakeLists.txt +++ b/src/core_ffi/CMakeLists.txt @@ -11,19 +11,7 @@ if (KLSK_C_FFI) endif() if (KLSK_PYTHON_FFI) - pybind11_add_module(klotski_py - py_ffi/binder.cc - py_ffi/binder/cases.cc - - py_ffi/wrapper/short_code.cc - py_ffi/wrapper/common_codec.cc - py_ffi/wrapper/cases.cc - py_ffi/wrapper/group_union.cc - py_ffi/wrapper/group.cc - ) - target_include_directories(klotski_py PRIVATE py_ffi) - target_link_libraries(klotski_py PRIVATE klotski::core) - set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski) + add_subdirectory(py_ffi) endif() if (KLSK_RUST_FFI) diff --git a/src/core_ffi/py_ffi/CMakeLists.txt b/src/core_ffi/py_ffi/CMakeLists.txt new file mode 100644 index 0000000..4d46608 --- /dev/null +++ b/src/core_ffi/py_ffi/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.12) +project(py_ffi) + +set(CMAKE_CXX_STANDARD 23) + +pybind11_add_module(klotski_py + klotski.cc + + binder/cases.cc + binder/common_code.cc + binder/short_code.cc + binder/group_union.cc + binder/group.cc + + wrapper/short_code.cc + wrapper/common_codec.cc + wrapper/cases.cc + wrapper/group_union.cc + wrapper/group.cc +) +target_include_directories(klotski_py PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_link_libraries(klotski_py PRIVATE klotski::core) +set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski) diff --git a/src/core_ffi/py_ffi/binder.cc b/src/core_ffi/py_ffi/binder.cc deleted file mode 100644 index 847c5d2..0000000 --- a/src/core_ffi/py_ffi/binder.cc +++ /dev/null @@ -1,143 +0,0 @@ -#include -#include -#include - -#include "include/py_common_code.h" -#include "include/py_short_code.h" -#include "include/py_cases.h" -#include "include/py_group.h" -#include "include/py_exception.h" - -namespace py = pybind11; - -using klotski::ffi::PyCases; -//using klotski::ffi::PyCasesIter; - -//using klotski::ffi::PyCodecExp; -using klotski::ffi::PyShortCode; -using klotski::ffi::PyCommonCode; - -using klotski::ffi::PyExc_CodecError; - -using klotski::ffi::PyGroup; -using klotski::ffi::PyGroupUnion; - -extern void bind_cases(const py::module_ &m); - -void bind_common_code(const py::module_ &m) { - py::class_(m, "CommonCode") - .def(py::init()) - .def(py::init()) - .def(py::init()) - - .def(py::hash(py::self)) - .def("__str__", &PyCommonCode::str) - .def("__int__", &PyCommonCode::value) - .def("__repr__", &PyCommonCode::repr) - - .def(py::self == py::self) - .def(py::self < py::self).def(py::self <= py::self) - .def(py::self > py::self).def(py::self >= py::self) - - .def(py::self == uint64_t()) - .def(py::self < uint64_t()).def(py::self <= uint64_t()) - .def(py::self > uint64_t()).def(py::self >= uint64_t()) - -// .def_property_readonly("str", &PyCommonCode::string) - .def_property_readonly("value", &PyCommonCode::value) - .def_property_readonly("short_code", &PyCommonCode::short_code) - - .def("to_string", &PyCommonCode::string, py::arg("shorten") = false) - - .def_static("check", static_cast(&PyCommonCode::check)) - .def_static("check", static_cast(&PyCommonCode::check)); -} - -void bind_short_code(const py::module_ &m) { - py::class_(m, "ShortCode") - .def(py::init()) - .def(py::init()) - .def(py::init()) - - .def(py::hash(py::self)) - .def("__str__", &PyShortCode::str) - .def("__int__", &PyShortCode::value) - .def("__repr__", &PyShortCode::repr) - - .def(py::self == py::self) - .def(py::self < py::self).def(py::self <= py::self) - .def(py::self > py::self).def(py::self >= py::self) - - .def(py::self == uint32_t()) - .def(py::self < uint32_t()).def(py::self <= uint32_t()) - .def(py::self > uint32_t()).def(py::self >= uint32_t()) - - .def_property_readonly("value", &PyShortCode::value) - .def_property_readonly("common_code", &PyShortCode::common_code) - - .def_static("check", static_cast(&PyShortCode::check)) - .def_static("check", static_cast(&PyShortCode::check)) - .def_static("speed_up", &PyShortCode::speed_up, py::arg("fast_mode") = false); -} - -#include "group/group.h" -#include "all_cases/all_cases.h" - -static PyCases group_demo() { - auto group_union = klotski::group::GroupUnion::unsafe_create(169); - auto cases = PyCases::from(group_union.cases()); - return cases; -} - -static PyCases all_cases() { - return PyCases::from_ref(klotski::cases::AllCases::instance().fetch()); -} - -PYBIND11_MODULE(klotski, m) { -// py::register_exception(m, "CodecExp", PyExc_ValueError); - py::register_exception(m, "CodecError", PyExc_ValueError); - - m.def("all_cases", &all_cases); - m.def("group_demo", &group_demo); - - bind_short_code(m); - bind_common_code(m); - - bind_cases(m); - - py::class_(m, "GroupUnion") - .def(py::init()) - .def(py::init()) - .def(py::init()) - -// .def(py::hash(py::self)) -// .def(py::self == py::self) -// .def("__str__", &PyShortCode::str) - .def("__int__", &PyGroupUnion::value) -// .def("__repr__", &PyGroupUnion::repr) - - .def("cases", &PyGroupUnion::cases) - .def("groups", &PyGroupUnion::groups) - - .def_property_readonly("size", &PyGroupUnion::size) - .def_property_readonly("value", &PyGroupUnion::value) - .def_property_readonly("group_num", &PyGroupUnion::group_num) - .def_property_readonly("pattern_num", &PyGroupUnion::pattern_num) - .def_property_readonly("max_group_size", &PyGroupUnion::max_group_size); - - py::class_(m, "Group") - .def_property_readonly("type_id", &PyGroup::type_id) - .def_property_readonly("pattern_id", &PyGroup::pattern_id) - - .def("__str__", &PyGroup::to_string) - - .def("cases", &PyGroup::cases) - .def("to_vertical_mirror", &PyGroup::to_vertical_mirror) - .def("to_horizontal_mirror", &PyGroup::to_horizontal_mirror) - - .def_property_readonly("size", &PyGroup::size) - .def_property_readonly("is_vertical_mirror", &PyGroup::is_vertical_mirror) - .def_property_readonly("is_horizontal_mirror", &PyGroup::is_horizontal_mirror); - - m.attr("__version__") = "version field"; -} diff --git a/src/core_ffi/py_ffi/binder/cases.cc b/src/core_ffi/py_ffi/binder/cases.cc index bec2c00..3a1eccb 100644 --- a/src/core_ffi/py_ffi/binder/cases.cc +++ b/src/core_ffi/py_ffi/binder/cases.cc @@ -1,12 +1,4 @@ -#include -#include - -#include "include/py_cases.h" - -namespace py = pybind11; - -using klotski::ffi::PyCases; -using klotski::ffi::PyCasesIter; +#include "binder.h" void bind_cases(const py::module_ &m) { py::class_(m, "Cases") diff --git a/src/core_ffi/py_ffi/binder/common_code.cc b/src/core_ffi/py_ffi/binder/common_code.cc new file mode 100644 index 0000000..d3e68ad --- /dev/null +++ b/src/core_ffi/py_ffi/binder/common_code.cc @@ -0,0 +1,30 @@ +#include "binder.h" + +void bind_common_code(const py::module_ &m) { + py::class_(m, "CommonCode") + .def(py::init()) + .def(py::init()) + .def(py::init()) + + .def(py::hash(py::self)) + .def("__str__", &PyCommonCode::str) + .def("__int__", &PyCommonCode::value) + .def("__repr__", &PyCommonCode::repr) + + .def(py::self == py::self) + .def(py::self < py::self).def(py::self <= py::self) + .def(py::self > py::self).def(py::self >= py::self) + + .def(py::self == uint64_t()) + .def(py::self < uint64_t()).def(py::self <= uint64_t()) + .def(py::self > uint64_t()).def(py::self >= uint64_t()) + +// .def_property_readonly("str", &PyCommonCode::string) + .def_property_readonly("value", &PyCommonCode::value) + .def_property_readonly("short_code", &PyCommonCode::short_code) + + .def("to_string", &PyCommonCode::string, py::arg("shorten") = false) + + .def_static("check", static_cast(&PyCommonCode::check)) + .def_static("check", static_cast(&PyCommonCode::check)); +} diff --git a/src/core_ffi/py_ffi/binder/group.cc b/src/core_ffi/py_ffi/binder/group.cc new file mode 100644 index 0000000..e2cf410 --- /dev/null +++ b/src/core_ffi/py_ffi/binder/group.cc @@ -0,0 +1,17 @@ +#include "binder.h" + +void bind_group(const py::module_ &m) { + py::class_(m, "Group") + .def_property_readonly("type_id", &PyGroup::type_id) + .def_property_readonly("pattern_id", &PyGroup::pattern_id) + + .def("__str__", &PyGroup::to_string) + + .def("cases", &PyGroup::cases) + .def("to_vertical_mirror", &PyGroup::to_vertical_mirror) + .def("to_horizontal_mirror", &PyGroup::to_horizontal_mirror) + + .def_property_readonly("size", &PyGroup::size) + .def_property_readonly("is_vertical_mirror", &PyGroup::is_vertical_mirror) + .def_property_readonly("is_horizontal_mirror", &PyGroup::is_horizontal_mirror); +} diff --git a/src/core_ffi/py_ffi/binder/group_union.cc b/src/core_ffi/py_ffi/binder/group_union.cc new file mode 100644 index 0000000..5b7329d --- /dev/null +++ b/src/core_ffi/py_ffi/binder/group_union.cc @@ -0,0 +1,23 @@ +#include "binder.h" + +void bind_group_union(const py::module_ &m) { + py::class_(m, "GroupUnion") + .def(py::init()) + .def(py::init()) + .def(py::init()) + +// .def(py::hash(py::self)) +// .def(py::self == py::self) +// .def("__str__", &PyShortCode::str) + .def("__int__", &PyGroupUnion::value) +// .def("__repr__", &PyGroupUnion::repr) + + .def("cases", &PyGroupUnion::cases) + .def("groups", &PyGroupUnion::groups) + + .def_property_readonly("size", &PyGroupUnion::size) + .def_property_readonly("value", &PyGroupUnion::value) + .def_property_readonly("group_num", &PyGroupUnion::group_num) + .def_property_readonly("pattern_num", &PyGroupUnion::pattern_num) + .def_property_readonly("max_group_size", &PyGroupUnion::max_group_size); +} diff --git a/src/core_ffi/py_ffi/binder/short_code.cc b/src/core_ffi/py_ffi/binder/short_code.cc new file mode 100644 index 0000000..ba47532 --- /dev/null +++ b/src/core_ffi/py_ffi/binder/short_code.cc @@ -0,0 +1,28 @@ +#include "binder.h" + +void bind_short_code(const py::module_ &m) { + py::class_(m, "ShortCode") + .def(py::init()) + .def(py::init()) + .def(py::init()) + + .def(py::hash(py::self)) + .def("__str__", &PyShortCode::str) + .def("__int__", &PyShortCode::value) + .def("__repr__", &PyShortCode::repr) + + .def(py::self == py::self) + .def(py::self < py::self).def(py::self <= py::self) + .def(py::self > py::self).def(py::self >= py::self) + + .def(py::self == uint32_t()) + .def(py::self < uint32_t()).def(py::self <= uint32_t()) + .def(py::self > uint32_t()).def(py::self >= uint32_t()) + + .def_property_readonly("value", &PyShortCode::value) + .def_property_readonly("common_code", &PyShortCode::common_code) + + .def_static("check", static_cast(&PyShortCode::check)) + .def_static("check", static_cast(&PyShortCode::check)) + .def_static("speed_up", &PyShortCode::speed_up, py::arg("fast_mode") = false); +} diff --git a/src/core_ffi/py_ffi/include/binder.h b/src/core_ffi/py_ffi/include/binder.h new file mode 100644 index 0000000..ce6c315 --- /dev/null +++ b/src/core_ffi/py_ffi/include/binder.h @@ -0,0 +1,26 @@ +#pragma once + +#include +#include +#include + +namespace py = pybind11; + +#include "py_ffi/cases.h" +#include "py_ffi/group.h" +#include "py_ffi/short_code.h" +#include "py_ffi/common_code.h" + +using klotski::ffi::PyCases; +using klotski::ffi::PyCasesIter; +using klotski::ffi::PyShortCode; +using klotski::ffi::PyCommonCode; +using klotski::ffi::PyGroupUnion; +using klotski::ffi::PyGroup; + +void bind_cases(const py::module_ &m); +void bind_short_code(const py::module_ &m); +void bind_common_code(const py::module_ &m); + +void bind_group_union(const py::module_ &m); +void bind_group(const py::module_ &m); diff --git a/src/core_ffi/py_ffi/include/py_exception.h b/src/core_ffi/py_ffi/include/exception.h similarity index 100% rename from src/core_ffi/py_ffi/include/py_exception.h rename to src/core_ffi/py_ffi/include/exception.h diff --git a/src/core_ffi/py_ffi/include/py_cases.h b/src/core_ffi/py_ffi/include/py_ffi/cases.h similarity index 97% rename from src/core_ffi/py_ffi/include/py_cases.h rename to src/core_ffi/py_ffi/include/py_ffi/cases.h index 0d8a171..96380af 100644 --- a/src/core_ffi/py_ffi/include/py_cases.h +++ b/src/core_ffi/py_ffi/include/py_ffi/cases.h @@ -5,7 +5,7 @@ #include #include -#include "py_common_code.h" +#include "py_ffi/common_code.h" namespace klotski::ffi { @@ -29,6 +29,8 @@ class PyCases { public: PyCases() = delete; + // TODO: add `all_cases` interface + // ------------------------------------------------------------------------------------- // /// Constructing from r-value. diff --git a/src/core_ffi/py_ffi/include/py_common_code.h b/src/core_ffi/py_ffi/include/py_ffi/common_code.h similarity index 98% rename from src/core_ffi/py_ffi/include/py_common_code.h rename to src/core_ffi/py_ffi/include/py_ffi/common_code.h index fa3870a..f37efd3 100644 --- a/src/core_ffi/py_ffi/include/py_common_code.h +++ b/src/core_ffi/py_ffi/include/py_ffi/common_code.h @@ -4,7 +4,7 @@ #include -#include "py_short_code.h" +#include "py_ffi/short_code.h" namespace klotski::ffi { diff --git a/src/core_ffi/py_ffi/include/py_group.h b/src/core_ffi/py_ffi/include/py_ffi/group.h similarity index 95% rename from src/core_ffi/py_ffi/include/py_group.h rename to src/core_ffi/py_ffi/include/py_ffi/group.h index e4db2d8..300a223 100644 --- a/src/core_ffi/py_ffi/include/py_group.h +++ b/src/core_ffi/py_ffi/include/py_ffi/group.h @@ -6,8 +6,8 @@ #include #include -#include "py_cases.h" -#include "py_common_code.h" +#include "py_ffi/cases.h" +#include "py_ffi/common_code.h" namespace klotski::ffi { diff --git a/src/core_ffi/py_ffi/include/py_short_code.h b/src/core_ffi/py_ffi/include/py_ffi/short_code.h similarity index 98% rename from src/core_ffi/py_ffi/include/py_short_code.h rename to src/core_ffi/py_ffi/include/py_ffi/short_code.h index cedb7fe..7a43965 100644 --- a/src/core_ffi/py_ffi/include/py_short_code.h +++ b/src/core_ffi/py_ffi/include/py_ffi/short_code.h @@ -4,7 +4,7 @@ #include -#include "py_common_code.h" +#include "py_ffi/common_code.h" namespace klotski::ffi { diff --git a/src/core_ffi/py_ffi/klotski.cc b/src/core_ffi/py_ffi/klotski.cc new file mode 100644 index 0000000..d33c2b1 --- /dev/null +++ b/src/core_ffi/py_ffi/klotski.cc @@ -0,0 +1,52 @@ +#include +// #include + +// #include "py_ffi/common_code.h" +// #include "py_ffi/short_code.h" +// #include "py_ffi/cases.h" +// #include "py_ffi/group.h" + +#include "exception.h" + +#include "binder.h" + +namespace py = pybind11; + +// using klotski::ffi::PyCases; +// using klotski::ffi::PyShortCode; +// using klotski::ffi::PyCommonCode; + +using klotski::ffi::PyExc_CodecError; +using klotski::ffi::PyExc_GroupError; + +// using klotski::ffi::PyGroup; +// using klotski::ffi::PyGroupUnion; + +#include "group/group.h" +#include "all_cases/all_cases.h" + +// static PyCases group_demo() { +// auto group_union = klotski::group::GroupUnion::unsafe_create(169); +// auto cases = PyCases::from(group_union.cases()); +// return cases; +// } +// +// static PyCases all_cases() { +// return PyCases::from_ref(klotski::cases::AllCases::instance().fetch()); +// } + +PYBIND11_MODULE(klotski, m) { + py::register_exception(m, "GroupError", PyExc_ValueError); + py::register_exception(m, "CodecError", PyExc_ValueError); + + // m.def("all_cases", &all_cases); + // m.def("group_demo", &group_demo); + + bind_cases(m); + bind_short_code(m); + bind_common_code(m); + bind_group(m); + bind_group_union(m); + + m.attr("__version__") = "version field"; +} diff --git a/src/core_ffi/py_ffi/wrapper/cases.cc b/src/core_ffi/py_ffi/wrapper/cases.cc index f82f8c7..065bc69 100644 --- a/src/core_ffi/py_ffi/wrapper/cases.cc +++ b/src/core_ffi/py_ffi/wrapper/cases.cc @@ -1,7 +1,7 @@ #include #include -#include "include/py_cases.h" +#include "py_ffi/cases.h" namespace py = pybind11; diff --git a/src/core_ffi/py_ffi/wrapper/common_codec.cc b/src/core_ffi/py_ffi/wrapper/common_codec.cc index e7e5015..21489ba 100644 --- a/src/core_ffi/py_ffi/wrapper/common_codec.cc +++ b/src/core_ffi/py_ffi/wrapper/common_codec.cc @@ -1,7 +1,7 @@ #include -#include "include/py_exception.h" -#include "include/py_common_code.h" +#include "exception.h" +#include "py_ffi/common_code.h" using namespace klotski::ffi; diff --git a/src/core_ffi/py_ffi/wrapper/group.cc b/src/core_ffi/py_ffi/wrapper/group.cc index 4e17632..7e64897 100644 --- a/src/core_ffi/py_ffi/wrapper/group.cc +++ b/src/core_ffi/py_ffi/wrapper/group.cc @@ -1,4 +1,4 @@ -#include "include/py_group.h" +#include "py_ffi/group.h" using namespace klotski::ffi; diff --git a/src/core_ffi/py_ffi/wrapper/group_union.cc b/src/core_ffi/py_ffi/wrapper/group_union.cc index f520397..a13ab22 100644 --- a/src/core_ffi/py_ffi/wrapper/group_union.cc +++ b/src/core_ffi/py_ffi/wrapper/group_union.cc @@ -1,6 +1,5 @@ -#include "include/py_group.h" - -#include "include/py_exception.h" +#include "exception.h" +#include "py_ffi/group.h" using klotski::ffi::PyExc_GroupError; diff --git a/src/core_ffi/py_ffi/wrapper/short_code.cc b/src/core_ffi/py_ffi/wrapper/short_code.cc index f95d540..2e85141 100644 --- a/src/core_ffi/py_ffi/wrapper/short_code.cc +++ b/src/core_ffi/py_ffi/wrapper/short_code.cc @@ -1,7 +1,7 @@ #include -#include "include/py_exception.h" -#include "include/py_short_code.h" +#include "exception.h" +#include "py_ffi/short_code.h" using namespace klotski::ffi;