Browse Source

feat: backtrack for multi-cases

master
Dnomd343 2 years ago
parent
commit
30e6fe5d25
  1. 2
      src/analyse/analyse.h
  2. 96
      src/analyse/backtrack.cc
  3. 8
      src/main.cc

2
src/analyse/analyse.h

@ -51,7 +51,7 @@ public:
std::list<backtrack_t*> next;
};
void backtrack_demo(uint64_t code);
void backtrack_demo(const std::vector<uint64_t> &codes);
/// backtrack definitions

96
src/analyse/backtrack.cc

@ -27,81 +27,74 @@ namespace std {
};
}
void Analyse::backtrack_demo(uint64_t code) {
void Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
// TODO: confirm code exist
std::cout << cases[code].step << std::endl;
// std::cout << cases[code].step << std::endl;
// TODO: only test for now
// uint32_t layer_size = cases[code].step + 1; // 81 steps -> 0 ~ 81 -> size = 82
std::vector<std::unordered_map<uint64_t, backtrack_t>> track_data;
/// init process -> insert backtrack begin cases
track_data.resize(82); // TODO: setting as (max steps + 1)
struct inner_t {
struct cache_t {
analyse_t *a;
backtrack_t *b;
};
std::queue<inner_t> track_cache;
auto rt = track_data[81].emplace(code, backtrack_t {
.code = code,
.layer_num = cases[code].step,
.last = std::list<backtrack_t*>{},
.next = std::list<backtrack_t*>{},
});
/// start backtrack
track_cache.emplace(inner_t {
.a = &cases[code],
.b = &rt.first->second,
});
while (!track_cache.front().a->src.empty()) {
std::queue<cache_t> track_cache;
std::vector<std::unordered_map<uint64_t, backtrack_t>> track_data;
auto curr = track_cache.front();
track_data.resize(82); // TODO: setting as (max steps + 1)
for (auto src : curr.a->src) {
auto find_ret = track_data[src->step].find(src->code);
// TODO: for diff layer cases, layer_num from big to small -> if found then skip search
if (find_ret != track_data[src->step].end()) { // found
for (const auto &code : codes) {
find_ret->second.next.emplace_back(curr.b);
auto &cc = cases[code];
curr.b->last.emplace_back(&find_ret->second);
/// enlarge track_data
if (cc.step >= track_data.size()) {
track_data.resize(cc.step + 1);
}
} else { // not found
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*>{},
}).first->second,
});
// std::cout << "new: " << src->code << std::endl;
}
auto ret = track_data[src->step].emplace(src->code, backtrack_t {
while (!track_cache.empty()) {
/// handle first element and pop it
auto curr = track_cache.front();
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
/// link (curr.b) and (t_src->second)
t_src->second.next.emplace_back(curr.b);
curr.b->last.emplace_back(&t_src->second);
} else { // src case not found
/// link (curr.b) and (t_src_new->second)
auto t_src_new = track_data[src->step].emplace(src->code, backtrack_t {
.code = src->code,
.layer_num = src->step,
.last = std::list<backtrack_t*>{},
.next = std::list<backtrack_t*>{curr.b},
});
curr.b->last.emplace_back(&ret.first->second);
track_cache.emplace(inner_t {
.next = std::list<backtrack_t*>{curr.b}, // link to curr.b
}).first;
curr.b->last.emplace_back(&t_src_new->second);
/// insert into working queue
track_cache.emplace(cache_t {
.a = src,
.b = &ret.first->second,
.b = &t_src_new->second,
});
}
}
track_cache.pop();
}
for (uint32_t i = 0; i < track_data.size(); ++i) {
const auto &ly = track_data[i];
@ -112,21 +105,16 @@ void Analyse::backtrack_demo(uint64_t code) {
std::cout << "----------------------------------" << std::endl;
for (const auto &c : ly) {
for (const auto &l : c.second.last) {
std::cout << l->code << " ";
}
std::cout << " <- [" << c.second.code << "] -> ";
for (const auto &n : c.second.next) {
std::cout << n->code << " ";
}
std::cout << std::endl;
}
// std::cout << l.size() << std::endl;
}

8
src/main.cc

@ -65,9 +65,15 @@ int main() {
// std::cout << r << std::endl;
// }
a.backtrack_demo(0x7F87E0E5BFFF492);
std::vector<uint64_t> dat = {
0x7F87E0E5BFFF492, 0x1FB1E36F9FFF492
};
// a.backtrack_demo(0x7F87E0E5BFFF492);
// a.backtrack_demo(0x1FB1E36F9FFF492);
a.backtrack_demo(dat);
// auto start_time = clock();
// std::cout << a.layer_export(81).size() << std::endl;

Loading…
Cancel
Save