Browse Source

feat: add Graph module

master
Dnomd343 2 years ago
parent
commit
f2a4ebc9d7
  1. 6
      src/CMakeLists.txt
  2. 10
      src/analyse/analyse.h
  3. 49
      src/analyse/backtrack.cc
  4. 4
      src/graph/CMakeLists.txt
  5. 30
      src/graph/graph.cc
  6. 11
      src/graph/graph.h
  7. 12
      src/main.cc

6
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)
################################################

10
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<backtrack_t*> last;
std::list<backtrack_t*> next;
std::list<track_t*> last;
std::list<track_t*> next;
};
typedef std::vector<std::unordered_map<uint64_t, backtrack_t>> backtrack_data_t;
typedef std::vector<std::unordered_map<uint64_t, track_t>> track_data_t;
// TODO: try using unordered_set
backtrack_data_t backtrack_demo(const std::vector<uint64_t> &codes);
track_data_t backtrack(const std::vector<uint64_t> &codes);
};

49
src/analyse/backtrack.cc

@ -1,15 +1,13 @@
#include <algorithm>
#include "analyse.h"
#include <iostream>
Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector<uint64_t> &codes) {
Analyse::track_data_t Analyse::backtrack(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()) { // 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<uint64_t> &c
struct cache_t {
analyse_t *a;
backtrack_t *b;
track_t *b;
};
std::queue<cache_t> 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<uint64_t> &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<backtrack_t *>{}, // without parent node
.next = std::list<backtrack_t *>{}, // without sub node
.last = std::list<track_t*>{}, // without parent node
.next = std::list<track_t*>{}, // without sub node
}).first->second,
});
}
@ -55,11 +53,11 @@ Analyse::backtrack_data_t Analyse::backtrack_demo(const std::vector<uint64_t> &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<backtrack_t*>{},
.next = std::list<backtrack_t*>{curr.b}, // link to curr.b
.last = std::list<track_t*>{},
.next = std::list<track_t*>{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<uint64_t> &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;
}

4
src/graph/CMakeLists.txt

@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 3.0)
add_library(graph graph.cc)
target_link_libraries(graph analyse)

30
src/graph/graph.cc

@ -0,0 +1,30 @@
#include "graph.h"
#include <iostream>
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;
}
}
}

11
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);
};

12
src/main.cc

@ -8,6 +8,7 @@
#include "fast_cal.h"
#include "analyse.h"
#include "common.h"
#include "graph.h"
#include <thread>
#include <algorithm>
@ -65,14 +66,11 @@ int main() {
// std::cout << r << std::endl;
// }
std::vector<uint64_t> 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();

Loading…
Cancel
Save