-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist_wasm.cpp
More file actions
76 lines (63 loc) · 1.77 KB
/
Copy pathlist_wasm.cpp
File metadata and controls
76 lines (63 loc) · 1.77 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
72
73
74
75
76
#include <emscripten/bind.h>
#include <sstream>
#include "LinkedList.hpp"
using namespace emscripten;
// Wrapper class to expose OrderedLinkedList<int> with JS-friendly methods
class LinkedListWrapper {
private:
OrderedLinkedList<int> list;
public:
LinkedListWrapper() : list() {}
void insert(int item) {
list.insert(item);
}
void remove(int item) {
list.remove(item);
}
int find(int item) {
return list.find(item);
}
int get(int pos) {
try {
return list.get(pos);
} catch (...) {
return -1; // Return -1 for invalid positions
}
}
int size() {
return list.size();
}
bool empty() {
return list.empty();
}
void clear() {
list.clear();
}
std::string toString() {
std::ostringstream oss;
oss << list;
return oss.str();
}
// Returns all elements as a space-separated string for JS parsing
std::string getAll() {
std::ostringstream oss;
for (int i = 0; i < list.size(); i++) {
if (i > 0) oss << " ";
oss << list.get(i);
}
return oss.str();
}
};
EMSCRIPTEN_BINDINGS(list_module) {
class_<LinkedListWrapper>("LinkedList")
.constructor<>()
.function("insert", &LinkedListWrapper::insert)
.function("remove", &LinkedListWrapper::remove)
.function("find", &LinkedListWrapper::find)
.function("get", &LinkedListWrapper::get)
.function("size", &LinkedListWrapper::size)
.function("empty", &LinkedListWrapper::empty)
.function("clear", &LinkedListWrapper::clear)
.function("toString", &LinkedListWrapper::toString)
.function("getAll", &LinkedListWrapper::getAll);
}