Browse Source

perf: speed up backtrack

legacy
Dnomd343 2 years ago
parent
commit
f990c9857e
  1. 88
      src/analyse/backtrack.cc

88
src/analyse/backtrack.cc

@ -5,32 +5,54 @@
#include <set> #include <set>
#include <unordered_set> #include <unordered_set>
#include <algorithm>
#include "common_code.h" #include "common_code.h"
// TODO: using const RawCode& instead of uint64_t // TODO: using const RawCode& instead of uint64_t
namespace std { //namespace std {
template<> // template<>
struct hash<Analyse::backtrack_t> { // struct hash<Analyse::backtrack_t> {
std::size_t operator()(const Analyse::backtrack_t &b) const { // std::size_t operator()(const Analyse::backtrack_t &b) const {
std::cout << "get hash: " << b.code << std::endl; // std::cout << "get hash: " << b.code << std::endl;
return std::hash<uint64_t>()(b.code); // return std::hash<uint64_t>()(b.code);
// }
// };
//
// template<>
// struct equal_to<Analyse::backtrack_t> {
// bool operator()(const Analyse::backtrack_t &b1, const Analyse::backtrack_t &b2) const {
// std::cout << "get eq: " << b1.code << " ? " << b2.code << std::endl;
// return b1.code == b2.code;
// }
// };
//}
void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
std::vector<std::vector<analyse_t*>> todos;
for (const auto &code : codes) {
auto c = cases.find(code);
if (c == cases.end()) {
// TODO: invalid input
return;
} }
};
template<> if (c->second.step >= todos.size()) {
struct equal_to<Analyse::backtrack_t> { todos.resize(c->second.step + 1);
bool operator()(const Analyse::backtrack_t &b1, const Analyse::backtrack_t &b2) const {
std::cout << "get eq: " << b1.code << " ? " << b2.code << std::endl;
return b1.code == b2.code;
} }
};
todos[c->second.step].emplace_back(&c->second);
} }
void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) { std::reverse(todos.begin(), todos.end());
// TODO: confirm code exist
// std::cout << cases[code].step << std::endl;
struct cache_t { struct cache_t {
analyse_t *a; analyse_t *a;
@ -40,32 +62,40 @@ void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
std::queue<cache_t> track_cache; std::queue<cache_t> track_cache;
std::vector<std::unordered_map<uint64_t, backtrack_t>> track_data; std::vector<std::unordered_map<uint64_t, backtrack_t>> track_data;
track_data.resize(82); // TODO: setting as (max steps + 1) // track_data.resize(82); // TODO: setting as (max steps + 1)
track_data.resize(todos.size());
// TODO: for diff layer cases, layer_num from big to small -> if found then skip search // TODO: for diff layer cases, layer_num from big to small -> if found then skip search
for (const auto &code : codes) { for (const auto &todo : todos) {
auto &cc = cases[code];
/// enlarge track_data if (todo.empty()) {
if (cc.step >= track_data.size()) { continue;
track_data.resize(cc.step + 1);
} }
/// clear track cache
std::queue<cache_t>{}.swap(track_cache);
for (const auto c : todo) {
// TODO: if c already exist -> skip
if (track_data[c->step].find(c->code) == track_data[c->step].end()) {
track_cache.emplace(cache_t{ track_cache.emplace(cache_t{
.a = &cc, .a = c,
.b = &track_data[cc.step].emplace(code, backtrack_t { .b = &track_data[c->step].emplace(c->code, backtrack_t{
.code = code, .code = c->code,
.layer_num = cc.step, .layer_num = c->step,
.last = std::list<backtrack_t *>{}, .last = std::list<backtrack_t *>{},
.next = std::list<backtrack_t *>{}, .next = std::list<backtrack_t *>{},
}).first->second, }).first->second,
}); });
}
} }
while (!track_cache.empty()) { while (!track_cache.empty()) {
/// handle first element and pop it /// handle first element and pop it
auto curr = track_cache.front(); auto curr = track_cache.front();
@ -95,6 +125,10 @@ void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
} }
}
for (uint32_t i = 0; i < track_data.size(); ++i) { for (uint32_t i = 0; i < track_data.size(); ++i) {
const auto &ly = track_data[i]; const auto &ly = track_data[i];

Loading…
Cancel
Save