forked from dilsonpereira/chinese-postman-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppWASM.cpp
85 lines (69 loc) · 1.57 KB
/
AppWASM.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "./Minimum-Cost-Perfect-Matching/Graph.h"
#include "ChinesePostman.h"
#include <string>
#include <sstream>
#include <vector>
#include <emscripten/bind.h>
using namespace std;
pair< Graph, vector<double> > ReadWeightedGraph(string graphData)
{
//Please see Graph.h for a description of the interface
stringstream file(graphData);
string s;
getline(file, s);
stringstream ss(s);
int n;
ss >> n;
getline(file, s);
ss.str(s);
ss.clear();
int m;
ss >> m;
Graph G(n);
vector<double> cost(m);
for(int i = 0; i < m; i++)
{
getline(file, s);
ss.str(s);
ss.clear();
int u, v;
double c;
ss >> u >> v >> c;
G.AddEdge(u, v);
cost[G.GetEdgeIndex(u, v)] = c;
}
return make_pair(G, cost);
}
string calculate(string graphData)
{
string result = "{\n";
try
{
Graph G;
vector<double> cost;
//Read the graph
pair< Graph, vector<double> > p = ReadWeightedGraph(graphData);
G = p.first;
cost = p.second;
//Solve the problem
pair< list<int> , double > sol = ChinesePostman(G, cost);
result += "\t\"totalCost\": " + to_string(sol.second) + ",\n";
list<int> s = sol.first;
//Print edges in the solution
result += "\t\"result\": [";
for(list<int>::iterator it = s.begin(); it != s.end(); it++) {
result += to_string(*it);
if (next(it) != s.end()) { result += ", "; }
}
result += "]\n";
} catch(const char * msg)
{
result += "\"error\": '" + string(msg) + "'\n";
}
result += "}";
return result;
}
// Binding code
EMSCRIPTEN_BINDINGS(my_module) {
emscripten::function("calculate", &calculate);
}