From 470d7415e79094f9710d8ecaa2d1f004d52bb1e6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sat, 2 Nov 2024 16:30:07 +0800 Subject: [PATCH] refactor: remove exposed group extend interface --- src/core/CMakeLists.txt | 1 - src/core/benchmark/group.cc | 21 +++++++- src/core/group/group.h | 3 -- src/core/group/internal/extend.cc | 42 ---------------- src/core/group/internal/group.cc | 66 ++++++++++++++++++++++++-- src/core/group/internal/group.inl | 8 ++-- src/core/group/internal/group_cases.cc | 34 ++++++++++++- 7 files changed, 119 insertions(+), 56 deletions(-) delete mode 100644 src/core/group/internal/extend.cc diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 3183728..b9664fb 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -23,7 +23,6 @@ set(KLOTSKI_CORE_SRC fast_cal/internal/fast_cal.cc group/internal/group_union.cc - group/internal/extend.cc group/internal/group.cc group/internal/group_cases.cc diff --git a/src/core/benchmark/group.cc b/src/core/benchmark/group.cc index 5eca71b..37fe2fc 100644 --- a/src/core/benchmark/group.cc +++ b/src/core/benchmark/group.cc @@ -16,6 +16,9 @@ using klotski::cases::AllCases; using klotski::group::Group; using klotski::group::GroupUnion; +using klotski::codec::RawCode; +using klotski::codec::CommonCode; + /// Build all valid CommonCodes. static std::vector all_common_codes() { std::vector codes; @@ -110,7 +113,7 @@ static void GroupExtend(benchmark::State &state) { for (auto _ : state) { - volatile auto ret = klotski::group::Group_extend(src, 0); + // volatile auto ret = klotski::group::Group_extend(src, 0); // std::cout << ret.size() << std::endl; } @@ -263,6 +266,18 @@ static void SpawnGroups(benchmark::State &state) { } +static void GroupFromRawCode(benchmark::State &state) { + + auto code = CommonCode::unsafe_create(0x1A9BF0C00).to_raw_code(); + + for (auto _ : state) { + + volatile auto g = Group::from_raw_code(code); + + } + +} + // BENCHMARK(CommonCodeToTypeId)->Arg(8)->Arg(64)->Arg(256); // BENCHMARK(RawCodeToTypeId)->Arg(8)->Arg(64)->Arg(256); @@ -278,6 +293,8 @@ static void SpawnGroups(benchmark::State &state) { // BENCHMARK(RangesDerive)->Unit(benchmark::kMillisecond); -BENCHMARK(SpawnGroups); +// BENCHMARK(SpawnGroups); + +BENCHMARK(GroupFromRawCode)->Unit(benchmark::kMillisecond); BENCHMARK_MAIN(); diff --git a/src/core/group/group.h b/src/core/group/group.h index a3f9f17..2b2fe74 100644 --- a/src/core/group/group.h +++ b/src/core/group/group.h @@ -390,9 +390,6 @@ private: // ------------------------------------------------------------------------------------- // }; -/// Spawn all the unsorted codes of the current group. -std::vector Group_extend(codec::RawCode raw_code, uint32_t reserve = 0); - } // namespace klotski::group #include "internal/type_id.inl" diff --git a/src/core/group/internal/extend.cc b/src/core/group/internal/extend.cc deleted file mode 100644 index 304de53..0000000 --- a/src/core/group/internal/extend.cc +++ /dev/null @@ -1,42 +0,0 @@ -#include - -#include "mover/mover.h" -#include "group/group.h" - -using klotski::codec::RawCode; -using klotski::codec::CommonCode; -using klotski::cases::RangesUnion; - -using klotski::mover::MaskMover; -using klotski::group::GroupUnion; - -// TODO: maybe we can perf with mirror cases - -std::vector klotski::group::Group_extend(RawCode raw_code, uint32_t reserve) { - std::vector codes; - phmap::flat_hash_map cases; // -// reserve = reserve ? reserve : GroupUnion::from_raw_code(raw_code).max_group_size(); -// reserve = 25955; - codes.reserve(GroupUnion::from_raw_code(raw_code).max_group_size()); - cases.reserve(25955 * 1.56); - - auto core = MaskMover([&codes, &cases](RawCode code, uint64_t mask) { - if (const auto match = cases.find(code.unwrap()); match != cases.end()) { - match->second |= mask; // update mask - return; - } - cases.emplace(code, mask); - codes.emplace_back(code); // new case - }); - - uint64_t offset = 0; - codes.emplace_back(raw_code); - cases.emplace(raw_code, 0); // without mask - while (offset != codes.size()) { - auto curr = codes[offset++].unwrap(); - core.next_cases(RawCode::unsafe_create(curr), cases.find(curr)->second); - } -// std::cout << cases.size() << std::endl; -// std::cout << cases.load_factor() << std::endl; - return codes; -} diff --git a/src/core/group/internal/group.cc b/src/core/group/internal/group.cc index 502d675..d231a79 100644 --- a/src/core/group/internal/group.cc +++ b/src/core/group/internal/group.cc @@ -2,15 +2,50 @@ #include "group/group.h" +#include + +#include + using klotski::codec::RawCode; using klotski::codec::CommonCode; using klotski::group::Group; +using klotski::group::GroupUnion; using klotski::cases::RangesUnion; +using klotski::mover::MaskMover; + using klotski::group::GROUP_DATA; using klotski::group::PATTERN_DATA; +// TODO: maybe we can perf with mirror cases + +/// Spawn all the unsorted codes of the current group. +static std::vector Group_extend_for_cases(RawCode raw_code, uint32_t reserve) { + std::vector codes; + phmap::flat_hash_map cases; // + codes.reserve(reserve); + cases.reserve(reserve * 1.56); + + auto core = MaskMover([&codes, &cases](RawCode code, uint64_t mask) { + if (const auto match = cases.find(code.unwrap()); match != cases.end()) { + match->second |= mask; // update mask + return; + } + cases.emplace(code, mask); + codes.emplace_back(code); // new case + }); + + uint64_t offset = 0; + codes.emplace_back(raw_code); + cases.emplace(raw_code, 0); // without mask + while (offset != codes.size()) { + auto curr = codes[offset++].unwrap(); + core.next_cases(RawCode::unsafe_create(curr), cases.find(curr)->second); + } + return codes; +} + RangesUnion Group::cases() const { // TODO: add white list for single-group unions @@ -28,7 +63,7 @@ RangesUnion Group::cases() const { seed = seed.to_vertical_mirror().to_horizontal_mirror(); } - auto codes = Group_extend(seed.to_raw_code(), size()); + auto codes = Group_extend_for_cases(seed.to_raw_code(), size()); RangesUnion data; for (auto raw_code : codes) { @@ -57,10 +92,35 @@ static std::unordered_map build_map_data() { return data; } -Group Group::from_raw_code(codec::RawCode raw_code) { +static std::vector Group_extend_for_from_raw_code(RawCode raw_code) { + std::vector codes; + phmap::flat_hash_map cases; // + codes.reserve(GroupUnion::from_raw_code(raw_code).max_group_size()); + cases.reserve(GroupUnion::from_raw_code(raw_code).max_group_size() * 1.56); + + auto core = MaskMover([&codes, &cases](RawCode code, uint64_t mask) { + if (const auto match = cases.find(code.unwrap()); match != cases.end()) { + match->second |= mask; // update mask + return; + } + cases.emplace(code, mask); + codes.emplace_back(code); // new case + }); + + uint64_t offset = 0; + codes.emplace_back(raw_code); + cases.emplace(raw_code, 0); // without mask + while (offset != codes.size()) { + auto curr = codes[offset++].unwrap(); + core.next_cases(RawCode::unsafe_create(curr), cases.find(curr)->second); + } + return codes; +} + +Group Group::from_raw_code(const RawCode raw_code) { static auto map_data = build_map_data(); - auto raw_codes = Group_extend(raw_code); + auto raw_codes = Group_extend_for_from_raw_code(raw_code); auto common_codes = raw_codes | std::views::transform([](const RawCode r) { return r.to_common_code(); }) | std::ranges::to(); // TODO: search min_element directly diff --git a/src/core/group/internal/group.inl b/src/core/group/internal/group.inl index ce02763..a1f3171 100644 --- a/src/core/group/internal/group.inl +++ b/src/core/group/internal/group.inl @@ -6,6 +6,10 @@ namespace klotski::group { // ----------------------------------------------------------------------------------------- // +constexpr auto Group::toward() const -> Toward { + return toward_; +} + constexpr uint_fast8_t Group::type_id() const { return type_id_; } @@ -14,10 +18,6 @@ constexpr uint_fast16_t Group::pattern_id() const { return pattern_id_; } -constexpr auto Group::toward() const -> Toward { - return toward_; -} - constexpr char Group::toward_char() const { // TODO: select chars from pre-build std::array switch (mirror_type()) { diff --git a/src/core/group/internal/group_cases.cc b/src/core/group/internal/group_cases.cc index 364ebea..89d03b2 100644 --- a/src/core/group/internal/group_cases.cc +++ b/src/core/group/internal/group_cases.cc @@ -2,8 +2,12 @@ #include +#include + #include "group/group.h" +#include "mover/mover.h" +using klotski::codec::RawCode; using klotski::codec::ShortCode; using klotski::codec::CommonCode; @@ -14,6 +18,8 @@ using klotski::cases::RangesUnion; using klotski::group::CaseInfo; +using klotski::mover::MaskMover; + using klotski::group::ALL_GROUP_NUM; using klotski::group::TYPE_ID_LIMIT; using klotski::cases::ALL_CASES_NUM_; @@ -195,8 +201,34 @@ static std::unordered_map build_map_data() { return data; } +static std::vector Group_extend_for_obtain_info(RawCode raw_code) { + std::vector codes; + phmap::flat_hash_map cases; // + + codes.reserve(GroupUnion::from_raw_code(raw_code).max_group_size()); + cases.reserve(GroupUnion::from_raw_code(raw_code).max_group_size() * 1.56); + + auto core = MaskMover([&codes, &cases](RawCode code, uint64_t mask) { + if (const auto match = cases.find(code.unwrap()); match != cases.end()) { + match->second |= mask; // update mask + return; + } + cases.emplace(code, mask); + codes.emplace_back(code); // new case + }); + + uint64_t offset = 0; + codes.emplace_back(raw_code); + cases.emplace(raw_code, 0); // without mask + while (offset != codes.size()) { + auto curr = codes[offset++].unwrap(); + core.next_cases(RawCode::unsafe_create(curr), cases.find(curr)->second); + } + return codes; +} + CaseInfo GroupCases::tiny_obtain_info(CommonCode common_code) { - auto raw_codes = Group_extend(common_code.to_raw_code()); + auto raw_codes = Group_extend_for_obtain_info(common_code.to_raw_code()); std::vector common_codes; common_codes.reserve(raw_codes.size()); for (auto raw_code : raw_codes) {