Browse Source

feat: more interfaces of klotski Layout

legacy
Dnomd343 3 days ago
parent
commit
814778af0f
  1. 1
      src/core_ffi/python_ffi/binder/fast_cal.cc
  2. 1
      src/core_ffi/python_ffi/binder/group.cc
  3. 1
      src/core_ffi/python_ffi/binder/klsk_cases.cc
  4. 19
      src/core_ffi/python_ffi/binder/klsk_code.cc
  5. 4
      src/core_ffi/python_ffi/include/py_ffi/cases.h
  6. 5
      src/core_ffi/python_ffi/include/py_ffi/group.h
  7. 57
      src/core_ffi/python_ffi/include/py_ffi/layout.h
  8. 4
      src/core_ffi/python_ffi/packing/src/klotski/__init__.py
  9. 1
      src/core_ffi/python_ffi/wrapper/cases.cc
  10. 1
      src/core_ffi/python_ffi/wrapper/group_union.cc

1
src/core_ffi/python_ffi/binder/fast_cal.cc

@ -1,3 +1,4 @@
#include <pybind11/stl.h>
#include <py_ffi/fast_cal.h>
#include "binder.h"

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

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

19
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_<PyToward>(mod, "Toward")
.value("A", PyToward::A)
.value("B", PyToward::B)
.value("C", PyToward::C)
.value("D", PyToward::D);
py::class_<PyLayout>(mod, "Layout")
.def(py::init<uint64_t>())
.def(py::init<PyShortCode>())
@ -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<uint64_t>(&PyLayout::check))
.def_static("check", py::overload_cast<std::string_view>(&PyLayout::check));

4
src/core_ffi/python_ffi/include/py_ffi/cases.h

@ -5,12 +5,12 @@
#include <variant>
#include <ranges/ranges.h>
#include "py_ffi/layout.h"
namespace klotski::ffi {
using cases::RangesUnion;
class PyLayout;
class PyCasesIter {
public:
/// Construct from RangesUnion reference.

5
src/core_ffi/python_ffi/include/py_ffi/group.h

@ -7,7 +7,6 @@
#include <group/group.h>
#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("<klotski.Group '{}'>", group.to_string()); // TODO: maybe add size info
}
private:
Group group_;
};

57
src/core_ffi/python_ffi/include/py_ffi/layout.h

@ -2,9 +2,11 @@
#pragma once
#include <mover/mover.h>
#include <group/group.h>
#include <mover/mover.h> // TODO: move to `.cc` file
#include <common_code/common_code.h>
#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<PyGroup>(group);
}
[[nodiscard]] uint32_t case_id() const noexcept {
const auto info = group::GroupCases::obtain_info(code_);
return info.case_id();
}
[[nodiscard]] std::tuple<PyGroup, uint32_t> case_info() const noexcept {
const auto info = group::GroupCases::obtain_info(code_);
return std::make_tuple(std::bit_cast<PyGroup>(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_;
};

4
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']

1
src/core_ffi/python_ffi/wrapper/cases.cc

@ -2,6 +2,7 @@
#include <pybind11/pybind11.h>
#include "py_ffi/cases.h"
#include "py_ffi/layout.h"
namespace py = pybind11;

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

Loading…
Cancel
Save