From 1588336db0e88029bba515cadf7cdc76a1da451e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 19 Jan 2023 20:37:30 +0800 Subject: [PATCH] feat: using RawCode instead of uint64_t in Analyse --- src/analyse/analyse.cc | 27 +++++++++++++++++++-------- src/analyse/analyse.h | 10 ++++++++-- src/fast_cal/cal_core.cc | 4 +++- src/fast_cal/fast_cal.h | 2 +- src/main.cc | 10 ++++++++-- 5 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/analyse/analyse.cc b/src/analyse/analyse.cc index ca5a6ca..46e4969 100644 --- a/src/analyse/analyse.cc +++ b/src/analyse/analyse.cc @@ -1,6 +1,14 @@ -#include #include "analyse.h" +Analyse::Analyse(const RawCode &code) { + this->root = (uint64_t)code; +} + +void Analyse::set_root(const RawCode &code) { + this->root = (uint64_t)code; +} + +/// memory initialize and return klotski core Core Analyse::init(uint64_t code) { /// reset working data cases.clear(); @@ -41,22 +49,25 @@ void Analyse::new_case(uint64_t code, uint64_t mask) { } } -void Analyse::build(uint64_t code) { - auto core = init(code); +/// analyse and build klotski tree +void Analyse::build() { + auto core = init(root); while (!cache.empty()) { core.next_cases(cache.front()->code, cache.front()->mask); cache.pop(); } } -std::vector Analyse::build_until(uint64_t code, const match_t &match) { - auto core = init(code); +std::vector Analyse::build_until(const match_t &match) { + auto core = init(root); auto layer_end = cache.back(); - std::vector matched; // matched list + std::vector matched; // matched list /// start BFS search while (!cache.empty()) { if (match(cache.front()->code)) { // match target - matched.emplace_back(cache.front()->code); + matched.emplace_back( + RawCode::unsafe_create(cache.front()->code) // record matched cases + ); } core.next_cases(cache.front()->code, cache.front()->mask); if (cache.front() == layer_end) { // reach layer ending @@ -67,5 +78,5 @@ std::vector Analyse::build_until(uint64_t code, const match_t &match) } cache.pop(); } - return std::vector{}; // no target found + return std::vector{}; // no target found } diff --git a/src/analyse/analyse.h b/src/analyse/analyse.h index 2a9f222..474ad52 100644 --- a/src/analyse/analyse.h +++ b/src/analyse/analyse.h @@ -6,6 +6,7 @@ #include #include #include "core.h" +#include "raw_code.h" const uint32_t ANY_MAP_RESERVE = 65536; @@ -13,8 +14,13 @@ class Analyse { public: typedef std::function match_t; - void build(uint64_t code); - std::vector build_until(uint64_t code, const match_t &match); + /// setting root code + void set_root(const RawCode &code); + explicit Analyse(const RawCode &code); + + /// BFS search functions + void build(); + std::vector build_until(const match_t &match); private: struct analyse_t { diff --git a/src/fast_cal/cal_core.cc b/src/fast_cal/cal_core.cc index b7cac3c..1480282 100644 --- a/src/fast_cal/cal_core.cc +++ b/src/fast_cal/cal_core.cc @@ -88,7 +88,9 @@ std::vector FastCal::target_multi(const match_t &match) { /// start BFS search while (!cache.empty()) { if (match(cache.front()->code)) { // match target - matched.emplace_back(cache.front()->code); + matched.emplace_back( + RawCode::unsafe_create(cache.front()->code) // record matched cases + ); } core.next_cases(cache.front()->code, cache.front()->mask); if (cache.front() == layer_end) { // reach layer ending diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index 32e0664..9b77549 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -17,7 +17,7 @@ class FastCal { public: typedef std::function match_t; - /// FastCal start case + /// setting root code void set_root(const RawCode &code); explicit FastCal(const RawCode &code); diff --git a/src/main.cc b/src/main.cc index 636f6cc..d061092 100644 --- a/src/main.cc +++ b/src/main.cc @@ -53,8 +53,14 @@ int main() { // } - auto a = Analyse(); - a.build((uint64_t)RawCode::from_common_code("1a9bf0c")); + auto a = Analyse(RawCode::from_common_code("1a9bf0c")); +// a.build(); + auto ret = a.build_until([](uint64_t code) { + return ((code >> (3 * 0xD)) & 0b111) == B_2x2; + }); + for (const auto &r : ret) { + std::cout << r << std::endl; + } // auto ret = a.build_until((uint64_t)RawCode::from_common_code("1a9bf0c"), [](uint64_t code) { // return ((code >> (3 * 0xD)) & 0b111) == B_2x2;