From b8bb0d2ef55182641843d04e44cb2a8e80bd4a1d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 9 Apr 2023 13:25:58 +0800 Subject: [PATCH] feat: search group cases --- src/klotski_core/ffi/tmain.cc | 7 ++++-- src/klotski_core/group/group.cc | 43 ++++++++++++--------------------- src/klotski_core/group/group.h | 8 +++++- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 7b1d7a2..f279941 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -56,11 +56,14 @@ void tmain() { // Group::demo(RawCode::from_common_code(0x1A9BF0C00)); // Group::demo(RawCode::from_common_code(0x4FEA13400)); -// std::cout << Group::demo(RawCode::from_common_code(0x1A9BF0C00)) << std::endl; +// auto ret = Group::group_cases(RawCode::from_common_code(0x1A9BF0C00)); +// std::cout << ret.size() << std::endl; +// std::cout << ret[0] << std::endl; +// std::cout << ret[1] << std::endl; auto tmp = RawCode::from_common_code(0x1A9BF0C00); for (int i = 0; i < 100; ++i) { - Group::demo(tmp); + Group::group_cases(tmp); } std::cout << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl; diff --git a/src/klotski_core/group/group.cc b/src/klotski_core/group/group.cc index 4ef38b1..4ac2a61 100644 --- a/src/klotski_core/group/group.cc +++ b/src/klotski_core/group/group.cc @@ -1,4 +1,5 @@ #include +#include #include "core.h" #include "group.h" #include "common.h" @@ -46,51 +47,37 @@ Group::block_num_t Group::block_num(const CommonCode &common_code) { return result; } -uint32_t Group::demo(const RawCode &seed) { +std::vector Group::group_cases(const RawCode &seed) { std::queue cache; - -// uint32_t index = 0; -// std::vector temp; -// temp.reserve(65535 * 8); - - absl::flat_hash_map cases; - - cases.reserve(65535 * 8); - + absl::flat_hash_map cases; // + cases.reserve(max_group_size(seed)); cases.emplace(seed.unwrap(), 0); // without mask cache.emplace(seed.unwrap()); -// temp.emplace_back(seed.unwrap()); - auto core = Core( - [&cases, &cache](auto &&code, auto &&mask) { + [&cache, &cases](auto &&code, auto &&mask) { // callback function auto current = cases.find(code); - if (current != cases.end()) { - current->second |= mask; // update mask info + current->second |= mask; // update mask return; } - cases.emplace(code, mask); cache.emplace(code); -// temp.emplace_back(code); - } ); - - while (!cache.empty()) { -// while (index != temp.size()) { - + while (!cache.empty()) { // until BFS without elements core.next_cases(cache.front(), cases.find(cache.front())->second); -// core.next_cases(temp[index], cases.find(temp[index])->second); - - cache.pop(); -// ++index; - + cache.pop(); // case dequeue } - return cases.size(); + auto result = std::vector(); + result.reserve(cases.size()); + for (auto &&raw_code : cases) { // export group cases + result.emplace_back(RawCode::unsafe_create(raw_code.first)); + } + return result; } + } // namespace klotski diff --git a/src/klotski_core/group/group.h b/src/klotski_core/group/group.h index 9a3e184..956db25 100644 --- a/src/klotski_core/group/group.h +++ b/src/klotski_core/group/group.h @@ -15,10 +15,16 @@ public: uint8_t n_2x1 = 0; }; +// static block_num_t block_num(uint32_t type_id); static block_num_t block_num(const RawCode &raw_code); static block_num_t block_num(const CommonCode &common_code); - static uint32_t demo(const RawCode &seed); +// static uint32_t max_group_size(uint32_t type_id); + static uint32_t max_group_size(const RawCode &raw_code) { + return 65535 * 8; + }; + + static std::vector group_cases(const RawCode &seed); };