Browse Source

feat: backtrack simple demo

master
Dnomd343 2 years ago
parent
commit
e3a7f82f74
  1. 88
      src/analyse/analyse.cc
  2. 2
      src/analyse/analyse.h

88
src/analyse/analyse.cc

@ -27,57 +27,101 @@ void Analyse::backtrack(uint64_t code) {
// std::cout << "src size: " << last_1->src.size() << std::endl;
std::vector<backtrack_t*> track_data;
std::unordered_map<uint64_t, backtrack_t> track_data;
// std::vector<backtrack_t*> track_data;
std::queue<analyse_t*> track_cache;
std::vector<std::vector<backtrack_t*> > layer;
// TODO: confirm that code exist
track_cache.emplace(&cases[code]);
track_data.emplace_back(new backtrack_t {
track_data.emplace(code, backtrack_t {
.code = code,
// .next = nullptr,
.layer_num = track_cache.front()->step,
.layer_index = 0, // TODO: pay attention to multi-code
.next = std::list<backtrack_t*>{}, // without next cases
});
auto me = 0;
layer.resize(track_cache.front()->step + 1);
layer[track_cache.front()->step].emplace_back(&track_data[code]);
backtrack_t *root;
for (;;) {
if (track_cache.front()->src.empty()) {
root = &track_data[track_cache.front()->code];
break;
}
for (auto t : track_cache.front()->src) {
track_cache.emplace(t);
track_data.emplace_back(new backtrack_t {
.code = t->code,
.layer_num = track_cache.front()->step - 1,
.next = std::list<backtrack_t*>{track_data[me]},
});
auto current = track_data.find(t->code);
if (current != track_data.end()) { // already exist
// link
current->second.next.emplace_back(
&track_data[track_cache.front()->code]
);
} else { // insert new case
track_cache.emplace(t);
track_data.emplace(t->code, backtrack_t {
.code = t->code,
.layer_num = t->step,
.layer_index = (uint32_t)layer[t->step].size(),
.next = std::list<backtrack_t*>{&track_data[track_cache.front()->code]},
});
layer[t->step].emplace_back(&track_data[t->code]);
}
}
track_cache.pop();
++me;
}
std::cout << "size: " << track_data.size() << std::endl;
auto root = track_data.back();
std::cout << "layer " << root->layer_num << std::endl;
std::cout << "Root" << std::endl;
std::cout << RawCode(root->code).dump_case() << std::endl;
//
// std::queue<backtrack_t*> t_cache;
// t_cache.emplace(root);
//
// while (!t_cache.empty()) {
//
// for (auto t : t_cache.front()->next) {
// std::cout << "layer " << t->layer_num << std::endl;
// std::cout << RawCode(t->code).dump_case() << std::endl;
// t_cache.emplace(t);
// }
//
// t_cache.pop();
//
// }
for (auto t : root->next) {
std::cout << "layer " << t->layer_num << std::endl;
std::cout << RawCode(t->code).dump_case() << std::endl;
}
for (auto l : layer) {
std::cout << "-----------------------" << std::endl;
std::cout << "layer " << l.front()->layer_num;
std::cout << " -> size = " << l.size() << std::endl;
for (auto t : root->next.front()->next) {
std::cout << "layer " << t->layer_num << std::endl;
std::cout << RawCode(t->code).dump_case() << std::endl;
}
for (auto c : l) {
std::cout << "(" << c->layer_num << ", " << c->layer_index << ") -> ";
for (auto t : c->next) {
std::cout << "(" << t->layer_num << ", " << t->layer_index << ") ";
}
std::cout << std::endl;
std::cout << RawCode(c->code).dump_case() << std::endl;
}
}
}

2
src/analyse/analyse.h

@ -28,7 +28,7 @@ public:
uint64_t code;
uint32_t layer_num;
// uint32_t layer_index;
uint32_t layer_index;
std::list<backtrack_t*> next;
};

Loading…
Cancel
Save