Browse Source

update: short code fast search demo

legacy
Dnomd343 2 years ago
parent
commit
a9a1f829bd
  1. 50
      all_cases/short_code.cc

50
all_cases/short_code.cc

@ -1,9 +1,19 @@
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <unordered_map>
#include "all_cases.h" #include "all_cases.h"
// TODO: try to remove: `0` `O` `I` `l`
const uint32_t ALL_CASES_NUMBER = 29334498; 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) { std::string code_to_string(uint32_t short_code) {
if (short_code >= ALL_CASES_NUMBER) { if (short_code >= ALL_CASES_NUMBER) {
throw std::range_error("short code out of range"); 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() { int main() {
// auto a = AllCases(); auto a = AllCases();
// a.find_all_cases(); a.find_all_cases();
// int sum = 0;
// for (auto &all_case : a.all_cases) { std::vector<uint64_t> all_cases;
// sum += (int)all_case.size(); for (int n = 0; n < 16; ++n) {
// } uint64_t prefix = (uint64_t)n << 32;
// std::cout << sum << std::endl; for (auto &code : a.all_cases[n]) {
all_cases.emplace_back(prefix | code);
}
}
std::unordered_map<uint64_t, uint32_t> 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_to_string(14323231) << std::endl;
std::cout << code_from_string("8IzVj") << std::endl; // std::cout << code_from_string("8IzVj") << std::endl;
return 0; return 0;
} }

Loading…
Cancel
Save