From e5b1e24839326db32176674fd3f0ecce697c5805 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 22:04:46 +0800 Subject: [PATCH] feat: `to_common_code` interface --- klotski/short_code.h | 2 ++ src/main.cc | 6 ++++-- src/short_code/convert.cc | 9 +++++++++ src/short_code/short_code.h | 9 ++++----- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/klotski/short_code.h b/klotski/short_code.h index 7487f21..2492d29 100644 --- a/klotski/short_code.h +++ b/klotski/short_code.h @@ -40,6 +40,7 @@ public: static bool check(uint32_t short_code); uint32_t zip_short_code(uint64_t common_code); + /// ok uint64_t unzip_short_code(uint32_t short_code); /// ok @@ -60,6 +61,7 @@ private: enum Mode check_mode(); void build_base_ranges(); + /// ok uint64_t tiny_decode(uint32_t short_code); uint32_t tiny_encode(uint64_t common_code); }; diff --git a/src/main.cc b/src/main.cc index 7ecf07f..2daea28 100644 --- a/src/main.cc +++ b/src/main.cc @@ -105,8 +105,10 @@ int main() { // std::cout << ShortCode("EP4HZ", ShortCode::NORMAL).unwrap() << std::endl; // std::cout << ShortCode("eP4hZ", ShortCode::FAST).to_string() << std::endl; - std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl; - printf("%09lX\n", ShortCode::tiny_decode(14323231)); +// std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl; +// printf("%09lX\n", ShortCode::tiny_decode(14323231)); + + std::cout << ShortCode(14323231).to_common_code().to_string() << std::endl; return 0; } diff --git a/src/short_code/convert.cc b/src/short_code/convert.cc index 48d1e44..dce2c26 100644 --- a/src/short_code/convert.cc +++ b/src/short_code/convert.cc @@ -3,6 +3,14 @@ #include "basic_ranges.h" #include "short_code_mark.h" +CommonCode ShortCode::to_common_code() const { // convert to common code + if (ShortCode::check_mode() == ShortCode::NORMAL) { + return CommonCode(tiny_decode(code)); // using normal mode + } + return CommonCode(all_cases_list[code]); // using fast mode +} + +/// ensure that input common code is valid uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common_code --low-memory--> short_code uint32_t offset = 0; uint32_t head = common_code >> 32; // common code head @@ -21,6 +29,7 @@ uint32_t ShortCode::tiny_encode(uint64_t common_code) { // common_code --low-mem return ALL_CASES_OFFSET[head] + RANGE_PREFIX_OFFSET[head][prefix] + offset; } +/// ensure that input short code is valid uint64_t ShortCode::tiny_decode(uint32_t short_code) { // short_code --low-memory--> common_code uint32_t head = 0, prefix = 0; for (; head < 16; ++head) { diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h index 16037e8..3372030 100644 --- a/src/short_code/short_code.h +++ b/src/short_code/short_code.h @@ -4,6 +4,7 @@ #include #include #include +#include "common_code.h" class ShortCode { public: @@ -12,6 +13,7 @@ public: }; uint32_t unwrap() const; std::string to_string() const; + CommonCode to_common_code() const; static bool check(uint32_t short_code); static enum Mode check_mode(); @@ -26,8 +28,6 @@ public: speed_up(mode); } - static uint64_t tiny_decode(uint32_t short_code); - static uint32_t tiny_encode(uint64_t common_code); private: uint32_t code; @@ -39,7 +39,6 @@ private: static std::unordered_map all_cases_dict; // common_code -> short_code static void build_mappings(); - -// static uint64_t tiny_decode(uint32_t short_code); -// static uint32_t tiny_encode(uint64_t common_code); + static uint64_t tiny_decode(uint32_t short_code); + static uint32_t tiny_encode(uint64_t common_code); };