From 79b3345c78eb12f01a9ab7488cceebb7321c7f78 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Fri, 13 Jan 2023 14:08:35 +0800 Subject: [PATCH] feat: demo of analyse backtrack --- src/analyse/analyse.cc | 76 +++++++++++++++++++++++++--------------- src/analyse/analyse.h | 10 ++++++ src/fast_cal/fast_cal.cc | 1 + src/main.cc | 1 + 4 files changed, 59 insertions(+), 29 deletions(-) diff --git a/src/analyse/analyse.cc b/src/analyse/analyse.cc index 66e7f84..2a51ad9 100644 --- a/src/analyse/analyse.cc +++ b/src/analyse/analyse.cc @@ -2,6 +2,7 @@ #include "analyse.h" #include +#include "raw_code.h" Core Analyse::new_core() { return Core( @@ -11,6 +12,20 @@ Core Analyse::new_core() { ); } +void Analyse::backtrack(uint64_t code) { + + // backtrack start at code + std::cout << "start backtrack" << std::endl; + + std::cout << RawCode(code).dump_case() << std::endl; + std::cout << "src size: " << cases[code].src.size() << std::endl; + + auto last_1 = cases[code].src.front(); + std::cout << RawCode(last_1->code).dump_case() << std::endl; + std::cout << "src size: " << last_1->src.size() << std::endl; + +} + void Analyse::start_analyse(uint64_t code) { auto core = new_core(); @@ -24,6 +39,7 @@ void Analyse::start_analyse(uint64_t code) { .code = code, .mask = 0, .step = 0, +// .highlight = false, // .src = std::move(std::list{}), }).first->second); @@ -40,39 +56,41 @@ void Analyse::start_analyse(uint64_t code) { void Analyse::new_case(uint64_t code, uint64_t mask) { - auto temp = cases.emplace(code, analyse_t { - .code = code, - .mask = mask, - .step = cache.front()->step + 1, - .src = std::move(std::list{cache.front()}) - }); +// auto temp = cases.emplace(code, analyse_t { +// .code = code, +// .mask = mask, +// .step = cache.front()->step + 1, +// .src = std::move(std::list{cache.front()}) +// }); +// +// if (temp.second) { +// cache.emplace(&temp.first->second); +// } else { +// temp.first->second.mask |= mask; +// +// if (temp.first->second.step != cache.front()->step) { +// temp.first->second.src.push_back(cache.front()); +// } +// +// } - if (temp.second) { - cache.emplace(&temp.first->second); - } else { - temp.first->second.mask |= mask; + auto current = cases.find(code); + if (current != cases.end()) { // find existed case + current->second.mask |= mask; // update mask info - if (temp.first->second.step != cache.front()->step) { - temp.first->second.src.push_back(cache.front()); + if (current->second.step != cache.front()->step) { + current->second.src.push_back(cache.front()); } + return; } -// auto current = cases.find(code); -// if (current != cases.end()) { // find existed case -// current->second.mask |= mask; // update mask info -// -// if (current->second.step != cache.front()->step) { -// current->second.src.push_back(cache.front()); -// } -// -// return; -// } -// -// cache.emplace(&cases.emplace(code, analyse_t { -// .code = code, -// .mask = mask, -// .step = cache.front()->step + 1, -// .src = std::move(std::list{cache.front()}), -// }).first->second); + cache.emplace(&cases.emplace(code, analyse_t { + .code = code, + .mask = mask, + .step = cache.front()->step + 1, +// .highlight = false, + .src = std::move(std::list{cache.front()}), + }).first->second); + } diff --git a/src/analyse/analyse.h b/src/analyse/analyse.h index 7d438b2..92adc3d 100644 --- a/src/analyse/analyse.h +++ b/src/analyse/analyse.h @@ -16,17 +16,27 @@ public: int step; +// bool highlight; + // std::vector src; std::list src; // std::set src; // std::unordered_set src; }; + struct backtrack_t { + uint64_t code; + std::list next; + }; + std::queue cache; std::unordered_map cases; inline Core new_core(); + // TODO: backtrack for multi-codes + void backtrack(uint64_t code); + void start_analyse(uint64_t code); void new_case(uint64_t code, uint64_t mask); diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index 269f78a..0d6250c 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -88,6 +88,7 @@ void FastCal::fast_cal(uint64_t code) { auto solution = cache.front(); while (solution != nullptr) { +// printf("%016lX\n", solution->code); // std::cout << RawCode(solution->code).dump_case() << std::endl; solution = solution->last; } diff --git a/src/main.cc b/src/main.cc index 52d0567..86f183a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -202,6 +202,7 @@ int main() { auto a = Analyse(); a.start_analyse(raw_code); + a.backtrack(0x07F87E0E5BFFF492); // int sum = 0; // for (auto const &raw_code : all_cases_raw) {