Browse Source

feat: short code mapping function

legacy
Dnomd343 2 years ago
parent
commit
cfa7f08348
  1. 2
      klotski/CMakeLists.txt
  2. 27
      klotski/main.cc
  3. 27
      klotski/short_code.cc
  4. 23
      klotski/short_code.h
  5. 0
      klotski/short_code_mark.h

2
klotski/CMakeLists.txt

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)
add_executable(klotski main.cc common.cc all_cases.cc) add_executable(klotski main.cc common.cc all_cases.cc short_code.cc)

27
klotski/main.cc

@ -1,20 +1,31 @@
#include <iostream> #include <iostream>
#include "all_cases.h" //#include "all_cases.h"
#include "short_code.h"
int main() { int main() {
// auto a = AllCases(); // auto a = AllCases();
// auto a = AllCases(AllCases::InitType::WITH_NOTHING); // auto a = AllCases(AllCases::InitType::WITH_NOTHING);
// auto a = AllCases(AllCases::InitType::WITH_BASIC_RANGES); // auto a = AllCases(AllCases::InitType::WITH_BASIC_RANGES);
auto a = AllCases(AllCases::InitType::WITH_ALL_CASES); // auto a = AllCases(AllCases::InitType::WITH_ALL_CASES);
std::cout << "start get basic ranges" << std::endl; // std::cout << "start get basic ranges" << std::endl;
std::cout << "basic range: " << a.get_basic_ranges()->size() << std::endl; // std::cout << "basic range: " << a.get_basic_ranges()->size() << std::endl;
std::cout << "start get all cases" << std::endl; // std::cout << "start get all cases" << std::endl;
for (const auto &temp : *a.get_all_cases()) { // for (const auto &temp : *a.get_all_cases()) {
std::cout << temp.size() << std::endl; // std::cout << temp.size() << std::endl;
} // }
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;
return 0; return 0;
} }

27
klotski/short_code.cc

@ -0,0 +1,27 @@
#include "all_cases.h"
#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() {
if (all_cases_list.empty()) {
build_mapping();
}
}

23
klotski/short_code.h

@ -0,0 +1,23 @@
#pragma once
#include <vector>
#include <cstdint>
#include <unordered_map>
class ShortCode {
public:
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();
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();
};

0
all_cases/short_code_mark.h → klotski/short_code_mark.h

Loading…
Cancel
Save