From 19b0ca79eac9a29ab5e4f57c77d33551a90a44ab Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Thu, 19 Jan 2023 13:42:21 +0800 Subject: [PATCH] feat: using RawCode instead of uint64_t in FastCal --- src/fast_cal/cal_core.cc | 35 ++++++++++++++++++++--------------- src/fast_cal/fast_cal.cc | 4 ++-- src/fast_cal/fast_cal.h | 17 ++++++++++++----- src/main.cc | 11 +++++++++-- src/raw_code/raw_code.cc | 8 ++++++++ src/raw_code/raw_code.h | 4 ++++ 6 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/fast_cal/cal_core.cc b/src/fast_cal/cal_core.cc index 94944e9..039c10c 100644 --- a/src/fast_cal/cal_core.cc +++ b/src/fast_cal/cal_core.cc @@ -39,24 +39,26 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { } /// found first matched target -uint64_t FastCal::target(uint64_t code, const check_t &match) { - auto core = init(code); - while (!cache.empty()) { // bfs search +RawCode FastCal::target(RawCode code, const check_t &match) { + auto core = init((uint64_t)code); + /// start bfs search + while (!cache.empty()) { if (match(cache.front()->code)) { - return cache.front()->code; // match target + return RawCode::unsafe_create(cache.front()->code); // match target } core.next_cases(cache.front()->code, cache.front()->mask); cache.pop(); } - return FastCal::NOT_FOUND; // target not found + return FC_NOT_FOUND; // target not found } /// found multi-targets matched in first same layer -std::vector FastCal::target_multi(uint64_t code, const check_t &match) { - auto core = init(code); +std::vector FastCal::target_multi(RawCode code, const check_t &match) { + auto core = init((uint64_t)code); auto layer_end = cache.back(); - std::vector matched; // matched list - while (!cache.empty()) { // bfs search + std::vector matched; // matched list + /// start bfs search + while (!cache.empty()) { if (match(cache.front()->code)) { // match target matched.emplace_back(cache.front()->code); } @@ -69,17 +71,20 @@ std::vector FastCal::target_multi(uint64_t code, const check_t &match) } cache.pop(); } - return std::vector{}; // no target found + return std::vector{}; // no target found } /// found all of the furthest cases -std::vector FastCal::furthest(uint64_t code) { - auto core = init(code); +std::vector FastCal::furthest(RawCode code) { + auto core = init((uint64_t)code); auto layer_end = cache.back(); - std::vector layer_cases; - while (!cache.empty()) { // bfs search + std::vector layer_cases; + /// start bfs search + while (!cache.empty()) { core.next_cases(cache.front()->code, cache.front()->mask); - layer_cases.emplace_back(cache.front()->code); // record layer cases + layer_cases.emplace_back( + RawCode::unsafe_create(cache.front()->code) // record layer cases + ); if (cache.front() == layer_end) { // reach layer ending if (cache.size() == 1) { break; // stop loop at last layer diff --git a/src/fast_cal/fast_cal.cc b/src/fast_cal/fast_cal.cc index f18b53a..7a0d180 100644 --- a/src/fast_cal/fast_cal.cc +++ b/src/fast_cal/fast_cal.cc @@ -7,13 +7,13 @@ #include #include -uint64_t FastCal::solve(uint64_t code) { +RawCode FastCal::solve(RawCode code) { return FastCal::target(code, [](uint64_t code) { return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address }); } -std::vector FastCal::solve_multi(uint64_t code) { +std::vector FastCal::solve_multi(RawCode code) { return FastCal::target_multi(code, [](uint64_t code) { return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address }); diff --git a/src/fast_cal/fast_cal.h b/src/fast_cal/fast_cal.h index cf1cd5d..46b3f2f 100644 --- a/src/fast_cal/fast_cal.h +++ b/src/fast_cal/fast_cal.h @@ -4,22 +4,29 @@ #include #include #include +#include "raw_code.h" + +/// FastCal not found -> return invalid raw code +const RawCode FC_NOT_FOUND = RawCode::unsafe_create(0); class FastCal { public: const static auto NOT_FOUND = (uint64_t)0; typedef std::function check_t; +// const static RawCode *test = (RawCode*)nullptr; + /// xxx_multi only search until same layer // TODO: shall we use RawCode instead of uint64_t? - uint64_t solve(uint64_t code); - uint64_t target(uint64_t code, const check_t &match); + RawCode solve(RawCode code); + RawCode target(RawCode code, const check_t &match); + + std::vector furthest(RawCode code); - std::vector furthest(uint64_t code); - std::vector solve_multi(uint64_t code); - std::vector target_multi(uint64_t code, const check_t &match); + std::vector solve_multi(RawCode code); + std::vector target_multi(RawCode code, const check_t &match); // TODO: search / search_multi / resolve / resolve_multi // TODO: static furthest function diff --git a/src/main.cc b/src/main.cc index 948c28d..2759ee3 100644 --- a/src/main.cc +++ b/src/main.cc @@ -27,9 +27,16 @@ int main() { auto f = FastCal(); // auto ret = f.solve((uint64_t)RawCode::from_common_code("1a9bf0c")); - auto ret = f.solve((uint64_t)RawCode::from_common_code("1a9bf0c")); +// auto ret = f.solve(RawCode::from_common_code("1a9bf0c")); + auto ret = f.solve(RawCode::from_common_code("1aaef0c")); - std::cout << f.step_num(ret) << std::endl; + if (ret == FC_NOT_FOUND) { + std::cout << "not found" << std::endl; + } else { + std::cout << ret << std::endl; + } + +// std::cout << f.step_num(ret) << std::endl; // auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap()); // auto ret = f.solve(); diff --git a/src/raw_code/raw_code.cc b/src/raw_code/raw_code.cc index 611ca91..719a293 100644 --- a/src/raw_code/raw_code.cc +++ b/src/raw_code/raw_code.cc @@ -6,6 +6,10 @@ uint64_t RawCode::unwrap() const { return code; // raw uint64_t code } +bool RawCode::valid() const { + return RawCode::check(code); +} + RawCode RawCode::create(uint64_t raw_code) { return RawCode(raw_code); } @@ -23,6 +27,10 @@ RawCode::RawCode(uint64_t raw_code) { code = raw_code; } +bool RawCode::operator==(const RawCode &raw_code) const { + return this->code == raw_code.code; +} + std::ostream& operator<<(std::ostream &out, const RawCode &self) { char code[16]; char dump_map[] = { diff --git a/src/raw_code/raw_code.h b/src/raw_code/raw_code.h index 65a7907..54dc37c 100644 --- a/src/raw_code/raw_code.h +++ b/src/raw_code/raw_code.h @@ -9,7 +9,11 @@ class CommonCode; class RawCode { public: + bool valid() const; static bool check(uint64_t raw_code); + + /// Operators of RawCode + bool operator==(const RawCode &raw_code) const; explicit operator uint64_t() const { return code; } friend std::ostream& operator<<(std::ostream &out, const RawCode &self);