diff --git a/src/analyse/analyse.h b/src/analyse/analyse.h index 0100d9f..cf6b145 100644 --- a/src/analyse/analyse.h +++ b/src/analyse/analyse.h @@ -11,18 +11,6 @@ // TODO: try double or 4-times size const uint32_t ANY_MAP_RESERVE = 65536; -#include - -class Test { -public: - Test() { - std::cout << "Test init" << std::endl; - } - ~Test() { - std::cout << "Test destroy" << std::endl; - } -}; - class Analyse { public: typedef std::function match_t; @@ -57,18 +45,10 @@ private: public: struct backtrack_t { -// backtrack_t(int i); - uint64_t code; uint32_t layer_num; std::list last; std::list next; - - Test test; - -// bool operator==(const backtrack_t &b) const { -// return b.code == code; -// } }; void backtrack_demo(uint64_t code); diff --git a/src/analyse/backtrack.cc b/src/analyse/backtrack.cc index 9e2621e..71e7e94 100644 --- a/src/analyse/backtrack.cc +++ b/src/analyse/backtrack.cc @@ -5,6 +5,8 @@ #include #include +#include "common_code.h" + // TODO: using const RawCode& instead of uint64_t namespace std { @@ -22,11 +24,6 @@ namespace std { std::cout << "get eq: " << b1.code << " ? " << b2.code << std::endl; return b1.code == b2.code; } - -// bool operator()(const Analyse::backtrack_t &b, uint64_t c) const { -// std::cout << "eq: " << b.code << " ? " << c << std::endl; -// return b.code == c; -// } }; } @@ -35,33 +32,102 @@ void Analyse::backtrack_demo(uint64_t code) { // TODO: confirm code exist std::cout << cases[code].step << std::endl; -// std::vector> dat; + // TODO: only test for now +// uint32_t layer_size = cases[code].step + 1; // 81 steps -> 0 ~ 81 -> size = 82 - std::unordered_set test; + std::vector> track_data; + /// init process -> insert backtrack begin cases - for (uint32_t i = 0; i < 20; ++i) { - test.emplace(backtrack_t { - .code = i * 1000, - .layer_num = 1, - .last = std::list{}, - .next = std::list{}, + track_data.resize(82); // TODO: setting as (max steps + 1) - }); - } + struct inner_t { + analyse_t *a; + backtrack_t *b; + }; + + std::queue track_cache; + + auto rt = track_data[81].emplace(code, backtrack_t { + .code = code, + .layer_num = cases[code].step, + .last = std::list{}, + .next = std::list{}, + }); + + /// start backtrack + + track_cache.emplace(inner_t { + .a = &cases[code], + .b = &rt.first->second, + }); + + while (!track_cache.front().a->src.empty()) { + + auto curr = track_cache.front(); + + for (auto src : curr.a->src) { + + auto find_ret = track_data[src->step].find(src->code); + + if (find_ret != track_data[src->step].end()) { // found + + find_ret->second.next.emplace_back(curr.b); + + curr.b->last.emplace_back(&find_ret->second); + + } else { // not found + +// std::cout << "new: " << src->code << std::endl; - std::cout << "------------------------" << std::endl; - std::cout << "hashmap size: " << test.size() << std::endl; - std::cout << "bucket count: " << test.bucket_count() << std::endl; - std::cout << "load factor: " << test.load_factor() << std::endl; + auto ret = track_data[src->step].emplace(src->code, backtrack_t { + .code = src->code, + .layer_num = src->step, + .last = std::list{}, + .next = std::list{curr.b}, + }); + + curr.b->last.emplace_back(&ret.first->second); + + track_cache.emplace(inner_t { + .a = src, + .b = &ret.first->second, + }); + + } + + } + + track_cache.pop(); - for (uint32_t i = 0; i < test.bucket_count(); ++i) { - std::cout << "bucket " << i << " -> " << test.bucket_size(i) << std::endl; } - uint64_t dat = 4000; - auto ret = test.find((backtrack_t&)(dat)); + for (uint32_t i = 0; i < track_data.size(); ++i) { + + const auto &ly = track_data[i]; + + std::cout << std::endl; + std::cout << "----------------------------------"; + std::cout << " layer " << i << " "; + 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; + } - std::cout << "insert demo ending" << std::endl; }