diff --git a/src/core_ffi/CMakeLists.txt b/src/core_ffi/CMakeLists.txt index c6b8896..df93ee1 100644 --- a/src/core_ffi/CMakeLists.txt +++ b/src/core_ffi/CMakeLists.txt @@ -13,14 +13,14 @@ 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/py_group_union.cc - py_ffi/wrapper/py_group.cc - py_ffi/binder/cases.cc + py_ffi/wrapper/group_union.cc + py_ffi/wrapper/group.cc ) - target_include_directories(klotski_py PRIVATE py_ffi/include) 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) diff --git a/src/core_ffi/py_ffi/binder.cc b/src/core_ffi/py_ffi/binder.cc index d6d5864..847c5d2 100644 --- a/src/core_ffi/py_ffi/binder.cc +++ b/src/core_ffi/py_ffi/binder.cc @@ -2,13 +2,11 @@ #include #include -#include "py_exps.h" -#include "py_common_code.h" -#include "py_short_code.h" -#include "py_cases.h" -#include "py_group.h" - -#include "py_exception.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; @@ -86,7 +84,7 @@ void bind_short_code(const py::module_ &m) { #include "all_cases/all_cases.h" static PyCases group_demo() { - auto group_union = klotski::cases::GroupUnion::unsafe_create(169); + auto group_union = klotski::group::GroupUnion::unsafe_create(169); auto cases = PyCases::from(group_union.cases()); return cases; } diff --git a/src/core_ffi/py_ffi/include/py_cases.h b/src/core_ffi/py_ffi/include/py_cases.h index 1f4ee6d..0d8a171 100644 --- a/src/core_ffi/py_ffi/include/py_cases.h +++ b/src/core_ffi/py_ffi/include/py_cases.h @@ -3,9 +3,9 @@ #pragma once #include +#include #include "py_common_code.h" -#include "ranges/ranges.h" namespace klotski::ffi { @@ -64,7 +64,7 @@ private: // ------------------------------------------------------------------------------------- // - /// Get const reference of the cases data. + /// Get const reference of cases data. [[nodiscard]] const RangesUnion& data_ref() const noexcept; /// Stores actual cases data or its references. diff --git a/src/core_ffi/py_ffi/include/py_common_code.h b/src/core_ffi/py_ffi/include/py_common_code.h index 57226d7..fa3870a 100644 --- a/src/core_ffi/py_ffi/include/py_common_code.h +++ b/src/core_ffi/py_ffi/include/py_common_code.h @@ -2,8 +2,9 @@ #pragma once +#include + #include "py_short_code.h" -#include "common_code/common_code.h" namespace klotski::ffi { @@ -83,15 +84,11 @@ constexpr auto operator<=>(const PyCommonCode &lhs, const PyCommonCode &rhs) { // ----------------------------------------------------------------------------------------- // -namespace std { - -template<> -struct std::hash { +template <> +struct ::std::hash { size_t operator()(const klotski::ffi::PyCommonCode &common_code) const noexcept { return std::hash{}(common_code.value()); } }; -} // namespace std - // ----------------------------------------------------------------------------------------- // diff --git a/src/core_ffi/py_ffi/include/py_exception.h b/src/core_ffi/py_ffi/include/py_exception.h index 92d2c05..0ba2169 100644 --- a/src/core_ffi/py_ffi/include/py_exception.h +++ b/src/core_ffi/py_ffi/include/py_exception.h @@ -21,4 +21,18 @@ private: std::string msg_; }; +class PyExc_GroupError final : std::exception { +public: + explicit PyExc_GroupError(const std::string_view &msg) : msg_(msg) {} + + ~PyExc_GroupError() override = default; + + [[nodiscard]] const char* what() const noexcept override { + return msg_.c_str(); + } + +private: + std::string msg_; +}; + } // namespace klotski::ffi diff --git a/src/core_ffi/py_ffi/include/py_exps.h b/src/core_ffi/py_ffi/include/py_exps.h deleted file mode 100644 index 07efe5f..0000000 --- a/src/core_ffi/py_ffi/include/py_exps.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include - -namespace klotski::ffi { - -//class PyCodecExp final : std::exception { -//public: -// explicit PyCodecExp(const std::string_view &msg) : msg_(msg) {} -// -// ~PyCodecExp() override = default; -// -// [[nodiscard]] const char* what() const noexcept override { -// return msg_.c_str(); -// } -// -//private: -// std::string msg_; -//}; - -class PyGroupExp final : std::exception { -public: - explicit PyGroupExp(const std::string_view &msg) : msg_(msg) {} - - ~PyGroupExp() override = default; - - [[nodiscard]] const char* what() const noexcept override { - return msg_.c_str(); - } - -private: - std::string msg_; -}; - -} // namespace klotski::ffi diff --git a/src/core_ffi/py_ffi/include/py_group.h b/src/core_ffi/py_ffi/include/py_group.h index 455f2a6..e4db2d8 100644 --- a/src/core_ffi/py_ffi/include/py_group.h +++ b/src/core_ffi/py_ffi/include/py_group.h @@ -4,17 +4,15 @@ #include #include - -#include "group/group.h" +#include #include "py_cases.h" - #include "py_common_code.h" namespace klotski::ffi { -using klotski::cases::Group; -using klotski::cases::GroupUnion; +using group::Group; +using group::GroupUnion; class PyGroup { public: @@ -68,4 +66,4 @@ private: GroupUnion group_union_; }; -} // namespace klotski +} // namespace klotski::ffi diff --git a/src/core_ffi/py_ffi/include/py_short_code.h b/src/core_ffi/py_ffi/include/py_short_code.h index fa157e1..cedb7fe 100644 --- a/src/core_ffi/py_ffi/include/py_short_code.h +++ b/src/core_ffi/py_ffi/include/py_short_code.h @@ -2,8 +2,9 @@ #pragma once +#include + #include "py_common_code.h" -#include "short_code/short_code.h" namespace klotski::ffi { @@ -53,7 +54,7 @@ public: // ------------------------------------------------------------------------------------- // /// Build conversion index for ShortCode. - static void speed_up(bool fast_mode) { // TODO: move to `SpeedUp` + static void speed_up(const bool fast_mode) { // TODO: move to `SpeedUp` ShortCode::speed_up(fast_mode); } @@ -85,15 +86,11 @@ constexpr auto operator<=>(const PyShortCode &lhs, const PyShortCode &rhs) { // ----------------------------------------------------------------------------------------- // -namespace std { - -template<> -struct std::hash { +template <> +struct ::std::hash { size_t operator()(const klotski::ffi::PyShortCode &short_code) const noexcept { return std::hash{}(short_code.value()); } }; -} - // ----------------------------------------------------------------------------------------- // diff --git a/src/core_ffi/py_ffi/wrapper/common_codec.cc b/src/core_ffi/py_ffi/wrapper/common_codec.cc index 3207e77..e7e5015 100644 --- a/src/core_ffi/py_ffi/wrapper/common_codec.cc +++ b/src/core_ffi/py_ffi/wrapper/common_codec.cc @@ -11,7 +11,7 @@ uint64_t PyCommonCode::value() const noexcept { return code_.unwrap(); } -std::string PyCommonCode::string(bool shorten) const noexcept { +std::string PyCommonCode::string(const bool shorten) const noexcept { return code_.to_string(shorten); } diff --git a/src/core_ffi/py_ffi/wrapper/py_group.cc b/src/core_ffi/py_ffi/wrapper/group.cc similarity index 100% rename from src/core_ffi/py_ffi/wrapper/py_group.cc rename to src/core_ffi/py_ffi/wrapper/group.cc diff --git a/src/core_ffi/py_ffi/wrapper/py_group_union.cc b/src/core_ffi/py_ffi/wrapper/group_union.cc similarity index 72% rename from src/core_ffi/py_ffi/wrapper/py_group_union.cc rename to src/core_ffi/py_ffi/wrapper/group_union.cc index 48b4a41..f520397 100644 --- a/src/core_ffi/py_ffi/wrapper/py_group_union.cc +++ b/src/core_ffi/py_ffi/wrapper/group_union.cc @@ -1,26 +1,26 @@ #include "include/py_group.h" -#include "include/py_exps.h" +#include "include/py_exception.h" -using klotski::ffi::PyGroupExp; +using klotski::ffi::PyExc_GroupError; using klotski::ffi::PyGroupUnion; -static klotski::cases::GroupUnion convert(uint8_t type_id) { - auto group_union = klotski::cases::GroupUnion::create(type_id); +static klotski::group::GroupUnion convert(uint8_t type_id) { + auto group_union = klotski::group::GroupUnion::create(type_id); if (!group_union.has_value()) { - throw PyGroupExp(std::format("invalid type id -> {}", type_id)); + throw PyExc_GroupError(std::format("invalid type id -> {}", type_id)); } return group_union.value(); } -PyGroupUnion::PyGroupUnion(uint8_t type_id) : +PyGroupUnion::PyGroupUnion(const uint8_t type_id) : group_union_(convert(type_id)) {} -PyGroupUnion::PyGroupUnion(PyShortCode short_code) : +PyGroupUnion::PyGroupUnion(const PyShortCode short_code) : group_union_(GroupUnion::from_short_code(std::bit_cast(short_code))) {} -PyGroupUnion::PyGroupUnion(PyCommonCode common_code) : +PyGroupUnion::PyGroupUnion(const PyCommonCode common_code) : group_union_(GroupUnion::from_common_code(std::bit_cast(common_code))) {} uint32_t PyGroupUnion::value() const {