From 3a8579180de7a5867356033261bdcd32536f484e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 6 Jan 2023 01:28:35 +0800 Subject: [PATCH] feat: fast decode and encode for short code --- klotski/main.cc | 7 +++---- klotski/short_code.cc | 17 +++++++++++------ klotski/short_code.h | 5 +++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/klotski/main.cc b/klotski/main.cc index e17033b..4822f18 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -20,12 +20,11 @@ int main() { auto s = ShortCode(); s.speed_up(); - std::cout << s.all_cases_list.size() << std::endl; - std::cout << s.all_cases_dict.size() << std::endl; - -// s.speed_up(); // 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)); + return 0; } diff --git a/klotski/short_code.cc b/klotski/short_code.cc index 3e16bf6..2f39f99 100644 --- a/klotski/short_code.cc +++ b/klotski/short_code.cc @@ -2,26 +2,31 @@ #include "short_code.h" #include "short_code_mark.h" -//#include - void ShortCode::build_mapping() { // build fast search mapping auto all = AllCases(AllCases::InitType::WITH_ALL_CASES); -// std::cout << "start build" << std::endl; 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 } } -// std::cout << "stage-1 ok" << std::endl; for (int n = 0; n < all_cases_list.size(); ++n) { all_cases_dict[all_cases_list[n]] = n; // common_code => short_code } -// std::cout << "complete build" << std::endl; } -void ShortCode::speed_up() { +void ShortCode::speed_up() { // speed up for zip / unzip short code if (all_cases_list.empty()) { build_mapping(); } } + +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]; +} + +uint32_t ShortCode::fast_encode(uint64_t common_code) { // common_code -> short_code + // TODO: ensure input common_code valid + return all_cases_dict[common_code]; +} diff --git a/klotski/short_code.h b/klotski/short_code.h index c5a36ed..8a49694 100644 --- a/klotski/short_code.h +++ b/klotski/short_code.h @@ -12,6 +12,11 @@ public: void speed_up(); +// uint32_t zip_short_code(uint64_t code); +// uint64_t unzip_short_code(uint32_t short_code); + + uint64_t fast_decode(uint32_t short_code); + uint32_t fast_encode(uint64_t common_code); private: // std::vector all_cases_list; // short_code -> common_code