From 36495525a01507d6ee34733be82ea4a1a34c5f19 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 19:12:31 +0800 Subject: [PATCH] feat: next ShortCode module --- src/CMakeLists.txt | 4 ++- src/all_cases/basic_ranges.h | 2 +- src/main.cc | 22 ++++++++++---- src/short_code/CMakeLists.txt | 4 +++ src/short_code/short_code.cc | 55 +++++++++++++++++++++++++++++++++++ src/short_code/short_code.h | 30 +++++++++++++++++++ 6 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 src/short_code/CMakeLists.txt create mode 100644 src/short_code/short_code.cc create mode 100644 src/short_code/short_code.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5cc8d8c..b59255a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,13 +2,15 @@ cmake_minimum_required(VERSION 3.0) include_directories(common) include_directories(all_cases) +include_directories(short_code) include_directories(common_code) - add_subdirectory(common) add_subdirectory(all_cases) +add_subdirectory(short_code) add_subdirectory(common_code) add_executable(klotski main.cc) target_link_libraries(klotski all_cases) +target_link_libraries(klotski short_code) target_link_libraries(klotski common_code) diff --git a/src/all_cases/basic_ranges.h b/src/all_cases/basic_ranges.h index 85a8b29..e5b0f87 100644 --- a/src/all_cases/basic_ranges.h +++ b/src/all_cases/basic_ranges.h @@ -12,7 +12,7 @@ public: AVAILABLE, }; static void build_basic_ranges(); - static enum Status basic_ranges_status(); + static enum Status basic_ranges_status(); // TODO: convert to get_status function static const std::vector* get_basic_ranges(); private: diff --git a/src/main.cc b/src/main.cc index a6e3021..5f927d3 100644 --- a/src/main.cc +++ b/src/main.cc @@ -2,6 +2,7 @@ #include "all_cases.h" #include "basic_ranges.h" #include "common_code.h" +#include "short_code.h" //void get_status() { // switch (BasicRanges::basic_ranges_status()) { @@ -53,12 +54,23 @@ int main() { // std::cout << CommonCode::to_string(0x1A9BF0C00) << std::endl; // std::cout << CommonCode::to_string(0x1A9BF0C00, true) << std::endl; - auto c = CommonCode("1A9bF0c0"); - std::cout << c.to_string(true) << std::endl; - std::cout << c.to_string() << std::endl; - printf("%09lX\n", c.unwrap()); +// auto c = CommonCode("1A9bF0c0"); +// std::cout << c.to_string(true) << std::endl; +// std::cout << c.to_string() << std::endl; +// printf("%09lX\n", c.unwrap()); +// +// std::cout << CommonCode(0x1A9BF0C00).to_string() << std::endl; + + + std::cout << "start NORMAL speed up" << std::endl; + ShortCode::speed_up(ShortCode::NORMAL); + std::cout << "NORMAL speed up complete" << std::endl; + + std::cout << "start FAST speed up" << std::endl; + ShortCode::speed_up(ShortCode::FAST); + std::cout << "FAST speed up complete" << std::endl; - std::cout << CommonCode(0x1A9BF0C00).to_string() << std::endl; + std::cout << ShortCode::all_cases_list.size() << std::endl; return 0; } diff --git a/src/short_code/CMakeLists.txt b/src/short_code/CMakeLists.txt new file mode 100644 index 0000000..5979540 --- /dev/null +++ b/src/short_code/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.0) + +add_library(short_code short_code.cc) +target_link_libraries(short_code all_cases) diff --git a/src/short_code/short_code.cc b/src/short_code/short_code.cc new file mode 100644 index 0000000..6d70279 --- /dev/null +++ b/src/short_code/short_code.cc @@ -0,0 +1,55 @@ +#include "all_cases.h" +#include "short_code.h" +#include "basic_ranges.h" + +std::mutex ShortCode::map_building; + +bool ShortCode::fast_mode_available = false; +bool ShortCode::normal_mode_available = false; + +std::vector ShortCode::all_cases_list; +std::unordered_map ShortCode::all_cases_dict; + +void ShortCode::speed_up(ShortCode::Mode mode) { + if (fast_mode_available) { + return; // fast mode already available + } + if (mode == ShortCode::FAST) { // build fast mode data + + // build_mappings + build_mappings(); + + } else if (mode == ShortCode::NORMAL && !normal_mode_available) { // build normal mode data + BasicRanges::build_basic_ranges(); // blocking function + normal_mode_available = true; + } + +} + +void ShortCode::build_mappings() { // build fast search mappings + + if (fast_mode_available) { + return; // all cases map already built + } + + if (map_building.try_lock()) { // lock success -> start building + + AllCases::build_all_cases(); // blocking function + + for (int head = 0; head < 16; ++head) { + uint64_t prefix = (uint64_t)head << 32; + for (const auto &range : (*AllCases::get_all_cases())[head]) { + all_cases_list.emplace_back(prefix | range); // short_code -> common_code + } + } + for (int index = 0; index < all_cases_list.size(); ++index) { + all_cases_dict[all_cases_list[index]] = index; // common_code -> short_code + } + + fast_mode_available = true; // set available flag + } else { // another thread building + map_building.lock(); // blocking waiting + } + map_building.unlock(); + +} diff --git a/src/short_code/short_code.h b/src/short_code/short_code.h new file mode 100644 index 0000000..2c6f701 --- /dev/null +++ b/src/short_code/short_code.h @@ -0,0 +1,30 @@ +#pragma once + +#include +#include +#include +#include + +class ShortCode { +public: + + enum Mode { + NORMAL, FAST + }; + + static void build_mappings(); + + static void speed_up(enum Mode mode); + + static std::mutex map_building; + + static bool fast_mode_available; + static bool normal_mode_available; + + +// static std::vector *basic_ranges; + + static std::vector all_cases_list; // short_code -> common_code + static std::unordered_map all_cases_dict; // common_code -> short_code + +};