From b5861820b94853bc628cec513caf1b97160c27cc Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Wed, 18 Jan 2023 16:27:08 +0800 Subject: [PATCH] update: FastCal module --- src/fast_cal/fast_cal.cc | 64 +++++++++++++++------------------------- src/fast_cal/fast_cal.h | 8 ++++- src/main.cc | 6 +++- 3 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index 8a6c3e9..d8040b1 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -5,8 +5,18 @@ #include "raw_code.h" #include +#include + +Core FastCal::import_core() { + return Core( + [this](auto &&code, auto &&mask) { // lambda as function pointer + new_case(std::forward(code), std::forward(mask)); + } + ); +} void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for new case + auto current = cases.find(code); if (current != cases.end()) { // find existed case current->second.mask |= mask; // update mask info @@ -19,50 +29,21 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for .last = cache.front(), }).first->second); - -// auto insert_ret = cases.emplace(code, fast_cal_t { -// .code = code, -// .mask = mask, -// .last = cache.front(), -// }); -// -// if (insert_ret.second) { // insert success -// cache.emplace(&insert_ret.first->second); -// } else { -// insert_ret.first->second.mask |= mask; -// } - - -// cases[code] = fast_cal_t { // insert into cases map -// .code = code, -// .mask = mask, -// .last = cache.front(), // parent case -// }; -// cache.emplace(&cases[code]); // add in working queue } -void FastCal::fast_cal(uint64_t code) { +std::vector FastCal::search(uint64_t code) { - auto core = Core( - [this](auto &&code, auto &&mask) { // lambda as function pointer - new_case(std::forward(code), std::forward(mask)); - } - ); + auto core = import_core(); + + // clear data + cases.clear(); - cases.empty(); - cache.empty(); + auto empty = std::queue{}; + std::swap(empty, cache); -// cases[code] = fast_cal_t { -// .code = code, -// .mask = 0, -// .last = nullptr, -// }; -// cache.emplace(&cases[code]); -// cases.reserve(30000); cases.reserve(65536); -// cases.reserve(65536 * 2); -// cases.reserve(65536 * 4); + cache.emplace(&cases.emplace(code, fast_cal_t { .code = code, @@ -73,7 +54,6 @@ void FastCal::fast_cal(uint64_t code) { while (!cache.empty()) { /// break check point - if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) { std::cout << "Resolved" << std::endl; break; @@ -87,10 +67,14 @@ void FastCal::fast_cal(uint64_t code) { auto solution = cache.front(); + std::vector solution_path; + while (solution != nullptr) { -// printf("%016lX\n", solution->code); -// std::cout << RawCode(solution->code).dump_case() << std::endl; + solution_path.emplace_back(solution->code); solution = solution->last; } + std::reverse(solution_path.begin(), solution_path.end()); + return solution_path; + } diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index 5d8108f..2f5272c 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -4,6 +4,8 @@ #include #include +#include + class FastCal { public: struct fast_cal_t { @@ -15,8 +17,12 @@ public: std::queue cache; std::unordered_map cases; - void fast_cal(uint64_t code); + 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 d19b315..5cde688 100644 --- a/src/main.cc +++ b/src/main.cc @@ -26,7 +26,11 @@ int main() { // AllCases::build(); auto f = FastCal(); - f.fast_cal(RawCode::from_common_code("1a9bf0c").unwrap()); + auto ret = f.search(RawCode::from_common_code("1a9bf0c").unwrap()); + +// for (const auto &c : ret) { +// std::cout << RawCode(c) << std::endl; +// } // auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap(); // auto raw_code = CommonCode("A5D3AF0").to_raw_code().unwrap();