From 85077dc7f7d771d1dcd09a9a9e5522c67ec8a7f5 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 19 Jan 2023 15:32:34 +0800 Subject: [PATCH] update: mark RawCode param as const ref --- src/fast_cal/cal_core.cc | 6 +++--- src/fast_cal/fast_cal.cc | 31 ++++++++++++++++--------------- src/fast_cal/fast_cal.h | 24 +++++++++++------------- src/main.cc | 17 ++++++++--------- 4 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/fast_cal/cal_core.cc b/src/fast_cal/cal_core.cc index 2388c4d..8753f6b 100644 --- a/src/fast_cal/cal_core.cc +++ b/src/fast_cal/cal_core.cc @@ -37,7 +37,7 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { } /// found first matched target -RawCode FastCal::target(RawCode code, const match_t &match) { +RawCode FastCal::target(const RawCode &code, const match_t &match) { auto core = init((uint64_t)code); /// start bfs search while (!cache.empty()) { @@ -51,7 +51,7 @@ RawCode FastCal::target(RawCode code, const match_t &match) { } /// found multi-targets matched in first same layer -std::vector FastCal::target_multi(RawCode code, const match_t &match) { +std::vector FastCal::target_multi(const RawCode &code, const match_t &match) { auto core = init((uint64_t)code); auto layer_end = cache.back(); std::vector matched; // matched list @@ -73,7 +73,7 @@ std::vector FastCal::target_multi(RawCode code, const match_t &match) { } /// found all of the furthest cases -std::vector FastCal::furthest(RawCode code) { +std::vector FastCal::furthest(const RawCode &code) { auto core = init((uint64_t)code); auto layer_end = cache.back(); std::vector layer_cases; diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index 9f4cf42..9598786 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -4,21 +4,29 @@ #include "fast_cal.h" #include "raw_code.h" +/// klotski resolved -> 2x2 block at address 13 (aka 0xD) auto resolved = [](uint64_t code) { return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address }; -/// klotski resolved -> 2x2 block at address 13 (aka 0xD) -RawCode FastCal::solve(RawCode code) { +RawCode FastCal::solve(const RawCode &code) { return FastCal::target(code, resolved); } -std::vector FastCal::solve_multi(RawCode code) { +std::vector FastCal::solve_multi(const RawCode &code) { return FastCal::target_multi(code, resolved); } +std::vector FastCal::resolve(const RawCode &start) { + return FastCal::search(start, resolved); +} + +std::vector> FastCal::resolve_multi(const RawCode &start) { + return FastCal::search_multi(start, resolved); +} + /// backtrack of FastCal tree -int FastCal::step_num(RawCode code) { +int FastCal::step_num(const RawCode &code) { auto tmp = cases.find((uint64_t)code); if (tmp == cases.end()) { return -1; // code not exist @@ -46,15 +54,8 @@ std::vector FastCal::backtrack(const RawCode &code) { return path; } -std::vector FastCal::resolve(RawCode start) { - return FastCal::search(start, resolved); -} - -std::vector> FastCal::resolve_multi(RawCode start) { - return FastCal::search_multi(start, resolved); -} - -std::vector> FastCal::to_furthest(RawCode start) { +/// static BFS search functions +std::vector> FastCal::to_furthest(const RawCode &start) { auto fc = FastCal(); std::vector> result; for (const auto &furthest : fc.furthest(start)) { @@ -63,7 +64,7 @@ std::vector> FastCal::to_furthest(RawCode start) { return result; } -std::vector FastCal::search(RawCode start, const match_t &match) { +std::vector FastCal::search(const RawCode &start, const match_t &match) { auto fc = FastCal(); auto result = fc.target(start, match); if (result == FC_NOT_FOUND) { @@ -72,7 +73,7 @@ std::vector FastCal::search(RawCode start, const match_t &match) { return fc.backtrack(result); // backtrack target path } -std::vector> FastCal::search_multi(RawCode start, const match_t &match) { +std::vector> FastCal::search_multi(const RawCode &start, const match_t &match) { auto fc = FastCal(); std::vector> result; for (const auto &target : fc.target_multi(start, match)) { diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index ad51b49..6fbb844 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -16,25 +16,23 @@ public: typedef std::function match_t; /// backtrack functions - int step_num(RawCode code); + int step_num(const RawCode &code); std::vector backtrack(const RawCode &code); /// BFS search functions // TODO: build function with void return -> build total tree - RawCode solve(RawCode code); - std::vector furthest(RawCode code); - std::vector solve_multi(RawCode code); - RawCode target(RawCode code, const match_t &match); - std::vector target_multi(RawCode code, const match_t &match); + RawCode solve(const RawCode &code); + std::vector furthest(const RawCode &code); + std::vector solve_multi(const RawCode &code); + RawCode target(const RawCode &code, const match_t &match); + std::vector target_multi(const RawCode &code, const match_t &match); /// static BFS search functions - static std::vector resolve(RawCode start); - static std::vector> to_furthest(RawCode start); - static std::vector> resolve_multi(RawCode start); - static std::vector search(RawCode start, const match_t &match); - static std::vector> search_multi(RawCode start, const match_t &match); - - // TODO: (RawCode code) -> (const RawCode &code) + static std::vector resolve(const RawCode &start); + static std::vector> to_furthest(const RawCode &start); + static std::vector> resolve_multi(const RawCode &start); + static std::vector search(const RawCode &start, const match_t &match); + static std::vector> search_multi(const RawCode &start, const match_t &match); private: struct fast_cal_t { diff --git a/src/main.cc b/src/main.cc index 6fd91d9..354915a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -26,7 +26,6 @@ int main() { // AllCases::build(); // auto f = FastCal(); -// auto ret = f.solve((uint64_t)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) { @@ -45,17 +44,17 @@ 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) { - std::cout << c << std::endl; - } - std::cout << "--------------------------------------------" << std::endl; } +// for (const auto &s : FastCal::to_furthest(RawCode::from_common_code("1a9bf0c"))) { +// for (const auto &c : s) { +// std::cout << c << std::endl; +// } +// std::cout << "--------------------------------------------" << std::endl; +// } + // std::cout << f.step_num(ret) << std::endl; // auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap());