Browse Source

feat: using RawCode instead of uint64_t in Analyse

master
Dnomd343 2 years ago
parent
commit
1588336db0
  1. 27
      src/analyse/analyse.cc
  2. 10
      src/analyse/analyse.h
  3. 4
      src/fast_cal/cal_core.cc
  4. 2
      src/fast_cal/fast_cal.h
  5. 10
      src/main.cc

27
src/analyse/analyse.cc

@ -1,6 +1,14 @@
#include <queue>
#include "analyse.h"
Analyse::Analyse(const RawCode &code) {
this->root = (uint64_t)code;
}
void Analyse::set_root(const RawCode &code) {
this->root = (uint64_t)code;
}
/// memory initialize and return klotski core
Core Analyse::init(uint64_t code) {
/// reset working data
cases.clear();
@ -41,22 +49,25 @@ void Analyse::new_case(uint64_t code, uint64_t mask) {
}
}
void Analyse::build(uint64_t code) {
auto core = init(code);
/// analyse and build klotski tree
void Analyse::build() {
auto core = init(root);
while (!cache.empty()) {
core.next_cases(cache.front()->code, cache.front()->mask);
cache.pop();
}
}
std::vector<uint64_t> Analyse::build_until(uint64_t code, const match_t &match) {
auto core = init(code);
std::vector<RawCode> Analyse::build_until(const match_t &match) {
auto core = init(root);
auto layer_end = cache.back();
std::vector<uint64_t> matched; // matched list
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);
matched.emplace_back(
RawCode::unsafe_create(cache.front()->code) // record matched cases
);
}
core.next_cases(cache.front()->code, cache.front()->mask);
if (cache.front() == layer_end) { // reach layer ending
@ -67,5 +78,5 @@ std::vector<uint64_t> Analyse::build_until(uint64_t code, const match_t &match)
}
cache.pop();
}
return std::vector<uint64_t>{}; // no target found
return std::vector<RawCode>{}; // no target found
}

10
src/analyse/analyse.h

@ -6,6 +6,7 @@
#include <functional>
#include <unordered_map>
#include "core.h"
#include "raw_code.h"
const uint32_t ANY_MAP_RESERVE = 65536;
@ -13,8 +14,13 @@ class Analyse {
public:
typedef std::function<bool(uint64_t)> match_t;
void build(uint64_t code);
std::vector<uint64_t> build_until(uint64_t code, const match_t &match);
/// setting root code
void set_root(const RawCode &code);
explicit Analyse(const RawCode &code);
/// BFS search functions
void build();
std::vector<RawCode> build_until(const match_t &match);
private:
struct analyse_t {

4
src/fast_cal/cal_core.cc

@ -88,7 +88,9 @@ std::vector<RawCode> FastCal::target_multi(const match_t &match) {
/// start BFS search
while (!cache.empty()) {
if (match(cache.front()->code)) { // match target
matched.emplace_back(cache.front()->code);
matched.emplace_back(
RawCode::unsafe_create(cache.front()->code) // record matched cases
);
}
core.next_cases(cache.front()->code, cache.front()->mask);
if (cache.front() == layer_end) { // reach layer ending

2
src/fast_cal/fast_cal.h

@ -17,7 +17,7 @@ class FastCal {
public:
typedef std::function<bool(uint64_t)> match_t;
/// FastCal start case
/// setting root code
void set_root(const RawCode &code);
explicit FastCal(const RawCode &code);

10
src/main.cc

@ -53,8 +53,14 @@ int main() {
// }
auto a = Analyse();
a.build((uint64_t)RawCode::from_common_code("1a9bf0c"));
auto a = Analyse(RawCode::from_common_code("1a9bf0c"));
// a.build();
auto ret = a.build_until([](uint64_t code) {
return ((code >> (3 * 0xD)) & 0b111) == B_2x2;
});
for (const auto &r : ret) {
std::cout << r << std::endl;
}
// auto ret = a.build_until((uint64_t)RawCode::from_common_code("1a9bf0c"), [](uint64_t code) {
// return ((code >> (3 * 0xD)) & 0b111) == B_2x2;

Loading…
Cancel
Save