diff --git a/klotski/main.cc b/klotski/main.cc index 4822f18..a9a2c8d 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -19,12 +19,15 @@ int main() { auto s = ShortCode(); - s.speed_up(); +// s.speed_up(ShortCode::Mode::FAST); // 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)); +// printf("%d\n", s.fast_encode(0x6EC0F8800)); +// printf("%09lX\n", s.fast_decode(14323231)); + + s.speed_up(ShortCode::Mode::NORMAL); + std::cout << s.basic_ranges.size() << std::endl; return 0; } diff --git a/klotski/short_code.cc b/klotski/short_code.cc index 2f39f99..6e93d61 100644 --- a/klotski/short_code.cc +++ b/klotski/short_code.cc @@ -2,7 +2,12 @@ #include "short_code.h" #include "short_code_mark.h" -void ShortCode::build_mapping() { // build fast search mapping +void ShortCode::build_base_ranges() { + auto all = AllCases(AllCases::InitType::WITH_BASIC_RANGES); + basic_ranges = *all.get_basic_ranges(); +} + +void ShortCode::build_mappings() { // build fast search mapping auto all = AllCases(AllCases::InitType::WITH_ALL_CASES); for (int head = 0; head < 16; ++head) { uint64_t prefix = (uint64_t)head << 32; @@ -15,9 +20,18 @@ void ShortCode::build_mapping() { // build fast search mapping } } -void ShortCode::speed_up() { // speed up for zip / unzip short code - if (all_cases_list.empty()) { - build_mapping(); +void ShortCode::speed_up(enum Mode mode) { // speed up handle short code + switch (mode) { + case Mode::NORMAL: // speed up into normal mode + if (basic_ranges.empty()) { + build_base_ranges(); // basic ranges initialize + } + break; + case Mode::FAST: // speed up into fast mode + if (all_cases_list.empty()) { + build_mappings(); // all cases mapping initialize + } + break; } } diff --git a/klotski/short_code.h b/klotski/short_code.h index 8a49694..a6d0cf4 100644 --- a/klotski/short_code.h +++ b/klotski/short_code.h @@ -7,10 +7,14 @@ class ShortCode { public: + enum Mode {NORMAL, FAST}; + + std::vector basic_ranges; + std::vector all_cases_list; // short_code -> common_code std::unordered_map all_cases_dict; // common_code -> short_code - void speed_up(); + void speed_up(enum Mode mode); // uint32_t zip_short_code(uint64_t code); // uint64_t unzip_short_code(uint32_t short_code); @@ -18,11 +22,14 @@ public: uint64_t fast_decode(uint32_t short_code); uint32_t fast_encode(uint64_t common_code); + uint64_t tiny_decode(uint32_t short_code); + uint32_t tiny_encode(uint64_t common_code); + private: // std::vector all_cases_list; // short_code -> common_code // std::unordered_map all_cases_dict; // common_code -> short_code - void build_mapping(); - + void build_mappings(); + void build_base_ranges(); };