From 7979398963b525db01597e6fd4420426321953c7 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 6 Jan 2023 18:42:55 +0800 Subject: [PATCH] feat: short code to string --- klotski/main.cc | 59 +++++++++++++++++++++++-------------------- klotski/short_code.cc | 13 ++++++++++ klotski/short_code.h | 26 +++++++++++++++++++ 3 files changed, 70 insertions(+), 28 deletions(-) diff --git a/klotski/main.cc b/klotski/main.cc index 911e9c2..42aa8c0 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -30,36 +30,39 @@ int main() { // printf("%09lX\n", s.unzip_short_code(14323231)); - std::cout << "preparing..." << std::endl; - std::vector all_cases; - auto a = AllCases(AllCases::ALL_CASES); - for (int head = 0; head < 16; ++head) { - uint64_t prefix = (uint64_t)head << 32; - for (const auto &range : (*a.get_all_cases())[head]) { - all_cases.emplace_back(prefix | range); - } - } - -// auto s = ShortCode(ShortCode::Mode::NORMAL); - auto s = ShortCode(ShortCode::Mode::FAST); - std::cout << "start working" << std::endl; - - for (auto short_code = 0; short_code < all_cases.size(); ++short_code) { - uint64_t common_code = all_cases[short_code]; +// std::cout << "preparing..." << std::endl; +// std::vector all_cases; +// auto a = AllCases(AllCases::ALL_CASES); +// for (int head = 0; head < 16; ++head) { +// uint64_t prefix = (uint64_t)head << 32; +// for (const auto &range : (*a.get_all_cases())[head]) { +// all_cases.emplace_back(prefix | range); +// } +// } +// +//// auto s = ShortCode(ShortCode::Mode::NORMAL); +// auto s = ShortCode(ShortCode::Mode::FAST); +// std::cout << "start working" << std::endl; +// +// for (auto short_code = 0; short_code < all_cases.size(); ++short_code) { +// uint64_t common_code = all_cases[short_code]; +// +// if (short_code != s.zip_short_code(common_code)) { +// printf("ERROR: zip %d\n", short_code); +// } +// if (common_code != s.unzip_short_code(short_code)) { +// printf("ERROR: unzip %09lX\n", common_code); +// } +// +// if (short_code % 2000000 == 0) { +//// if (short_code % 10000 == 0) { +// std::cout << ((float)short_code / (float)all_cases.size() * 100) << "%" << std::endl; +// } +// } +// std::cout << "complete verify" << std::endl; - if (short_code != s.zip_short_code(common_code)) { - printf("ERROR: zip %d\n", short_code); - } - if (common_code != s.unzip_short_code(short_code)) { - printf("ERROR: unzip %09lX\n", common_code); - } - if (short_code % 2000000 == 0) { -// if (short_code % 10000 == 0) { - std::cout << ((float)short_code / (float)all_cases.size() * 100) << "%" << std::endl; - } - } - std::cout << "complete verify" << std::endl; + std::cout << ShortCode::code_to_string(14323231) << std::endl; return 0; } diff --git a/klotski/short_code.cc b/klotski/short_code.cc index b00b661..a4e61dc 100644 --- a/klotski/short_code.cc +++ b/klotski/short_code.cc @@ -13,6 +13,19 @@ bool ShortCode::check(uint32_t short_code) { return short_code < ShortCode::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) } +std::string ShortCode::code_to_string(uint32_t short_code) { + if (!ShortCode::check(short_code)) { + throw std::range_error("short code out of range"); + } + std::string result(5, '\0'); // short code length 5 + for (int n = 0; n < 5; ++n) { + uint8_t bit = short_code % 32; + short_code = (short_code - bit) / 32; + result[4 - n] = SHORT_CODE_TABLE[bit]; + } + return result; +} + void ShortCode::speed_up(enum Mode mode) { // speed up handle short code switch (mode) { case Mode::NORMAL: // speed up into normal mode diff --git a/klotski/short_code.h b/klotski/short_code.h index d91c3da..d5715fa 100644 --- a/klotski/short_code.h +++ b/klotski/short_code.h @@ -1,9 +1,31 @@ #pragma once #include +#include #include #include +const char SHORT_CODE_TABLE[32] = { + '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', + 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', +}; + +// 1 -> 48 +// ... +// 9 -> 57 +// A -> 65 +// ... +// H -> 72 +// J -> 74 +// ... +// K -> 75 +// M -> 77 +// N -> 78 +// P -> 80 +// ... +// Z -> 90 + class ShortCode { public: enum Mode {NORMAL, FAST}; @@ -12,9 +34,13 @@ public: void speed_up(enum Mode mode); explicit ShortCode(enum Mode mode); static bool check(uint32_t short_code); + uint32_t zip_short_code(uint64_t common_code); uint64_t unzip_short_code(uint32_t short_code); + static std::string code_to_string(uint32_t short_code); +// static uint32_t code_from_string(const std::string &short_code); + private: static const uint32_t SHORT_CODE_LIMIT = 29334498;