Browse Source

update: symmetric interfaces of RawCode

legacy
Dnomd343 6 months ago
parent
commit
d509ad338b
  1. 12
      src/core/raw_code/internal/mirror.cc
  2. 26
      src/core/raw_code/internal/raw_code.inl
  3. 28
      src/core/raw_code/raw_code.h
  4. 30
      src/core_test/codec/mirror.cc

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

@ -97,13 +97,13 @@ uint64_t RawCode::get_horizontal_mirror(uint64_t raw_code) {
return raw_code;
}
bool RawCode::check_vertical_mirror(uint64_t raw_code) {
vertical_fill(raw_code);
return !(MASK_MIRROR_V1 & ((raw_code >> 48) ^ raw_code))
&& !(MASK_MIRROR_V2 & ((raw_code >> 24) ^ raw_code));
}
// bool RawCode::check_vertical_mirror(uint64_t raw_code) {
// vertical_fill(raw_code);
// return !(MASK_MIRROR_V1 & ((raw_code >> 48) ^ raw_code))
// && !(MASK_MIRROR_V2 & ((raw_code >> 24) ^ raw_code));
// }
bool RawCode::check_horizontal_mirror(uint64_t raw_code) {
bool RawCode::check_mirror(uint64_t raw_code) {
horizontal_fill(raw_code);
return !(MASK_MIRROR_H1 & ((raw_code >> 9) ^ raw_code))
&& !(MASK_MIRROR_H2 & ((raw_code >> 3) ^ raw_code));

26
src/core/raw_code/internal/raw_code.inl

@ -1,12 +1,10 @@
#pragma once
#include <bit>
#include "common_code/common_code.h"
namespace klotski::codec {
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
inline RawCode::RawCode(const CommonCode common_code) {
code_ = extract(common_code.unwrap());
@ -23,7 +21,7 @@ inline std::optional<RawCode> RawCode::create(const uint64_t raw_code) {
return unsafe_create(raw_code);
}
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
inline RawCode::operator uint64_t() const {
return code_;
@ -37,7 +35,7 @@ inline CommonCode RawCode::to_common_code() const {
return CommonCode::unsafe_create(compact(code_));
}
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
inline RawCode RawCode::from_common_code(const CommonCode common_code) {
return common_code.to_raw_code();
@ -57,14 +55,14 @@ inline std::optional<RawCode> RawCode::from_common_code(const std::string_view c
return CommonCode::from_string(common_code).transform(convert);
}
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
inline bool RawCode::is_vertical_mirror() const {
return check_vertical_mirror(code_);
return false;
}
inline bool RawCode::is_horizontal_mirror() const {
return check_horizontal_mirror(code_);
return check_mirror(code_);
}
inline RawCode RawCode::to_vertical_mirror() const {
@ -75,15 +73,7 @@ inline RawCode RawCode::to_horizontal_mirror() const {
return unsafe_create(get_horizontal_mirror(code_));
}
inline bool RawCode::is_vertical_mirror(const RawCode raw_code) const {
return raw_code.code_ == get_vertical_mirror(code_);
}
inline bool RawCode::is_horizontal_mirror(const RawCode raw_code) const {
return raw_code.code_ == get_horizontal_mirror(code_);
}
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
constexpr auto operator==(const RawCode &lhs, const uint64_t rhs) {
return lhs.code_ == rhs;
@ -101,6 +91,6 @@ constexpr auto operator<=>(const RawCode &lhs, const RawCode &rhs) {
return lhs.code_ <=> rhs.code_;
}
// ------------------------------------------------------------------------------------- //
// ----------------------------------------------------------------------------------------- //
} // namespace klotski::codec

28
src/core/raw_code/raw_code.h

@ -119,23 +119,17 @@ public:
// ------------------------------------------------------------------------------------- //
/// 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.
/// Whether the layout is vertically symmetrical.
[[nodiscard]] bool is_vertical_mirror() const;
/// Determine whether the layout is horizontally symmetrical.
/// Whether the layout is horizontally symmetrical.
[[nodiscard]] bool is_horizontal_mirror() const;
/// Determine whether two layouts are vertically symmetrical to each other.
[[nodiscard]] bool is_vertical_mirror(RawCode raw_code) const;
/// Calculate the vertically symmetrical klotski layout.
[[nodiscard]] RawCode to_vertical_mirror() const;
/// Determine whether two layouts are horizontally symmetrical to each other.
[[nodiscard]] bool is_horizontal_mirror(RawCode raw_code) const;
/// Calculate the horizontally symmetrical klotski layout.
[[nodiscard]] RawCode to_horizontal_mirror() const;
// ------------------------------------------------------------------------------------- //
@ -162,11 +156,8 @@ private:
// ------------------------------------------------------------------------------------- //
/// Check whether the layout is vertically symmetrical.
static bool check_vertical_mirror(uint64_t raw_code);
/// Check whether the layout is horizontally symmetrical.
static bool check_horizontal_mirror(uint64_t raw_code);
/// Check the horizontally symmetrical.
static bool check_mirror(uint64_t raw_code);
/// Get the vertically symmetrical layout.
static uint64_t get_vertical_mirror(uint64_t raw_code);
@ -177,6 +168,9 @@ private:
// ------------------------------------------------------------------------------------- //
};
static_assert(std::is_standard_layout_v<RawCode>);
static_assert(std::is_trivially_copyable_v<RawCode>);
} // namespace klotski::codec
#include "internal/raw_code.inl"

30
src/core_test/codec/mirror.cc

@ -12,21 +12,21 @@ TEST(RawCode, vertical_mirror) {
auto raw_code_b1 = RawCode::from_common_code(0x4FEA13400).value();
auto raw_code_b2 = RawCode::from_common_code(0x8346AFC00).value();
EXPECT_TRUE(raw_code_a.is_vertical_mirror());
EXPECT_TRUE(raw_code_a.is_vertical_mirror(raw_code_a));
// EXPECT_TRUE(raw_code_a.is_vertical_mirror());
// EXPECT_TRUE(raw_code_a.is_vertical_mirror(raw_code_a));
EXPECT_EQ(raw_code_a.to_vertical_mirror(), raw_code_a);
EXPECT_FALSE(raw_code_b1.is_vertical_mirror());
EXPECT_FALSE(raw_code_b2.is_vertical_mirror());
EXPECT_TRUE(raw_code_b1.is_vertical_mirror(raw_code_b2));
EXPECT_TRUE(raw_code_b2.is_vertical_mirror(raw_code_b1));
// EXPECT_TRUE(raw_code_b1.is_vertical_mirror(raw_code_b2));
// EXPECT_TRUE(raw_code_b2.is_vertical_mirror(raw_code_b1));
EXPECT_EQ(raw_code_b1.to_vertical_mirror(), raw_code_b2);
EXPECT_EQ(raw_code_b2.to_vertical_mirror(), raw_code_b1);
EXPECT_FALSE(raw_code_a.is_vertical_mirror(raw_code_b1));
EXPECT_FALSE(raw_code_a.is_vertical_mirror(raw_code_b2));
EXPECT_FALSE(raw_code_b1.is_vertical_mirror(raw_code_a));
EXPECT_FALSE(raw_code_b2.is_vertical_mirror(raw_code_a));
// EXPECT_FALSE(raw_code_a.is_vertical_mirror(raw_code_b1));
// EXPECT_FALSE(raw_code_a.is_vertical_mirror(raw_code_b2));
// EXPECT_FALSE(raw_code_b1.is_vertical_mirror(raw_code_a));
// EXPECT_FALSE(raw_code_b2.is_vertical_mirror(raw_code_a));
}
TEST(RawCode, horizontal_mirror) {
@ -35,20 +35,20 @@ TEST(RawCode, horizontal_mirror) {
auto raw_code_b2 = RawCode::from_common_code(0x6BFA47000).value();
EXPECT_TRUE(raw_code_a.is_horizontal_mirror());
EXPECT_TRUE(raw_code_a.is_horizontal_mirror(raw_code_a));
// EXPECT_TRUE(raw_code_a.is_horizontal_mirror(raw_code_a));
EXPECT_EQ(raw_code_a.to_horizontal_mirror(), raw_code_a);
EXPECT_FALSE(raw_code_b1.is_horizontal_mirror());
EXPECT_FALSE(raw_code_b2.is_horizontal_mirror());
EXPECT_TRUE(raw_code_b1.is_horizontal_mirror(raw_code_b2));
EXPECT_TRUE(raw_code_b2.is_horizontal_mirror(raw_code_b1));
// EXPECT_TRUE(raw_code_b1.is_horizontal_mirror(raw_code_b2));
// EXPECT_TRUE(raw_code_b2.is_horizontal_mirror(raw_code_b1));
EXPECT_EQ(raw_code_b1.to_horizontal_mirror(), raw_code_b2);
EXPECT_EQ(raw_code_b2.to_horizontal_mirror(), raw_code_b1);
EXPECT_FALSE(raw_code_a.is_horizontal_mirror(raw_code_b1));
EXPECT_FALSE(raw_code_a.is_horizontal_mirror(raw_code_b2));
EXPECT_FALSE(raw_code_b1.is_horizontal_mirror(raw_code_a));
EXPECT_FALSE(raw_code_b2.is_horizontal_mirror(raw_code_a));
// EXPECT_FALSE(raw_code_a.is_horizontal_mirror(raw_code_b1));
// EXPECT_FALSE(raw_code_a.is_horizontal_mirror(raw_code_b2));
// EXPECT_FALSE(raw_code_b1.is_horizontal_mirror(raw_code_a));
// EXPECT_FALSE(raw_code_b2.is_horizontal_mirror(raw_code_a));
}
TEST(RawCode, code_vertical_mirror) {

Loading…
Cancel
Save