From 3a1d0a4d7e7753b03ef02eddd65ecce6cc31dfa8 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 13 Jan 2023 13:17:30 +0800 Subject: [PATCH] feat: framework of Analyse module --- src/CMakeLists.txt | 3 +++ src/analyse/CMakeLists.txt | 4 ++++ src/analyse/analyse.cc | 49 ++++++++++++++++++++++++++++++++++++++ src/analyse/analyse.h | 27 +++++++++++++++++++++ src/main.cc | 8 +++++-- 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 src/analyse/CMakeLists.txt create mode 100644 src/analyse/analyse.cc create mode 100644 src/analyse/analyse.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 93495f5..e92a17d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories(short_code) include_directories(common_code) include_directories(fast_cal) +include_directories(analyse) ################################ @@ -23,6 +24,7 @@ add_subdirectory(short_code) add_subdirectory(common_code) add_subdirectory(fast_cal) +add_subdirectory(analyse) ################################ @@ -39,6 +41,7 @@ target_link_libraries(klotski short_code) target_link_libraries(klotski common_code) target_link_libraries(klotski fast_cal) +target_link_libraries(klotski analyse) target_link_libraries(klotski pthread) diff --git a/src/analyse/CMakeLists.txt b/src/analyse/CMakeLists.txt new file mode 100644 index 0000000..bc7a1e9 --- /dev/null +++ b/src/analyse/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.0) + +add_library(analyse analyse.cc) +target_link_libraries(analyse core) diff --git a/src/analyse/analyse.cc b/src/analyse/analyse.cc new file mode 100644 index 0000000..c468262 --- /dev/null +++ b/src/analyse/analyse.cc @@ -0,0 +1,49 @@ +#include "core.h" +#include "analyse.h" + +#include + +Core Analyse::new_core() { + return Core( + [this](auto &&code, auto &&mask) { // lambda as function pointer + new_case(std::forward(code), std::forward(mask)); + } + ); +} + +void Analyse::start_analyse(uint64_t code) { + + auto core = new_core(); + + cases.empty(); + cache.empty(); + + cases.reserve(65536); + + cache.emplace(&cases.emplace(code, analyse_t { + .code = code, + .mask = 0, + }).first->second); + + while (!cache.empty()) { + + core.next_step(cache.front()->code, cache.front()->mask); + cache.pop(); + } + + std::cout << "size: " << cases.size() << std::endl; + +} + +void Analyse::new_case(uint64_t code, uint64_t mask) { + auto current = cases.find(code); + if (current != cases.end()) { // find existed case + current->second.mask |= mask; // update mask info + return; + } + + cache.emplace(&cases.emplace(code, analyse_t { + .code = code, + .mask = mask, + }).first->second); +} diff --git a/src/analyse/analyse.h b/src/analyse/analyse.h new file mode 100644 index 0000000..5080a2e --- /dev/null +++ b/src/analyse/analyse.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +class Analyse { +public: + struct analyse_t { + uint64_t code; + uint64_t mask; + +// int step; + // TODO: try set / unordered_set +// std::list src; + }; + + std::queue cache; + std::unordered_map cases; + + inline Core new_core(); + + void start_analyse(uint64_t code); + + void new_case(uint64_t code, uint64_t mask); + +}; diff --git a/src/main.cc b/src/main.cc index f98c3bd..52d0567 100644 --- a/src/main.cc +++ b/src/main.cc @@ -6,6 +6,7 @@ #include "short_code.h" #include "raw_code.h" #include "fast_cal.h" +#include "analyse.h" //#include "core_demo.h" @@ -196,8 +197,11 @@ int main() { auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap(); // std::cout << fast_cal(raw_code) << std::endl; - auto f = FastCal(); - f.fast_cal(raw_code); +// auto f = FastCal(); +// f.fast_cal(raw_code); + + auto a = Analyse(); + a.start_analyse(raw_code); // int sum = 0; // for (auto const &raw_code : all_cases_raw) {