Browse Source

update: FastCal module

legacy
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 "raw_code.h"
#include <iostream> #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 void FastCal::new_case(uint64_t code, uint64_t mask) { // callback function for new case
auto current = cases.find(code); auto current = cases.find(code);
if (current != cases.end()) { // find existed case if (current != cases.end()) { // find existed case
current->second.mask |= mask; // update mask info 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(), .last = cache.front(),
}).first->second); }).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( auto core = import_core();
[this](auto &&code, auto &&mask) { // lambda as function pointer
new_case(std::forward<decltype(code)>(code), std::forward<decltype(mask)>(mask)); // clear data
} cases.clear();
);
cases.empty(); auto empty = std::queue<fast_cal_t*>{};
cache.empty(); 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);
// cases.reserve(65536 * 2);
// cases.reserve(65536 * 4);
cache.emplace(&cases.emplace(code, fast_cal_t { cache.emplace(&cases.emplace(code, fast_cal_t {
.code = code, .code = code,
@ -73,7 +54,6 @@ void FastCal::fast_cal(uint64_t code) {
while (!cache.empty()) { while (!cache.empty()) {
/// break check point /// break check point
if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) { if (((cache.front()->code >> (3 * 0xD)) & 0b111) == B_2x2) {
std::cout << "Resolved" << std::endl; std::cout << "Resolved" << std::endl;
break; break;
@ -87,10 +67,14 @@ void FastCal::fast_cal(uint64_t code) {
auto solution = cache.front(); auto solution = cache.front();
std::vector<uint64_t> solution_path;
while (solution != nullptr) { while (solution != nullptr) {
// printf("%016lX\n", solution->code); solution_path.emplace_back(solution->code);
// std::cout << RawCode(solution->code).dump_case() << std::endl;
solution = solution->last; 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 <cstdint>
#include <unordered_map> #include <unordered_map>
#include <vector>
class FastCal { class FastCal {
public: public:
struct fast_cal_t { struct fast_cal_t {
@ -15,8 +17,12 @@ public:
std::queue<fast_cal_t*> cache; std::queue<fast_cal_t*> cache;
std::unordered_map<uint64_t, fast_cal_t> cases; 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); void new_case(uint64_t code, uint64_t mask);
}; };

6
src/main.cc

@ -26,7 +26,11 @@ int main() {
// AllCases::build(); // AllCases::build();
auto f = FastCal(); 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("1a9bf0c").to_raw_code().unwrap();
// auto raw_code = CommonCode("A5D3AF0").to_raw_code().unwrap(); // auto raw_code = CommonCode("A5D3AF0").to_raw_code().unwrap();

Loading…
Cancel
Save