Browse Source

feat: using RawCode instead of uint64_t in Analyse

legacy
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" #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) { Core Analyse::init(uint64_t code) {
/// reset working data /// reset working data
cases.clear(); cases.clear();
@ -41,22 +49,25 @@ void Analyse::new_case(uint64_t code, uint64_t mask) {
} }
} }
void Analyse::build(uint64_t code) { /// analyse and build klotski tree
auto core = init(code); void Analyse::build() {
auto core = init(root);
while (!cache.empty()) { while (!cache.empty()) {
core.next_cases(cache.front()->code, cache.front()->mask); core.next_cases(cache.front()->code, cache.front()->mask);
cache.pop(); cache.pop();
} }
} }
std::vector<uint64_t> Analyse::build_until(uint64_t code, const match_t &match) { std::vector<RawCode> Analyse::build_until(const match_t &match) {
auto core = init(code); auto core = init(root);
auto layer_end = cache.back(); auto layer_end = cache.back();
std::vector<uint64_t> matched; // matched list std::vector<RawCode> matched; // matched list
/// start BFS search /// start BFS search
while (!cache.empty()) { while (!cache.empty()) {
if (match(cache.front()->code)) { // match target 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); core.next_cases(cache.front()->code, cache.front()->mask);
if (cache.front() == layer_end) { // reach layer ending 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(); 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 <functional>
#include <unordered_map> #include <unordered_map>
#include "core.h" #include "core.h"
#include "raw_code.h"
const uint32_t ANY_MAP_RESERVE = 65536; const uint32_t ANY_MAP_RESERVE = 65536;
@ -13,8 +14,13 @@ class Analyse {
public: public:
typedef std::function<bool(uint64_t)> match_t; typedef std::function<bool(uint64_t)> match_t;
void build(uint64_t code); /// setting root code
std::vector<uint64_t> build_until(uint64_t code, const match_t &match); 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: private:
struct analyse_t { 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 /// start BFS search
while (!cache.empty()) { while (!cache.empty()) {
if (match(cache.front()->code)) { // match target 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); core.next_cases(cache.front()->code, cache.front()->mask);
if (cache.front() == layer_end) { // reach layer ending if (cache.front() == layer_end) { // reach layer ending

2
src/fast_cal/fast_cal.h

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

10
src/main.cc

@ -53,8 +53,14 @@ int main() {
// } // }
auto a = Analyse(); auto a = Analyse(RawCode::from_common_code("1a9bf0c"));
a.build((uint64_t)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) { // auto ret = a.build_until((uint64_t)RawCode::from_common_code("1a9bf0c"), [](uint64_t code) {
// return ((code >> (3 * 0xD)) & 0b111) == B_2x2; // return ((code >> (3 * 0xD)) & 0b111) == B_2x2;

Loading…
Cancel
Save