Browse Source

feat: FastCal module

master
Dnomd343 2 years ago
parent
commit
b98f2db7b5
  1. 19
      src/CMakeLists.txt
  2. 3
      src/core/core.cc
  3. 9
      src/core/core.h
  4. 5
      src/fast_cal.h
  5. 4
      src/fast_cal/CMakeLists.txt
  6. 65
      src/fast_cal/fast_cal.cc
  7. 22
      src/fast_cal/fast_cal.h
  8. 4
      src/main.cc

19
src/CMakeLists.txt

@ -1,5 +1,7 @@
cmake_minimum_required(VERSION 3.0)
################################
include_directories(core)
include_directories(utils)
include_directories(all_cases)
@ -8,6 +10,10 @@ include_directories(raw_code)
include_directories(short_code)
include_directories(common_code)
include_directories(fast_cal)
################################
add_subdirectory(core)
add_subdirectory(utils)
add_subdirectory(all_cases)
@ -16,8 +22,13 @@ add_subdirectory(raw_code)
add_subdirectory(short_code)
add_subdirectory(common_code)
#add_executable(klotski main.cc)
add_executable(klotski main.cc fast_cal.cc)
add_subdirectory(fast_cal)
################################
add_executable(klotski main.cc)
################################
target_link_libraries(klotski core)
target_link_libraries(klotski utils)
@ -27,4 +38,8 @@ target_link_libraries(klotski raw_code)
target_link_libraries(klotski short_code)
target_link_libraries(klotski common_code)
target_link_libraries(klotski fast_cal)
target_link_libraries(klotski pthread)
################################

3
src/core/core.cc

@ -173,7 +173,8 @@ void Core::next_step(uint64_t code, uint64_t mask) { // search next step cases
// TODO: try to send multi-items data
for (int i = 1; i < cache_size; ++i) {
release(cache[i].code, cache[i].mask); // release next cases
// release(cache[i].code, cache[i].mask); // release next cases
release_next(cache[i].code, cache[i].mask); // release next cases
}
cache_size = 1; // reset cache size

9
src/core/core.h

@ -1,6 +1,7 @@
#pragma once
#include <cstdint>
#include <functional>
#define UP (-4 * 3)
#define LEFT (-1 * 3)
@ -10,10 +11,12 @@
class Core {
public:
typedef void (*release_t)(uint64_t, uint64_t);
// typedef void (*release_t)(uint64_t, uint64_t);
std::function<void(uint64_t, uint64_t)> release_next;
void next_step(uint64_t code, uint64_t mask);
explicit Core(release_t release_func) : release(release_func) {}
// explicit Core(release_t release_func) : release(release_func) {}
private:
struct cache_t {
@ -25,7 +28,7 @@ private:
int cache_size = 1;
cache_t cache[16] = {0};
release_t release; // release code and mask
// release_t release; // release code and mask
void move_1x1(uint64_t code, int addr);
void move_1x2(uint64_t code, int addr);

5
src/fast_cal.h

@ -1,5 +0,0 @@
#pragma once
#include <cstdint>
uint32_t fast_cal(uint64_t start_code);

4
src/fast_cal/CMakeLists.txt

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.0)
add_library(fast_cal fast_cal.cc)
target_link_libraries(fast_cal core)

65
src/fast_cal/fast_cal.cc

@ -0,0 +1,65 @@
#include "core.h"
#include "common.h"
#include "fast_cal.h"
#include "raw_code.h"
#include <iostream>
void FastCal::fast_cal(uint64_t code) {
std::cout << RawCode(code).dump_case() << std::endl;
auto core = Core();
core.release_next = std::bind(&FastCal::add_new_case, this, std::placeholders::_1, std::placeholders::_2);
cases.empty();
cache.empty();
cases[code] = fast_cal_t {
.code = code,
.mask = 0,
.last = nullptr,
};
cache.emplace(&cases[code]);
while (!cache.empty()) {
// break check point
if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) {
std::cout << "Resolved" << std::endl;
// std::cout << RawCode(cache.front()->code).dump_case() << std::endl;
break;
}
core.next_step(cache.front()->code, cache.front()->mask);
cache.pop();
}
auto solution = cache.front();
while (solution != nullptr) {
// std::cout << RawCode(solution->code).dump_case() << std::endl;
solution = solution->last;
}
}
void FastCal::add_new_case(uint64_t code, uint64_t mask) {
auto exist_case = cases.find(code);
if (exist_case != cases.end()) { // find existed case
exist_case->second.mask |= mask; // mask update
return;
}
cases[code] = fast_cal_t {
.code = code,
.mask = mask,
.last = cache.front(),
};;
cache.emplace(&cases[code]);
}

22
src/fast_cal/fast_cal.h

@ -0,0 +1,22 @@
#pragma once
#include <queue>
#include <cstdint>
#include <unordered_map>
class FastCal {
public:
struct fast_cal_t {
uint64_t code;
uint64_t mask;
fast_cal_t *last;
};
std::queue<fast_cal_t*> cache;
std::unordered_map<uint64_t, fast_cal_t> cases;
void fast_cal(uint64_t code);
void add_new_case(uint64_t code, uint64_t mask);
};

4
src/main.cc

@ -194,7 +194,9 @@ int main() {
// }
auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap();
std::cout << fast_cal(raw_code) << std::endl;
// std::cout << fast_cal(raw_code) << std::endl;
auto f = FastCal();
f.fast_cal(raw_code);
// int sum = 0;
// for (auto const &raw_code : all_cases_raw) {

Loading…
Cancel
Save