From 9e33a23b76962e2a137a252564d3bc433690fc69 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 19 Oct 2024 18:09:18 +0800 Subject: [PATCH] feat: obtain group toward character --- src/core/group/group.h | 11 +++-- src/core/group/internal/group.inl | 45 +++++++++++++++++++-- src/core/group/internal/group_union.inl | 4 ++ src/core/main.cc | 54 +++++++++++++------------ 4 files changed, 81 insertions(+), 33 deletions(-) diff --git a/src/core/group/group.h b/src/core/group/group.h index bf15f0c..042c603 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -158,6 +158,10 @@ class Group { public: Group() = delete; + // TODO: add stream output for debug + + [[nodiscard]] constexpr std::string to_string() const; + // ------------------------------------------------------------------------------------- // enum class Toward { @@ -180,16 +184,15 @@ public: /// Get the mirror toward. [[nodiscard]] constexpr Toward toward() const; + /// Get the toward character. + [[nodiscard]] constexpr char toward_char() const; + /// Get the original type id. [[nodiscard]] constexpr uint32_t type_id() const; /// Get the original pattern id. [[nodiscard]] constexpr uint32_t pattern_id() const; - // TODO: add toward char interface - - // TODO: add stream output for debug - // ------------------------------------------------------------------------------------- // /// Create Group without any check. diff --git a/src/core/group/internal/group.inl b/src/core/group/internal/group.inl index 189ca5f..36a81eb 100644 --- a/src/core/group/internal/group.inl +++ b/src/core/group/internal/group.inl @@ -4,18 +4,57 @@ namespace klotski::cases { -[[nodiscard]] constexpr uint32_t Group::type_id() const { +constexpr uint32_t Group::type_id() const { return type_id_; } -[[nodiscard]] constexpr uint32_t Group::pattern_id() const { +constexpr uint32_t Group::pattern_id() const { return pattern_id_; } -[[nodiscard]] constexpr auto Group::toward() const -> Toward { +constexpr auto Group::toward() const -> Toward { return toward_; } +constexpr char Group::toward_char() const { + // TODO: select chars from pre-build std::array + switch (mirror_type()) { + case MirrorType::Full: + return '\0'; + case MirrorType::Horizontal: + return (toward_ == Toward::A) ? 'n' : 'u'; + case MirrorType::Centro: + return (toward_ == Toward::A) ? '?' : '!'; // TODO: select chars for centro + case MirrorType::Vertical: + return (toward_ == Toward::A) ? 'p' : 'q'; + case MirrorType::Ordinary: + if (toward_ == Toward::A) { + return 'a'; + } else if (toward_ == Toward::B) { + return 'b'; + } else if (toward_ == Toward::C) { + return 'c'; + } else if (toward_ == Toward::D) { + return 'd'; + } + return '\0'; // TODO: never reach + } +} + +constexpr std::string Group::to_string() const { // TODO: `std::string` not support constexpr + auto c = toward_char(); + if (c == '\0') { + return std::format("{}-{}", type_id_, pattern_id_); + } + return std::format("{}-{}{}", type_id_, pattern_id_, c); +} + +constexpr auto operator==(const Group &lhs, const Group &rhs) { + return lhs.type_id_ == rhs.type_id_ + && lhs.pattern_id_ == rhs.pattern_id_ + && lhs.toward_ == rhs.toward_; +} + constexpr Group Group::unsafe_create(uint32_t type_id, uint32_t pattern_id, Toward toward) { return {type_id, pattern_id, toward}; } diff --git a/src/core/group/internal/group_union.inl b/src/core/group/internal/group_union.inl index c457977..9de5699 100644 --- a/src/core/group/internal/group_union.inl +++ b/src/core/group/internal/group_union.inl @@ -53,6 +53,10 @@ constexpr uint32_t GroupUnion::max_group_size() const { // return std::nullopt; //} +constexpr auto operator==(const GroupUnion &lhs, const GroupUnion &rhs) { + return lhs.type_id_ == rhs.type_id_; +} + // ----------------------------------------------------------------------------------------- // constexpr uint32_t GroupUnion::pattern_num() const { diff --git a/src/core/main.cc b/src/core/main.cc index 84ccbda..dd8be80 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -59,32 +59,34 @@ int main() { // std::cout << data_s.size() << std::endl; // std::cout << data_c.size() << std::endl; -// auto group = Group::from_common_code(CommonCode::unsafe_create(0x1A9BF0C00)); -// std::cout << group.type_id() << std::endl; -// std::cout << group.pattern_id() << std::endl; -// std::cout << (int)group.toward() << std::endl; - - auto info_1 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00)); - std::cout << std::format("{}-{}-{}-{}\n", info_1.group.type_id(), info_1.group.pattern_id(), (int)info_1.group.toward(), info_1.case_id); - auto code_1 = GroupCases::obtain_code(info_1); - std::cout << code_1 << std::endl; - - auto info_2 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code()); - std::cout << std::format("{}-{}-{}-{}\n", info_2.group.type_id(), info_2.group.pattern_id(), (int)info_2.group.toward(), info_2.case_id); - auto code_2 = GroupCases::obtain_code(info_2); - std::cout << code_2 << std::endl; - - GroupCases::build(); - - auto info_3 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00)); - std::cout << std::format("{}-{}-{}-{}\n", info_3.group.type_id(), info_3.group.pattern_id(), (int)info_3.group.toward(), info_3.case_id); - auto code_3 = GroupCases::obtain_code(info_3); - std::cout << code_3 << std::endl; - - auto info_4 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code()); - std::cout << std::format("{}-{}-{}-{}\n", info_4.group.type_id(), info_4.group.pattern_id(), (int)info_4.group.toward(), info_4.case_id); - auto code_4 = GroupCases::obtain_code(info_4); - std::cout << code_4 << std::endl; + auto group = Group::from_common_code(CommonCode::unsafe_create(0x1A9BF0C00)); + std::cout << group.type_id() << std::endl; + std::cout << group.pattern_id() << std::endl; + std::cout << (int)group.toward() << std::endl; + std::cout << group.toward_char() << std::endl; + std::cout << group.to_string() << std::endl; + +// auto info_1 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00)); +// std::cout << std::format("{}-{}-{}-{}\n", info_1.group.type_id(), info_1.group.pattern_id(), (int)info_1.group.toward(), info_1.case_id); +// auto code_1 = GroupCases::obtain_code(info_1); +// std::cout << code_1 << std::endl; +// +// auto info_2 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code()); +// std::cout << std::format("{}-{}-{}-{}\n", info_2.group.type_id(), info_2.group.pattern_id(), (int)info_2.group.toward(), info_2.case_id); +// auto code_2 = GroupCases::obtain_code(info_2); +// std::cout << code_2 << std::endl; +// +// GroupCases::build(); +// +// auto info_3 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00)); +// std::cout << std::format("{}-{}-{}-{}\n", info_3.group.type_id(), info_3.group.pattern_id(), (int)info_3.group.toward(), info_3.case_id); +// auto code_3 = GroupCases::obtain_code(info_3); +// std::cout << code_3 << std::endl; +// +// auto info_4 = GroupCases::obtain_info(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code()); +// std::cout << std::format("{}-{}-{}-{}\n", info_4.group.type_id(), info_4.group.pattern_id(), (int)info_4.group.toward(), info_4.case_id); +// auto code_4 = GroupCases::obtain_code(info_4); +// std::cout << code_4 << std::endl; // const auto common_code = CommonCode::unsafe_create(0x1A9BF0C00); // const auto group = Group::from_common_code(common_code);