-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphRepository.cpp
More file actions
71 lines (65 loc) · 1.91 KB
/
Copy pathGraphRepository.cpp
File metadata and controls
71 lines (65 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "GraphRepository.h"
GraphRepository::GraphRepository(string filepath)
{
readGraphsFromJson(filepath);
}
GraphRepository::~GraphRepository()
{
for (auto graph : graphs)
{
delete graph;
}
graphs.clear();
for (auto directedGraph : directedGraphs)
{
delete directedGraph;
}
directedGraphs.clear();
}
Graph* GraphRepository::getGraph(int id)
{
auto predicate = [=](Graph* graph)
{
return graph->getId() == id;
};
auto result = std::find_if(graphs.begin(), graphs.end(), predicate);
return result != graphs.end() ? (*result)->clone() : nullptr;
}
DirectedGraph* GraphRepository::getDirectedGraph(int id)
{
auto predicate = [=](DirectedGraph* directedGraph)
{
return directedGraph->getId() == id;
};
auto result = std::find_if(directedGraphs.begin(), directedGraphs.end(), predicate);
return result != directedGraphs.end() ? (*result)->clone() : nullptr;
}
void GraphRepository::readGraphsFromJson(string path)
{
ifstream t(path);
stringstream buffer;
buffer << t.rdbuf();
string graphName = "\"graph\": ";
string directedGraphName = "\"directedGraph\": ";
size_t pos = 0;
size_t nextStopPos = 0;
Graph* graph;
DirectedGraph* directedGraph;
// read graphs from json
// graphs should be formatted as [{graph, directedGraph}, {graph, directedGraph}, ...]
while (true)
{
pos = buffer.str().find(graphName, pos);
if (pos == string::npos) break;
// graphs always end with "},"
nextStopPos = buffer.str().find("},", pos);
graph = new Graph(utility::conversions::to_string_t(buffer.str().substr(pos, nextStopPos - pos + 1)));
graphs.push_back(graph);
pos = buffer.str().find(directedGraphName, nextStopPos);
if (pos == string::npos) break;
// directed graphs always end with "}}"
nextStopPos = buffer.str().find("}}", pos);
directedGraph = new DirectedGraph(utility::conversions::to_string_t(buffer.str().substr(pos, nextStopPos - pos + 1)));
directedGraphs.push_back(directedGraph);
}
}