Browse Source

feat: parse group info

master
Dnomd343 2 years ago
parent
commit
bcd83aab83
  1. 22
      src/klotski_core/ffi/tmain.cc
  2. 25
      src/klotski_core/group/group_info.cc

22
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;

25
src/klotski_core/group/group_info.cc

@ -2,8 +2,6 @@
#include "group.h"
#include "type_id.h"
#include <iostream>
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<CommonCode> 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<CommonCode> 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

Loading…
Cancel
Save