From acab8830d8ce47557ed69801e5842e619fd80d42 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 16 Jan 2023 20:50:07 +0800 Subject: [PATCH] update: enhance ShortCode module --- src/all_cases/all_cases.h | 3 -- src/main.cc | 7 +++-- src/short_code/serialize.cc | 18 +++++------- src/short_code/short_code.cc | 53 +++++++++++++++++------------------- src/short_code/short_code.h | 11 ++++---- 5 files changed, 43 insertions(+), 49 deletions(-) diff --git a/src/all_cases/all_cases.h b/src/all_cases/all_cases.h index ae47baf..3884e9b 100644 --- a/src/all_cases/all_cases.h +++ b/src/all_cases/all_cases.h @@ -18,13 +18,10 @@ public: static enum Status status(); static const std::vector (&fetch())[16]; -// static int check_case(uint32_t head, uint32_t range); - private: static bool available; static std::mutex building; static std::vector data[16]; static void build_data(); -// static int check_case(uint32_t head, uint32_t range); }; diff --git a/src/main.cc b/src/main.cc index 8d4b820..125c35c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -279,8 +279,11 @@ int main() { // printf("%09lX\n", ShortCode::tiny_decode(14323231)); // std::cout << ShortCode::tiny_encode(0x6EC0F8800) << std::endl; - std::cout << ShortCode(14323231).to_string() << std::endl; - std::cout << ShortCode::from_string("EP4HZ").unwrap() << std::endl; +// std::cout << ShortCode(14323231).to_string() << std::endl; +// std::cout << ShortCode::from_string("EP4HZ").unwrap() << std::endl; + + std::cout << ShortCode("EP4HZ") << std::endl; + std::cout << ShortCode(14323231) << std::endl; // printf("%09lX\n", ShortCode::fast_decode(14323231)); // std::cout << ShortCode::fast_encode(0x6EC0F8800) << std::endl; diff --git a/src/short_code/serialize.cc b/src/short_code/serialize.cc index 07b2bde..f26c221 100644 --- a/src/short_code/serialize.cc +++ b/src/short_code/serialize.cc @@ -10,9 +10,8 @@ std::string ShortCode::to_string() const { // encode as 5-bits string uint32_t short_code = code; 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]; + result[4 - n] = SHORT_CODE_TABLE[short_code & 0b11111]; // aka _ % 32 + short_code >>= 5; // aka _ / 32 } return result; } @@ -21,24 +20,21 @@ ShortCode::ShortCode(const std::string &short_code) { // 5-bits string decode if (short_code.length() != 5) { // check string length throw std::invalid_argument("short code format error"); } - uint64_t result = 0; for (auto bit : short_code) { - result *= 32; + result <<= 5; // aka _ * 32 if (bit >= 'a' && bit <= 'z') { bit -= 32; // convert to uppercase } - if (bit >= '1' && bit <= 'Z') { + if (bit >= '1' && bit <= 'Z') { // valid characters result += (bit = SHORT_CODE_TABLE_REV[bit - 49]); // table convert - if (bit != -1) { - continue; // pass check + if (bit == -1) { + throw std::invalid_argument("short code format error"); // unknown characters } } - throw std::invalid_argument("short code format error"); // unknown characters } - if (!ShortCode::check(result)) { // check converted short code throw std::invalid_argument("invalid short code"); } - code = result; + code = result; // apply convert result } diff --git a/src/short_code/short_code.cc b/src/short_code/short_code.cc index 9b5946c..e4c441f 100644 --- a/src/short_code/short_code.cc +++ b/src/short_code/short_code.cc @@ -1,21 +1,14 @@ #include "all_cases.h" #include "short_code.h" -bool ShortCode::fast_mode_available = false; -bool ShortCode::normal_mode_available = false; - -uint32_t ShortCode::unwrap() const { // get raw uint32_t code - return code; +uint32_t ShortCode::unwrap() const { + return code; // raw uint32_t code } ShortCode ShortCode::create(uint32_t short_code) { return ShortCode(short_code); } -bool ShortCode::check(uint32_t short_code) { - return short_code < ShortCode::SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) -} - ShortCode::ShortCode(uint32_t short_code) { if (!ShortCode::check(short_code)) { // check input short code throw std::invalid_argument("invalid short code"); @@ -23,24 +16,18 @@ ShortCode::ShortCode(uint32_t short_code) { code = short_code; } +bool ShortCode::check(uint32_t short_code) { + return short_code < SHORT_CODE_LIMIT; // 0 ~ (SHORT_CODE_LIMIT - 1) +} - -void ShortCode::speed_up(ShortCode::Mode mode) { - if (fast_mode_available) { - return; // fast mode already available - } - if (mode == ShortCode::FAST) { // build fast mode data -// build_mappings(); - - // TODO: confirm AllCases data available - AllCases::build(); - - } else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data - BasicRanges::build(); // blocking function - normal_mode_available = true; - } +std::ostream& operator<<(std::ostream &out, const ShortCode &self) { + out << self.to_string() << "(" << self.code << ")"; // short code info + return out; } +bool ShortCode::fast_mode_available = false; +bool ShortCode::normal_mode_available = false; + ShortCode::Mode ShortCode::mode() { // ensure speed up enabled and return current mode if (fast_mode_available) { return ShortCode::FAST; // fast mode already enabled @@ -48,9 +35,19 @@ ShortCode::Mode ShortCode::mode() { // ensure speed up enabled and return curren if (normal_mode_available) { return ShortCode::NORMAL; // normal mode already enabled } - speed_up(ShortCode::Mode::NORMAL); // without initialized -> enter normal mode - return ShortCode::Mode::NORMAL; // use normal mode + speed_up(ShortCode::Mode::NORMAL); // uninitialized -> enable normal mode + return ShortCode::Mode::NORMAL; // normal mode enabled } - - +void ShortCode::speed_up(ShortCode::Mode mode) { + if (fast_mode_available) { + return; // fast mode already available + } + if (mode == ShortCode::FAST) { // build fast mode data + AllCases::build(); // blocking function + fast_mode_available = true; + } else if (!normal_mode_available) { // build normal mode data + BasicRanges::build(); // blocking function + normal_mode_available = true; + } +} diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h index 453176d..374921c 100644 --- a/src/short_code/short_code.h +++ b/src/short_code/short_code.h @@ -1,21 +1,24 @@ #pragma once #include +#include #include "common_code.h" +const uint32_t SHORT_CODE_LIMIT = 29334498; + class CommonCode; class ShortCode { public: enum Mode {NORMAL, FAST}; - static void speed_up(enum Mode mode); uint32_t unwrap() const; std::string to_string() const; CommonCode to_common_code() const; - static bool check(uint32_t short_code); - // TODO: std::cout << ShortCode(...) << std::endl; + static void speed_up(enum Mode mode); + static bool check(uint32_t short_code); + friend std::ostream& operator<<(std::ostream &out, const ShortCode &self); explicit ShortCode(uint32_t short_code); explicit ShortCode(const std::string &short_code); @@ -33,10 +36,8 @@ private: uint32_t code; static bool fast_mode_available; static bool normal_mode_available; - static const uint32_t SHORT_CODE_LIMIT = 29334498; static enum Mode mode(); - static uint64_t fast_decode(uint32_t short_code); static uint32_t fast_encode(uint64_t common_code); static uint64_t tiny_decode(uint32_t short_code);