diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a1b014f..4c32ed7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,8 @@ include_directories(core) include_directories(analyse) include_directories(fast_cal) +include_directories(graph) + ################################################ add_subdirectory(utils) @@ -26,6 +28,8 @@ add_subdirectory(core) add_subdirectory(analyse) add_subdirectory(fast_cal) +add_subdirectory(graph) + ################################################ add_executable(klotski main.cc) @@ -43,6 +47,8 @@ target_link_libraries(klotski core) target_link_libraries(klotski analyse) target_link_libraries(klotski fast_cal) +target_link_libraries(klotski graph) + target_link_libraries(klotski pthread) ################################################ diff --git a/src/analyse/analyse.h b/src/analyse/analyse.h index f8a330d..3888962 100644 --- a/src/analyse/analyse.h +++ b/src/analyse/analyse.h @@ -45,15 +45,15 @@ private: /// backtrack definitions public: - struct backtrack_t { + struct track_t { uint64_t code; uint32_t layer_num; - std::list last; - std::list next; + std::list last; + std::list next; }; - typedef std::vector> backtrack_data_t; + typedef std::vector> track_data_t; // TODO: try using unordered_set - backtrack_data_t backtrack_demo(const std::vector &codes); + track_data_t backtrack(const std::vector &codes); }; diff --git a/src/analyse/backtrack.cc b/src/analyse/backtrack.cc index 1e88fd7..a755696 100644 --- a/src/analyse/backtrack.cc +++ b/src/analyse/backtrack.cc @@ -1,15 +1,13 @@ #include #include "analyse.h" -#include - -Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector &codes) { +Analyse::track_data_t Analyse::backtrack(const std::vector &codes) { /// codes pre-check and sort by steps std::vector> todos; for (const auto &code : codes) { auto c = cases.find(code); if (c == cases.end()) { // invalid input - return backtrack_data_t{}; // return empty data + return track_data_t{}; // return empty data } if (c->second.step >= todos.size()) { todos.resize(c->second.step + 1); // enlarge schedule list @@ -20,10 +18,10 @@ Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector &c struct cache_t { analyse_t *a; - backtrack_t *b; + track_t *b; }; std::queue track_cache; - Analyse::backtrack_data_t track_data(todos.size()); + track_data_t track_data(todos.size()); /// start backtrack process for (const auto &todo : todos) { if (todo.empty()) { @@ -35,11 +33,11 @@ Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector &c 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{ + .b = &track_data[c->step].emplace(c->code, track_t{ .code = c->code, .layer_num = c->step, - .last = std::list{}, // without parent node - .next = std::list{}, // without sub node + .last = std::list{}, // without parent node + .next = std::list{}, // without sub node }).first->second, }); } @@ -55,11 +53,11 @@ Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector &c 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 { + auto t_src_new = track_data[src->step].emplace(src->code, track_t { .code = src->code, .layer_num = src->step, - .last = std::list{}, - .next = std::list{curr.b}, // link to curr.b + .last = std::list{}, + .next = std::list{curr.b}, // link to curr.b }).first; curr.b->last.emplace_back(&t_src_new->second); /// insert into working queue @@ -72,32 +70,5 @@ Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector &c track_cache.pop(); } } - - - - 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; - } - - } - - return track_data; - } diff --git a/src/graph/CMakeLists.txt b/src/graph/CMakeLists.txt new file mode 100644 index 0000000..df15942 --- /dev/null +++ b/src/graph/CMakeLists.txt @@ -0,0 +1,4 @@ +cmake_minimum_required(VERSION 3.0) + +add_library(graph graph.cc) +target_link_libraries(graph analyse) diff --git a/src/graph/graph.cc b/src/graph/graph.cc new file mode 100644 index 0000000..e29c0cd --- /dev/null +++ b/src/graph/graph.cc @@ -0,0 +1,30 @@ +#include "graph.h" + +#include + +void Graph::svg_demo(Analyse::track_data_t track_data) { + + + 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; + } + + } + +} diff --git a/src/graph/graph.h b/src/graph/graph.h new file mode 100644 index 0000000..7aeb3fa --- /dev/null +++ b/src/graph/graph.h @@ -0,0 +1,11 @@ +#pragma once + +#include "analyse.h" + +class Graph { + +public: + + void svg_demo(Analyse::track_data_t track_data); + +}; diff --git a/src/main.cc b/src/main.cc index ebb6d43..2048c39 100644 --- a/src/main.cc +++ b/src/main.cc @@ -8,6 +8,7 @@ #include "fast_cal.h" #include "analyse.h" #include "common.h" +#include "graph.h" #include #include @@ -65,14 +66,11 @@ int main() { // std::cout << r << std::endl; // } - std::vector dat = { + auto ret = a.backtrack({ 0x7F87E0E5BFFF492, 0x1FB1E36F9FFF492 - }; - -// a.backtrack_demo(0x7F87E0E5BFFF492); -// a.backtrack_demo(0x1FB1E36F9FFF492); - - a.backtrack_demo(dat); + }); + auto g = Graph(); + g.svg_demo(ret); // auto start_time = clock();