From c496dfc883bb1029f9c5dcb4c1025b360977490d Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 5 Jan 2023 18:46:36 +0800 Subject: [PATCH] update: unzip short code function --- all_cases/short_code.cc | 122 ++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 75 deletions(-) diff --git a/all_cases/short_code.cc b/all_cases/short_code.cc index 0ac6453..8cfba33 100644 --- a/all_cases/short_code.cc +++ b/all_cases/short_code.cc @@ -8,7 +8,7 @@ const uint32_t ALL_CASES_NUMBER = 29334498; -const uint32_t ALL_CASES_SIZE[] = { +const uint32_t ALL_CASES_INDEX[] = { 2942906, 2260392, 2942906, 0, 2322050, 1876945, 2322050, 0, 2322050, 1876945, 2322050, 0, @@ -55,96 +55,36 @@ uint32_t code_from_string(const std::string &short_code) { return result; } -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); -// } -// } -// -// std::unordered_map all_cases_dict; -// for (int i = 0; i < all_cases.size(); ++i) { -// all_cases_dict[all_cases[i]] = i; -// } +uint64_t unzip_short_code(uint32_t short_code) { - uint32_t sum = 0; - uint32_t index[256]; - for (int i = 0; i < 256; ++i) { - index[i] = sum; - sum += BASIC_RANGES_INDEX[i]; - } -// for (int i = 0; i < 256; ++i) { -// if (i % 8 == 0) { -// printf(" "); -// } -// printf("%7d, ", index[i]); -// if (i % 8 == 7) { -// printf("\n"); -// } -// } -// return 0; + auto a = AllCases(); // load basic ranges + std::cout << "short code: " << short_code << std::endl; - 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 << 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 - -// uint32_t short_code = 5203298; - int head = 0; + uint32_t head = 0; for (; head < 16; ++head) { - if (short_code < ALL_CASES_SIZE[head]) { + if (short_code < ALL_CASES_INDEX[head]) { break; } - short_code -= ALL_CASES_SIZE[head]; + short_code -= ALL_CASES_INDEX[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 << "short code: " << short_code << std::endl; - int prefix = 0; + uint32_t prefix = 0; for (; prefix < 256; ++prefix) { if (short_code < SHORT_CODE_MARK[head][prefix]) { break; } short_code -= SHORT_CODE_MARK[head][prefix]; } - std::cout << "prefix: " << prefix << std::endl; - std::cout << "sub sub short code: " << short_code << std::endl; -// uint32_t basic_ranges_start = 0; -// for (int i = 0; i < prefix; ++i) { -// basic_ranges_start += BASIC_RANGES_INDEX[i]; -// } -// std::cout << "basic ranges start: " << basic_ranges_start << std::endl; - std::cout << "basic range offset: " << BASIC_RANGES_OFFSET[prefix] << std::endl; + std::cout << "prefix: " << prefix << std::endl; + std::cout << "short code: " << short_code << std::endl; uint32_t range; for (int i = 0; i < BASIC_RANGES_INDEX[prefix]; ++i) { -// range = a.basic_ranges[i + basic_ranges_start]; range = a.basic_ranges[i + BASIC_RANGES_OFFSET[prefix]]; if (AllCases::check_case(head, range)) { if (short_code == 0) { @@ -153,9 +93,41 @@ int main() { --short_code; } } - range = AllCases::binary_reverse(range); - printf("range -> %08X\n", range); - printf("result code -> %X%08X\n", head, range); + return (uint64_t)head << 32 | AllCases::binary_reverse(range); +} + + +int main() { + + auto ret_code = unzip_short_code(14323231); + printf("result -> %08lX\n", ret_code); + + return 0; + + 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); +// } +// } +// +// 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; +// printf("%09lX\n", all_cases[0]); +// printf("%09lX\n", all_cases[short_code]); +// printf("%09lX\n", all_cases[ALL_CASES_NUMBER - 1]); + +// 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(14323231) << std::endl; // std::cout << code_from_string("8IzVj") << std::endl;