From c2184035f85cdbb0e7cb604c15cfd7909125bf9e Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 18 Jan 2023 16:58:23 +0800 Subject: [PATCH] feat: using lambda for confirm target --- src/fast_cal/fast_cal.cc | 28 ++++++++++++++++++++-------- src/fast_cal/fast_cal.h | 30 ++++++++++++++++++++++++++---- src/main.cc | 7 +++++-- 3 files changed, 51 insertions(+), 14 deletions(-) diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index d8040b1..587bb52 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -31,22 +31,31 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for } -std::vector FastCal::search(uint64_t code) { +std::vector FastCal::solve() { + + auto resolved = [](uint64_t code) { + return ((code >> (3 * 0xD)) & 0b111) == B_2x2; + }; + return target(resolved); + +} + +std::vector FastCal::target(const std::function &match) { auto core = import_core(); // clear data cases.clear(); - auto empty = std::queue{}; - std::swap(empty, cache); + std::queue{}.swap(cache); +// auto empty = std::queue{}; +// std::swap(empty, cache); cases.reserve(65536); - - cache.emplace(&cases.emplace(code, fast_cal_t { - .code = code, + cache.emplace(&cases.emplace(root, fast_cal_t { + .code = root, .mask = 0, .last = nullptr, }).first->second); @@ -54,8 +63,7 @@ std::vector FastCal::search(uint64_t code) { while (!cache.empty()) { /// break check point - if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) { - std::cout << "Resolved" << std::endl; + if (match(cache.front()->code)) { break; } @@ -65,6 +73,10 @@ std::vector FastCal::search(uint64_t code) { std::cout << "size: " << cases.size() << std::endl; + // TODO: single backtrack function + + // TODO: cache may empty -> never found + auto solution = cache.front(); std::vector solution_path; diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index 2f5272c..c303ecd 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -8,21 +8,43 @@ class FastCal { public: + + explicit FastCal(uint64_t code) : root(code) {} + + // search resolve + // search all min-step resolve + // search the furthest cases + // search target by code + // search target by lambda + + /// xxx_multi only search until same layer + +// std::vector solve(uint64_t code); + std::vector solve(); +// solve_multi + + std::vector target(const std::function &match); + + + // TODO: continue search process? -> reuse exist data (ensure working code not changed) + + // TODO: static search functions + +private: struct fast_cal_t { uint64_t code; uint64_t mask; fast_cal_t *last; }; + + uint64_t root; + std::queue cache; std::unordered_map cases; Core import_core(); - std::vector search(uint64_t code); - void new_case(uint64_t code, uint64_t mask); - - }; diff --git a/src/main.cc b/src/main.cc index 5cde688..e55f0f8 100644 --- a/src/main.cc +++ b/src/main.cc @@ -25,8 +25,11 @@ int main() { // AllCases::build(); - auto f = FastCal(); - auto ret = f.search(RawCode::from_common_code("1a9bf0c").unwrap()); +// auto f = FastCal(); +// auto ret = f.solve(RawCode::from_common_code("1a9bf0c").unwrap()); + + auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap()); + auto ret = f.solve(); // for (const auto &c : ret) { // std::cout << RawCode(c) << std::endl;