Browse Source

update: cleanup inline functions of raw code

legacy
Dnomd343 8 months ago
parent
commit
424bdefbc5
  1. 1
      src/core/CMakeLists.txt
  2. 4
      src/core/raw_code/internal/convert.cc
  3. 20
      src/core/raw_code/internal/mirror.cc
  4. 10
      src/core/raw_code/internal/raw_code.cc
  5. 72
      src/core/raw_code/internal/raw_code.inl
  6. 40
      src/core/raw_code/internal/sundry.cc
  7. 51
      src/core/raw_code/raw_code.h

1
src/core/CMakeLists.txt

@ -12,7 +12,6 @@ set(KLOTSKI_CORE_SRC
raw_code/internal/raw_code.cc
raw_code/internal/convert.cc
raw_code/internal/sundry.cc
raw_code/internal/mirror.cc
short_code/internal/convert.cc

4
src/core/raw_code/internal/convert.cc

@ -2,7 +2,7 @@
#include "utils/utility.h"
#include "raw_code/raw_code.h"
namespace klotski::codec {
using klotski::codec::RawCode;
uint64_t RawCode::compact(uint64_t raw_code) {
int unfilled = 16;
@ -57,5 +57,3 @@ uint64_t RawCode::extract(uint64_t common_code) {
}
return code;
}
} // namespace klotski::codec

20
src/core/raw_code/internal/mirror.cc

@ -1,9 +1,7 @@
#include "utils/common.h"
#include "raw_code/raw_code.h"
namespace klotski::codec {
// ----------------------------------------------------------------------------------------- //
using klotski::codec::RawCode;
/// MASK_MIRROR_H1 | MASK_MIRROR_H2
/// 111 000 000 000 | 000 111 000 000
@ -26,9 +24,7 @@ constexpr uint64_t MASK_MIRROR_V1 = 0x0'000'000'000'000'FFF;
constexpr uint64_t MASK_MIRROR_V2 = 0x0'000'000'000'FFF'000;
constexpr uint64_t MASK_MIRROR_V3 = 0x0'000'000'FFF'000'000;
// ----------------------------------------------------------------------------------------- //
inline static void vertical_fill(uint64_t &raw_code) {
static void vertical_fill(uint64_t &raw_code) {
uint64_t mask = 0;
for (int addr = 0; addr < 60; addr += 3) { // traverse every 3-bit
switch ((raw_code >> addr) & 0b111) {
@ -49,7 +45,7 @@ inline static void vertical_fill(uint64_t &raw_code) {
}
}
inline static void horizontal_fill(uint64_t &raw_code) {
static void horizontal_fill(uint64_t &raw_code) {
for (int addr = 0; addr < 60; addr += 3) { // traverse every 3-bit
switch ((raw_code >> addr) & 0b111) {
case BLOCK_1x2:
@ -64,7 +60,7 @@ inline static void horizontal_fill(uint64_t &raw_code) {
}
}
inline static void vertical_clear(uint64_t &raw_code) {
static void vertical_clear(uint64_t &raw_code) {
for (int addr = 0; addr < 60; addr += 3) { // traverse every 3-bit
switch ((raw_code >> addr) & 0b111) {
case BLOCK_2x1:
@ -74,7 +70,7 @@ inline static void vertical_clear(uint64_t &raw_code) {
}
}
inline static void horizontal_clear(uint64_t &raw_code) {
static void horizontal_clear(uint64_t &raw_code) {
for (int addr = 0; addr < 60; addr += 3) { // traverse every 3-bit
switch ((raw_code >> addr) & 0b111) {
case BLOCK_1x2:
@ -84,8 +80,6 @@ inline static void horizontal_clear(uint64_t &raw_code) {
}
}
// ----------------------------------------------------------------------------------------- //
uint64_t RawCode::get_vertical_mirror(uint64_t raw_code) {
vertical_fill(raw_code);
raw_code = (raw_code & MASK_MIRROR_V3)
@ -114,7 +108,3 @@ bool RawCode::check_horizontal_mirror(uint64_t raw_code) {
return !(MASK_MIRROR_H1 & ((raw_code >> 9) ^ raw_code))
&& !(MASK_MIRROR_H2 & ((raw_code >> 3) ^ raw_code));
}
// ----------------------------------------------------------------------------------------- //
} // namespace klotski::codec

10
src/core/raw_code/internal/raw_code.cc

@ -1,16 +1,17 @@
#include "utils/common.h"
#include "raw_code/raw_code.h"
namespace klotski::codec {
using klotski::codec::RawCode;
std::ostream& operator<<(std::ostream &out, const RawCode self) {
#ifndef KLSK_NDEBUG
std::ostream& klotski::codec::operator<<(std::ostream &out, const RawCode self) {
char *code;
asprintf(&code, "%015llX\n", self.code_); // code length -> 15
out << code;
free(code);
constexpr char map[] = {
// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill
// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill
'.', '~', '|', '*', '@', '?', '?', '+'
};
for (int addr = 0; addr < 60; addr += 3) {
@ -19,6 +20,7 @@ std::ostream& operator<<(std::ostream &out, const RawCode self) {
}
return out;
}
#endif
bool RawCode::check(uint64_t raw_code) {
/// MASK_1x1 | MASK_1x2 | MASK_2x1 | MASK_2x2
@ -68,5 +70,3 @@ bool RawCode::check(uint64_t raw_code) {
}
return head_num == 1 && space_num >= 2; // one head and at least 2 space
}
} // namespace klotski::codec

72
src/core/raw_code/internal/raw_code.inl

@ -2,47 +2,59 @@
#include <bit>
#include "common_code/common_code.h"
namespace klotski::codec {
// ------------------------------------------------------------------------------------- //
inline uint64_t RawCode::unwrap() const {
return code_;
inline RawCode::RawCode(const CommonCode common_code) {
code_ = extract(common_code.unwrap());
}
inline RawCode::operator uint64_t() const {
return code_;
inline RawCode RawCode::unsafe_create(const uint64_t raw_code) {
return std::bit_cast<RawCode>(raw_code); // init directly
}
// ------------------------------------------------------------------------------------- //
constexpr auto operator==(const RawCode &lhs, const uint64_t rhs) {
return lhs.code_ == rhs;
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);
}
constexpr auto operator<=>(const RawCode &lhs, const uint64_t rhs) {
return lhs.code_ <=> rhs;
// ------------------------------------------------------------------------------------- //
inline RawCode::operator uint64_t() const {
return code_;
}
constexpr auto operator==(const RawCode &lhs, const RawCode &rhs) {
return lhs.code_ == rhs.code_;
inline uint64_t RawCode::unwrap() const {
return code_;
}
constexpr auto operator<=>(const RawCode &lhs, const RawCode &rhs) {
return lhs.code_ <=> rhs.code_;
inline CommonCode RawCode::to_common_code() const {
return CommonCode::unsafe_create(compact(code_));
}
// ------------------------------------------------------------------------------------- //
inline RawCode RawCode::unsafe_create(const uint64_t raw_code) {
return std::bit_cast<RawCode>(raw_code); // init directly
inline RawCode RawCode::from_common_code(const CommonCode common_code) {
return common_code.to_raw_code();
}
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 std::optional<RawCode> RawCode::from_common_code(const uint64_t common_code) {
const auto convert = [](const CommonCode code) {
return code.to_raw_code();
};
return CommonCode::create(common_code).transform(convert);
}
inline std::optional<RawCode> RawCode::from_common_code(const std::string &common_code) {
const auto convert = [](const CommonCode code) {
return code.to_raw_code();
};
return CommonCode::from_string(common_code).transform(convert);
}
// ------------------------------------------------------------------------------------- //
@ -73,4 +85,22 @@ inline bool RawCode::is_horizontal_mirror(const RawCode raw_code) const {
// ------------------------------------------------------------------------------------- //
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_;
}
// ------------------------------------------------------------------------------------- //
} // namespace klotski::codec

40
src/core/raw_code/internal/sundry.cc

@ -1,40 +0,0 @@
#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

51
src/core/raw_code/raw_code.h

@ -74,15 +74,29 @@ class RawCode {
public:
// ------------------------------------------------------------------------------------- //
RawCode() = delete;
/// Construct RawCode from CommonCode.
explicit RawCode(CommonCode common_code);
/// Create RawCode without any check.
static RawCode unsafe_create(uint64_t raw_code);
/// Create RawCode with validity check.
static std::optional<RawCode> create(uint64_t raw_code);
// ------------------------------------------------------------------------------------- //
/// Explicit conversion to u64 code.
explicit operator uint64_t() const;
/// Check the validity of the original RawCode.
static bool check(uint64_t raw_code);
// TODO: add macro check here
#ifndef KLSK_NDEBUG
/// Output string encoding of RawCode only for debug.
friend std::ostream& operator<<(std::ostream &out, RawCode self);
#endif
// ------------------------------------------------------------------------------------- //
@ -94,19 +108,6 @@ public:
// ------------------------------------------------------------------------------------- //
RawCode() = delete;
/// Construct RawCode from CommonCode.
explicit RawCode(CommonCode common_code);
/// Create RawCode without any check.
static RawCode unsafe_create(uint64_t raw_code);
/// Create RawCode with validity check.
static std::optional<RawCode> create(uint64_t raw_code);
// ------------------------------------------------------------------------------------- //
/// Create RawCode from CommonCode.
static RawCode from_common_code(CommonCode common_code);
@ -118,24 +119,12 @@ public:
// ------------------------------------------------------------------------------------- //
/// Compare RawCode with u64 values.
friend constexpr auto operator==(const RawCode &lhs, uint64_t rhs);
friend constexpr auto operator<=>(const RawCode &lhs, uint64_t rhs);
/// Compare the original values of two RawCodes.
friend constexpr auto operator==(const RawCode &lhs, const RawCode &rhs);
friend constexpr auto operator<=>(const RawCode &lhs, const RawCode &rhs);
// ------------------------------------------------------------------------------------- //
/// Calculate vertically symmetrical layout.
[[nodiscard]] RawCode to_vertical_mirror() const;
/// Calculate horizontally symmetrical layout.
[[nodiscard]] RawCode to_horizontal_mirror() const;
// ------------------------------------------------------------------------------------- //
/// Determine whether the layout is vertically symmetrical.
[[nodiscard]] bool is_vertical_mirror() const;
@ -150,6 +139,16 @@ public:
// ------------------------------------------------------------------------------------- //
/// Compare RawCode with u64 value.
friend constexpr auto operator==(const RawCode &lhs, uint64_t rhs);
friend constexpr auto operator<=>(const RawCode &lhs, uint64_t rhs);
/// Compare the original values of two RawCodes.
friend constexpr auto operator==(const RawCode &lhs, const RawCode &rhs);
friend constexpr auto operator<=>(const RawCode &lhs, const RawCode &rhs);
// ------------------------------------------------------------------------------------- //
private:
uint64_t code_;

Loading…
Cancel
Save