From 4f4831a0a254112d2729b77390b71a06f998d364 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 20:50:10 +0800 Subject: [PATCH] perf: CommonCode module --- src/common_code/common_code.cc | 7 +- src/common_code/common_code.h | 6 +- src/main.cc | 125 ++++++++++++++++----------------- src/short_code/short_code.h | 1 - 4 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/common_code/common_code.cc b/src/common_code/common_code.cc index 6d4974e..e70def5 100644 --- a/src/common_code/common_code.cc +++ b/src/common_code/common_code.cc @@ -45,16 +45,16 @@ CommonCode::CommonCode(const std::string &common_code_str) { code = common_code; } -std::string CommonCode::to_string(bool shorten) const { +std::string CommonCode::to_string(bool shorten) const { // convert uint64_t code to string char result[10]; // max length 9-bits sprintf(result, "%09lX", code); if (shorten) { // remove `0` after common code if (code == 0x000000000) { - return "0"; // special case + return "0"; // special case -> only one `0` } result[9 - last_zero_num(code) / 4] = '\0'; // truncate string } - return result; + return result; // char* -> std::string } bool CommonCode::check(uint64_t common_code) { // check whether common code is valid @@ -63,6 +63,7 @@ bool CommonCode::check(uint64_t common_code) { // check whether common code is v return false; // invalid common code } + /// ensure that there are >= 2 space blocks uint32_t fill_num = 0, space_num = 0; auto range = Common::range_reverse((uint32_t)common_code); // get common code range for (int i = 0; i < 32; i += 2) { // traverse range diff --git a/src/common_code/common_code.h b/src/common_code/common_code.h index f6d236e..5c2badb 100644 --- a/src/common_code/common_code.h +++ b/src/common_code/common_code.h @@ -5,13 +5,13 @@ class CommonCode { public: - explicit CommonCode(uint64_t common_code); - explicit CommonCode(const std::string &common_code_str); - uint64_t unwrap() const; static bool check(uint64_t common_code); std::string to_string(bool shorten = false) const; + explicit CommonCode(uint64_t common_code); + explicit CommonCode(const std::string &common_code_str); + private: uint64_t code; }; diff --git a/src/main.cc b/src/main.cc index 2b498ef..4e04ed9 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,77 +6,74 @@ #include -void get_status() { - switch (BasicRanges::status()) { - case BasicRanges::NO_INIT: - std::cout << "basic ranges no init" << std::endl; - break; - case BasicRanges::BUILDING: - std::cout << "basic ranges building" << std::endl; - break; - case BasicRanges::AVAILABLE: - std::cout << "basic ranges available" << std::endl; - break; - } - - switch (AllCases::status()) { - case AllCases::NO_INIT: - std::cout << "all cases no init" << std::endl; - break; - case AllCases::BUILDING: - std::cout << "all cases building" << std::endl; - break; - case AllCases::AVAILABLE: - std::cout << "all cases available" << std::endl; - break; - } -} +//void get_status() { +// switch (BasicRanges::status()) { +// case BasicRanges::NO_INIT: +// std::cout << "basic ranges no init" << std::endl; +// break; +// case BasicRanges::BUILDING: +// std::cout << "basic ranges building" << std::endl; +// break; +// case BasicRanges::AVAILABLE: +// std::cout << "basic ranges available" << std::endl; +// break; +// } +// +// switch (AllCases::status()) { +// case AllCases::NO_INIT: +// std::cout << "all cases no init" << std::endl; +// break; +// case AllCases::BUILDING: +// std::cout << "all cases building" << std::endl; +// break; +// case AllCases::AVAILABLE: +// std::cout << "all cases available" << std::endl; +// break; +// } +//} int main() { - printf("%p\n", BasicRanges::build); - printf("%p\n", AllCases::build); - - printf("%p\n", BasicRanges::status); - printf("%p\n", AllCases::status); - - printf("%p\n", BasicRanges::fetch); - printf("%p\n", AllCases::fetch); - - get_status(); - BasicRanges::build(); - get_status(); - AllCases::build(); - get_status(); - - std::cout << BasicRanges::fetch()->size() << std::endl; - - uint32_t sum = 0; - for (auto const &all_case : *AllCases::fetch()) { - sum += all_case.size(); - std::cout << " " << all_case.size() << std::endl; - } - std::cout << sum << std::endl; +// printf("%p\n", BasicRanges::build); +// printf("%p\n", AllCases::build); +// +// printf("%p\n", BasicRanges::status); +// printf("%p\n", AllCases::status); +// +// printf("%p\n", BasicRanges::fetch); +// printf("%p\n", AllCases::fetch); +// +// get_status(); +// BasicRanges::build(); +// get_status(); +// AllCases::build(); +// get_status(); +// +// std::cout << BasicRanges::fetch()->size() << std::endl; +// +// uint32_t sum = 0; +// for (auto const &all_case : *AllCases::fetch()) { +// sum += all_case.size(); +// std::cout << " " << all_case.size() << std::endl; +// } +// std::cout << sum << std::endl; +// +// std::cout << AllCases::fetch() << std::endl; +// std::cout << AllCases::BasicRanges::fetch() << std::endl; +// std::cout << BasicRanges::fetch() << std::endl; - std::cout << AllCases::fetch() << std::endl; - std::cout << AllCases::BasicRanges::fetch() << std::endl; - std::cout << BasicRanges::fetch() << std::endl; + std::cout << CommonCode::check(0x123456789) << std::endl; + std::cout << CommonCode::check(0x4FEA13400) << std::endl; -// std::cout << CommonCode::check(0x123456789) << std::endl; -// std::cout << CommonCode::check(0x4FEA13400) << std::endl; -// -// // TODO: should we return a CommonCode object like String::new(...) in rust? -// printf("%09lX\n", CommonCode::from_string("1A9bF0c0")); -// std::cout << CommonCode::to_string(0x1A9BF0C00) << std::endl; -// std::cout << CommonCode::to_string(0x1A9BF0C00, true) << std::endl; + printf("%09lX\n", CommonCode("1A9bF0c0").unwrap()); + std::cout << CommonCode(0x1A9BF0C00).to_string() << std::endl; + std::cout << CommonCode(0x1A9BF0C00).to_string(true) << std::endl; -// auto c = CommonCode("1A9bF0c0"); -// std::cout << c.to_string(true) << std::endl; -// std::cout << c.to_string() << std::endl; -// printf("%09lX\n", c.unwrap()); -// -// std::cout << CommonCode(0x1A9BF0C00).to_string() << std::endl; + auto c = CommonCode("4Fea13400"); + std::cout << c.to_string(true) << std::endl; + std::cout << c.to_string() << std::endl; + printf("%09lX\n", c.unwrap()); // std::cout << ShortCode::check_mode() << std::endl; diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h index 9ab0075..e63b4ab 100644 --- a/src/short_code/short_code.h +++ b/src/short_code/short_code.h @@ -13,7 +13,6 @@ public: static enum Mode check_mode(); static void speed_up(enum Mode mode); - private: static std::mutex map_building; static bool fast_mode_available;