From 5c710620af7b2095447f7b6855c4f0e257c088e1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 13 Apr 2023 18:38:27 +0800 Subject: [PATCH] feat: build single group --- src/klotski_core/ffi/tmain.cc | 12 ++++++++++++ src/klotski_core/group/build_cases.cc | 23 ++++++++++++++++++++--- src/klotski_core/group/group.h | 3 +++ test/group/build_cases.cc | 4 ++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index e56ce49..aa69fcf 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -33,6 +33,18 @@ using klotski::TYPE_ID_LIMIT; void tmain() { // printf("tmain start\n"); + + auto ret = Group::build_group(123, 0); + + std::cout << ret.size() << std::endl; + + std::cout << ret[0] << std::endl; + std::cout << ret[1] << std::endl; + std::cout << ret[2] << std::endl; + std::cout << ret[3] << std::endl; + + return; + // struct group_info_t { // uint16_t type_id; // uint16_t group_id; diff --git a/src/klotski_core/group/build_cases.cc b/src/klotski_core/group/build_cases.cc index 7f1be54..fa91e04 100644 --- a/src/klotski_core/group/build_cases.cc +++ b/src/klotski_core/group/build_cases.cc @@ -74,17 +74,34 @@ std::vector Group::group_cases(const RawCode &seed) { return result; } +std::vector Group::build_group(uint32_t type_id, uint32_t group_id) { + uint32_t group_num = 0; + auto all_cases = Group::all_cases(type_id); // load all cases of type_id + std::set cases(all_cases.begin(), all_cases.end()); + + while (!cases.empty()) { + if (group_id == group_num) { // found target group + auto group = group_cases(cases.begin()->to_raw_code()); + return {group.begin(), group.end()}; + } + for (auto &&tmp : group_cases(cases.begin()->to_raw_code())) { + cases.erase(tmp.to_common_code()); // remove from global union + } + ++group_num; + } + return {}; // group_id out of range +} + std::vector> Group::build_groups(uint32_t type_id) { auto all_cases = Group::all_cases(type_id); std::vector> groups; auto min = std::min_element(all_cases.begin(), all_cases.end()); // search min CommonCode - auto first_group = Group::group_cases(min->to_raw_code()); // expand the first group + auto first_group = group_cases(min->to_raw_code()); // expand the first group groups.emplace_back(first_group.begin(), first_group.end()); if (first_group.size() == all_cases.size()) { // only contains one group return groups; } - // TODO: do not insert the elements in first group std::set cases(all_cases.begin(), all_cases.end()); for (auto &&tmp : groups[0]) { cases.erase(tmp); // remove elements in first group @@ -92,7 +109,7 @@ std::vector> Group::build_groups(uint32_t type_id) { while (!cases.empty()) { groups.emplace_back(); // create empty vector auto current_group = groups.end() - 1; // insert into latest - for (auto &&tmp : Group::group_cases(cases.begin()->to_raw_code())) { + for (auto &&tmp : group_cases(cases.begin()->to_raw_code())) { auto common_code = tmp.to_common_code(); current_group->emplace_back(common_code); // insert into current group cases.erase(common_code); // remove from global union diff --git a/src/klotski_core/group/group.h b/src/klotski_core/group/group.h index 2a97537..b2fb9e5 100644 --- a/src/klotski_core/group/group.h +++ b/src/klotski_core/group/group.h @@ -40,6 +40,9 @@ public: /// Calculate all groups in the specified type_id. static std::vector> build_groups(uint32_t 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); + /// ---------------------------- xxxxxxxxxxxxxxxxx ---------------------------- diff --git a/test/group/build_cases.cc b/test/group/build_cases.cc index 7d29446..21385b4 100644 --- a/test/group/build_cases.cc +++ b/test/group/build_cases.cc @@ -69,6 +69,10 @@ TEST(Group, group_cases) { EXPECT_EQ(all_cases, AllCases::release()); // verify all released cases } +TEST(Group, build_group) { + +} + TEST(Group, build_groups) { struct group_info_t { uint16_t group_id;