From 814778af0f24e40bf9f84bd43166bf665c8e0f9e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 22 Dec 2024 14:45:11 +0800 Subject: [PATCH] feat: more interfaces of klotski Layout --- src/core_ffi/python_ffi/binder/fast_cal.cc | 1 + src/core_ffi/python_ffi/binder/group.cc | 1 + src/core_ffi/python_ffi/binder/klsk_cases.cc | 1 + src/core_ffi/python_ffi/binder/klsk_code.cc | 19 ++++++- .../python_ffi/include/py_ffi/cases.h | 4 +- .../python_ffi/include/py_ffi/group.h | 5 +- .../python_ffi/include/py_ffi/layout.h | 57 ++++++++++++++++++- .../packing/src/klotski/__init__.py | 4 +- src/core_ffi/python_ffi/wrapper/cases.cc | 1 + .../python_ffi/wrapper/group_union.cc | 1 + 10 files changed, 87 insertions(+), 7 deletions(-) diff --git a/src/core_ffi/python_ffi/binder/fast_cal.cc b/src/core_ffi/python_ffi/binder/fast_cal.cc index 3afe1dd..24a5dfd 100644 --- a/src/core_ffi/python_ffi/binder/fast_cal.cc +++ b/src/core_ffi/python_ffi/binder/fast_cal.cc @@ -1,3 +1,4 @@ +#include #include #include "binder.h" diff --git a/src/core_ffi/python_ffi/binder/group.cc b/src/core_ffi/python_ffi/binder/group.cc index 3874b1b..24d239a 100644 --- a/src/core_ffi/python_ffi/binder/group.cc +++ b/src/core_ffi/python_ffi/binder/group.cc @@ -20,6 +20,7 @@ void bind_group(const py::module_ &m) { .def_property_readonly("pattern_id", &PyGroup::pattern_id) .def("__str__", &PyGroup::to_string) + .def("__repr__", &PyGroup::repr) .def("cases", &PyGroup::cases) .def("to_vertical_mirror", &PyGroup::to_vertical_mirror) diff --git a/src/core_ffi/python_ffi/binder/klsk_cases.cc b/src/core_ffi/python_ffi/binder/klsk_cases.cc index ebc3f07..14db198 100644 --- a/src/core_ffi/python_ffi/binder/klsk_cases.cc +++ b/src/core_ffi/python_ffi/binder/klsk_cases.cc @@ -2,6 +2,7 @@ #include "binder.h" #include "py_ffi/cases.h" +#include "py_ffi/layout.h" using klotski::ffi::PyCases; using klotski::ffi::PyCasesIter; diff --git a/src/core_ffi/python_ffi/binder/klsk_code.cc b/src/core_ffi/python_ffi/binder/klsk_code.cc index 4b3e1f5..a278c63 100644 --- a/src/core_ffi/python_ffi/binder/klsk_code.cc +++ b/src/core_ffi/python_ffi/binder/klsk_code.cc @@ -8,6 +8,7 @@ using klotski::ffi::PyBlock; using klotski::ffi::PyLayout; +using klotski::ffi::PyToward; using klotski::ffi::PySpeedUp; using klotski::ffi::PyShortCode; @@ -30,6 +31,12 @@ static void bind_layout(const py::module_ &mod) { .value("B_2x2", PyBlock::B_2x2) .value("FILL", PyBlock::FILL); + py::enum_(mod, "Toward") + .value("A", PyToward::A) + .value("B", PyToward::B) + .value("C", PyToward::C) + .value("D", PyToward::D); + py::class_(mod, "Layout") .def(py::init()) .def(py::init()) @@ -55,7 +62,17 @@ static void bind_layout(const py::module_ &mod) { .def("to_string", &PyLayout::string, py::arg("shorten") = false) .def_property_readonly("value", &PyLayout::value) - // TODO: add n_1x1 / n_1x2 / n_2x1 / ... + .def_property_readonly("n_1x1", &PyLayout::n_1x1) + .def_property_readonly("n_1x2", &PyLayout::n_1x2) + .def_property_readonly("n_2x1", &PyLayout::n_2x1) + .def_property_readonly("n_2x2", &PyLayout::n_2x2) // TODO: return `1` directly + + .def_property_readonly("type_id", &PyLayout::type_id) + .def_property_readonly("pattern_id", &PyLayout::pattern_id) + .def_property_readonly("case_id", &PyLayout::case_id) + .def_property_readonly("toward", &PyLayout::toward) + .def_property_readonly("group", &PyLayout::group_info) + .def_property_readonly("case_info", &PyLayout::case_info) .def_static("check", py::overload_cast(&PyLayout::check)) .def_static("check", py::overload_cast(&PyLayout::check)); diff --git a/src/core_ffi/python_ffi/include/py_ffi/cases.h b/src/core_ffi/python_ffi/include/py_ffi/cases.h index 63dbae0..a78eb21 100644 --- a/src/core_ffi/python_ffi/include/py_ffi/cases.h +++ b/src/core_ffi/python_ffi/include/py_ffi/cases.h @@ -5,12 +5,12 @@ #include #include -#include "py_ffi/layout.h" - namespace klotski::ffi { using cases::RangesUnion; +class PyLayout; + class PyCasesIter { public: /// Construct from RangesUnion reference. diff --git a/src/core_ffi/python_ffi/include/py_ffi/group.h b/src/core_ffi/python_ffi/include/py_ffi/group.h index d96dd86..5b18d63 100644 --- a/src/core_ffi/python_ffi/include/py_ffi/group.h +++ b/src/core_ffi/python_ffi/include/py_ffi/group.h @@ -7,7 +7,6 @@ #include #include "py_ffi/cases.h" -#include "py_ffi/layout.h" #include "py_ffi/short_code.h" namespace klotski::ffi { @@ -39,6 +38,10 @@ public: [[nodiscard]] PyGroup to_horizontal_mirror() const; + static std::string repr(const PyGroup group) noexcept { + return std::format("", group.to_string()); // TODO: maybe add size info + } + private: Group group_; }; diff --git a/src/core_ffi/python_ffi/include/py_ffi/layout.h b/src/core_ffi/python_ffi/include/py_ffi/layout.h index 9630c61..98b808a 100644 --- a/src/core_ffi/python_ffi/include/py_ffi/layout.h +++ b/src/core_ffi/python_ffi/include/py_ffi/layout.h @@ -2,9 +2,11 @@ #pragma once -#include +#include +#include // TODO: move to `.cc` file #include +#include "py_ffi/group.h" #include "py_ffi/short_code.h" // TODO: add `copy` and `pickle` support @@ -20,6 +22,13 @@ enum class PyBlock : uint8_t { FILL = 0b111, }; +enum class PyToward : uint8_t { // TODO: from `Group::Toward` directly + A = 0, + B = 1, + C = 2, + D = 3, +}; + class PyLayout { public: PyLayout() = delete; @@ -70,6 +79,52 @@ public: // ------------------------------------------------------------------------------------- // + [[nodiscard]] uint8_t type_id() const noexcept { + return group::GroupUnion::from_common_code(code_).unwrap(); + } + + [[nodiscard]] uint16_t pattern_id() const noexcept { + return group_info().pattern_id(); + } + + [[nodiscard]] PyToward toward() const noexcept { + const auto group = group::GroupCases::obtain_group(code_); + return (PyToward)group.toward(); + } + + [[nodiscard]] PyGroup group_info() const noexcept { + const auto group = group::GroupCases::obtain_group(code_); + return std::bit_cast(group); + } + + [[nodiscard]] uint32_t case_id() const noexcept { + const auto info = group::GroupCases::obtain_info(code_); + return info.case_id(); + } + + [[nodiscard]] std::tuple case_info() const noexcept { + const auto info = group::GroupCases::obtain_info(code_); + return std::make_tuple(std::bit_cast(info.group()), info.case_id()); + } + + [[nodiscard]] uint8_t n_1x1() const noexcept { + return std::get<2>(group::BLOCK_NUM[type_id()]); + } + + [[nodiscard]] uint8_t n_1x2() const noexcept { + return std::get<0>(group::BLOCK_NUM[type_id()]) - n_2x1(); + } + + [[nodiscard]] uint8_t n_2x1() const noexcept { + return std::get<1>(group::BLOCK_NUM[type_id()]); + } + + [[nodiscard]] uint8_t n_2x2() const noexcept { + return 1; + } + + // ------------------------------------------------------------------------------------- // + private: codec::CommonCode code_; }; diff --git a/src/core_ffi/python_ffi/packing/src/klotski/__init__.py b/src/core_ffi/python_ffi/packing/src/klotski/__init__.py index e7c8909..66995c8 100644 --- a/src/core_ffi/python_ffi/packing/src/klotski/__init__.py +++ b/src/core_ffi/python_ffi/packing/src/klotski/__init__.py @@ -1,3 +1,3 @@ -from ._klotski import Cases, CommonCode, FastCal, Group, GroupUnion, ShortCode +from ._klotski import Cases, Layout, FastCal, Group, GroupUnion, ShortCode, Toward, Block -__all__ = ["Cases", "CommonCode", "FastCal", "Group", "GroupUnion", "ShortCode"] +__all__ = ['Cases', 'Layout', 'FastCal', 'Group', 'GroupUnion', 'ShortCode', 'Toward', 'Block'] diff --git a/src/core_ffi/python_ffi/wrapper/cases.cc b/src/core_ffi/python_ffi/wrapper/cases.cc index f75b37f..3a8f260 100644 --- a/src/core_ffi/python_ffi/wrapper/cases.cc +++ b/src/core_ffi/python_ffi/wrapper/cases.cc @@ -2,6 +2,7 @@ #include #include "py_ffi/cases.h" +#include "py_ffi/layout.h" namespace py = pybind11; diff --git a/src/core_ffi/python_ffi/wrapper/group_union.cc b/src/core_ffi/python_ffi/wrapper/group_union.cc index eac93ba..ca57f25 100644 --- a/src/core_ffi/python_ffi/wrapper/group_union.cc +++ b/src/core_ffi/python_ffi/wrapper/group_union.cc @@ -1,5 +1,6 @@ #include "exception.h" #include "py_ffi/group.h" +#include "py_ffi/layout.h" using klotski::codec::ShortCode; using klotski::codec::CommonCode;