Browse Source

perf: AllCases init parameter

master
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 "all_cases.h"
AllCases::AllCases(enum InitType init) {
switch (init) {
case InitType::WITH_ALL_CASES:
get_all_cases();
case InitType::WITH_BASIC_RANGES:
get_basic_ranges();
case WITH_NOTHING:
AllCases::AllCases(AllCases::Build type) { // class initialize
build(type);
}
void AllCases::build(AllCases::Build type) { // build specific type
switch (type) {
case Build::BASIC_RANGES:
get_basic_ranges(); // basic ranges initialize
break;
case Build::ALL_CASES:
get_all_cases(); // all cases initialize
break;
}
}

11
klotski/all_cases.h

@ -5,14 +5,11 @@
class AllCases {
public:
enum InitType {
WITH_NOTHING,
WITH_BASIC_RANGES,
WITH_ALL_CASES,
};
enum Build {BASIC_RANGES, ALL_CASES};
explicit AllCases(enum InitType init);
AllCases():AllCases(InitType::WITH_NOTHING) {};
AllCases() = default;
void build(enum Build type);
explicit AllCases(enum Build type);
const std::vector<uint32_t>* get_basic_ranges();
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(ShortCode::Mode::NORMAL);
auto s = ShortCode(ShortCode::Mode::FAST);
auto s = ShortCode(ShortCode::Mode::NORMAL);
// auto s = ShortCode(ShortCode::Mode::FAST);
// s.speed_up(ShortCode::Mode::FAST);
std::cout << s.all_cases_list.size() << std::endl;
std::cout << s.all_cases_dict.size() << std::endl;
// 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));
// s.speed_up(ShortCode::Mode::NORMAL);
// std::cout << s.basic_ranges.size() << std::endl;
std::cout << s.basic_ranges.size() << std::endl;
return 0;
}

40
klotski/short_code.cc

@ -2,22 +2,8 @@
#include "short_code.h"
#include "short_code_mark.h"
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;
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
}
ShortCode::ShortCode(ShortCode::Mode mode) { // class initialize
speed_up(mode);
}
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
// TODO: ensure input short_code < SHORT_CODE_LIMIT
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
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
ShortCode() = default;
explicit ShortCode(enum Mode mode);
void speed_up(enum Mode mode);
explicit ShortCode(enum Mode mode);
// uint32_t zip_short_code(uint64_t code);
// uint64_t unzip_short_code(uint32_t short_code);

Loading…
Cancel
Save