Browse Source

update: adapt core changes

legacy
Dnomd343 2 months ago
parent
commit
4dd1d0ef67
  1. 8
      src/core_ffi/CMakeLists.txt
  2. 14
      src/core_ffi/py_ffi/binder.cc
  3. 4
      src/core_ffi/py_ffi/include/py_cases.h
  4. 11
      src/core_ffi/py_ffi/include/py_common_code.h
  5. 14
      src/core_ffi/py_ffi/include/py_exception.h
  6. 35
      src/core_ffi/py_ffi/include/py_exps.h
  7. 10
      src/core_ffi/py_ffi/include/py_group.h
  8. 13
      src/core_ffi/py_ffi/include/py_short_code.h
  9. 2
      src/core_ffi/py_ffi/wrapper/common_codec.cc
  10. 0
      src/core_ffi/py_ffi/wrapper/group.cc
  11. 16
      src/core_ffi/py_ffi/wrapper/group_union.cc

8
src/core_ffi/CMakeLists.txt

@ -13,14 +13,14 @@ endif()
if (KLSK_PYTHON_FFI) if (KLSK_PYTHON_FFI)
pybind11_add_module(klotski_py pybind11_add_module(klotski_py
py_ffi/binder.cc py_ffi/binder.cc
py_ffi/binder/cases.cc
py_ffi/wrapper/short_code.cc py_ffi/wrapper/short_code.cc
py_ffi/wrapper/common_codec.cc py_ffi/wrapper/common_codec.cc
py_ffi/wrapper/cases.cc py_ffi/wrapper/cases.cc
py_ffi/wrapper/py_group_union.cc py_ffi/wrapper/group_union.cc
py_ffi/wrapper/py_group.cc py_ffi/wrapper/group.cc
py_ffi/binder/cases.cc
) )
target_include_directories(klotski_py PRIVATE py_ffi/include)
target_include_directories(klotski_py PRIVATE py_ffi) target_include_directories(klotski_py PRIVATE py_ffi)
target_link_libraries(klotski_py PRIVATE klotski::core) target_link_libraries(klotski_py PRIVATE klotski::core)
set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski) set_target_properties(klotski_py PROPERTIES OUTPUT_NAME klotski)

14
src/core_ffi/py_ffi/binder.cc

@ -2,13 +2,11 @@
#include <pybind11/operators.h> #include <pybind11/operators.h>
#include <pybind11/stl.h> #include <pybind11/stl.h>
#include "py_exps.h" #include "include/py_common_code.h"
#include "py_common_code.h" #include "include/py_short_code.h"
#include "py_short_code.h" #include "include/py_cases.h"
#include "py_cases.h" #include "include/py_group.h"
#include "py_group.h" #include "include/py_exception.h"
#include "py_exception.h"
namespace py = pybind11; namespace py = pybind11;
@ -86,7 +84,7 @@ void bind_short_code(const py::module_ &m) {
#include "all_cases/all_cases.h" #include "all_cases/all_cases.h"
static PyCases group_demo() { 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()); auto cases = PyCases::from(group_union.cases());
return cases; return cases;
} }

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

