Browse Source

update: cleanup inline functions of common code

legacy
Dnomd343 8 months ago
parent
commit
49ae5ba46f
  1. 1
      src/core/CMakeLists.txt
  2. 43
      src/core/common_code/common_code.h
  3. 12
      src/core/common_code/internal/common_code.cc
  4. 105
      src/core/common_code/internal/common_code.inl
  5. 4
      src/core/common_code/internal/serialize.cc
  6. 61
      src/core/common_code/internal/sundry.cc

1
src/core/CMakeLists.txt

@ -9,7 +9,6 @@ set(KLOTSKI_CORE_SRC
common_code/internal/common_code.cc common_code/internal/common_code.cc
common_code/internal/serialize.cc common_code/internal/serialize.cc
common_code/internal/sundry.cc
raw_code/internal/raw_code.cc raw_code/internal/raw_code.cc
raw_code/internal/convert.cc raw_code/internal/convert.cc

43
src/core/common_code/common_code.h

@ -72,15 +72,32 @@ class CommonCode {
public: public:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
CommonCode() = delete;
/// Construct CommonCode from RawCode.
explicit CommonCode(RawCode raw_code);
/// Construct CommonCode from ShortCode.
explicit CommonCode(ShortCode short_code);
/// Create CommonCode without any check.
static CommonCode unsafe_create(uint64_t common_code);
/// Create CommonCode with validity check.
static std::optional<CommonCode> create(uint64_t common_code);
// ------------------------------------------------------------------------------------- //
/// Explicit conversion to u64 code. /// Explicit conversion to u64 code.
explicit operator uint64_t() const; explicit operator uint64_t() const;
/// Check the validity of the original CommonCode. /// Check the validity of the original CommonCode.
static bool check(uint64_t common_code); static bool check(uint64_t common_code);
// TODO: add macro check here #ifndef KLSK_NDEBUG
/// Output string encoding of CommonCode only for debug. /// Output string encoding of CommonCode only for debug.
friend std::ostream& operator<<(std::ostream &out, CommonCode self); friend std::ostream& operator<<(std::ostream &out, CommonCode self);
#endif
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
@ -98,22 +115,6 @@ public:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
CommonCode() = delete;
/// Construct CommonCode from RawCode.
explicit CommonCode(RawCode raw_code);
/// Construct CommonCode from ShortCode.
explicit CommonCode(ShortCode short_code);
/// Create CommonCode without any check.
static CommonCode unsafe_create(uint64_t common_code);
/// Create CommonCode with validity check.
static std::optional<CommonCode> create(uint64_t common_code);
// ------------------------------------------------------------------------------------- //
/// Create CommonCode from string form. /// Create CommonCode from string form.
static std::optional<CommonCode> from_string(const std::string &common_code); static std::optional<CommonCode> from_string(const std::string &common_code);
@ -138,7 +139,7 @@ public:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Compare CommonCode with u64 values. /// Compare CommonCode with u64 value.
friend constexpr auto operator==(const CommonCode &lhs, uint64_t rhs); friend constexpr auto operator==(const CommonCode &lhs, uint64_t rhs);
friend constexpr auto operator<=>(const CommonCode &lhs, uint64_t rhs); friend constexpr auto operator<=>(const CommonCode &lhs, uint64_t rhs);
@ -153,13 +154,13 @@ private:
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
/// Serialize CommonCode into a 9-bit length string. /// Serialize CommonCode into 9-bit length string.
static std::string string_encode(uint64_t common_code); static std::string string_encode(uint64_t common_code);
/// Serialize CommonCode into a variable-length string, removing the trailing zero. /// Serialize CommonCode into a variable-length string without trailing zero.
static std::string string_encode_shorten(uint64_t common_code); static std::string string_encode_shorten(uint64_t common_code);
/// Deserialize CommonCode from string and return std::nullopt on error. /// Deserialize CommonCode from string and return nullopt on error.
static std::optional<uint64_t> string_decode(const std::string &common_code); static std::optional<uint64_t> string_decode(const std::string &common_code);
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //

12
src/core/common_code/internal/common_code.cc

@ -1,9 +1,9 @@
#include "utility.h" #include "utils/utility.h"
#include "common_code.h" #include "common_code/common_code.h"
namespace klotski::codec { using klotski::codec::CommonCode;
bool CommonCode::check(uint64_t common_code) { bool CommonCode::check(const uint64_t common_code) {
// TODO: optimization of synchronizing all_cases. // TODO: optimization of synchronizing all_cases.
/// M_1x1 | M_1x2 | M_2x1 | M_2x2 /// M_1x1 | M_1x2 | M_2x1 | M_2x2
@ -16,7 +16,7 @@ bool CommonCode::check(uint64_t common_code) {
constexpr uint32_t M_2x2 = 0b110011; constexpr uint32_t M_2x2 = 0b110011;
/// 2x2 address check (high 32-bit) /// 2x2 address check (high 32-bit)
uint32_t head = common_code >> 32; const uint32_t head = common_code >> 32;
if (head >= 16 || (head & 0b11) == 0b11) { // check 2x2 block address if (head >= 16 || (head & 0b11) == 0b11) { // check 2x2 block address
return false; // invalid common code return false; // invalid common code
} }
@ -56,5 +56,3 @@ bool CommonCode::check(uint64_t common_code) {
} }
} }
} }
} // namespace klotski::codec

