From 6d5595b8b3c5591869be6f5e62fc297401eb6a5f Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 15 Apr 2023 14:59:40 +0800 Subject: [PATCH] perf: cal group size --- src/klotski_core/group/build_cases.cc | 35 +++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/klotski_core/group/build_cases.cc b/src/klotski_core/group/build_cases.cc index eb9f860..d9c2a72 100644 --- a/src/klotski_core/group/build_cases.cc +++ b/src/klotski_core/group/build_cases.cc @@ -13,11 +13,7 @@ using Common::check_range; using Common::range_reverse; uint32_t Group::group_size(const GroupId &group_id) { - return group_cases(group_seed(group_id)).size(); -} - -uint32_t Group::group_size(const RawCode &raw_code) { - return group_cases(raw_code).size(); + return group_size(group_seed(group_id)); } uint32_t Group::group_size(const CommonCode &common_code) { @@ -67,12 +63,35 @@ std::vector Group::all_cases(const TypeId &type_id) { return all_cases; } +uint32_t Group::group_size(const RawCode &raw_code) { + std::queue cache({raw_code.unwrap()}); + absl::flat_hash_map cases; // + cases.reserve(Group::group_max_size(raw_code)); + cases.emplace(raw_code.unwrap(), 0b0); // without mask + + auto core = Core( + [&cache, &cases](auto &&code, auto &&mask) { // callback function + auto current = cases.find(code); + if (current != cases.end()) { + current->second |= mask; // update mask + return; + } + cases.emplace(code, mask); + cache.emplace(code); + } + ); + while (!cache.empty()) { // until BFS without elements + core.next_cases(cache.front(), cases.find(cache.front())->second); + cache.pop(); // case dequeue + } + return cases.size(); +} + std::vector Group::group_cases(const RawCode &raw_code) { - std::queue cache; + std::queue cache({raw_code.unwrap()}); absl::flat_hash_map cases; // cases.reserve(Group::group_max_size(raw_code)); - cases.emplace(raw_code.unwrap(), 0); // without mask - cache.emplace(raw_code.unwrap()); + cases.emplace(raw_code.unwrap(), 0b0); // without mask auto core = Core( [&cache, &cases](auto &&code, auto &&mask) { // callback function