@ -3,9 +3,9 @@
#pragma once #pragma once
#include <variant> #include <variant>
#include <ranges/ranges.h>
#include "py_common_code.h" #include "py_common_code.h"
#include "ranges/ranges.h"
namespace klotski::ffi { 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; [[nodiscard]] const RangesUnion& data_ref() const noexcept;
/// Stores actual cases data or its references. /// Stores actual cases data or its references.

11
src/core_ffi/py_ffi/include/py_common_code.h

@ -2,8 +2,9 @@
#pragma once #pragma once
#include <common_code/common_code.h>
#include "py_short_code.h" #include "py_short_code.h"
#include "common_code/common_code.h"
namespace klotski::ffi { namespace klotski::ffi {
@ -83,15 +84,11 @@ constexpr auto operator<=>(const PyCommonCode &lhs, const PyCommonCode &rhs) {
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
namespace std { template <>
struct ::std::hash<klotski::ffi::PyCommonCode> {
template<>
struct std::hash<klotski::ffi::PyCommonCode> {
size_t operator()(const klotski::ffi::PyCommonCode &common_code) const noexcept { size_t operator()(const klotski::ffi::PyCommonCode &common_code) const noexcept {
return std::hash<uint64_t>{}(common_code.value()); return std::hash<uint64_t>{}(common_code.value());
} }
}; };
} // namespace std
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //

14
src/core_ffi/py_ffi/include/py_exception.h

@ -21,4 +21,18 @@ private:
std::string msg_; 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 } // namespace klotski::ffi

35
src/core_ffi/py_ffi/include/py_exps.h

@ -1,35 +0,0 @@
#pragma once
#include <exception>
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

10
src/core_ffi/py_ffi/include/py_group.h

@ -4,17 +4,15 @@
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
#include <group/group.h>
#include "group/group.h"
#include "py_cases.h" #include "py_cases.h"
#include "py_common_code.h" #include "py_common_code.h"
namespace klotski::ffi { namespace klotski::ffi {
using klotski::cases::Group; using group::Group;
using klotski::cases::GroupUnion; using group::GroupUnion;
class PyGroup { class PyGroup {
public: public:
@ -68,4 +66,4 @@ private:
GroupUnion group_union_; GroupUnion group_union_;
}; };
} // namespace klotski } // namespace klotski::ffi

13
src/core_ffi/py_ffi/include/py_short_code.h

@ -2,8 +2,9 @@
#pragma once #pragma once
#include <short_code/short_code.h>
#include "py_common_code.h" #include "py_common_code.h"
#include "short_code/short_code.h"
namespace klotski::ffi { namespace klotski::ffi {
@ -53,7 +54,7 @@ public:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Build conversion index for ShortCode. /// 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); ShortCode::speed_up(fast_mode);
} }
@ -85,15 +86,11 @@ constexpr auto operator<=>(const PyShortCode &lhs, const PyShortCode &rhs) {
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //
namespace std { template <>
struct ::std::hash<klotski::ffi::PyShortCode> {
template<>
struct std::hash<klotski::ffi::PyShortCode> {
size_t operator()(const klotski::ffi::PyShortCode &short_code) const noexcept { size_t operator()(const klotski::ffi::PyShortCode &short_code) const noexcept {
return std::hash<uint32_t>{}(short_code.value()); return std::hash<uint32_t>{}(short_code.value());
} }
}; };
}
// ----------------------------------------------------------------------------------------- // // ----------------------------------------------------------------------------------------- //

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

@ -11,7 +11,7 @@ uint64_t PyCommonCode::value() const noexcept {
return code_.unwrap(); 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); return code_.to_string(shorten);
} }

0
src/core_ffi/py_ffi/wrapper/py_group.cc → src/core_ffi/py_ffi/wrapper/group.cc

16
src/core_ffi/py_ffi/wrapper/py_group_union.cc → src/core_ffi/py_ffi/wrapper/group_union.cc

@ -1,26 +1,26 @@
#include "include/py_group.h" #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; using klotski::ffi::PyGroupUnion;
static klotski::cases::GroupUnion convert(uint8_t type_id) { static klotski::group::GroupUnion convert(uint8_t type_id) {
auto group_union = klotski::cases::GroupUnion::create(type_id); auto group_union = klotski::group::GroupUnion::create(type_id);
if (!group_union.has_value()) { 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(); return group_union.value();
} }
PyGroupUnion::PyGroupUnion(uint8_t type_id) : PyGroupUnion::PyGroupUnion(const uint8_t type_id) :
group_union_(convert(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<ShortCode>(short_code))) {} group_union_(GroupUnion::from_short_code(std::bit_cast<ShortCode>(short_code))) {}
PyGroupUnion::PyGroupUnion(PyCommonCode common_code) : PyGroupUnion::PyGroupUnion(const PyCommonCode common_code) :
group_union_(GroupUnion::from_common_code(std::bit_cast<CommonCode>(common_code))) {} group_union_(GroupUnion::from_common_code(std::bit_cast<CommonCode>(common_code))) {}
uint32_t PyGroupUnion::value() const { uint32_t PyGroupUnion::value() const {
Loading…
Cancel
Save