From b98f2db7b5acfc3b896f0b9dc04b8c57b5ad7604 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 12 Jan 2023 13:30:33 +0800 Subject: [PATCH] feat: FastCal module --- src/CMakeLists.txt | 19 +++++++++-- src/core/core.cc | 3 +- src/core/core.h | 9 +++-- src/fast_cal.h | 5 --- src/fast_cal/CMakeLists.txt | 4 +++ src/fast_cal/fast_cal.cc | 65 +++++++++++++++++++++++++++++++++++++ src/fast_cal/fast_cal.h | 22 +++++++++++++ src/main.cc | 4 ++- 8 files changed, 119 insertions(+), 12 deletions(-) delete mode 100644 src/fast_cal.h create mode 100644 src/fast_cal/CMakeLists.txt create mode 100644 src/fast_cal/fast_cal.cc create mode 100644 src/fast_cal/fast_cal.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a22a93c..93495f5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,7 @@ cmake_minimum_required(VERSION 3.0) +################################ + include_directories(core) include_directories(utils) include_directories(all_cases) @@ -8,6 +10,10 @@ include_directories(raw_code) include_directories(short_code) include_directories(common_code) +include_directories(fast_cal) + +################################ + add_subdirectory(core) add_subdirectory(utils) add_subdirectory(all_cases) @@ -16,8 +22,13 @@ add_subdirectory(raw_code) add_subdirectory(short_code) add_subdirectory(common_code) -#add_executable(klotski main.cc) -add_executable(klotski main.cc fast_cal.cc) +add_subdirectory(fast_cal) + +################################ + +add_executable(klotski main.cc) + +################################ target_link_libraries(klotski core) target_link_libraries(klotski utils) @@ -27,4 +38,8 @@ target_link_libraries(klotski raw_code) target_link_libraries(klotski short_code) target_link_libraries(klotski common_code) +target_link_libraries(klotski fast_cal) + target_link_libraries(klotski pthread) + +################################ diff --git a/src/core/core.cc b/src/core/core.cc index f459a80..e154935 100644 --- a/src/core/core.cc +++ b/src/core/core.cc @@ -173,7 +173,8 @@ void Core::next_step(uint64_t code, uint64_t mask) { // search next step cases // TODO: try to send multi-items data for (int i = 1; i < cache_size; ++i) { - release(cache[i].code, cache[i].mask); // release next cases +// release(cache[i].code, cache[i].mask); // release next cases + release_next(cache[i].code, cache[i].mask); // release next cases } cache_size = 1; // reset cache size diff --git a/src/core/core.h b/src/core/core.h index b3bfaf6..11a0d41 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -1,6 +1,7 @@ #pragma once #include +#include #define UP (-4 * 3) #define LEFT (-1 * 3) @@ -10,10 +11,12 @@ class Core { public: - typedef void (*release_t)(uint64_t, uint64_t); +// typedef void (*release_t)(uint64_t, uint64_t); + + std::function release_next; void next_step(uint64_t code, uint64_t mask); - explicit Core(release_t release_func) : release(release_func) {} +// explicit Core(release_t release_func) : release(release_func) {} private: struct cache_t { @@ -25,7 +28,7 @@ private: int cache_size = 1; cache_t cache[16] = {0}; - release_t release; // release code and mask +// release_t release; // release code and mask void move_1x1(uint64_t code, int addr); void move_1x2(uint64_t code, int addr); diff --git a/src/fast_cal.h b/src/fast_cal.h deleted file mode 100644 index 939365e..0000000 --- a/src/fast_cal.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -#include - -uint32_t fast_cal(uint64_t start_code); diff --git a/src/fast_cal/CMakeLists.txt b/src/fast_cal/CMakeLists.txt new file mode 100644 index 0000000..5b21746 --- /dev/null +++ b/src/fast_cal/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.0) + +add_library(fast_cal fast_cal.cc) +target_link_libraries(fast_cal core) diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc new file mode 100644 index 0000000..acd7e5c --- /dev/null +++ b/src/fast_cal/fast_cal.cc @@ -0,0 +1,65 @@ +#include "core.h" +#include "common.h" +#include "fast_cal.h" + +#include "raw_code.h" + +#include + +void FastCal::fast_cal(uint64_t code) { + + std::cout << RawCode(code).dump_case() << std::endl; + + auto core = Core(); + core.release_next = std::bind(&FastCal::add_new_case, this, std::placeholders::_1, std::placeholders::_2); + + cases.empty(); + cache.empty(); + + cases[code] = fast_cal_t { + .code = code, + .mask = 0, + .last = nullptr, + }; + cache.emplace(&cases[code]); + + while (!cache.empty()) { + + // break check point + if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) { + std::cout << "Resolved" << std::endl; +// std::cout << RawCode(cache.front()->code).dump_case() << std::endl; + break; + } + + core.next_step(cache.front()->code, cache.front()->mask); + cache.pop(); + } + + auto solution = cache.front(); + + while (solution != nullptr) { +// std::cout << RawCode(solution->code).dump_case() << std::endl; + solution = solution->last; + } + +} + +void FastCal::add_new_case(uint64_t code, uint64_t mask) { + + auto exist_case = cases.find(code); + if (exist_case != cases.end()) { // find existed case + + exist_case->second.mask |= mask; // mask update + return; + + } + + cases[code] = fast_cal_t { + .code = code, + .mask = mask, + .last = cache.front(), + };; + cache.emplace(&cases[code]); + +} diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h new file mode 100644 index 0000000..f8dbc68 --- /dev/null +++ b/src/fast_cal/fast_cal.h @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +class FastCal { +public: + struct fast_cal_t { + uint64_t code; + uint64_t mask; + fast_cal_t *last; + }; + + std::queue cache; + std::unordered_map cases; + + void fast_cal(uint64_t code); + + void add_new_case(uint64_t code, uint64_t mask); + +}; diff --git a/src/main.cc b/src/main.cc index 4b1222f..5154e46 100644 --- a/src/main.cc +++ b/src/main.cc @@ -194,7 +194,9 @@ int main() { // } auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap(); - std::cout << fast_cal(raw_code) << std::endl; +// std::cout << fast_cal(raw_code) << std::endl; + auto f = FastCal(); + f.fast_cal(raw_code); // int sum = 0; // for (auto const &raw_code : all_cases_raw) {