From ce91b0810ae3c7c89714bbde489d284da7325d19 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 15 Sep 2024 10:40:31 +0800 Subject: [PATCH] feat: spawn Group from cases --- src/core/group/group.h | 21 +++++++------ src/core/group/internal/group.cc | 20 ++++++++++++ src/core/group/internal/group.inl | 32 ++++++++++++++----- src/core/group/internal/group_union.inl | 8 ++--- src/core/main.cc | 42 ++----------------------- 5 files changed, 62 insertions(+), 61 deletions(-) diff --git a/src/core/group/group.h b/src/core/group/group.h index e232c70..a5e9ced 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -145,6 +145,8 @@ private: // ------------------------------------------------------------------------------------- // }; +// TODO: add debug output + class Group { public: Group() = delete; @@ -152,25 +154,25 @@ public: // ------------------------------------------------------------------------------------- // /// Get the original type id. - [[nodiscard]] uint32_t type_id() const; + [[nodiscard]] constexpr uint32_t type_id() const; /// Get the original group id. - [[nodiscard]] uint32_t group_id() const; + [[nodiscard]] constexpr uint32_t group_id() const; /// Create Group without any check. - static Group unsafe_create(uint32_t type_id, uint32_t group_id); + static constexpr Group unsafe_create(uint32_t type_id, uint32_t group_id); /// Create Group with validity check. - static std::optional create(uint32_t type_id, uint32_t group_id); + static constexpr std::optional create(uint32_t type_id, uint32_t group_id); // ------------------------------------------------------------------------------------- // - /// Get the number of cases contained. - [[nodiscard]] uint32_t size() const; - - /// Get all cases under the current group. + /// Get all cases under current group. [[nodiscard]] RangesUnion cases() const; + /// Get the number of klotski cases contained. + [[nodiscard]] constexpr uint32_t size() const; + // ------------------------------------------------------------------------------------- // /// Create Group from RawCode. @@ -188,7 +190,8 @@ private: uint32_t type_id_; uint32_t group_id_; - [[nodiscard]] uint32_t flat_id() const; + /// Tiled merge of type_id and group_id. + [[nodiscard]] constexpr uint32_t flat_id() const; public: /// Spawn all the unsorted codes of the current group. diff --git a/src/core/group/internal/group.cc b/src/core/group/internal/group.cc index bbbc7f8..983de0f 100644 --- a/src/core/group/internal/group.cc +++ b/src/core/group/internal/group.cc @@ -38,6 +38,7 @@ std::vector Group::extend(RawCode raw_code, uint32_t reserve) { RangesUnion Group::cases() const { // TODO: add white list for single-group unions + // return GroupUnion::cases directly auto seed = CommonCode::unsafe_create(GROUP_SEED[flat_id()]); @@ -60,3 +61,22 @@ RangesUnion Group::cases() const { return data; } + +Group Group::from_raw_code(codec::RawCode raw_code) { + + auto raw_codes = extend(raw_code); + + auto common_codes = raw_codes | std::views::transform([](const RawCode r) { + return r.to_common_code(); + }) | std::ranges::to(); // TODO: search min_element directly + + auto seed = std::min_element(common_codes.begin(), common_codes.end()); + + std::cout << *seed << std::endl; + + // TODO: search type_id / group_id from map + auto flat_id = std::find(GROUP_SEED.begin(), GROUP_SEED.end(), *seed) - GROUP_SEED.begin(); + std::cout << flat_id << std::endl; + + return Group::unsafe_create(0, 0); // TODO: only for compile +} diff --git a/src/core/group/internal/group.inl b/src/core/group/internal/group.inl index 52bec79..36aeff7 100644 --- a/src/core/group/internal/group.inl +++ b/src/core/group/internal/group.inl @@ -4,31 +4,47 @@ namespace klotski::cases { -inline uint32_t Group::size() const { +// ----------------------------------------------------------------------------------------- // + +constexpr uint32_t Group::size() const { return GROUP_SIZE[flat_id()]; } -inline uint32_t Group::flat_id() const { +constexpr uint32_t Group::type_id() const { + return type_id_; +} + +constexpr uint32_t Group::group_id() const { + return group_id_; +} + +constexpr uint32_t Group::flat_id() const { return GROUP_OFFSET[type_id_] + group_id_; } -inline uint32_t Group::type_id() const { - return type_id_; +// ----------------------------------------------------------------------------------------- // + +inline Group Group::from_common_code(codec::CommonCode common_code) { + return from_raw_code(common_code.to_raw_code()); } -inline uint32_t Group::group_id() const { - return group_id_; +inline Group Group::from_short_code(codec::ShortCode short_code) { + return from_common_code(short_code.to_common_code()); } -inline Group Group::unsafe_create(const uint32_t type_id, const uint32_t group_id) { +// ----------------------------------------------------------------------------------------- // + +constexpr Group Group::unsafe_create(const uint32_t type_id, const uint32_t group_id) { return std::bit_cast(static_cast(group_id) << 32 | type_id); } -inline std::optional Group::create(const uint32_t type_id, const uint32_t group_id) { +constexpr std::optional Group::create(const uint32_t type_id, const uint32_t group_id) { if (type_id < TYPE_ID_LIMIT && group_id < GROUP_NUM[type_id]) { return unsafe_create(type_id, group_id); } return std::nullopt; } +// ----------------------------------------------------------------------------------------- // + } // namespace klotski::cases diff --git a/src/core/group/internal/group_union.inl b/src/core/group/internal/group_union.inl index e11a961..c412bef 100644 --- a/src/core/group/internal/group_union.inl +++ b/src/core/group/internal/group_union.inl @@ -6,7 +6,7 @@ namespace klotski::cases { -// ------------------------------------------------------------------------------------- // +// ----------------------------------------------------------------------------------------- // constexpr uint32_t GroupUnion::unwrap() const { return type_id_; @@ -23,7 +23,7 @@ constexpr std::optional GroupUnion::create(const uint32_t type_id) { return std::nullopt; } -// ------------------------------------------------------------------------------------- // +// ----------------------------------------------------------------------------------------- // constexpr uint32_t GroupUnion::size() const { return GROUP_UNION_SIZE[type_id_]; @@ -53,7 +53,7 @@ inline std::optional GroupUnion::group(const uint32_t group_id) const { return std::nullopt; } -// ------------------------------------------------------------------------------------- // +// ----------------------------------------------------------------------------------------- // inline GroupUnion GroupUnion::from_raw_code(const codec::RawCode raw_code) { return unsafe_create(type_id(raw_code)); @@ -67,6 +67,6 @@ inline GroupUnion GroupUnion::from_common_code(const codec::CommonCode common_co return unsafe_create(type_id(common_code)); } -// ------------------------------------------------------------------------------------- // +// ----------------------------------------------------------------------------------------- // } // namespace klotski::cases diff --git a/src/core/main.cc b/src/core/main.cc index f6861dc..e55c65f 100644 --- a/src/core/main.cc +++ b/src/core/main.cc @@ -40,48 +40,10 @@ int main() { const auto start = std::chrono::system_clock::now(); - // auto ret = CommonCode::is_mirror(0x1A9BF0C00); - // auto ret = CommonCode::is_mirror(0x4FEA13400); - // auto ret = CommonCode::is_mirror(0x100AA0300); - // auto ret = CommonCode::is_mirror(0x5000A0000); - // std::cout << "ret = " << ret << std::endl; - - // auto raw_code = CommonCode::unsafe_create(0x1A9BF0C00).to_raw_code(); - // std::cout << raw_code.is_horizontal_mirror() << std::endl; - - for (auto common_code : AllCases::instance().fetch().codes()) { - auto raw_code = common_code.to_raw_code(); - - // bool r1 = raw_code.is_horizontal_mirror(); - // bool r2 = common_code.is_horizontal_mirror(); - // - // if (r1 != r2) { - // std::cout << "found invalid" << std::endl; - // std::cout << common_code << std::endl; - // break; - // } - // - // // if (raw_code.is_vertical_mirror()) { - // // std::cout << "found invalid" << std::endl; - // // break; - // // } - - // auto rr = raw_code.to_vertical_mirror(); - // auto cc = common_code.to_vertical_mirror(); - auto rr = raw_code.to_horizontal_mirror(); - auto cc = common_code.to_horizontal_mirror(); - if (cc.to_raw_code() != rr) { - std::cout << "found invalid" << std::endl; - std::cout << common_code << std::endl; - break; - } - - } - - + const auto common_code = CommonCode::unsafe_create(0x1A9BF0C00); + const auto group = Group::from_common_code(common_code); // GroupCases::from_info_t({169, 1, 7472}); - // GroupCases::to_info_t(CommonCode::unsafe_create(0x1A9BF0C00).to_short_code()); std::cerr << std::chrono::system_clock::now() - start << std::endl;