From 91225ce0cee884adaa589df77dbcfb250dc58fd6 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 19 Jan 2023 15:40:01 +0800 Subject: [PATCH] feat: build total search tree for FastCal --- src/fast_cal/cal_core.cc | 16 +++++++++++++--- src/fast_cal/fast_cal.h | 2 +- src/main.cc | 7 ++++--- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/fast_cal/cal_core.cc b/src/fast_cal/cal_core.cc index 8753f6b..d2e1712 100644 --- a/src/fast_cal/cal_core.cc +++ b/src/fast_cal/cal_core.cc @@ -36,10 +36,20 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { }).first->second); } +/// build total search tree +void FastCal::build(const RawCode &code) { + auto core = init((uint64_t)code); + /// start BFS search + while (!cache.empty()) { + core.next_cases(cache.front()->code, cache.front()->mask); + cache.pop(); + } +} + /// found first matched target RawCode FastCal::target(const RawCode &code, const match_t &match) { auto core = init((uint64_t)code); - /// start bfs search + /// start BFS search while (!cache.empty()) { if (match(cache.front()->code)) { return RawCode::unsafe_create(cache.front()->code); // match target @@ -55,7 +65,7 @@ std::vector FastCal::target_multi(const RawCode &code, const match_t &m auto core = init((uint64_t)code); auto layer_end = cache.back(); std::vector matched; // matched list - /// start bfs search + /// start BFS search while (!cache.empty()) { if (match(cache.front()->code)) { // match target matched.emplace_back(cache.front()->code); @@ -77,7 +87,7 @@ std::vector FastCal::furthest(const RawCode &code) { auto core = init((uint64_t)code); auto layer_end = cache.back(); std::vector layer_cases; - /// start bfs search + /// start BFS search while (!cache.empty()) { core.next_cases(cache.front()->code, cache.front()->mask); layer_cases.emplace_back( diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index 6fbb844..a927aff 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -20,7 +20,7 @@ public: std::vector backtrack(const RawCode &code); /// BFS search functions - // TODO: build function with void return -> build total tree + void build(const RawCode &code); RawCode solve(const RawCode &code); std::vector furthest(const RawCode &code); std::vector solve_multi(const RawCode &code); diff --git a/src/main.cc b/src/main.cc index 354915a..97fa75c 100644 --- a/src/main.cc +++ b/src/main.cc @@ -25,7 +25,8 @@ int main() { // AllCases::build(); -// auto f = FastCal(); + auto f = FastCal(); + f.build(RawCode::from_common_code("1a9bf0c")); // auto ret = f.solve(RawCode::from_common_code("1a9bf0c")); // auto ret = f.target(RawCode::from_common_code("1a9bf0c"), [](uint64_t code) { @@ -44,9 +45,9 @@ int main() { // std::cout << "step number: " << f.step_num(ret) << std::endl; // } - for (const auto &c : FastCal::resolve(RawCode::from_common_code("1a9bf0c"))) { +// for (const auto &c : FastCal::resolve(RawCode::from_common_code("1a9bf0c"))) { // std::cout << c << std::endl; - } +// } // for (const auto &s : FastCal::to_furthest(RawCode::from_common_code("1a9bf0c"))) { // for (const auto &c : s) {