Browse Source

refactor: file structure of py_ffi

master
Dnomd343 2 weeks ago
parent
commit
b06a665041
  1. 14
      src/core_ffi/CMakeLists.txt
  2. 23
      src/core_ffi/py_ffi/CMakeLists.txt
  3. 143
      src/core_ffi/py_ffi/binder.cc
  4. 10
      src/core_ffi/py_ffi/binder/cases.cc
  5. 30
      src/core_ffi/py_ffi/binder/common_code.cc
  6. 17
      src/core_ffi/py_ffi/binder/group.cc
  7. 23
      src/core_ffi/py_ffi/binder/group_union.cc
  8. 28
      src/core_ffi/py_ffi/binder/short_code.cc
  9. 26
      src/core_ffi/py_ffi/include/binder.h
  10. 0
      src/core_ffi/py_ffi/include/exception.h
  11. 4
      src/core_ffi/py_ffi/include/py_ffi/cases.h
  12. 2
      src/core_ffi/py_ffi/include/py_ffi/common_code.h
  13. 4
      src/core_ffi/py_ffi/include/py_ffi/group.h
  14. 2
      src/core_ffi/py_ffi/include/py_ffi/short_code.h
  15. 52
      src/core_ffi/py_ffi/klotski.cc
  16. 2
      src/core_ffi/py_ffi/wrapper/cases.cc
  17. 4
      src/core_ffi/py_ffi/wrapper/common_codec.cc
  18. 2
      src/core_ffi/py_ffi/wrapper/group.cc
  19. 5
      src/core_ffi/py_ffi/wrapper/group_union.cc
  20. 4
      src/core_ffi/py_ffi/wrapper/short_code.cc

14
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)

23
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)

143
src/core_ffi/py_ffi/binder.cc

@ -1,143 +0,0 @@
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
#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_<PyCommonCode>(m, "CommonCode")
.def(py::init<uint64_t>())
.def(py::init<PyShortCode>())
.def(py::init<std::string_view>())
.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<bool (*)(uint64_t)>(&PyCommonCode::check))
.def_static("check", static_cast<bool (*)(std::string_view)>(&PyCommonCode::check));
}
void bind_short_code(const py::module_ &m) {
py::class_<PyShortCode>(m, "ShortCode")
.def(py::init<uint32_t>())
.def(py::init<PyCommonCode>())
.def(py::init<std::string_view>())
.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<bool (*)(uint32_t)>(&PyShortCode::check))
.def_static("check", static_cast<bool (*)(std::string_view)>(&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<PyCodecExp>(m, "CodecExp", PyExc_ValueError);
py::register_exception<PyExc_CodecError>(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_<PyGroupUnion>(m, "GroupUnion")
.def(py::init<uint8_t>())
.def(py::init<PyShortCode>())
.def(py::init<PyCommonCode>())
// .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_<PyGroup>(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";
}

10
src/core_ffi/py_ffi/binder/cases.cc

@ -1,12 +1,4 @@
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#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_<PyCases>(m, "Cases")

30
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_<PyCommonCode>(m, "CommonCode")
.def(py::init<uint64_t>())
.def(py::init<PyShortCode>())
.def(py::init<std::string_view>())
.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<bool (*)(uint64_t)>(&PyCommonCode::check))
.def_static("check", static_cast<bool (*)(std::string_view)>(&PyCommonCode::check));
}

17
src/core_ffi/py_ffi/binder/group.cc

@ -0,0 +1,17 @@
#include "binder.h"
void bind_group(const py::module_ &m) {
py::class_<PyGroup>(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);
}

23
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_<PyGroupUnion>(m, "GroupUnion")
.def(py::init<uint8_t>())
.def(py::init<PyShortCode>())
.def(py::init<PyCommonCode>())
// .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);
}

28
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_<PyShortCode>(m, "ShortCode")
.def(py::init<uint32_t>())
.def(py::init<PyCommonCode>())
.def(py::init<std::string_view>())
.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<bool (*)(uint32_t)>(&PyShortCode::check))
.def_static("check", static_cast<bool (*)(std::string_view)>(&PyShortCode::check))
.def_static("speed_up", &PyShortCode::speed_up, py::arg("fast_mode") = false);
}

26
src/core_ffi/py_ffi/include/binder.h

@ -0,0 +1,26 @@
#pragma once
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <pybind11/stl.h>
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);

0
src/core_ffi/py_ffi/include/py_exception.h → src/core_ffi/py_ffi/include/exception.h

4
src/core_ffi/py_ffi/include/py_cases.h → src/core_ffi/py_ffi/include/py_ffi/cases.h

@ -5,7 +5,7 @@
#include <variant>
#include <ranges/ranges.h>
#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.

2
src/core_ffi/py_ffi/include/py_common_code.h → src/core_ffi/py_ffi/include/py_ffi/common_code.h

@ -4,7 +4,7 @@
#include <common_code/common_code.h>
#include "py_short_code.h"
#include "py_ffi/short_code.h"
namespace klotski::ffi {

4
src/core_ffi/py_ffi/include/py_group.h → src/core_ffi/py_ffi/include/py_ffi/group.h

@ -6,8 +6,8 @@
#include <cstdint>
#include <group/group.h>
#include "py_cases.h"
#include "py_common_code.h"
#include "py_ffi/cases.h"
#include "py_ffi/common_code.h"
namespace klotski::ffi {

2
src/core_ffi/py_ffi/include/py_short_code.h → src/core_ffi/py_ffi/include/py_ffi/short_code.h

@ -4,7 +4,7 @@
#include <short_code/short_code.h>
#include "py_common_code.h"
#include "py_ffi/common_code.h"
namespace klotski::ffi {

52
src/core_ffi/py_ffi/klotski.cc

@ -0,0 +1,52 @@
#include <pybind11/pybind11.h>
// #include <pybind11/stl.h>
// #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<PyExc_GroupError>(m, "GroupError", PyExc_ValueError);
py::register_exception<PyExc_CodecError>(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";
}

2
src/core_ffi/py_ffi/wrapper/cases.cc

@ -1,7 +1,7 @@
#include <format>
#include <pybind11/pybind11.h>
#include "include/py_cases.h"
#include "py_ffi/cases.h"
namespace py = pybind11;

4
src/core_ffi/py_ffi/wrapper/common_codec.cc

@ -1,7 +1,7 @@
#include <format>
#include "include/py_exception.h"
#include "include/py_common_code.h"
#include "exception.h"
#include "py_ffi/common_code.h"
using namespace klotski::ffi;

2
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;

5
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;

4
src/core_ffi/py_ffi/wrapper/short_code.cc

@ -1,7 +1,7 @@
#include <format>
#include "include/py_exception.h"
#include "include/py_short_code.h"
#include "exception.h"
#include "py_ffi/short_code.h"
using namespace klotski::ffi;

Loading…
Cancel
Save