From bcd83aab83e25da899bf4d61809a19c020e4c664 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 15 Apr 2023 19:55:23 +0800 Subject: [PATCH] feat: parse group info --- src/klotski_core/ffi/tmain.cc | 22 +++++++++++++++++----- src/klotski_core/group/group_info.cc | 25 ++++++++++++------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/klotski_core/ffi/tmain.cc b/src/klotski_core/ffi/tmain.cc index 42ce27d..f1d355f 100644 --- a/src/klotski_core/ffi/tmain.cc +++ b/src/klotski_core/ffi/tmain.cc @@ -38,11 +38,23 @@ void tmain() { // auto start = clock(); // auto ret = GroupCase::encode(CommonCode(0x1A9BF0C00)); - auto ret = GroupCase::encode(CommonCode(0x4FEA13400)); - - std::cout << "type id: " << ret.type_id << std::endl; - std::cout << "group id: " << ret.group_id << std::endl; - std::cout << "group index: " << ret.group_index << std::endl; +// auto ret = GroupCase::encode(CommonCode(0x4FEA13400)); +// +// std::cout << "type id: " << ret.type_id << std::endl; +// std::cout << "group id: " << ret.group_id << std::endl; +// std::cout << "group index: " << ret.group_index << std::endl; + + std::cout << GroupCase::parse({ + .type_id = 169, + .group_id = 1, + .group_index = 7472, + }) << std::endl; + + std::cout << GroupCase::parse({ + .type_id = 164, + .group_id = 0, + .group_index = 30833, + }) << std::endl; // std::cerr << (clock() - start) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; diff --git a/src/klotski_core/group/group_info.cc b/src/klotski_core/group/group_info.cc index 8bb9335..bc57661 100644 --- a/src/klotski_core/group/group_info.cc +++ b/src/klotski_core/group/group_info.cc @@ -2,8 +2,6 @@ #include "group.h" #include "type_id.h" -#include - namespace klotski { /// --------------------------------------- Group Type ---------------------------------------- @@ -38,11 +36,21 @@ GroupCase::info_t GroupCase::encode(const RawCode &raw_code) noexcept { return encode(raw_code.to_common_code()); } -GroupCase::info_t GroupCase::encode(const CommonCode &common_code) noexcept { - auto cases = Group::cases(common_code); +CommonCode GroupCase::parse(const info_t &info) { + auto cases = Group(info.type_id, info.group_id).cases(); + if (cases.size() <= info.group_index) { + throw std::invalid_argument("group index overflow"); // check group index + } + std::vector group(cases.begin(), cases.end()); + std::nth_element(group.begin(), group.begin() + info.group_index, group.end()); + return group[info.group_index]; // located nth as target +} +GroupCase::info_t GroupCase::encode(const CommonCode &common_code) noexcept { uint32_t group_index = 0; + auto cases = Group::cases(common_code); + std::vector group(cases.begin(), cases.end()); for (auto &&code: group) { if (code < common_code) { ++group_index; // locate group index @@ -58,13 +66,4 @@ GroupCase::info_t GroupCase::encode(const CommonCode &common_code) noexcept { }; } -CommonCode GroupCase::parse(const info_t &info) { - - // TODO: check group index - - // TODO: function body - - return CommonCode(0); -} - } // namespace klotski