From cfa7f08348d7c37575e48acbeb6f47b2868bfffa Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 6 Jan 2023 01:18:19 +0800 Subject: [PATCH] feat: short code mapping function --- klotski/CMakeLists.txt | 2 +- klotski/main.cc | 27 +++++++++++++++++------- klotski/short_code.cc | 27 ++++++++++++++++++++++++ klotski/short_code.h | 23 ++++++++++++++++++++ {all_cases => klotski}/short_code_mark.h | 0 5 files changed, 70 insertions(+), 9 deletions(-) create mode 100644 klotski/short_code.cc create mode 100644 klotski/short_code.h rename {all_cases => klotski}/short_code_mark.h (100%) diff --git a/klotski/CMakeLists.txt b/klotski/CMakeLists.txt index ba0fdf1..261c9d5 100644 --- a/klotski/CMakeLists.txt +++ b/klotski/CMakeLists.txt @@ -1,4 +1,4 @@ cmake_minimum_required(VERSION 3.0) 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) diff --git a/klotski/main.cc b/klotski/main.cc index ad78994..e17033b 100644 --- a/klotski/main.cc +++ b/klotski/main.cc @@ -1,20 +1,31 @@ #include -#include "all_cases.h" +//#include "all_cases.h" +#include "short_code.h" int main() { // auto a = AllCases(); // auto a = AllCases(AllCases::InitType::WITH_NOTHING); // 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 << "basic range: " << a.get_basic_ranges()->size() << std::endl; +// std::cout << "start get basic ranges" << std::endl; +// std::cout << "basic range: " << a.get_basic_ranges()->size() << std::endl; - std::cout << "start get all cases" << std::endl; - for (const auto &temp : *a.get_all_cases()) { - std::cout << temp.size() << std::endl; - } +// std::cout << "start get all cases" << std::endl; +// for (const auto &temp : *a.get_all_cases()) { +// 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; } diff --git a/klotski/short_code.cc b/klotski/short_code.cc new file mode 100644 index 0000000..3e16bf6 --- /dev/null +++ b/klotski/short_code.cc @@ -0,0 +1,27 @@ +#include "all_cases.h" +#include "short_code.h" +#include "short_code_mark.h" + +//#include + +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(); + } +} diff --git a/klotski/short_code.h b/klotski/short_code.h new file mode 100644 index 0000000..c5a36ed --- /dev/null +++ b/klotski/short_code.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include +#include + +class ShortCode { +public: + + std::vector all_cases_list; // short_code -> common_code + std::unordered_map all_cases_dict; // common_code -> short_code + + void speed_up(); + + +private: +// std::vector all_cases_list; // short_code -> common_code +// std::unordered_map all_cases_dict; // common_code -> short_code + + void build_mapping(); + + +}; diff --git a/all_cases/short_code_mark.h b/klotski/short_code_mark.h similarity index 100% rename from all_cases/short_code_mark.h rename to klotski/short_code_mark.h