Browse Source

feat: fast cal demo

master
Dnomd343 2 years ago
parent
commit
d81b295ddb
  1. 4
      src/CMakeLists.txt
  2. 77
      src/fast_cal.cc
  3. 5
      src/fast_cal.h
  4. 25
      src/main.cc

4
src/CMakeLists.txt

@ -16,8 +16,8 @@ add_subdirectory(raw_code)
add_subdirectory(short_code)
add_subdirectory(common_code)
add_executable(klotski main.cc)
#add_executable(klotski main.cc core_demo.cc)
#add_executable(klotski main.cc)
add_executable(klotski main.cc fast_cal.cc)
target_link_libraries(klotski core)
target_link_libraries(klotski common)

77
src/fast_cal.cc

@ -0,0 +1,77 @@
#include <iostream>
#include <unordered_map>
#include <queue>
#include "core.h"
#include "fast_cal.h"
#include "raw_code.h"
struct fast_cal_t {
uint64_t code;
uint64_t mask;
// uint32_t step;
};
std::unordered_map<uint64_t, fast_cal_t> cal_data;
std::queue<fast_cal_t*> cal_temp;
void add_new_case(uint64_t code, uint64_t mask) {
auto exist_case = cal_data.find(code);
if (exist_case != cal_data.end()) { // find it
// mask update
// std::cout << "match exist case" << std::endl;
return;
}
// std::cout << RawCode(code).dump_case();
// std::cout << "~~~~~~~" << std::endl;
// for (int n = 0; n < 20; ++n, mask >>= 3) {
// std::cout << "+."[!(mask & 0b111)] << " \n"[!(~n & 0b11)];
// }
// std::cout << std::endl;
auto new_case = fast_cal_t {
.code = code,
.mask = mask,
};
cal_data[code] = new_case;
// TODO: avoid redundancy map search
cal_temp.emplace(&cal_data[code]);
}
void fast_cal(uint64_t start_raw_code) {
std::cout << RawCode(start_raw_code).dump_case() << std::endl;
auto core = Core();
auto setup = fast_cal_t {
.code = start_raw_code,
.mask = 0,
};
cal_data[start_raw_code] = setup;
cal_temp.emplace(&cal_data[start_raw_code]);
// core.next_step(cal_temp.front()->code, add_new_case);
// cal_temp.pop();
// std::cout << "start cal second case" << std::endl;
// std::cout << RawCode(cal_temp.front()->code).dump_case() << std::endl << std::endl;
// core.next_step(cal_temp.front()->code, add_new_case);
// cal_temp.pop();
while (!cal_temp.empty()) {
core.next_step(cal_temp.front()->code, add_new_case);
cal_temp.pop();
}
std::cout << "size = " << cal_data.size() << std::endl;
std::cout << "queue size = " << cal_temp.size() << std::endl;
}

5
src/fast_cal.h

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

25
src/main.cc

@ -5,6 +5,7 @@
#include "common_code.h"
#include "short_code.h"
#include "raw_code.h"
#include "fast_cal.h"
//#include "core_demo.h"
@ -37,14 +38,14 @@
// }
//}
void release(uint64_t code, uint64_t mask) {
std::cout << RawCode(code).dump_case();
std::cout << "~~~~~~~" << std::endl;
for (int n = 0; n < 20; ++n, mask >>= 3) {
std::cout << "+."[!(mask & 0b111)] << " \n"[!(~n & 0b11)];
}
std::cout << std::endl;
}
//void release(uint64_t code, uint64_t mask) {
// std::cout << RawCode(code).dump_case();
// std::cout << "~~~~~~~" << std::endl;
// for (int n = 0; n < 20; ++n, mask >>= 3) {
// std::cout << "+."[!(mask & 0b111)] << " \n"[!(~n & 0b11)];
// }
// std::cout << std::endl;
//}
int main() {
@ -153,11 +154,11 @@ int main() {
// std::cout << CommonCode(RawCode(0x0E58FC85FFEBC4DB)).to_string() << std::endl;
auto c = Core();
// auto c = Core();
// auto raw_code = RawCode(CommonCode("4fea134")).unwrap();
auto raw_code = RawCode(CommonCode("1A9bf0c")).unwrap();
c.next_step(raw_code, release);
// auto raw_code = RawCode(CommonCode("1A9bf0c")).unwrap();
// c.next_step(raw_code, release);
// std::vector<uint64_t> all_cases_raw;
// for (int head = 0; head < 16; ++head) {
@ -195,5 +196,7 @@ int main() {
// std::cout << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl;
// std::cout << "complete benchmark" << std::endl;
fast_cal(CommonCode("1a9bf0c").to_raw_code().unwrap());
return 0;
}

Loading…
Cancel
Save