-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_wasm.cpp
More file actions
59 lines (48 loc) · 1.67 KB
/
Copy pathgraph_wasm.cpp
File metadata and controls
59 lines (48 loc) · 1.67 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
#include <emscripten/bind.h>
#include <sstream>
#include "Graph.hpp"
using namespace emscripten;
// Wrapper class to expose Graph<std::string> with JS-friendly methods
class GraphWrapper {
private:
Graph<std::string> graph;
public:
GraphWrapper() : graph() {}
GraphWrapper(int maxVerts) : graph(maxVerts) {}
void addVertex(const std::string& vertex) {
graph.addVertex(vertex);
}
void addEdge(const std::string& source, const std::string& target) {
graph.addEdge(source, target);
}
bool vertexExists(const std::string& item) const {
return graph.vertexExists(item);
}
int getNumVertices() const {
return graph.getNumVertices();
}
// Returns path as vector (converts to JS array)
std::vector<std::string> getPath(const std::string& source, const std::string& target) {
std::deque<std::string> path;
graph.getPath(path, source, target);
return std::vector<std::string>(path.begin(), path.end());
}
// Returns graph as string for display
std::string toString() const {
std::ostringstream oss;
oss << graph;
return oss.str();
}
};
EMSCRIPTEN_BINDINGS(graph_module) {
register_vector<std::string>("VectorString");
class_<GraphWrapper>("Graph")
.constructor<>()
.constructor<int>()
.function("addVertex", &GraphWrapper::addVertex)
.function("addEdge", &GraphWrapper::addEdge)
.function("vertexExists", &GraphWrapper::vertexExists)
.function("getNumVertices", &GraphWrapper::getNumVertices)
.function("getPath", &GraphWrapper::getPath)
.function("toString", &GraphWrapper::toString);
}