Browse Source

perf: speed up backtrack

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

96
src/analyse/backtrack.cc

@ -5,32 +5,54 @@
#include <set>
#include <unordered_set>
#include <algorithm>
#include "common_code.h"
// TODO: using const RawCode& instead of uint64_t
namespace std {
template<>
struct hash<Analyse::backtrack_t> {
std::size_t operator()(const Analyse::backtrack_t &b) const {
std::cout << "get hash: " << b.code << std::endl;
return std::hash<uint64_t>()(b.code);
//namespace std {
// template<>
// struct hash<Analyse::backtrack_t> {
// std::size_t operator()(const Analyse::backtrack_t &b) const {
// std::cout << "get hash: " << b.code << std::endl;
// 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<>
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;
if (c->second.step >= todos.size()) {
todos.resize(c->second.step + 1);
}
};
}
void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
todos[c->second.step].emplace_back(&c->second);
}
std::reverse(todos.begin(), todos.end());
// TODO: confirm code exist
// std::cout << cases[code].step << std::endl;
struct cache_t {
analyse_t *a;
@ -40,32 +62,40 @@ void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
std::queue<cache_t> track_cache;
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
for (const auto &code : codes) {
auto &cc = cases[code];
for (const auto &todo : todos) {
/// enlarge track_data
if (cc.step >= track_data.size()) {
track_data.resize(cc.step + 1);
if (todo.empty()) {
continue;
}
track_cache.emplace(cache_t {
.a = &cc,
.b = &track_data[cc.step].emplace(code, backtrack_t {
.code = code,
.layer_num = cc.step,
.last = std::list<backtrack_t*>{},
.next = std::list<backtrack_t*>{},
/// 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{
.a = c,
.b = &track_data[c->step].emplace(c->code, backtrack_t{
.code = c->code,
.layer_num = c->step,
.last = std::list<backtrack_t *>{},
.next = std::list<backtrack_t *>{},
}).first->second,
});
}
}
while (!track_cache.empty()) {
/// handle first element and pop it
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) {
const auto &ly = track_data[i];

Loading…
Cancel
Save