From e7fb997fbee4d5cf1904acff3bbd11e8dd00e7b7 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 15 Apr 2023 10:34:27 +0800 Subject: [PATCH] update: search group seed --- src/klotski_core/group/build_cases.cc | 76 ++++++++++++++++++++------- src/klotski_core/group/group.h | 4 +- src/klotski_core/group/seeds.cc | 18 ++----- 3 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/klotski_core/group/build_cases.cc b/src/klotski_core/group/build_cases.cc index ed72b1f..3102dc4 100644 --- a/src/klotski_core/group/build_cases.cc +++ b/src/klotski_core/group/build_cases.cc @@ -47,12 +47,16 @@ std::vector Group::all_cases(const TypeId &type_id) { return all_cases; } -absl::flat_hash_map expansion(const RawCode &entry) { +//absl::flat_hash_map expansion(const RawCode &entry) { +// +//} + +std::vector Group::group_cases(const RawCode &raw_code) { std::queue cache; absl::flat_hash_map cases; // - cases.reserve(Group::group_max_size(entry)); - cases.emplace(entry.unwrap(), 0); // without mask - cache.emplace(entry.unwrap()); + cases.reserve(Group::group_max_size(raw_code)); + cases.emplace(raw_code.unwrap(), 0); // without mask + cache.emplace(raw_code.unwrap()); auto core = Core( [&cache, &cases](auto &&code, auto &&mask) { // callback function @@ -69,11 +73,7 @@ absl::flat_hash_map expansion(const RawCode &entry) { core.next_cases(cache.front(), cases.find(cache.front())->second); cache.pop(); // case dequeue } - return cases; -} -std::vector Group::group_cases(const RawCode &raw_code) { - auto cases = expansion(raw_code); auto result = std::vector(); result.reserve(cases.size()); for (auto &&tmp : cases) { // export group cases @@ -86,27 +86,65 @@ std::vector Group::group_cases(const CommonCode &common_code) { return group_cases(RawCode::from_common_code(common_code)); } +CommonCode Group::group_seed(const RawCode &raw_code) { -// TODO: refactor build_group -> using GROUP_SEEDS -std::vector Group::build_group(uint32_t type_id, uint32_t group_id) { +// auto t = expansion(raw_code); +// +// auto k = t.begin(); +// +// auto cmp = [](const std::pair &p1, const std::pair &p2) { +// return RawCode::unsafe_create(p1.first).to_common_code() < RawCode::unsafe_create(p2.first).to_common_code(); +// }; +// +// return std::min_element(t.begin(), t.end(), cmp)->first; - auto offset = TYPE_ID_OFFSET[type_id]; - std::cout << "size: " << TYPE_ID_GROUP_NUM[type_id] << std::endl; + auto cases = group_cases(raw_code); + std::vector group(cases.begin(), cases.end()); - auto k = GROUP_SEEDS_INDEX[offset + group_id]; + return *std::min_element(group.begin(), group.end()); - std::cout << "tmp index: " << k << std::endl; - auto r = k + offset; +// std::vector group; +// +// for (auto &&tmp : expansion(raw_code)) { +// group.emplace_back(RawCode::unsafe_create(tmp.first).to_common_code()); +// } +// +// return *std::min_element(group.begin(), group.end()); +} + +CommonCode Group::group_seed(const CommonCode &common_code) { - std::cout << "real index: " << r << std::endl; + return group_seed(common_code.to_raw_code()); + +} + +// TODO: refactor build_group -> using GROUP_SEEDS +std::vector Group::build_group(const GroupId &group_id) { + +// auto offset = TYPE_ID_OFFSET[type_id]; +// +// std::cout << "size: " << TYPE_ID_GROUP_NUM[type_id] << std::endl; +// +// auto k = GROUP_SEEDS_INDEX[offset + group_id]; +// +// std::cout << "tmp index: " << k << std::endl; +// +// auto r = k + offset; +// +// std::cout << "real index: " << r << std::endl; +// +// auto seed = CommonCode(GROUP_SEEDS[r]); +// +// std::cout << "seed: " << seed << std::endl; +// +// std::cout << RawCode(seed) << std::endl; - auto seed = CommonCode(GROUP_SEEDS[r]); + auto seed = group_seed(group_id); - std::cout << "seed: " << seed << std::endl; + return group_cases(seed); - std::cout << RawCode(seed) << std::endl; // uint32_t group_num = 0; // auto all_cases = Group::all_cases(type_id); // load all cases of type_id diff --git a/src/klotski_core/group/group.h b/src/klotski_core/group/group.h index 1473672..6a886a9 100644 --- a/src/klotski_core/group/group.h +++ b/src/klotski_core/group/group.h @@ -79,9 +79,9 @@ class Group { public: /// ----------------------------------- group seeds ----------------------------------- + static CommonCode group_seed(const GroupId &group_id); static CommonCode group_seed(const RawCode &raw_code); static CommonCode group_seed(const CommonCode &common_code); - static CommonCode group_seed(uint32_t type_id, uint32_t group_id); /// Get all seeds in the specified type id. static std::vector group_seeds(const TypeId &type_id); @@ -99,7 +99,7 @@ public: static std::vector> build_groups(const TypeId &type_id); /// Calculate the specified group using type_id and group_id. - static std::vector build_group(uint32_t type_id, uint32_t group_id); + static std::vector build_group(const GroupId &group_id); /// ----------------------------------- group info ------------------------------------ diff --git a/src/klotski_core/group/seeds.cc b/src/klotski_core/group/seeds.cc index 3355806..a26fb26 100644 --- a/src/klotski_core/group/seeds.cc +++ b/src/klotski_core/group/seeds.cc @@ -8,15 +8,17 @@ namespace klotski { -CommonCode Group::group_seed(uint32_t type_id, uint32_t group_id) { +CommonCode Group::group_seed(const GroupId &group_id) { // TODO: check value + auto type_id = group_id.type_id(); + auto offset = TYPE_ID_OFFSET[type_id]; std::cout << "size: " << TYPE_ID_GROUP_NUM[type_id] << std::endl; - auto k = GROUP_SEEDS_INDEX[offset + group_id]; + auto k = GROUP_SEEDS_INDEX[offset + group_id.unwrap()]; std::cout << "tmp index: " << k << std::endl; @@ -38,18 +40,6 @@ std::vector Group::group_seeds(const TypeId &type_id) { return {offset, offset + TYPE_ID_GROUP_NUM[type_id.unwrap()]}; } -CommonCode Group::group_seed(const RawCode &raw_code) { - - - - return CommonCode(0); -} -CommonCode Group::group_seed(const CommonCode &common_code) { - - - - return CommonCode(0); -} } // namespace klotski