From bf56576eb03363362f78f06d6a4b5cac74e2dd07 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 29 Apr 2023 21:29:53 +0800 Subject: [PATCH] perf: group module --- src/klotski_core/group/build_cases.cc | 18 +++++++------- src/klotski_core/group/group.h | 36 ++++++++++++++++++--------- src/klotski_core/group/group_info.cc | 9 +++---- src/klotski_core/group/seeds.cc | 5 +--- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/klotski_core/group/build_cases.cc b/src/klotski_core/group/build_cases.cc index e5a8ce3..7fccdd4 100644 --- a/src/klotski_core/group/build_cases.cc +++ b/src/klotski_core/group/build_cases.cc @@ -14,7 +14,7 @@ using Common::range_reverse; /// --------------------------------------- Group Type ---------------------------------------- -std::vector GroupType::cases() const noexcept { +CommonCodes GroupType::cases() const noexcept { std::vector ranges; // basic ranges of type_id ranges.reserve(TYPE_ID_SIZE[type_id_]); // over-allocation @@ -48,9 +48,9 @@ std::vector GroupType::cases() const noexcept { return all_cases; } -std::vector> GroupType::groups() const noexcept { +std::vector GroupType::groups() const noexcept { auto all_cases = GroupType::cases(); - std::vector> groups; + std::vector groups; auto min = std::min_element(all_cases.begin(), all_cases.end()); // search min CommonCode auto first_group = Group::cases(min->to_raw_code()); // expand the first group @@ -60,18 +60,18 @@ std::vector> GroupType::groups() const noexcept { } absl::btree_set cases(all_cases.begin(), all_cases.end()); - for (auto &&tmp : *groups.begin()) { + for (auto &&tmp : groups.front()) { cases.erase(tmp); // remove elements from first group } while (!cases.empty()) { auto group = Group::cases(cases.begin()->to_raw_code()); groups.emplace_back(group.begin(), group.end()); // release new group - for (auto &&tmp : *(groups.end() - 1)) { + for (auto &&tmp : groups.back()) { cases.erase(tmp); // remove selected cases } } - auto cmp_func = [](const std::vector &v1, const std::vector &v2) { + auto cmp_func = [](const CommonCodes &v1, const CommonCodes &v2) { return v1.size() > v2.size(); // sort by vector size }; std::stable_sort(groups.begin(), groups.end(), cmp_func); // using stable sort for ordered index @@ -114,15 +114,15 @@ uint32_t Group::size(const RawCode &raw_code) noexcept { /// --------------------------------------- Group Cases --------------------------------------- -std::vector Group::cases() const noexcept { +RawCodes Group::cases() const noexcept { return cases(seed()); } -std::vector Group::cases(const CommonCode &common_code) noexcept { +RawCodes Group::cases(const CommonCode &common_code) noexcept { return cases(common_code.to_raw_code()); } -std::vector Group::cases(const RawCode &raw_code) noexcept { +RawCodes Group::cases(const RawCode &raw_code) noexcept { std::queue cache({raw_code.unwrap()}); absl::flat_hash_map cases; // cases.reserve(GroupType::max_size(raw_code)); diff --git a/src/klotski_core/group/group.h b/src/klotski_core/group/group.h index 3787331..74be9bf 100644 --- a/src/klotski_core/group/group.h +++ b/src/klotski_core/group/group.h @@ -113,7 +113,7 @@ public: std::vector cases() const noexcept; /// Calculate all groups of the current type_id. - std::vector> groups() const noexcept; + std::vector groups() const noexcept; }; /// ------------------------------------------ Group ------------------------------------------ @@ -145,9 +145,9 @@ public: static CommonCode seed(const CommonCode &common_code) noexcept; /// Calculate the current group. - std::vector cases() const noexcept; - static std::vector cases(const RawCode &raw_code) noexcept; - static std::vector cases(const CommonCode &common_code) noexcept; + RawCodes cases() const noexcept; + static RawCodes cases(const RawCode &raw_code) noexcept; + static RawCodes cases(const CommonCode &common_code) noexcept; }; /// --------------------------------------- Group Case ---------------------------------------- @@ -177,22 +177,34 @@ public: inline bool operator==(const GroupType &t1, const GroupType &t2) { return t1.unwrap() == t2.unwrap(); } +inline bool operator!=(const GroupType &t1, const GroupType &t2) { + return !(t1 == t2); +} inline bool operator==(const Group &g1, const Group &g2) { - return g1.type_id() == g2.type_id() && g1.unwrap() == g2.unwrap(); + return g1.type_id() == g2.type_id() + && g1.unwrap() == g2.unwrap(); +} +inline bool operator!=(const Group &g1, const Group &g2) { + return !(g1 == g2); } inline bool operator==(const GroupType::block_num_t &b1, const GroupType::block_num_t &b2) { - return (b1.n_1x1 == b2.n_1x1) && (b1.n_1x2 == b2.n_1x2) && (b1.n_2x1 == b2.n_2x1); + return (b1.n_1x1 == b2.n_1x1) + && (b1.n_1x2 == b2.n_1x2) + && (b1.n_2x1 == b2.n_2x1); +} +inline bool operator!=(const GroupType::block_num_t &b1, const GroupType::block_num_t &b2) { + return !(b1 == b2); } inline bool operator==(const GroupCase::info_t &i1, const GroupCase::info_t &i2) { - return (i1.type_id == i2.type_id) && (i1.group_id == i2.group_id) && (i1.group_index == i2.group_index); + return (i1.type_id == i2.type_id) + && (i1.group_id == i2.group_id) + && (i1.group_index == i2.group_index); +} +inline bool operator!=(const GroupCase::info_t &i1, const GroupCase::info_t &i2) { + return !(i1 == i2); } - -inline bool operator!=(const Group &g1, const Group &g2) { return !(g1 == g2); } -inline bool operator!=(const GroupType &t1, const GroupType &t2) { return !(t1 == t2); } -inline bool operator!=(const GroupCase::info_t &i1, const GroupCase::info_t &i2) { return !(i1 == i2); } -inline bool operator!=(const GroupType::block_num_t &b1, const GroupType::block_num_t &b2) { return !(b1 == b2); } } // namespace klotski diff --git a/src/klotski_core/group/group_info.cc b/src/klotski_core/group/group_info.cc index 36390c6..eb236d5 100644 --- a/src/klotski_core/group/group_info.cc +++ b/src/klotski_core/group/group_info.cc @@ -44,17 +44,16 @@ CommonCode GroupCase::parse(const info_t &info) { throw std::invalid_argument("group index overflow"); // check group index } - std::vector group(cases.begin(), cases.end()); + auto group = CommonCode::convert(cases); 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) { + auto group = CommonCode::convert(Group::cases(common_code)); + for (auto &&tmp: group) { + if (tmp < common_code) { ++group_index; // locate group index } } diff --git a/src/klotski_core/group/seeds.cc b/src/klotski_core/group/seeds.cc index 7b32b22..2065604 100644 --- a/src/klotski_core/group/seeds.cc +++ b/src/klotski_core/group/seeds.cc @@ -3,8 +3,6 @@ #include "type_id.h" #include "group_seeds.h" -#include - namespace klotski { std::vector GroupType::seeds() const noexcept { @@ -19,8 +17,7 @@ CommonCode Group::seed() const noexcept { // group_id -> seed } CommonCode Group::seed(const RawCode &raw_code) noexcept { - auto cases = Group::cases(raw_code); - std::vector group(cases.begin(), cases.end()); + auto group = CommonCode::convert(Group::cases(raw_code)); return *std::min_element(group.begin(), group.end()); }