mirror of https://github.com/dnomd343/klotski.git
Dnomd343
6 months ago
8 changed files with 232 additions and 191 deletions
@ -1,56 +0,0 @@ |
|||
#pragma once |
|||
|
|||
namespace klotski { |
|||
namespace codec { |
|||
|
|||
/// Get the original 64-bit code.
|
|||
inline uint64_t CommonCode::unwrap() const noexcept { |
|||
return code_; |
|||
} |
|||
|
|||
/// Implicit conversion to 64-bit code.
|
|||
inline CommonCode::operator uint64_t() const noexcept { |
|||
return code_; |
|||
} |
|||
|
|||
/// Equality comparison between CommonCode and numbers.
|
|||
inline bool operator==(CommonCode c1, uint64_t c2) noexcept { |
|||
return c1.unwrap() == c2; |
|||
} |
|||
|
|||
/// CommonCode equal comparison implement.
|
|||
inline bool operator==(CommonCode c1, CommonCode c2) noexcept { |
|||
return c1.unwrap() == c2.unwrap(); |
|||
} |
|||
|
|||
/// CommonCode less than comparison implement.
|
|||
inline bool operator<(CommonCode c1, CommonCode c2) noexcept { |
|||
return c1.unwrap() < c2.unwrap(); |
|||
} |
|||
|
|||
/// CommonCode greater than comparison implement.
|
|||
inline bool operator>(CommonCode c1, CommonCode c2) noexcept { |
|||
return c1.unwrap() > c2.unwrap(); |
|||
} |
|||
|
|||
/// CommonCode create without any check.
|
|||
inline CommonCode CommonCode::unsafe_create(uint64_t common_code) noexcept { |
|||
return *reinterpret_cast<CommonCode*>(&common_code); // init directly
|
|||
} |
|||
|
|||
/// CommonCode create with valid check.
|
|||
inline std::optional<CommonCode> CommonCode::create(uint64_t common_code) noexcept { |
|||
if (!CommonCode::check(common_code)) { |
|||
return std::nullopt; // invalid common code
|
|||
} |
|||
return CommonCode::unsafe_create(common_code); |
|||
} |
|||
|
|||
/// Output string encoding of CommonCode.
|
|||
inline std::ostream& operator<<(std::ostream &out, CommonCode self) { |
|||
out << CommonCode::string_encode(self.code_); |
|||
return out; |
|||
} |
|||
|
|||
} // namespace codec
|
|||
} // namespace klotski
|
@ -0,0 +1,71 @@ |
|||
#pragma once |
|||
|
|||
#include <bit> |
|||
|
|||
namespace klotski::codec { |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline uint64_t CommonCode::unwrap() const { |
|||
return code_; |
|||
} |
|||
|
|||
inline CommonCode::operator uint64_t() const { |
|||
return code_; |
|||
} |
|||
|
|||
inline std::ostream& operator<<(std::ostream &out, const CommonCode self) { |
|||
out << CommonCode::string_encode(self.code_); |
|||
return out; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
constexpr auto operator==(const CommonCode &c1, const uint64_t c2) { |
|||
return c1.code_ == c2; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const CommonCode &c1, const uint64_t c2) { |
|||
return c1.code_ <=> c2; |
|||
} |
|||
|
|||
constexpr auto operator==(const CommonCode &c1, const CommonCode &c2) { |
|||
return c1.code_ == c2.code_; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const CommonCode &c1, const CommonCode &c2) { |
|||
return c1.code_ <=> c2.code_; |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline CommonCode CommonCode::unsafe_create(const uint64_t common_code) { |
|||
return std::bit_cast<CommonCode>(common_code); // init directly |
|||
} |
|||
|
|||
inline std::optional<CommonCode> CommonCode::create(const uint64_t common_code) { |
|||
if (!check(common_code)) { |
|||
return std::nullopt; // invalid common code |
|||
} |
|||
return unsafe_create(common_code); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline std::string CommonCode::to_string(const bool shorten) const { |
|||
if (!shorten) { |
|||
return string_encode(code_); // with full length |
|||
} |
|||
return string_encode_shorten(code_); // without trailing zero |
|||
} |
|||
|
|||
inline std::optional<CommonCode> CommonCode::from_string(const std::string &common_code) { |
|||
auto construct = [](const uint64_t code) { |
|||
return unsafe_create(code); |
|||
}; |
|||
return string_decode(common_code).transform(construct); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
} // namespace klotski::codec |
@ -0,0 +1,61 @@ |
|||
#include "raw_code.h" |
|||
#include "short_code.h" |
|||
#include "common_code.h" |
|||
|
|||
// TODO: move to inline header
|
|||
|
|||
namespace klotski::codec { |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode::CommonCode(RawCode raw_code) { |
|||
code_ = raw_code.to_common_code().code_; |
|||
} |
|||
|
|||
CommonCode::CommonCode(ShortCode short_code) { |
|||
code_ = short_code.to_common_code().code_; |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
RawCode CommonCode::to_raw_code() const { |
|||
return RawCode(*this); |
|||
} |
|||
|
|||
ShortCode CommonCode::to_short_code() const { |
|||
return ShortCode(*this); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode CommonCode::from_raw_code(RawCode raw_code) { |
|||
return raw_code.to_common_code(); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_raw_code(uint64_t raw_code) { |
|||
return RawCode::create(raw_code).transform([](auto raw_code) { |
|||
return raw_code.to_common_code(); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode CommonCode::from_short_code(ShortCode short_code) { |
|||
return short_code.to_common_code(); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_short_code(uint32_t short_code) { |
|||
return ShortCode::create(short_code).transform([](auto short_code) { |
|||
return short_code.to_common_code(); |
|||
}); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_short_code(const std::string &short_code) { |
|||
return ShortCode::from_string(short_code).transform([](auto short_code) { |
|||
return short_code.to_common_code(); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace klotski::codec
|
@ -1,86 +0,0 @@ |
|||
#include "raw_code.h" |
|||
#include "short_code.h" |
|||
#include "common_code.h" |
|||
|
|||
namespace klotski { |
|||
namespace codec { |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode::CommonCode(RawCode raw_code) noexcept { |
|||
code_ = raw_code.to_common_code().code_; |
|||
} |
|||
|
|||
CommonCode::CommonCode(ShortCode short_code) noexcept { |
|||
code_ = short_code.to_common_code().code_; |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
RawCode CommonCode::to_raw_code() const noexcept { |
|||
return RawCode(*this); |
|||
} |
|||
|
|||
ShortCode CommonCode::to_short_code() const noexcept { |
|||
return ShortCode(*this); |
|||
} |
|||
|
|||
std::string CommonCode::to_string(bool shorten) const noexcept { |
|||
if (!shorten) { |
|||
return string_encode(code_); // with full length
|
|||
} |
|||
return string_encode_shorten(code_); // without trailing zero
|
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
std::optional<CommonCode> CommonCode::from_string(std::string &&common_code) noexcept { |
|||
return CommonCode::from_string(common_code); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_string(const std::string &common_code) noexcept { |
|||
return string_decode(common_code).transform([](auto code) { |
|||
return CommonCode::unsafe_create(code); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode CommonCode::from_raw_code(RawCode raw_code) noexcept { |
|||
return raw_code.to_common_code(); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_raw_code(uint64_t raw_code) noexcept { |
|||
return RawCode::create(raw_code).transform([](auto raw_code) { |
|||
return raw_code.to_common_code(); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
CommonCode CommonCode::from_short_code(ShortCode short_code) noexcept { |
|||
return short_code.to_common_code(); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_short_code(uint32_t short_code) noexcept { |
|||
return ShortCode::create(short_code).transform([](auto short_code) { |
|||
return short_code.to_common_code(); |
|||
}); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_short_code(std::string &&short_code) noexcept { |
|||
return ShortCode::from_string(std::move(short_code)).transform([](auto short_code) { |
|||
return short_code.to_common_code(); |
|||
}); |
|||
} |
|||
|
|||
std::optional<CommonCode> CommonCode::from_short_code(const std::string &short_code) noexcept { |
|||
return ShortCode::from_string(short_code).transform([](auto short_code) { |
|||
return short_code.to_common_code(); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace codec
|
|||
} // namespace klotski
|
Loading…
Reference in new issue