Browse Source

feat: backtrack function for FastCal

legacy
Dnomd343 2 years ago
parent
commit
ee58b52c26
  1. 36
      src/fast_cal/fast_cal.cc
  2. 9
      src/fast_cal/fast_cal.h
  3. 1
      src/main.cc

36
src/fast_cal/fast_cal.cc

@ -31,7 +31,7 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for
} }
std::vector<uint64_t> FastCal::solve() { uint64_t FastCal::solve() {
auto resolved = [](uint64_t code) { auto resolved = [](uint64_t code) {
return ((code >> (3 * 0xD)) & 0b111) == B_2x2; return ((code >> (3 * 0xD)) & 0b111) == B_2x2;
@ -40,7 +40,24 @@ std::vector<uint64_t> FastCal::solve() {
} }
std::vector<uint64_t> FastCal::target(const std::function<bool(uint64_t)> &match) { // TODO: single backtrack function
std::vector<uint64_t> FastCal::backtrack(uint64_t code) {
std::vector<uint64_t> path;
// TODO: confirm code exist
auto node = &cases[code];
while (node != nullptr) {
path.emplace_back(node->code);
node = node->last;
}
std::reverse(path.begin(), path.end());
return path;
}
uint64_t FastCal::target(const std::function<bool(uint64_t)> &match) {
auto core = import_core(); auto core = import_core();
@ -62,7 +79,6 @@ std::vector<uint64_t> FastCal::target(const std::function<bool(uint64_t)> &match
while (!cache.empty()) { while (!cache.empty()) {
/// break check point
if (match(cache.front()->code)) { if (match(cache.front()->code)) {
break; break;
} }
@ -73,20 +89,12 @@ std::vector<uint64_t> FastCal::target(const std::function<bool(uint64_t)> &match
std::cout << "size: " << cases.size() << std::endl; std::cout << "size: " << cases.size() << std::endl;
// TODO: single backtrack function
// TODO: cache may empty -> never found // TODO: cache may empty -> never found
if (!cache.empty()) {
auto solution = cache.front(); return cache.front()->code;
std::vector<uint64_t> solution_path;
while (solution != nullptr) {
solution_path.emplace_back(solution->code);
solution = solution->last;
} }
return FastCal::NOT_FOUND;
std::reverse(solution_path.begin(), solution_path.end());
return solution_path;
} }

9
src/fast_cal/fast_cal.h

@ -20,16 +20,21 @@ public:
/// xxx_multi only search until same layer /// xxx_multi only search until same layer
// std::vector<uint64_t> solve(uint64_t code); // std::vector<uint64_t> solve(uint64_t code);
std::vector<uint64_t> solve(); // std::vector<uint64_t> solve();
uint64_t solve();
// solve_multi // solve_multi
std::vector<uint64_t> target(const std::function<bool(uint64_t)> &match); // std::vector<uint64_t> target(const std::function<bool(uint64_t)> &match);
uint64_t target(const std::function<bool(uint64_t)> &match);
std::vector<uint64_t> backtrack(uint64_t code);
// TODO: continue search process? -> reuse exist data (ensure working code not changed) // TODO: continue search process? -> reuse exist data (ensure working code not changed)
// TODO: static search functions // TODO: static search functions
const static auto NOT_FOUND = (uint64_t)0;
private: private:
struct fast_cal_t { struct fast_cal_t {
uint64_t code; uint64_t code;

1
src/main.cc

@ -30,6 +30,7 @@ int main() {
auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap()); auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap());
auto ret = f.solve(); auto ret = f.solve();
std::cout << RawCode(ret) << std::endl;
// for (const auto &c : ret) { // for (const auto &c : ret) {
// std::cout << RawCode(c) << std::endl; // std::cout << RawCode(c) << std::endl;

Loading…
Cancel
Save