105
src/core/common_code/internal/common_code.inl

@ -2,68 +2,121 @@
#include <bit> #include <bit>
#include "raw_code/raw_code.h"
#include "short_code/short_code.h"
namespace klotski::codec { namespace klotski::codec {
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
inline uint64_t CommonCode::unwrap() const { inline CommonCode::CommonCode(const RawCode raw_code) {
return code_; code_ = raw_code.to_common_code().code_;
}
inline CommonCode::CommonCode(const ShortCode short_code) {
code_ = short_code.to_common_code().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 CommonCode::operator uint64_t() const { inline CommonCode::operator uint64_t() const {
return code_; return code_;
} }
#ifndef KLSK_NDEBUG
inline std::ostream& operator<<(std::ostream &out, const CommonCode self) { inline std::ostream& operator<<(std::ostream &out, const CommonCode self) {
out << CommonCode::string_encode(self.code_); out << CommonCode::string_encode(self.code_);
return out; return out;
} }
#endif
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
constexpr auto operator==(const CommonCode &lhs, const uint64_t rhs) { inline uint64_t CommonCode::unwrap() const {
return lhs.code_ == rhs; return code_;
} }
constexpr auto operator<=>(const CommonCode &lhs, const uint64_t rhs) { inline RawCode CommonCode::to_raw_code() const {
return lhs.code_ <=> rhs; return RawCode(*this);
} }
constexpr auto operator==(const CommonCode &lhs, const CommonCode &rhs) { inline ShortCode CommonCode::to_short_code() const {
return lhs.code_ == rhs.code_; return ShortCode(*this);
} }
constexpr auto operator<=>(const CommonCode &lhs, const CommonCode &rhs) { inline std::string CommonCode::to_string(const bool shorten) const {
return lhs.code_ <=> rhs.code_; if (!shorten) {
return string_encode(code_); // with full length
}
return string_encode_shorten(code_); // without trailing zero
} }
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
inline CommonCode CommonCode::unsafe_create(const uint64_t common_code) { inline std::optional<CommonCode> CommonCode::from_string(const std::string &common_code) {
return std::bit_cast<CommonCode>(common_code); // init directly return string_decode(common_code).transform(unsafe_create);
} }
inline std::optional<CommonCode> CommonCode::create(const uint64_t common_code) { // ------------------------------------------------------------------------------------- //
if (!check(common_code)) {
return std::nullopt; // invalid common code inline CommonCode CommonCode::from_raw_code(const RawCode raw_code) {
} return raw_code.to_common_code();
return unsafe_create(common_code); }
inline std::optional<CommonCode> CommonCode::from_raw_code(const uint64_t raw_code) {
const auto convert = [](const RawCode code) {
return code.to_common_code();
};
return RawCode::create(raw_code).transform(convert);
} }
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //
inline std::string CommonCode::to_string(const bool shorten) const { inline CommonCode CommonCode::from_short_code(const ShortCode short_code) {
if (!shorten) { return short_code.to_common_code();
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) { inline std::optional<CommonCode> CommonCode::from_short_code(const uint32_t short_code) {
auto construct = [](const uint64_t code) { const auto convert = [](const ShortCode code) {
return unsafe_create(code); return code.to_common_code();
};
return ShortCode::create(short_code).transform(convert);
}
inline std::optional<CommonCode> CommonCode::from_short_code(const std::string &short_code) {
const auto convert = [](const ShortCode code) {
return code.to_common_code();
}; };
return string_decode(common_code).transform(construct); return ShortCode::from_string(short_code).transform(convert);
}
// ------------------------------------------------------------------------------------- //
constexpr auto operator==(const CommonCode &lhs, const uint64_t rhs) {
return lhs.code_ == rhs;
}
constexpr auto operator<=>(const CommonCode &lhs, const uint64_t rhs) {
return lhs.code_ <=> rhs;
}
constexpr auto operator==(const CommonCode &lhs, const CommonCode &rhs) {
return lhs.code_ == rhs.code_;
}
constexpr auto operator<=>(const CommonCode &lhs, const CommonCode &rhs) {
return lhs.code_ <=> rhs.code_;
} }
// ------------------------------------------------------------------------------------- // // ------------------------------------------------------------------------------------- //

4
src/core/common_code/internal/serialize.cc

@ -1,9 +1,9 @@
#include "common_code.h" #include "common_code/common_code.h"
namespace klotski::codec { namespace klotski::codec {
/// Convert a single hexadecimal digit to a character. /// Convert a single hexadecimal digit to a character.
inline static char to_hex_char(uint64_t hex_bit) { static char to_hex_char(const uint64_t hex_bit) {
if (hex_bit < 0xA) { if (hex_bit < 0xA) {
return char(hex_bit + '0'); return char(hex_bit + '0');
} }

61
src/core/common_code/internal/sundry.cc

@ -1,61 +0,0 @@
#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
Loading…
Cancel
Save