Browse Source

update: FastCal module

master
Dnomd343 2 years ago
parent
commit
b5861820b9
  1. 64
      src/fast_cal/fast_cal.cc
  2. 8
      src/fast_cal/fast_cal.h
  3. 6
      src/main.cc

64
src/fast_cal/fast_cal.cc

@ -5,8 +5,18 @@
#include "raw_code.h"
#include <iostream>
#include <algorithm>
Core FastCal::import_core() {
return Core(
[this](auto &&code, auto &&mask) { // lambda as function pointer
new_case(std::forward<decltype(code)>(code), std::forward<decltype(mask)>(mask));
}
);
}
void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for new case
auto current = cases.find(code);
if (current != cases.end()) { // find existed case
current->second.mask |= mask; // update mask info
@ -19,50 +29,21 @@ void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for
.last = cache.front(),
}).first->second);
// auto insert_ret = cases.emplace(code, fast_cal_t {
// .code = code,
// .mask = mask,
// .last = cache.front(),
// });
//
// if (insert_ret.second) { // insert success
// cache.emplace(&insert_ret.first->second);
// } else {
// insert_ret.first->second.mask |= mask;
// }
// cases[code] = fast_cal_t { // insert into cases map
// .code = code,
// .mask = mask,
// .last = cache.front(), // parent case
// };
// cache.emplace(&cases[code]); // add in working queue
}
void FastCal::fast_cal(uint64_t code) {
std::vector<uint64_t> FastCal::search(uint64_t code) {
auto core = Core(
[this](auto &&code, auto &&mask) { // lambda as function pointer
new_case(std::forward<decltype(code)>(code), std::forward<decltype(mask)>(mask));
}
);
auto core = import_core();
// clear data
cases.clear();
cases.empty();
cache.empty();
auto empty = std::queue<fast_cal_t*>{};
std::swap(empty, cache);
// cases[code] = fast_cal_t {
// .code = code,
// .mask = 0,
// .last = nullptr,
// };
// cache.emplace(&cases[code]);
// cases.reserve(30000);
cases.reserve(65536);
// cases.reserve(65536 * 2);
// cases.reserve(65536 * 4);
cache.emplace(&cases.emplace(code, fast_cal_t {
.code = code,
@ -73,7 +54,6 @@ void FastCal::fast_cal(uint64_t code) {
while (!cache.empty()) {
/// break check point
if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) {
std::cout << "Resolved" << std::endl;
break;
@ -87,10 +67,14 @@ void FastCal::fast_cal(uint64_t code) {
auto solution = cache.front();
std::vector<uint64_t> solution_path;
while (solution != nullptr) {
// printf("%016lX\n", solution->code);
// std::cout << RawCode(solution->code).dump_case() << std::endl;
solution_path.emplace_back(solution->code);
solution = solution->last;
}
std::reverse(solution_path.begin(), solution_path.end());
return solution_path;
}

8
src/fast_cal/fast_cal.h

@ -4,6 +4,8 @@
#include <cstdint>
#include <unordered_map>
#include <vector>
class FastCal {
public:
struct fast_cal_t {
@ -15,8 +17,12 @@ public:
std::queue<fast_cal_t*> cache;
std::unordered_map<uint64_t, fast_cal_t> cases;
void fast_cal(uint64_t code);
Core import_core();
std::vector<uint64_t> search(uint64_t code);
void new_case(uint64_t code, uint64_t mask);
};

6
src/main.cc

@ -26,7 +26,11 @@ int main() {
// AllCases::build();
auto f = FastCal();
f.fast_cal(RawCode::from_common_code("1a9bf0c").unwrap());
auto ret = f.search(RawCode::from_common_code("1a9bf0c").unwrap());
// for (const auto &c : ret) {
// std::cout << RawCode(c) << std::endl;
// }
// auto raw_code = CommonCode("1a9bf0c").to_raw_code().unwrap();
// auto raw_code = CommonCode("A5D3AF0").to_raw_code().unwrap();

Loading…
Cancel
Save