From 2b358e9b051ca83df3b3bf1a5977b6e8cc3a921f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 19 Jan 2025 12:22:21 +0800 Subject: [PATCH] feat: add diagonal mirror of RawCode --- src/core/raw_code/internal/mirror.inl | 7 +++++-- src/core/raw_code/internal/raw_code.inl | 4 ++++ src/core/raw_code/raw_code.h | 6 ++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/core/raw_code/internal/mirror.inl b/src/core/raw_code/internal/mirror.inl index a1d7516..5799be8 100644 --- a/src/core/raw_code/internal/mirror.inl +++ b/src/core/raw_code/internal/mirror.inl @@ -2,8 +2,6 @@ namespace klotski::codec { -// TODO: add `diagonal_mirror` support - constexpr uint64_t RawCode::get_vertical_mirror(const uint64_t raw_code) { uint64_t code = ((raw_code >> 36) & 0x000'000'000'fff'fff) | ((raw_code << 36) & 0xfff'fff'000'000'000); code = ((code >> 12) & 0x000'fff'000'000'fff) | ((code << 12) & 0xfff'000'000'fff'000); @@ -25,6 +23,11 @@ constexpr uint64_t RawCode::get_horizontal_mirror(const uint64_t raw_code) { return (code | m1 | m2) & ~(m1 >> 3) & ~(m2 >> 3); } +constexpr uint64_t RawCode::get_diagonal_mirror(const uint64_t raw_code) { + // TODO: perf it + return get_horizontal_mirror(get_vertical_mirror(raw_code)); +} + constexpr bool RawCode::check_mirror(const uint64_t raw_code) { const uint64_t m1 = ~raw_code & (raw_code << 1) & 0x492'492'492'492'492; const uint64_t m2 = raw_code & 0x924'924'924'924'924; diff --git a/src/core/raw_code/internal/raw_code.inl b/src/core/raw_code/internal/raw_code.inl index 80c3075..4c81b86 100644 --- a/src/core/raw_code/internal/raw_code.inl +++ b/src/core/raw_code/internal/raw_code.inl @@ -67,6 +67,10 @@ constexpr bool RawCode::is_horizontal_mirror() const { return check_mirror(code_); } +constexpr RawCode RawCode::to_diagonal_mirror() const { + return unsafe_create(get_diagonal_mirror(code_)); +} + constexpr RawCode RawCode::to_vertical_mirror() const { return unsafe_create(get_vertical_mirror(code_)); } diff --git a/src/core/raw_code/raw_code.h b/src/core/raw_code/raw_code.h index 0201e42..f1c076c 100644 --- a/src/core/raw_code/raw_code.h +++ b/src/core/raw_code/raw_code.h @@ -127,6 +127,9 @@ public: /// Whether the layout is horizontally symmetrical. [[nodiscard]] constexpr bool is_horizontal_mirror() const; + /// Calculate the diagonally symmetrical klotski layout. + [[nodiscard]] constexpr RawCode to_diagonal_mirror() const; + /// Calculate the vertically symmetrical klotski layout. [[nodiscard]] constexpr RawCode to_vertical_mirror() const; @@ -161,6 +164,9 @@ private: /// Check the horizontally symmetrical. static constexpr bool check_mirror(uint64_t raw_code); + /// Get the diagonally symmetrical layout. + static constexpr uint64_t get_diagonal_mirror(uint64_t raw_code); + /// Get the vertically symmetrical layout. static constexpr uint64_t get_vertical_mirror(uint64_t raw_code);