Browse Source

update: backtrack interface of Analyse

legacy
Dnomd343 2 years ago
parent
commit
1a25d8f30c
  1. 18
      src/analyse/analyse.h
  2. 88
      src/analyse/backtrack.cc

18
src/analyse/analyse.h

@ -43,6 +43,7 @@ private:
void new_case(uint64_t code, uint64_t mask);
/// backtrack definitions
public:
struct backtrack_t {
uint64_t code;
@ -50,20 +51,9 @@ public:
std::list<backtrack_t*> last;
std::list<backtrack_t*> next;
};
typedef std::vector<std::unordered_map<uint64_t, backtrack_t>> backtrack_data_t;
void backtrack_demo(const std::vector<uint64_t> &codes);
/// backtrack definitions
// struct backtrack_t {
// uint64_t code;
// uint32_t layer_num;
// uint32_t layer_index;
// std::list<backtrack_t*> next;
// };
// TODO: backtrack for multi-codes
// void backtrack(uint64_t code);
// void backtrack(const std::vector<uint64_t> &raw_code_list);
// TODO: try using unordered_set
backtrack_data_t backtrack_demo(const std::vector<uint64_t> &codes);
};

88
src/analyse/backtrack.cc

@ -1,104 +1,52 @@
#include <algorithm>
#include "analyse.h"
#include <iostream>
#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);
// }
// };
//
// 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) {
Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
/// codes pre-check and sort by steps
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;
if (c == cases.end()) { // invalid input
return backtrack_data_t{}; // return empty data
}
if (c->second.step >= todos.size()) {
todos.resize(c->second.step + 1);
todos.resize(c->second.step + 1); // enlarge schedule list
}
todos[c->second.step].emplace_back(&c->second);
}
std::reverse(todos.begin(), todos.end());
std::reverse(todos.begin(), todos.end()); // backtrack start from further layer
struct cache_t {
analyse_t *a;
backtrack_t *b;
};
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(todos.size());
// TODO: for diff layer cases, layer_num from big to small -> if found then skip search
Analyse::backtrack_data_t track_data(todos.size());
/// start backtrack process
for (const auto &todo : todos) {
if (todo.empty()) {
continue;
continue; // without scheduled cases
}
/// clear track cache
std::queue<cache_t>{}.swap(track_cache);
std::queue<cache_t>{}.swap(track_cache); // clear track cache
for (const auto c : todo) {
// TODO: if c already exist -> skip
/// case already exist -> skip its backtrack
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 *>{},
.last = std::list<backtrack_t *>{}, // without parent node
.next = std::list<backtrack_t *>{}, // without sub node
}).first->second,
});
}
}
/// backtrack until root case
while (!track_cache.empty()) {
/// handle first element and pop it
auto curr = track_cache.front();
auto curr = track_cache.front(); // handle first element
for (auto src : curr.a->src) { // traverse src cases of current node
auto t_src = track_data[src->step].find(src->code);
if (t_src != track_data[src->step].end()) { // match src case
@ -123,8 +71,6 @@ void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
}
track_cache.pop();
}
}
@ -152,4 +98,6 @@ void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
}
return track_data;
}

Loading…
Cancel
Save