Browse Source

update: speed up with fast and normal mode

master
Dnomd343 2 years ago
parent
commit
614447e3a2
  1. 9
      klotski/main.cc
  2. 22
      klotski/short_code.cc
  3. 13
      klotski/short_code.h

9
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;
}

22
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;
}
}

13
klotski/short_code.h

@ -7,10 +7,14 @@
class ShortCode {
public:
enum Mode {NORMAL, FAST};
std::vector<uint32_t> basic_ranges;
std::vector<uint64_t> all_cases_list; // short_code -> common_code
std::unordered_map<uint64_t, uint32_t> 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<uint64_t> all_cases_list; // short_code -> common_code
// std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code
void build_mapping();
void build_mappings();
void build_base_ranges();
};

Loading…
Cancel
Save