mirror of https://github.com/dnomd343/klotski.git
Dnomd343
8 months ago
23 changed files with 242 additions and 237 deletions
@ -1,56 +0,0 @@ |
|||
#pragma once |
|||
|
|||
namespace klotski { |
|||
namespace codec { |
|||
|
|||
/// Get the original 32-bit code.
|
|||
inline uint32_t ShortCode::unwrap() const noexcept { |
|||
return code_; |
|||
} |
|||
|
|||
/// Implicit conversion to 32-bit code.
|
|||
inline ShortCode::operator uint32_t() const noexcept { |
|||
return code_; |
|||
} |
|||
|
|||
/// Equality comparison between ShortCode and numbers.
|
|||
inline bool operator==(ShortCode s1, uint32_t s2) noexcept { |
|||
return s1.unwrap() == s2; |
|||
} |
|||
|
|||
/// ShortCode equal comparison implement.
|
|||
inline bool operator==(ShortCode s1, ShortCode s2) noexcept { |
|||
return s1.unwrap() == s2.unwrap(); |
|||
} |
|||
|
|||
/// ShortCode less than comparison implement.
|
|||
inline bool operator<(ShortCode s1, ShortCode s2) noexcept { |
|||
return s1.unwrap() < s2.unwrap(); |
|||
} |
|||
|
|||
/// ShortCode greater than comparison implement.
|
|||
inline bool operator>(ShortCode s1, ShortCode s2) noexcept { |
|||
return s1.unwrap() > s2.unwrap(); |
|||
} |
|||
|
|||
/// ShortCode create without any check.
|
|||
inline ShortCode ShortCode::unsafe_create(uint32_t short_code) noexcept { |
|||
return *reinterpret_cast<ShortCode*>(&short_code); // init directly
|
|||
} |
|||
|
|||
/// ShortCode create with valid check.
|
|||
inline std::optional<ShortCode> ShortCode::create(uint32_t short_code) noexcept { |
|||
if (!ShortCode::check(short_code)) { |
|||
return std::nullopt; // invalid short code
|
|||
} |
|||
return ShortCode::unsafe_create(short_code); |
|||
} |
|||
|
|||
/// Output string encoding of ShortCode.
|
|||
inline std::ostream& operator<<(std::ostream &out, ShortCode self) { |
|||
out << ShortCode::string_encode(self.code_); |
|||
return out; |
|||
} |
|||
|
|||
} // namespace codec
|
|||
} // namespace klotski
|
@ -0,0 +1,120 @@ |
|||
#pragma once |
|||
|
|||
#include <bit> |
|||
|
|||
#include "common_code/common_code.h" |
|||
|
|||
namespace klotski::codec { |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline ShortCode::ShortCode(const CommonCode common_code) { |
|||
// TODO: test the affect of CPU branch prediction. |
|||
if (cases::AllCases::instance().is_available()) { |
|||
code_ = fast_encode(common_code.unwrap()); |
|||
} else { |
|||
code_ = tiny_encode(common_code.unwrap()); |
|||
} |
|||
} |
|||
|
|||
inline ShortCode ShortCode::unsafe_create(const uint32_t short_code) { |
|||
return std::bit_cast<ShortCode>(short_code); // init directly |
|||
} |
|||
|
|||
inline std::optional<ShortCode> ShortCode::create(const uint32_t short_code) { |
|||
if (!check(short_code)) { |
|||
return std::nullopt; // invalid short code |
|||
} |
|||
return unsafe_create(short_code); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
inline ShortCode::operator uint32_t() const { |
|||
return code_; |
|||
} |
|||
|
|||
inline bool ShortCode::check(const uint32_t short_code) { |
|||
return short_code < SHORT_CODE_LIMIT; // [0, SHORT_CODE_LIMIT) |
|||
} |
|||
|
|||
inline void ShortCode::speed_up(const bool fast_mode) { |
|||
if (fast_mode) { |
|||
cases::AllCases::instance().build(); |
|||
} else { |
|||
cases::BasicRanges::instance().build(); |
|||
} |
|||
} |
|||
|
|||
#ifndef KLSK_NDEBUG |
|||
inline std::ostream& operator<<(std::ostream &out, const ShortCode self) { |
|||
out << ShortCode::string_encode(self.code_); |
|||
return out; |
|||
} |
|||
#endif |
|||
|
|||
// ----------------------------------------------------------------------------------------- // |
|||
|
|||
inline uint32_t ShortCode::unwrap() const { |
|||
return code_; |
|||
} |
|||
|
|||
inline std::string ShortCode::to_string() const { |
|||
return string_encode(code_); |
|||
} |
|||
|
|||
inline CommonCode ShortCode::to_common_code() const { |
|||
// TODO: test the affect of CPU branch prediction. |
|||
if (cases::AllCases::instance().is_available()) { |
|||
return CommonCode::unsafe_create(fast_decode(code_)); |
|||
} |
|||
return CommonCode::unsafe_create(tiny_decode(code_)); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- // |
|||
|
|||
inline std::optional<ShortCode> ShortCode::from_string(const std::string &short_code) { |
|||
return string_decode(short_code).transform(unsafe_create); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- // |
|||
|
|||
inline ShortCode ShortCode::from_common_code(const CommonCode common_code) { |
|||
return common_code.to_short_code(); |
|||
} |
|||
|
|||
inline std::optional<ShortCode> ShortCode::from_common_code(const uint64_t common_code) { |
|||
const auto convert = [](const CommonCode code) { |
|||
return code.to_short_code(); |
|||
}; |
|||
return CommonCode::create(common_code).transform(convert); |
|||
} |
|||
|
|||
inline std::optional<ShortCode> ShortCode::from_common_code(const std::string &common_code) { |
|||
const auto convert = [](const CommonCode code) { |
|||
return code.to_short_code(); |
|||
}; |
|||
return CommonCode::from_string(common_code).transform(convert); |
|||
} |
|||
|
|||
// ------------------------------------------------------------------------------------- // |
|||
|
|||
constexpr auto operator==(const ShortCode &lhs, const uint32_t rhs) { |
|||
return lhs.code_ == rhs; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const ShortCode &lhs, const uint32_t rhs) { |
|||
return lhs.code_ <=> rhs; |
|||
} |
|||
|
|||
constexpr auto operator==(const ShortCode &lhs, const ShortCode &rhs) { |
|||
return lhs.code_ == rhs.code_; |
|||
} |
|||
|
|||
constexpr auto operator<=>(const ShortCode &lhs, const ShortCode &rhs) { |
|||
return lhs.code_ <=> rhs.code_; |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- // |
|||
|
|||
} // namespace klotski::codec |
@ -1,24 +0,0 @@ |
|||
#include "all_cases.h" |
|||
#include "short_code.h" |
|||
|
|||
using klotski::cases::AllCases; |
|||
using klotski::cases::BasicRanges; |
|||
|
|||
namespace klotski { |
|||
namespace codec { |
|||
|
|||
/// Check the validity of the original ShortCode.
|
|||
bool ShortCode::check(uint32_t short_code) noexcept { |
|||
return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1)
|
|||
} |
|||
|
|||
void ShortCode::speed_up(bool fast_mode) noexcept { |
|||
if (fast_mode) { |
|||
AllCases::instance().build(); |
|||
} else { |
|||
BasicRanges::instance().build(); |
|||
} |
|||
} |
|||
|
|||
} // namespace codec
|
|||
} // namespace klotski
|
@ -1,71 +0,0 @@ |
|||
#include "short_code.h" |
|||
#include "common_code.h" |
|||
|
|||
using klotski::cases::AllCases; |
|||
|
|||
namespace klotski { |
|||
namespace codec { |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
ShortCode::ShortCode(CommonCode common_code) noexcept { |
|||
if (AllCases::instance().is_available()) { |
|||
code_ = fast_encode(common_code.unwrap()); |
|||
} else { |
|||
code_ = tiny_encode(common_code.unwrap()); |
|||
} |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
std::string ShortCode::to_string() const noexcept { |
|||
return string_encode(code_); |
|||
} |
|||
|
|||
CommonCode ShortCode::to_common_code() const noexcept { |
|||
if (AllCases::instance().is_available()) { |
|||
return CommonCode::unsafe_create(fast_decode(code_)); |
|||
} |
|||
return CommonCode::unsafe_create(tiny_decode(code_)); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
std::optional<ShortCode> ShortCode::from_string(std::string &&short_code) noexcept { |
|||
return ShortCode::from_string(short_code); |
|||
} |
|||
|
|||
std::optional<ShortCode> ShortCode::from_string(const std::string &short_code) noexcept { |
|||
return ShortCode::string_decode(short_code).transform([](auto code) { |
|||
return ShortCode::unsafe_create(code); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
ShortCode ShortCode::from_common_code(CommonCode common_code) noexcept { |
|||
return common_code.to_short_code(); |
|||
} |
|||
|
|||
std::optional<ShortCode> ShortCode::from_common_code(uint64_t common_code) noexcept { |
|||
return CommonCode::create(common_code).transform([](auto common_code) { |
|||
return common_code.to_short_code(); |
|||
}); |
|||
} |
|||
|
|||
std::optional<ShortCode> ShortCode::from_common_code(std::string &&common_code) noexcept { |
|||
return CommonCode::from_string(std::move(common_code)).transform([](auto common_code) { |
|||
return common_code.to_short_code(); |
|||
}); |
|||
} |
|||
|
|||
std::optional<ShortCode> ShortCode::from_common_code(const std::string &common_code) noexcept { |
|||
return CommonCode::from_string(common_code).transform([](auto common_code) { |
|||
return common_code.to_short_code(); |
|||
}); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace codec
|
|||
} // namespace klotski
|
Loading…
Reference in new issue