From 4cf9b88c4907e98035dc60409d8c367006d75fba Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 6 Jan 2023 16:10:43 +0800 Subject: [PATCH] feat: short code check function --- klotski/main.cc | 17 ++++++++++++----- klotski/short_code.cc | 29 +++++++++++++++++++++-------- klotski/short_code.h | 3 ++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/klotski/main.cc b/klotski/main.cc index 250412d..f6aca29 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -38,13 +38,20 @@ int main() { // printf("%d\n", s.tiny_encode(0x6EC0F8800)); // printf("%09lX\n", s.tiny_decode(14323231)); -// auto s = ShortCode(); + auto s = ShortCode(); // auto s = ShortCode(ShortCode::Mode::NORMAL); - auto s = ShortCode(ShortCode::Mode::FAST); +// auto s = ShortCode(ShortCode::Mode::FAST); + +// std::cout << "start" << std::endl; +// std::cout << s.zip_short_code(0x6EC0F8800) << std::endl; +// std::cout << "complete" << std::endl; - std::cout << "start" << std::endl; - std::cout << s.check_mode() << std::endl; - std::cout << "complete" << std::endl; +// if (ShortCode::check(14323231)) { + if (ShortCode::check(87654321)) { + std::cout << "true" << std::endl; + } else { + std::cout << "false" << std::endl; + } return 0; } diff --git a/klotski/short_code.cc b/klotski/short_code.cc index b785796..19ac48d 100644 --- a/klotski/short_code.cc +++ b/klotski/short_code.cc @@ -1,3 +1,4 @@ +#include #include "common.h" #include "all_cases.h" #include "short_code.h" @@ -7,6 +8,10 @@ ShortCode::ShortCode(ShortCode::Mode mode) { // class initialize speed_up(mode); } +bool ShortCode::check(uint32_t short_code) { + return short_code < ShortCode::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) +} + void ShortCode::speed_up(enum Mode mode) { // speed up handle short code switch (mode) { case Mode::NORMAL: // speed up into normal mode @@ -104,22 +109,30 @@ uint32_t ShortCode::zip_short_code(uint64_t common_code) { // common_code --zip- // TODO: confirm common_code valid - // TODO: check Mode => NORMAL / FAST - - // TODO: usage fast_encode or tiny_encode - - return 0; + switch (check_mode()) { + case ShortCode::Mode::NORMAL: + return tiny_encode(common_code); + case ShortCode::Mode::FAST: + return fast_encode(common_code); + default: + throw std::runtime_error("unknown error"); + } } uint64_t ShortCode::unzip_short_code(uint32_t short_code) { // short_code --unzip--> common_code // TODO: confirm short_code valid - // TODO: check Mode => NORMAL / FAST - // TODO: usage fast_decode / tiny_decode - return 0; + switch (check_mode()) { + case ShortCode::Mode::NORMAL: + return tiny_decode(short_code); + case ShortCode::Mode::FAST: + return fast_decode(short_code); + default: + throw std::runtime_error("unknown error"); + } } enum ShortCode::Mode ShortCode::check_mode() { // ensure speed up enabled and return current mode diff --git a/klotski/short_code.h b/klotski/short_code.h index 1066992..24480c1 100644 --- a/klotski/short_code.h +++ b/klotski/short_code.h @@ -23,12 +23,13 @@ public: enum Mode check_mode(); + static bool check(uint32_t short_code); private: // std::vector all_cases_list; // short_code -> common_code // std::unordered_map all_cases_dict; // common_code -> short_code - const uint32_t SHORT_CODE_LIMIT = 29334498; + static const uint32_t SHORT_CODE_LIMIT = 29334498; uint64_t fast_decode(uint32_t short_code); uint32_t fast_encode(uint64_t common_code);