Browse Source

feat: fast decode and encode for short code

master
Dnomd343 2 years ago
parent
commit
3a8579180d
  1. 7
      klotski/main.cc
  2. 17
      klotski/short_code.cc
  3. 5
      klotski/short_code.h

7
klotski/main.cc

@ -20,12 +20,11 @@ int main() {
auto s = ShortCode();
s.speed_up();
std::cout << s.all_cases_list.size() << std::endl;
std::cout << s.all_cases_dict.size() << std::endl;
// s.speed_up();
// std::cout << s.all_cases_list.size() << std::endl;
// std::cout << s.all_cases_dict.size() << std::endl;
printf("%d\n", s.fast_encode(0x6EC0F8800));
printf("%09lX\n", s.fast_decode(14323231));
return 0;
}

17
klotski/short_code.cc

@ -2,26 +2,31 @@
#include "short_code.h"
#include "short_code_mark.h"
//#include <iostream>
void ShortCode::build_mapping() { // build fast search mapping
auto all = AllCases(AllCases::InitType::WITH_ALL_CASES);
// std::cout << "start build" << std::endl;
for (int head = 0; head < 16; ++head) {
uint64_t prefix = (uint64_t)head << 32;
for (const auto &range : (*all.get_all_cases())[head]) {
all_cases_list.emplace_back(prefix | range); // short_code => common_code
}
}
// std::cout << "stage-1 ok" << std::endl;
for (int n = 0; n < all_cases_list.size(); ++n) {
all_cases_dict[all_cases_list[n]] = n; // common_code => short_code
}
// std::cout << "complete build" << std::endl;
}
void ShortCode::speed_up() {
void ShortCode::speed_up() { // speed up for zip / unzip short code
if (all_cases_list.empty()) {
build_mapping();
}
}
uint64_t ShortCode::fast_decode(uint32_t short_code) { // short_code -> common_code
// TODO: ensure input short_code < SHORT_CODE_LIMIT
return all_cases_list[short_code];
}
uint32_t ShortCode::fast_encode(uint64_t common_code) { // common_code -> short_code
// TODO: ensure input common_code valid
return all_cases_dict[common_code];
}

5
klotski/short_code.h

@ -12,6 +12,11 @@ public:
void speed_up();
// uint32_t zip_short_code(uint64_t code);
// uint64_t unzip_short_code(uint32_t short_code);
uint64_t fast_decode(uint32_t short_code);
uint32_t fast_encode(uint64_t common_code);
private:
// std::vector<uint64_t> all_cases_list; // short_code -> common_code

Loading…
Cancel
Save