Browse Source

feat: using lambda for confirm target

master
Dnomd343 2 years ago
parent
commit
c2184035f8
  1. 28
      src/fast_cal/fast_cal.cc
  2. 30
      src/fast_cal/fast_cal.h
  3. 7
      src/main.cc

28
src/fast_cal/fast_cal.cc

@ -31,22 +31,31 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for
}
std::vector<uint64_t> FastCal::search(uint64_t code) {
std::vector<uint64_t> FastCal::solve() {
auto resolved = [](uint64_t code) {
return ((code >> (3 * 0xD)) & 0b111) == B_2x2;
};
return target(resolved);
}
std::vector<uint64_t> FastCal::target(const std::function<bool(uint64_t)> &match) {
auto core = import_core();
// clear data
cases.clear();
auto empty = std::queue<fast_cal_t*>{};
std::swap(empty, cache);
std::queue<fast_cal_t*>{}.swap(cache);
// auto empty = std::queue<fast_cal_t*>{};
// std::swap(empty, cache);
cases.reserve(65536);
cache.emplace(&cases.emplace(code, fast_cal_t {
.code = code,
cache.emplace(&cases.emplace(root, fast_cal_t {
.code = root,
.mask = 0,
.last = nullptr,
}).first->second);
@ -54,8 +63,7 @@ std::vector<uint64_t> FastCal::search(uint64_t code) {
while (!cache.empty()) {
/// break check point
if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) {
std::cout << "Resolved" << std::endl;
if (match(cache.front()->code)) {
break;
}
@ -65,6 +73,10 @@ std::vector<uint64_t> FastCal::search(uint64_t code) {
std::cout << "size: " << cases.size() << std::endl;
// TODO: single backtrack function
// TODO: cache may empty -> never found
auto solution = cache.front();
std::vector<uint64_t> solution_path;

30
src/fast_cal/fast_cal.h

@ -8,21 +8,43 @@
class FastCal {
public:
explicit FastCal(uint64_t code) : root(code) {}
// search resolve
// search all min-step resolve
// search the furthest cases
// search target by code
// search target by lambda
/// xxx_multi only search until same layer
// std::vector<uint64_t> solve(uint64_t code);
std::vector<uint64_t> solve();
// solve_multi
std::vector<uint64_t> target(const std::function<bool(uint64_t)> &match);
// TODO: continue search process? -> reuse exist data (ensure working code not changed)
// TODO: static search functions
private:
struct fast_cal_t {
uint64_t code;
uint64_t mask;
fast_cal_t *last;
};
uint64_t root;
std::queue<fast_cal_t*> cache;
std::unordered_map<uint64_t, fast_cal_t> cases;
Core import_core();
std::vector<uint64_t> search(uint64_t code);
void new_case(uint64_t code, uint64_t mask);
};

7
src/main.cc

@ -25,8 +25,11 @@ int main() {
// AllCases::build();
auto f = FastCal();
auto ret = f.search(RawCode::from_common_code("1a9bf0c").unwrap());
// auto f = FastCal();
// auto ret = f.solve(RawCode::from_common_code("1a9bf0c").unwrap());
auto f = FastCal(RawCode::from_common_code("1a9bf0c").unwrap());
auto ret = f.solve();
// for (const auto &c : ret) {
// std::cout << RawCode(c) << std::endl;

Loading…
Cancel
Save