From a9a1f829bde5442d920f0dcfcc2cfa8c1ea056b9 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 5 Jan 2023 15:34:38 +0800 Subject: [PATCH] update: short code fast search demo --- all_cases/short_code.cc | 50 +++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 9 deletions(-) diff --git a/all_cases/short_code.cc b/all_cases/short_code.cc index 3081834..67808fe 100644 --- a/all_cases/short_code.cc +++ b/all_cases/short_code.cc @@ -1,9 +1,19 @@ #include #include +#include #include "all_cases.h" +// TODO: try to remove: `0` `O` `I` `l` + const uint32_t ALL_CASES_NUMBER = 29334498; +const uint32_t ALL_CASES_SIZE[] = { + 2942906, 2260392, 2942906, 0, + 2322050, 1876945, 2322050, 0, + 2322050, 1876945, 2322050, 0, + 2942906, 2260392, 2942906, 0, +}; + std::string code_to_string(uint32_t short_code) { if (short_code >= ALL_CASES_NUMBER) { throw std::range_error("short code out of range"); @@ -46,16 +56,38 @@ uint32_t code_from_string(const std::string &short_code) { int main() { -// auto a = AllCases(); -// a.find_all_cases(); -// int sum = 0; -// for (auto &all_case : a.all_cases) { -// sum += (int)all_case.size(); -// } -// std::cout << sum << std::endl; + 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 << "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; + +// printf("%d\n", all_cases_dict[0x6EC0F8800]); +// printf("%d\n", all_cases_dict[0x1A9BF0C00]); +// printf("%d\n", all_cases_dict[0x4FEA13400]); - std::cout << code_to_string(14323231) << std::endl; - std::cout << code_from_string("8IzVj") << std::endl; +// std::cout << code_to_string(14323231) << std::endl; +// std::cout << code_from_string("8IzVj") << std::endl; return 0; }