mirror of https://github.com/dnomd343/klotski.git
Dnomd343
6 months ago
9 changed files with 243 additions and 185 deletions
@ -1,70 +0,0 @@ |
|||
#pragma once |
|||
|
|||
namespace klotski { |
|||
namespace codec { |
|||
|
|||
/// Get the original 64-bit code.
|
|||
inline uint64_t RawCode::unwrap() const noexcept { |
|||
return code_; |
|||
} |
|||
|
|||
/// Implicit conversion to 64-bit code.
|
|||
inline RawCode::operator uint64_t() const noexcept { |
|||
return code_; |
|||
} |
|||
|
|||
/// Equality comparison between RawCode and numbers.
|
|||
inline bool operator==(RawCode r1, uint64_t r2) noexcept { |
|||
return r1.unwrap() == r2; |
|||
} |
|||
|
|||
/// RawCode equal comparison implement.
|
|||
inline bool operator==(RawCode r1, RawCode r2) noexcept { |
|||
return r1.unwrap() == r2.unwrap(); |
|||
} |
|||
|
|||
/// RawCode create without any check.
|
|||
inline RawCode RawCode::unsafe_create(uint64_t raw_code) noexcept { |
|||
return *reinterpret_cast<RawCode*>(&raw_code); // init directly
|
|||
} |
|||
|
|||
/// RawCode create with valid check.
|
|||
inline std::optional<RawCode> RawCode::create(uint64_t raw_code) noexcept { |
|||
if (!RawCode::check(raw_code)) { |
|||
return std::nullopt; // invalid raw code
|
|||
} |
|||
return RawCode::unsafe_create(raw_code); |
|||
} |
|||
|
|||
/// Calculate vertically symmetrical case.
|
|||
inline RawCode RawCode::to_vertical_mirror() const noexcept { |
|||
return RawCode::unsafe_create(get_vertical_mirror(code_)); |
|||
} |
|||
|
|||
/// Calculate horizontally symmetrical case.
|
|||
inline RawCode RawCode::to_horizontal_mirror() const noexcept { |
|||
return RawCode::unsafe_create(get_horizontal_mirror(code_)); |
|||
} |
|||
|
|||
/// Determine whether the case is vertically symmetrical.
|
|||
inline bool RawCode::is_vertical_mirror() const noexcept { |
|||
return check_vertical_mirror(code_); |
|||
} |
|||
|
|||
/// Determine whether the case is horizontally symmetrical.
|
|||
inline bool RawCode::is_horizontal_mirror() const noexcept { |
|||
return check_horizontal_mirror(code_); |
|||
} |
|||
|
|||
/// Determine whether two cases are vertically symmetrical to each other.
|
|||
inline bool RawCode::is_vertical_mirror(RawCode raw_code) const noexcept { |
|||
return raw_code.code_ == get_vertical_mirror(code_); |
|||
} |
|||
|
|||
/// Determine whether two cases are horizontally symmetrical to each other.
|
|||
inline bool RawCode::is_horizontal_mirror(RawCode raw_code) const noexcept { |
|||
return raw_code.code_ == get_horizontal_mirror(code_); |
|||
} |
|||
|
|||
} // namespace codec
|
|||
} // namespace klotski
|
@ -0,0 +1,76 @@ |
|||
#pragma once |
|||
|
|||
#include <bit> |
|||
|
|||
namespace klotski::codec { |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline uint64_t RawCode::unwrap() const { |
|||
return code_; |
|||
} |
|||
|
|||
inline RawCode::operator uint64_t() const { |
|||
return code_; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
constexpr auto operator==(const RawCode &lhs, const uint64_t rhs) { |
|||
return lhs.code_ == rhs; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const RawCode &lhs, const uint64_t rhs) { |
|||
return lhs.code_ <=> rhs; |
|||
} |
|||
|
|||
constexpr auto operator==(const RawCode &lhs, const RawCode &rhs) { |
|||
return lhs.code_ == rhs.code_; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const RawCode &lhs, const RawCode &rhs) { |
|||
return lhs.code_ <=> rhs.code_; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline RawCode RawCode::unsafe_create(const uint64_t raw_code) { |
|||
return std::bit_cast<RawCode>(raw_code); // init directly |
|||
} |
|||
|
|||
inline std::optional<RawCode> RawCode::create(const uint64_t raw_code) { |
|||
if (!check(raw_code)) { |
|||
return std::nullopt; // invalid raw code |
|||
} |
|||
return unsafe_create(raw_code); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline bool RawCode::is_vertical_mirror() const { |
|||
return check_vertical_mirror(code_); |
|||
} |
|||
|
|||
inline bool RawCode::is_horizontal_mirror() const { |
|||
return check_horizontal_mirror(code_); |
|||
} |
|||
|
|||
inline RawCode RawCode::to_vertical_mirror() const { |
|||
return unsafe_create(get_vertical_mirror(code_)); |
|||
} |
|||
|
|||
inline RawCode RawCode::to_horizontal_mirror() const { |
|||
return unsafe_create(get_horizontal_mirror(code_)); |
|||
} |
|||
|
|||
inline bool RawCode::is_vertical_mirror(const RawCode raw_code) const { |
|||
return raw_code.code_ == get_vertical_mirror(code_); |
|||
} |
|||
|
|||
inline bool RawCode::is_horizontal_mirror(const RawCode raw_code) const { |
|||
return raw_code.code_ == get_horizontal_mirror(code_); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
} // namespace klotski::codec |
@ -0,0 +1,40 @@ |
|||
#include "raw_code.h" |
|||
#include "common_code.h" |
|||
|
|||
namespace klotski::codec { |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
RawCode::RawCode(const CommonCode common_code) { |
|||
code_ = extract(common_code.unwrap()); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode RawCode::to_common_code() const { |
|||
return CommonCode::unsafe_create(compact(code_)); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
RawCode RawCode::from_common_code(const CommonCode common_code) { |
|||
return common_code.to_raw_code(); |
|||
} |
|||
|
|||
std::optional<RawCode> RawCode::from_common_code(const uint64_t common_code) { |
|||
auto convert = [](const CommonCode code) { |
|||
return code.to_raw_code(); |
|||
}; |
|||
return CommonCode::create(common_code).transform(convert); |
|||
} |
|||
|
|||
std::optional<RawCode> RawCode::from_common_code(const std::string &common_code) { |
|||
auto convert = [](const CommonCode code) { |
|||
return code.to_raw_code(); |
|||
}; |
|||
return CommonCode::from_string(common_code).transform(convert); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace klotski::codec
|
@ -1,46 +0,0 @@ |
|||
#include "raw_code.h" |
|||
#include "common_code.h" |
|||
|
|||
namespace klotski { |
|||
namespace codec { |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
RawCode::RawCode(CommonCode common_code) noexcept { |
|||
code_ = RawCode::extract(common_code.unwrap()); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode RawCode::to_common_code() const noexcept { |
|||
return CommonCode::unsafe_create(RawCode::compact(code_)); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
RawCode RawCode::from_common_code(CommonCode common_code) noexcept { |
|||
return common_code.to_raw_code(); |
|||
} |
|||
|
|||
std::optional<RawCode> RawCode::from_common_code(uint64_t common_code) noexcept { |
|||
return CommonCode::create(common_code).transform([](auto common_code) { |
|||
return common_code.to_raw_code(); |
|||
}); |
|||
} |
|||
|
|||
std::optional<RawCode> RawCode::from_common_code(std::string &&common_code) noexcept { |
|||
return CommonCode::from_string(std::move(common_code)).transform([](auto common_code) { |
|||
return common_code.to_raw_code(); |
|||
}); |
|||
} |
|||
|
|||
std::optional<RawCode> RawCode::from_common_code(const std::string &common_code) noexcept { |
|||
return CommonCode::from_string(common_code).transform([](auto common_code) { |
|||
return common_code.to_raw_code(); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace codec
|
|||
} // namespace klotski
|
Loading…
Reference in new issue