From c8225df67a416bda1a429bc499fc49e8f18ac22a Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 17 Jan 2023 14:59:22 +0800 Subject: [PATCH] perf: check function for RawCode --- src/main.cc | 23 ++++++--- src/raw_code/raw_code.cc | 103 ++++++++++++++------------------------- src/raw_code/raw_code.h | 7 ++- 3 files changed, 56 insertions(+), 77 deletions(-) diff --git a/src/main.cc b/src/main.cc index eb72a9d..21066d6 100644 --- a/src/main.cc +++ b/src/main.cc @@ -362,13 +362,13 @@ int main() { // printf("%09lX\n", CommonCode::from_string("1a9bf0c").unwrap()); // std::cout << CommonCode("4feA134") << std::endl; -// std::vector all_cases; -// for (uint64_t head = 0; head < 16; ++head) { -// for (const auto &range : AllCases::fetch()[head]) { -// all_cases.emplace_back(head << 32 | range); -// } -// } -// std::cout << "test size: " << all_cases.size() << std::endl; + std::vector all_cases; + for (uint64_t head = 0; head < 16; ++head) { + for (const auto &range : AllCases::fetch()[head]) { + all_cases.emplace_back(head << 32 | range); + } + } + std::cout << "test size: " << all_cases.size() << std::endl; // for (const auto &common_code : all_cases) { // if (RawCode::compact(RawCode::extract(common_code)) != common_code) { @@ -380,7 +380,14 @@ int main() { // std::cout << CommonCode(0x4FEA13400).to_raw_code().to_common_code() << std::endl; - std::cout << RawCode::check(CommonCode(0x4FEA13400).to_raw_code().unwrap()) << std::endl; +// std::cout << RawCode::check(CommonCode(0x4FEA13400).to_raw_code().unwrap()) << std::endl; + + for (const auto &common_code : all_cases) { + if (!RawCode::check(RawCode::extract(common_code))) { + std::cout << "Error: " << CommonCode(common_code) << std::endl; + } + } + // std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl; diff --git a/src/raw_code/raw_code.cc b/src/raw_code/raw_code.cc index 4a62031..dfb8b99 100644 --- a/src/raw_code/raw_code.cc +++ b/src/raw_code/raw_code.cc @@ -6,89 +6,58 @@ uint64_t RawCode::unwrap() const { // get raw uint64_t code return code; } -#include - -/// we can check any raw_code in this function -bool RawCode::check(uint64_t raw_code) { +std::string RawCode::dump_case() const { + // TODO: using stack char * + std::string result; + result.reserve(40); // 5 lines * ("x x x x\n") + char dump_map[] = { + /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill + '.', '~', '|', '*', '@', '?', '?', '+' + }; + auto raw_code = code; + for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) { + result.push_back(dump_map[raw_code & 0b111]); + if ((addr & 0b11) == 0b11) { + result.push_back('\n'); // new line + } else { + result.push_back(' '); // add space + } + } + return result; +} +bool RawCode::check(uint64_t raw_code) { // check whether raw code is valid if (raw_code >> 60) { return false; // high 4-bits must be zero } - - int head_num = 0; - int space_num = 0; - + int head_num = 0, space_num = 0; // statistics for space and 2x2 number for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) { - -// std::cout << "addr = " << addr << " | block = " << (raw_code & 0b111) << std::endl; - switch (raw_code & 0b111) { + case B_1x1: + case B_fill: + continue; // do nothing case B_space: ++space_num; - break; - case B_1x1: - /// do nothing - break; - case B_1x2: - - if ((addr & 0b11) == 0b11 || (raw_code & F_1x2) != C_1x2) { - return false; - } - - break; + continue; case B_2x1: - if (addr > 15 || (raw_code & F_2x1) != C_2x1) { - return false; + return false; // invalid 2x1 block } - - break; + continue; + case B_1x2: + if ((addr & 0b11) == 0b11 || (raw_code & F_1x2) != C_1x2) { + return false; // invalid 1x2 block + } + continue; case B_2x2: ++head_num; - if (addr > 14 || (addr & 0b11) == 0b11 || (raw_code & F_2x2) != C_2x2) { - return false; + return false; // invalid 2x2 block } - - break; - - case B_fill: - /// do nothing - break; - + continue; default: - return false; // unknown flag + return false; // unknown flag -> 0b101 / 0b110 } - } - - // TODO: one 2x2 / >=2 space - - // TODO: check head_num and space_num - - if (head_num != 1 || space_num < 2) { - return false; - } - - return true; -} - -std::string RawCode::dump_case() const { - std::string result; - result.reserve(40); // 5 lines * ("x x x x\n") - char dump_map[] = { - /// 0x0 1x2 2x1 1x1 2x2 b101 b110 fill - '.', '~', '|', '*', '@', '?', '?', '+' - }; - - auto raw_code = code; - for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) { - result.push_back(dump_map[raw_code & 0b111]); - if ((addr & 0b11) == 0b11) { - result.push_back('\n'); // new line - } else { - result.push_back(' '); // add space - } - } - return result; + return head_num == 1 && space_num >= 2; // one head and at least 2 space } diff --git a/src/raw_code/raw_code.h b/src/raw_code/raw_code.h index e7a2084..7a75c4e 100644 --- a/src/raw_code/raw_code.h +++ b/src/raw_code/raw_code.h @@ -29,9 +29,12 @@ public: // TODO: unsafe_create static function + static uint64_t compact(uint64_t raw_code); + static uint64_t extract(uint64_t common_code); + private: uint64_t code; - static uint64_t compact(uint64_t raw_code); - static uint64_t extract(uint64_t common_code); +// static uint64_t compact(uint64_t raw_code); +// static uint64_t extract(uint64_t common_code); };