From 375c121f3f8e20afe53f52bee0d6fa14bd4e43bb Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 6 Jan 2023 02:11:54 +0800 Subject: [PATCH] perf: AllCases init parameter --- klotski/all_cases.cc | 18 +++++++++++------- klotski/all_cases.h | 11 ++++------- klotski/main.cc | 10 +++++----- klotski/short_code.cc | 40 ++++++++++++++++++++-------------------- klotski/short_code.h | 3 +-- 5 files changed, 41 insertions(+), 41 deletions(-) diff --git a/klotski/all_cases.cc b/klotski/all_cases.cc index daa38c3..c6d4515 100644 --- a/klotski/all_cases.cc +++ b/klotski/all_cases.cc @@ -2,13 +2,17 @@ #include "common.h" #include "all_cases.h" -AllCases::AllCases(enum InitType init) { - switch (init) { - case InitType::WITH_ALL_CASES: - get_all_cases(); - case InitType::WITH_BASIC_RANGES: - get_basic_ranges(); - case WITH_NOTHING: +AllCases::AllCases(AllCases::Build type) { // class initialize + build(type); +} + +void AllCases::build(AllCases::Build type) { // build specific type + switch (type) { + case Build::BASIC_RANGES: + get_basic_ranges(); // basic ranges initialize + break; + case Build::ALL_CASES: + get_all_cases(); // all cases initialize break; } } diff --git a/klotski/all_cases.h b/klotski/all_cases.h index 1c45259..68abeb9 100644 --- a/klotski/all_cases.h +++ b/klotski/all_cases.h @@ -5,14 +5,11 @@ class AllCases { public: - enum InitType { - WITH_NOTHING, - WITH_BASIC_RANGES, - WITH_ALL_CASES, - }; + enum Build {BASIC_RANGES, ALL_CASES}; - explicit AllCases(enum InitType init); - AllCases():AllCases(InitType::WITH_NOTHING) {}; + AllCases() = default; + void build(enum Build type); + explicit AllCases(enum Build type); const std::vector* get_basic_ranges(); const std::vector (*get_all_cases())[16]; diff --git a/klotski/main.cc b/klotski/main.cc index 511a124..d4d77e2 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -18,18 +18,18 @@ int main() { // } // auto s = ShortCode(); -// auto s = ShortCode(ShortCode::Mode::NORMAL); - auto s = ShortCode(ShortCode::Mode::FAST); + auto s = ShortCode(ShortCode::Mode::NORMAL); +// auto s = ShortCode(ShortCode::Mode::FAST); // s.speed_up(ShortCode::Mode::FAST); - std::cout << s.all_cases_list.size() << std::endl; - std::cout << s.all_cases_dict.size() << std::endl; +// std::cout << s.all_cases_list.size() << std::endl; +// std::cout << s.all_cases_dict.size() << std::endl; // printf("%d\n", s.fast_encode(0x6EC0F8800)); // printf("%09lX\n", s.fast_decode(14323231)); // s.speed_up(ShortCode::Mode::NORMAL); -// std::cout << s.basic_ranges.size() << std::endl; + std::cout << s.basic_ranges.size() << std::endl; return 0; } diff --git a/klotski/short_code.cc b/klotski/short_code.cc index ecf1342..3997bee 100644 --- a/klotski/short_code.cc +++ b/klotski/short_code.cc @@ -2,22 +2,8 @@ #include "short_code.h" #include "short_code_mark.h" -void ShortCode::build_base_ranges() { - auto all = AllCases(AllCases::InitType::WITH_BASIC_RANGES); - basic_ranges = *all.get_basic_ranges(); -} - -void ShortCode::build_mappings() { // build fast search mapping - auto all = AllCases(AllCases::InitType::WITH_ALL_CASES); - for (int head = 0; head < 16; ++head) { - uint64_t prefix = (uint64_t)head << 32; - for (const auto &range : (*all.get_all_cases())[head]) { - all_cases_list.emplace_back(prefix | range); // short_code => common_code - } - } - for (int n = 0; n < all_cases_list.size(); ++n) { - all_cases_dict[all_cases_list[n]] = n; // common_code => short_code - } +ShortCode::ShortCode(ShortCode::Mode mode) { // class initialize + speed_up(mode); } void ShortCode::speed_up(enum Mode mode) { // speed up handle short code @@ -35,6 +21,24 @@ void ShortCode::speed_up(enum Mode mode) { // speed up handle short code } } +void ShortCode::build_base_ranges() { // build basic ranges + auto all = AllCases(AllCases::Build::BASIC_RANGES); + basic_ranges = *all.get_basic_ranges(); +} + +void ShortCode::build_mappings() { // build fast search mappings + auto all = AllCases(AllCases::Build::ALL_CASES); + for (int head = 0; head < 16; ++head) { + uint64_t prefix = (uint64_t)head << 32; + for (const auto &range : (*all.get_all_cases())[head]) { + all_cases_list.emplace_back(prefix | range); // short_code => common_code + } + } + for (int n = 0; n < all_cases_list.size(); ++n) { + all_cases_dict[all_cases_list[n]] = n; // common_code => short_code + } +} + uint64_t ShortCode::fast_decode(uint32_t short_code) { // short_code -> common_code // TODO: ensure input short_code < SHORT_CODE_LIMIT return all_cases_list[short_code]; @@ -44,7 +48,3 @@ uint32_t ShortCode::fast_encode(uint64_t common_code) { // common_code -> short_ // TODO: ensure input common_code valid return all_cases_dict[common_code]; } - -ShortCode::ShortCode(ShortCode::Mode mode) { - speed_up(mode); -} diff --git a/klotski/short_code.h b/klotski/short_code.h index a253873..ee9ce49 100644 --- a/klotski/short_code.h +++ b/klotski/short_code.h @@ -15,9 +15,8 @@ public: std::unordered_map all_cases_dict; // common_code -> short_code ShortCode() = default; - explicit ShortCode(enum Mode mode); - void speed_up(enum Mode mode); + explicit ShortCode(enum Mode mode); // uint32_t zip_short_code(uint64_t code); // uint64_t unzip_short_code(uint32_t short_code);