Browse Source

perf: AllCases init parameter

legacy
Dnomd343 2 years ago
parent
commit
375c121f3f
  1. 18
      klotski/all_cases.cc
  2. 11
      klotski/all_cases.h
  3. 10
      klotski/main.cc
  4. 40
      klotski/short_code.cc
  5. 3
      klotski/short_code.h

18
klotski/all_cases.cc

@ -2,13 +2,17 @@
#include "common.h" #include "common.h"
#include "all_cases.h" #include "all_cases.h"
AllCases::AllCases(enum InitType init) { AllCases::AllCases(AllCases::Build type) { // class initialize
switch (init) { build(type);
case InitType::WITH_ALL_CASES: }
get_all_cases();
case InitType::WITH_BASIC_RANGES: void AllCases::build(AllCases::Build type) { // build specific type
get_basic_ranges(); switch (type) {
case WITH_NOTHING: case Build::BASIC_RANGES:
get_basic_ranges(); // basic ranges initialize
break;
case Build::ALL_CASES:
get_all_cases(); // all cases initialize
break; break;
} }
} }

11
klotski/all_cases.h

@ -5,14 +5,11 @@
class AllCases { class AllCases {
public: public:
enum InitType { enum Build {BASIC_RANGES, ALL_CASES};
WITH_NOTHING,
WITH_BASIC_RANGES,
WITH_ALL_CASES,
};
explicit AllCases(enum InitType init); AllCases() = default;
AllCases():AllCases(InitType::WITH_NOTHING) {}; void build(enum Build type);
explicit AllCases(enum Build type);
const std::vector<uint32_t>* get_basic_ranges(); const std::vector<uint32_t>* get_basic_ranges();
const std::vector<uint32_t> (*get_all_cases())[16]; const std::vector<uint32_t> (*get_all_cases())[16];

10
klotski/main.cc

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

40
klotski/short_code.cc

@ -2,22 +2,8 @@
#include "short_code.h" #include "short_code.h"
#include "short_code_mark.h" #include "short_code_mark.h"
void ShortCode::build_base_ranges() { ShortCode::ShortCode(ShortCode::Mode mode) { // class initialize
auto all = AllCases(AllCases::InitType::WITH_BASIC_RANGES); speed_up(mode);
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;
for (const auto &range : (*all.get_all_cases())[head]) {
all_cases_list.emplace_back(prefix | range); // short_code => common_code
}
}
for (int n = 0; n < all_cases_list.size(); ++n) {
all_cases_dict[all_cases_list[n]] = n; // common_code => short_code
}
} }
void ShortCode::speed_up(enum Mode mode) { // speed up handle short code void ShortCode::speed_up(enum Mode mode) { // speed up handle short code
@ -35,6 +21,24 @@ void ShortCode::speed_up(enum Mode mode) { // speed up handle short code
} }
} }
void ShortCode::build_base_ranges() { // build basic ranges
auto all = AllCases(AllCases::Build::BASIC_RANGES);
basic_ranges = *all.get_basic_ranges();
}
void ShortCode::build_mappings() { // build fast search mappings
auto all = AllCases(AllCases::Build::ALL_CASES);
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
}
}
for (int n = 0; n < all_cases_list.size(); ++n) {
all_cases_dict[all_cases_list[n]] = n; // common_code => short_code
}
}
uint64_t ShortCode::fast_decode(uint32_t short_code) { // short_code -> common_code uint64_t ShortCode::fast_decode(uint32_t short_code) { // short_code -> common_code
// TODO: ensure input short_code < SHORT_CODE_LIMIT // TODO: ensure input short_code < SHORT_CODE_LIMIT
return all_cases_list[short_code]; return all_cases_list[short_code];
@ -44,7 +48,3 @@ uint32_t ShortCode::fast_encode(uint64_t common_code) { // common_code -> short_
// TODO: ensure input common_code valid // TODO: ensure input common_code valid
return all_cases_dict[common_code]; return all_cases_dict[common_code];
} }
ShortCode::ShortCode(ShortCode::Mode mode) {
speed_up(mode);
}

3
klotski/short_code.h

@ -15,9 +15,8 @@ public:
std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code std::unordered_map<uint64_t, uint32_t> all_cases_dict; // common_code -> short_code
ShortCode() = default; ShortCode() = default;
explicit ShortCode(enum Mode mode);
void speed_up(enum Mode mode); void speed_up(enum Mode mode);
explicit ShortCode(enum Mode mode);
// uint32_t zip_short_code(uint64_t code); // uint32_t zip_short_code(uint64_t code);
// uint64_t unzip_short_code(uint32_t short_code); // uint64_t unzip_short_code(uint32_t short_code);

Loading…
Cancel
Save