From 2981b02b137b5e42fdd8c3a2db476711d241f942 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 5 Jan 2023 15:54:19 +0800 Subject: [PATCH] update: short code common search demo --- all_cases/all_cases.cc | 2 +- all_cases/all_cases.h | 5 +++- all_cases/short_code.cc | 63 +++++++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 23 deletions(-) diff --git a/all_cases/all_cases.cc b/all_cases/all_cases.cc index 2a19a29..f0c8707 100644 --- a/all_cases/all_cases.cc +++ b/all_cases/all_cases.cc @@ -11,7 +11,7 @@ inline uint32_t binary_count(uint32_t bin) { // get number of non-zero bits return bin & 0b111111; } -inline uint32_t binary_reverse(uint32_t bin) { // reverse binary every 2-bits +uint32_t AllCases::binary_reverse(uint32_t bin) { // reverse binary every 2-bits bin = ((bin << 16) & 0xFFFF0000) | ((bin >> 16) & 0x0000FFFF); bin = ((bin << 8) & 0xFF00FF00) | ((bin >> 8) & 0x00FF00FF); bin = ((bin << 4) & 0xF0F0F0F0) | ((bin >> 4) & 0x0F0F0F0F); diff --git a/all_cases/all_cases.h b/all_cases/all_cases.h index bd63c77..2988610 100644 --- a/all_cases/all_cases.h +++ b/all_cases/all_cases.h @@ -12,8 +12,11 @@ public: std::vector all_cases[16]; std::vector basic_ranges; + static uint32_t binary_reverse(uint32_t bin); + static bool check_case(uint32_t head, uint32_t range); + private: void load_basic_ranges(); void generate_ranges(int n1, int n2, int n3, int n4); - static bool check_case(uint32_t head, uint32_t range); +// static bool check_case(uint32_t head, uint32_t range); }; diff --git a/all_cases/short_code.cc b/all_cases/short_code.cc index 67808fe..ca2c06e 100644 --- a/all_cases/short_code.cc +++ b/all_cases/short_code.cc @@ -57,34 +57,55 @@ uint32_t code_from_string(const std::string &short_code) { int main() { auto a = AllCases(); - a.find_all_cases(); - std::vector all_cases; - for (int n = 0; n < 16; ++n) { - uint64_t prefix = (uint64_t)n << 32; - for (auto &code : a.all_cases[n]) { - all_cases.emplace_back(prefix | code); - } - } +// a.find_all_cases(); +// +// std::vector all_cases; +// for (int n = 0; n < 16; ++n) { +// uint64_t prefix = (uint64_t)n << 32; +// for (auto &code : a.all_cases[n]) { +// all_cases.emplace_back(prefix | code); +// } +// } +// +// std::unordered_map all_cases_dict; +// for (int i = 0; i < all_cases.size(); ++i) { +// all_cases_dict[all_cases[i]] = i; +// } - std::unordered_map all_cases_dict; - for (int i = 0; i < all_cases.size(); ++i) { - all_cases_dict[all_cases[i]] = i; - } - -// uint32_t short_code = 14323231; + uint32_t short_code = 14323231; // printf("%09lX\n", all_cases[0]); // printf("%09lX\n", all_cases[short_code]); // printf("%09lX\n", all_cases[ALL_CASES_NUMBER - 1]); - std::cout << "start search" << std::endl; - std::cout << code_to_string(all_cases_dict[0x6EC0F8800]) << std::endl; - std::cout << code_to_string(all_cases_dict[0x1A9BF0C00]) << std::endl; - std::cout << code_to_string(all_cases_dict[0x4FEA13400]) << std::endl; +// std::cout << code_to_string(all_cases_dict[0x6EC0F8800]) << std::endl; +// std::cout << code_to_string(all_cases_dict[0x1A9BF0C00]) << std::endl; +// std::cout << code_to_string(all_cases_dict[0x4FEA13400]) << std::endl; + + // located code header + // 0 <= short_code < ALL_CASES_NUMBER -// printf("%d\n", all_cases_dict[0x6EC0F8800]); -// printf("%d\n", all_cases_dict[0x1A9BF0C00]); -// printf("%d\n", all_cases_dict[0x4FEA13400]); +// uint32_t short_code = 5203298; + int head = 0; + for (; head < 16; ++head) { + if (short_code < ALL_CASES_SIZE[head]) { + break; + } + short_code -= ALL_CASES_SIZE[head]; + } + std::cout << "head: " << head << std::endl; + std::cout << "sub short code: " << short_code << std::endl; + + int index = 0; + for (auto &range : a.basic_ranges) { + if (AllCases::check_case(head, range)) { + if (index == short_code) { + printf("%08X\n", AllCases::binary_reverse(range)); + break; + } + ++index; + } + } // std::cout << code_to_string(14323231) << std::endl; // std::cout << code_from_string("8IzVj") << std::endl;