mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 months ago
13 changed files with 261 additions and 192 deletions
@ -1,135 +0,0 @@ |
|||
#pragma once |
|||
|
|||
#include "short_code/short_code.h" |
|||
#include "common_code/common_code.h" |
|||
|
|||
namespace klotski::ffi { |
|||
|
|||
using codec::ShortCode; |
|||
using codec::CommonCode; |
|||
|
|||
class PyCommonCode; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
class PyShortCode { |
|||
public: |
|||
explicit PyShortCode(uint32_t code); |
|||
explicit PyShortCode(PyCommonCode code); |
|||
explicit PyShortCode(std::string_view code); |
|||
|
|||
/// Get original value.
|
|||
[[nodiscard]] uint32_t value() const; |
|||
|
|||
/// Convert ShortCode to CommonCode.
|
|||
[[nodiscard]] PyCommonCode common_code() const; |
|||
|
|||
/// Verify ShortCode in u32 form.
|
|||
static bool check(uint32_t code); |
|||
|
|||
/// Verify ShortCode in string form.
|
|||
static bool check(std::string_view code); |
|||
|
|||
/// Build conversion index for ShortCode.
|
|||
static void speed_up(bool fast_mode); |
|||
|
|||
/// Wrapper of `__str__` method in Python.
|
|||
static std::string str(PyShortCode code); |
|||
|
|||
/// Wrapper of `__repr__` method in Python.
|
|||
static std::string repr(PyShortCode code); |
|||
|
|||
private: |
|||
ShortCode code_; |
|||
}; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
constexpr auto operator==(const PyShortCode &lhs, const uint32_t rhs) { |
|||
return lhs.value() == rhs; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyShortCode &lhs, const uint32_t rhs) { |
|||
return lhs.value() <=> rhs; |
|||
} |
|||
|
|||
constexpr auto operator==(const PyShortCode &lhs, const PyShortCode &rhs) { |
|||
return lhs.value() == rhs.value(); |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyShortCode &lhs, const PyShortCode &rhs) { |
|||
return lhs.value() <=> rhs.value(); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
class PyCommonCode { |
|||
public: |
|||
explicit PyCommonCode(uint64_t code); |
|||
explicit PyCommonCode(PyShortCode code); |
|||
explicit PyCommonCode(std::string_view code); |
|||
|
|||
/// Get original value.
|
|||
[[nodiscard]] uint64_t value() const; |
|||
|
|||
/// Convert as shorten string form.
|
|||
[[nodiscard]] std::string string() const; |
|||
|
|||
/// Convert CommonCode to ShortCode.
|
|||
[[nodiscard]] PyShortCode short_code() const; |
|||
|
|||
/// Verify CommonCode in u64 form.
|
|||
static bool check(uint64_t code); |
|||
|
|||
/// Verify CommonCode in string form.
|
|||
static bool check(std::string_view code); |
|||
|
|||
/// Wrapper of `__str__` method in Python.
|
|||
static std::string str(PyCommonCode code); |
|||
|
|||
/// Wrapper of `__repr__` method in Python.
|
|||
static std::string repr(PyCommonCode code); |
|||
|
|||
private: |
|||
CommonCode code_; |
|||
}; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
constexpr auto operator==(const PyCommonCode &lhs, const uint64_t rhs) { |
|||
return lhs.value() == rhs; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyCommonCode &lhs, const uint64_t rhs) { |
|||
return lhs.value() <=> rhs; |
|||
} |
|||
|
|||
constexpr auto operator==(const PyCommonCode &lhs, const PyCommonCode &rhs) { |
|||
return lhs.value() == rhs.value(); |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyCommonCode &lhs, const PyCommonCode &rhs) { |
|||
return lhs.value() <=> rhs.value(); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace klotski::ffi
|
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
template<> |
|||
struct std::hash<klotski::ffi::PyShortCode> { |
|||
size_t operator()(const klotski::ffi::PyShortCode &short_code) const noexcept { |
|||
return std::hash<uint32_t>()(short_code.value()); |
|||
} |
|||
}; |
|||
|
|||
template<> |
|||
struct std::hash<klotski::ffi::PyCommonCode> { |
|||
size_t operator()(const klotski::ffi::PyCommonCode &common_code) const noexcept { |
|||
return std::hash<uint64_t>()(common_code.value()); |
|||
} |
|||
}; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
@ -0,0 +1,97 @@ |
|||
/// Klotski Engine Python FFI by Dnomd343 @2024
|
|||
|
|||
#pragma once |
|||
|
|||
#include "py_short_code.h" |
|||
#include "common_code/common_code.h" |
|||
|
|||
namespace klotski::ffi { |
|||
|
|||
using codec::CommonCode; |
|||
|
|||
class PyShortCode; |
|||
|
|||
class PyCommonCode { |
|||
public: |
|||
PyCommonCode() = delete; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Construct from origin u64 value.
|
|||
explicit PyCommonCode(uint64_t code); |
|||
|
|||
/// Construct from origin string form.
|
|||
explicit PyCommonCode(std::string_view code); |
|||
|
|||
/// Construct from PyShortCode object.
|
|||
explicit PyCommonCode(PyShortCode code) noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Verify CommonCode in u64 form.
|
|||
static bool check(uint64_t code) noexcept; |
|||
|
|||
/// Verify CommonCode in string form.
|
|||
static bool check(std::string_view code) noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Get original value.
|
|||
[[nodiscard]] uint64_t value() const noexcept; |
|||
|
|||
/// Convert CommonCode as ShortCode.
|
|||
[[nodiscard]] PyShortCode short_code() const noexcept; |
|||
|
|||
/// Convert CommonCode as string form.
|
|||
[[nodiscard]] std::string string(bool shorten) const noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Wrapper of `__str__` method in Python.
|
|||
static std::string str(PyCommonCode code) noexcept; |
|||
|
|||
/// Wrapper of `__repr__` method in Python.
|
|||
static std::string repr(PyCommonCode code) noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
private: |
|||
CommonCode code_; |
|||
}; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
constexpr auto operator==(const PyCommonCode &lhs, const uint64_t rhs) { |
|||
return lhs.value() == rhs; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyCommonCode &lhs, const uint64_t rhs) { |
|||
return lhs.value() <=> rhs; |
|||
} |
|||
|
|||
constexpr auto operator==(const PyCommonCode &lhs, const PyCommonCode &rhs) { |
|||
return lhs.value() == rhs.value(); |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyCommonCode &lhs, const PyCommonCode &rhs) { |
|||
return lhs.value() <=> rhs.value(); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace klotski::ffi
|
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
namespace std { |
|||
|
|||
template<> |
|||
struct std::hash<klotski::ffi::PyCommonCode> { |
|||
size_t operator()(const klotski::ffi::PyCommonCode &common_code) const noexcept { |
|||
return std::hash<uint64_t>{}(common_code.value()); |
|||
} |
|||
}; |
|||
|
|||
} // namespace std
|
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
@ -0,0 +1,24 @@ |
|||
/// Klotski Engine Python FFI by Dnomd343 @2024
|
|||
|
|||
#pragma once |
|||
|
|||
#include <string> |
|||
#include <exception> |
|||
|
|||
namespace klotski::ffi { |
|||
|
|||
class PyExc_CodecError final : std::exception { |
|||
public: |
|||
explicit PyExc_CodecError(const std::string_view &msg) : msg_(msg) {} |
|||
|
|||
~PyExc_CodecError() override = default; |
|||
|
|||
[[nodiscard]] const char* what() const noexcept override { |
|||
return msg_.c_str(); |
|||
} |
|||
|
|||
private: |
|||
std::string msg_; |
|||
}; |
|||
|
|||
} // namespace klotski::ffi
|
@ -0,0 +1,99 @@ |
|||
/// Klotski Engine Python FFI by Dnomd343 @2024
|
|||
|
|||
#pragma once |
|||
|
|||
#include "py_common_code.h" |
|||
#include "short_code/short_code.h" |
|||
|
|||
namespace klotski::ffi { |
|||
|
|||
using codec::ShortCode; |
|||
|
|||
class PyCommonCode; |
|||
|
|||
class PyShortCode { |
|||
public: |
|||
PyShortCode() = delete; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Construct from origin u32 value.
|
|||
explicit PyShortCode(uint32_t code); |
|||
|
|||
/// Construct from origin string form.
|
|||
explicit PyShortCode(std::string_view code); |
|||
|
|||
/// Construct from PyCommonCode object.
|
|||
explicit PyShortCode(PyCommonCode code) noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Verify ShortCode in u32 form.
|
|||
static bool check(uint32_t code) noexcept; |
|||
|
|||
/// Verify ShortCode in string form.
|
|||
static bool check(std::string_view code) noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Get original value.
|
|||
[[nodiscard]] uint32_t value() const noexcept; |
|||
|
|||
/// Convert ShortCode as CommonCode.
|
|||
[[nodiscard]] PyCommonCode common_code() const noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Wrapper of `__str__` method in Python.
|
|||
static std::string str(PyShortCode code) noexcept; |
|||
|
|||
/// Wrapper of `__repr__` method in Python.
|
|||
static std::string repr(PyShortCode code) noexcept; |
|||
|
|||
// ------------------------------------------------------------------------------------- //
|
|||
|
|||
/// Build conversion index for ShortCode.
|
|||
static void speed_up(bool fast_mode) { // TODO: move to `SpeedUp`
|
|||
ShortCode::speed_up(fast_mode); |
|||
} |
|||
|
|||
private: |
|||
ShortCode code_; |
|||
}; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
constexpr auto operator==(const PyShortCode &lhs, const uint32_t rhs) { |
|||
return lhs.value() == rhs; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyShortCode &lhs, const uint32_t rhs) { |
|||
return lhs.value() <=> rhs; |
|||
} |
|||
|
|||
constexpr auto operator==(const PyShortCode &lhs, const PyShortCode &rhs) { |
|||
return lhs.value() == rhs.value(); |
|||
} |
|||
|
|||
constexpr auto operator<=>(const PyShortCode &lhs, const PyShortCode &rhs) { |
|||
return lhs.value() <=> rhs.value(); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace klotski::ffi
|
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
namespace std { |
|||
|
|||
template<> |
|||
struct std::hash<klotski::ffi::PyShortCode> { |
|||
size_t operator()(const klotski::ffi::PyShortCode &short_code) const noexcept { |
|||
return std::hash<uint32_t>{}(short_code.value()); |
|||
} |
|||
}; |
|||
|
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
Loading…
Reference in new issue