Browse Source

feat: using RawCode instead of uint64_t in FastCal

master
Dnomd343 2 years ago
parent
commit
19b0ca79ea
  1. 35
      src/fast_cal/cal_core.cc
  2. 4
      src/fast_cal/fast_cal.cc
  3. 17
      src/fast_cal/fast_cal.h
  4. 11
      src/main.cc
  5. 8
      src/raw_code/raw_code.cc
  6. 4
      src/raw_code/raw_code.h

35
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<uint64_t> FastCal::target_multi(uint64_t code, const check_t &match) {
auto core = init(code);
std::vector<RawCode> FastCal::target_multi(RawCode code, const check_t &match) {
auto core = init((uint64_t)code);
auto layer_end = cache.back();
std::vector<uint64_t> matched; // matched list
while (!cache.empty()) { // bfs search
std::vector<RawCode> 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<uint64_t> FastCal::target_multi(uint64_t code, const check_t &match)
}
cache.pop();
}
return std::vector<uint64_t>{}; // no target found
return std::vector<RawCode>{}; // no target found
}
/// found all of the furthest cases
std::vector<uint64_t> FastCal::furthest(uint64_t code) {
auto core = init(code);
std::vector<RawCode> FastCal::furthest(RawCode code) {
auto core = init((uint64_t)code);
auto layer_end = cache.back();
std::vector<uint64_t> layer_cases;
while (!cache.empty()) { // bfs search
std::vector<RawCode> 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

4
src/fast_cal/fast_cal.cc

@ -7,13 +7,13 @@
#include <iostream>
#include <algorithm>
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<uint64_t> FastCal::solve_multi(uint64_t code) {
std::vector<RawCode> FastCal::solve_multi(RawCode code) {
return FastCal::target_multi(code, [](uint64_t code) {
return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // check 2x2 block address
});

17
src/fast_cal/fast_cal.h

@ -4,22 +4,29 @@
#include <vector>
#include <cstdint>
#include <unordered_map>
#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<bool(uint64_t)> 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<RawCode> furthest(RawCode code);
std::vector<uint64_t> furthest(uint64_t code);
std::vector<uint64_t> solve_multi(uint64_t code);
std::vector<uint64_t> target_multi(uint64_t code, const check_t &match);
std::vector<RawCode> solve_multi(RawCode code);
std::vector<RawCode> target_multi(RawCode code, const check_t &match);
// TODO: search / search_multi / resolve / resolve_multi
// TODO: static furthest function

11
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();

8
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[] = {

4
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);

Loading…
Cancel
Save