aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-05-25 12:11:34 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-05-25 12:11:34 +0200
commit238702e7dd860af59d22f33cbfe163f28c3beb4e (patch)
tree577adc495c4e5284f357e44e2877e8c848d79a24
parente1690a427040f710de5643643d7c6226c1b2364f (diff)
downloadnissy-core-238702e7dd860af59d22f33cbfe163f28c3beb4e.tar.gz
nissy-core-238702e7dd860af59d22f33cbfe163f28c3beb4e.zip
Cleanup
-rw-r--r--.gitignore3
-rw-r--r--README.md7
-rw-r--r--cpp/examples/solve_h48h3k2.cpp5
-rw-r--r--cpp/nissy.cpp23
-rw-r--r--cpp/nissy.h3
-rw-r--r--web/adapter.cpp16
-rw-r--r--web/examples/solve.mjs12
-rw-r--r--web/storage.cpp38
8 files changed, 38 insertions, 69 deletions
diff --git a/.gitignore b/.gitignore
index 8b1a5d1..63b37ff 100644
--- a/.gitignore
+++ b/.gitignore
@@ -22,8 +22,7 @@ runcpp
22runtest.js 22runtest.js
23runtest.wasm 23runtest.wasm
24web/*.wasm 24web/*.wasm
25web/http/*.wasm 25web/http/nissy_web_module.*
26web/http/*.mjs
27web/nissy_web_module.* 26web/nissy_web_module.*
28runtool 27runtool
29tools/.DS_Store 28tools/.DS_Store
diff --git a/README.md b/README.md
index 3f64e29..6f449cc 100644
--- a/README.md
+++ b/README.md
@@ -244,13 +244,6 @@ The JavaScript module can be built with
244$ ./build web 244$ ./build web
245``` 245```
246 246
247Some examples can be found in the `web/examples` folder. They can be run
248using [nodejs](https://nodejs.org):
249
250```
251$ node web/examples/[filename]
252```
253
254An example web app running nissy can be found in the `web/http` folder. 247An example web app running nissy can be found in the `web/http` folder.
255You can run a web server in that folder to check it out, but you need 248You can run a web server in that folder to check it out, but you need
256to set some extra headers to make it work. For example, if you are using 249to set some extra headers to make it work. For example, if you are using
diff --git a/cpp/examples/solve_h48h3k2.cpp b/cpp/examples/solve_h48h3k2.cpp
index f0d6f2f..2a226ba 100644
--- a/cpp/examples/solve_h48h3k2.cpp
+++ b/cpp/examples/solve_h48h3k2.cpp
@@ -80,10 +80,9 @@ int main() {
80 if (solve_result.solutions.size() == 0) { 80 if (solve_result.solutions.size() == 0) {
81 std::cout << "No solution found!" << std::endl; 81 std::cout << "No solution found!" << std::endl;
82 } else { 82 } else {
83 auto& sol = solve_result.solutions[0]; 83 auto len = nissy::count_moves(solve_result.solutions).value;
84 auto len = nissy::count_moves(sol).value;
85 std::cout << "Solution (" << len << " moves): " 84 std::cout << "Solution (" << len << " moves): "
86 << sol << std::endl; 85 << solve_result.solutions << std::endl;
87 } 86 }
88 87
89 return 0; 88 return 0;
diff --git a/cpp/nissy.cpp b/cpp/nissy.cpp
index ed0dc0f..36fb5f2 100644
--- a/cpp/nissy.cpp
+++ b/cpp/nissy.cpp
@@ -56,6 +56,7 @@ namespace nissy {
56 namespace size { 56 namespace size {
57 constexpr size_t CUBE = 24; 57 constexpr size_t CUBE = 24;
58 constexpr size_t TRANSFORMATION = 12; 58 constexpr size_t TRANSFORMATION = 12;
59 constexpr size_t SOLVE_STATS = 10;
59 constexpr size_t DATAID = 255; 60 constexpr size_t DATAID = 255;
60 } 61 }
61 62
@@ -163,34 +164,28 @@ namespace nissy {
163 unsigned threads, int (*poll_status)(void *), 164 unsigned threads, int (*poll_status)(void *),
164 void *poll_status_data) const 165 void *poll_status_data) const
165 { 166 {
167 long long stats[size::SOLVE_STATS];
166 solver::solve_result result; 168 solver::solve_result result;
167 169
168 if (maxsols == 0) { 170 if (maxsols == 0) {
169 result.solutions = {}; 171 result.solutions = "";
170 result.err = error::OK; 172 result.err = error::OK;
171 return result; 173 return result;
172 } 174 }
173 175
174 const size_t len = 3 * (maxmoves+1) * maxsols; 176 const size_t len = 3 * (maxmoves+1) * maxsols;
175 std::vector<char> csols(len); 177 result.solutions.resize(len);
176 178
177 auto err = nissy_solve(cube.to_string().c_str(), 179 auto err = nissy_solve(cube.to_string().c_str(),
178 name.c_str(), niss.value, minmoves, maxmoves, maxsols, 180 name.c_str(), niss.value, minmoves, maxmoves, maxsols,
179 optimal, threads, data.size(), 181 optimal, threads, data.size(),
180 reinterpret_cast<const unsigned char *>(data.data()), len, 182 reinterpret_cast<const unsigned char *>(data.data()), len,
181 csols.data(), result.stats.data(), poll_status, 183 result.solutions.data(), stats,
182 poll_status_data); 184 poll_status, poll_status_data);
183 result.err = error{err};
184 185
185 if (err < 0) 186 int size = result.solutions.find_first_of('\0') + 1;
186 return result; 187 result.solutions.resize(size);
187 188 result.err = error{err};
188 std::string_view strsols(csols.data());
189 for (auto r : strsols | std::views::split('\n'))
190 if (r.begin() != r.end() ||
191 r.begin() == strsols.begin())
192 result.solutions.push_back(
193 std::string{r.begin(), r.end()});
194 189
195 return result; 190 return result;
196 } 191 }
diff --git a/cpp/nissy.h b/cpp/nissy.h
index 884043d..817307c 100644
--- a/cpp/nissy.h
+++ b/cpp/nissy.h
@@ -79,8 +79,7 @@ namespace nissy {
79 public: 79 public:
80 struct solve_result { 80 struct solve_result {
81 error err; 81 error err;
82 std::vector<std::string> solutions; 82 std::string solutions;
83 std::array<long long, 10> stats;
84 }; 83 };
85 84
86 const std::string name; 85 const std::string name;
diff --git a/web/adapter.cpp b/web/adapter.cpp
index 3613730..c3134e9 100644
--- a/web/adapter.cpp
+++ b/web/adapter.cpp
@@ -108,17 +108,13 @@ bool solver_valid(const std::string& name)
108 108
109int poll_status(void *arg) 109int poll_status(void *arg)
110{ 110{
111 return nissy::status::RUN.value; 111 if (arg == NULL || *(int *)arg == -1)
112/*
113TODO: reintroduce poll status
114 int id = *(int *)arg;
115 if (id == -1)
116 return nissy::status::RUN.value; 112 return nissy::status::RUN.value;
117 return callFunctionInt(id); 113
118*/ 114 return callFunctionInt(*(int *)arg);
119} 115}
120 116
121#if 0 117#if 1
122 118
123nissy::solver::solve_result solve(std::string name, 119nissy::solver::solve_result solve(std::string name,
124 nissy::cube cube, nissy::nissflag nissflag, unsigned minmoves, 120 nissy::cube cube, nissy::nissflag nissflag, unsigned minmoves,
@@ -212,17 +208,17 @@ EMSCRIPTEN_BINDINGS(Nissy)
212 .function("toString", &nissy::cube::to_string) 208 .function("toString", &nissy::cube::to_string)
213 ; 209 ;
214 210
211/*
215 emscripten::register_vector<std::string>("StringVector"); 212 emscripten::register_vector<std::string>("StringVector");
216 emscripten::value_array<nissy::solver::solve_result>("SolveResult") 213 emscripten::value_array<nissy::solver::solve_result>("SolveResult")
217 .element(&nissy::solver::solve_result::err) 214 .element(&nissy::solver::solve_result::err)
218 .element(&nissy::solver::solve_result::solutions) 215 .element(&nissy::solver::solve_result::solutions)
219 ; 216 ;
220/* 217*/
221 emscripten::class_<nissy::solver::solve_result>("SolveResult") 218 emscripten::class_<nissy::solver::solve_result>("SolveResult")
222 .property("err", &nissy::solver::solve_result::err) 219 .property("err", &nissy::solver::solve_result::err)
223 .property("solutions", &nissy::solver::solve_result::solutions) 220 .property("solutions", &nissy::solver::solve_result::solutions)
224 ; 221 ;
225*/
226 222
227 emscripten::function("countMoves", &nissy::count_moves); 223 emscripten::function("countMoves", &nissy::count_moves);
228 emscripten::function("solve", &solve, 224 emscripten::function("solve", &solve,
diff --git a/web/examples/solve.mjs b/web/examples/solve.mjs
deleted file mode 100644
index 8ff59c0..0000000
--- a/web/examples/solve.mjs
+++ /dev/null
@@ -1,12 +0,0 @@
1import Nissy from '../nissy_web_module.mjs'
2
3const nissy = await Nissy();
4
5var log = process.stdout.write.bind(process.stdout);
6//var log = console.log
7nissy.setLogger(nissy._addCallbackFunction(log))
8
9var cube = new nissy.Cube();
10cube.move('R\' U\' F');
11
12nissy.solve('h48h0k4', cube, nissy.NissFlag.normal, 0, 8, 2, 99, 4, -1);
diff --git a/web/storage.cpp b/web/storage.cpp
index 4be3066..4d0959f 100644
--- a/web/storage.cpp
+++ b/web/storage.cpp
@@ -13,29 +13,29 @@ std::string getprefix() {
13} 13}
14 14
15EM_ASYNC_JS(int, loadfs, (), { 15EM_ASYNC_JS(int, loadfs, (), {
16 const dir = '/tables'; 16 const dir = '/tables';
17 const inBrowser = typeof window !== 'undefined'; 17 const inBrowser = typeof window !== 'undefined';
18 const inWorker = typeof WorkerGlobalScope !== 'undefined' && 18 const inWorker = typeof WorkerGlobalScope !== 'undefined' &&
19 self instanceof WorkerGlobalScope; 19 self instanceof WorkerGlobalScope;
20 20
21 if (!(inBrowser || inWorker)) return; 21 if (!(inBrowser || inWorker))
22 return;
22 23
23 if (!FS.analyzePath(dir).exists) 24 if (!FS.analyzePath(dir).exists)
24 FS.mkdir(dir); 25 FS.mkdir(dir);
25 26
26 if (FS.analyzePath(dir).object.mount.mountpoint != dir) { 27 if (FS.analyzePath(dir).object.mount.mountpoint != dir) {
27 FS.mount(IDBFS, { autoPersist: true }, dir); 28 FS.mount(IDBFS, { autoPersist: true }, dir);
28 29
29 await new Promise((resolve, reject) => { 30 await new Promise((resolve, reject) => {
30 FS.syncfs(true, function (err) { 31 FS.syncfs(true, function (err) {
31 if (err) { 32 if (err)
32 reject(err); 33 reject(err);
33 } else { 34 else
34 resolve(true); 35 resolve(true);
35 } 36 });
36 }); 37 });
37 }); 38 }
38 }
39}); 39});
40 40
41bool storage::read(std::string key, size_t data_size, char *data) 41bool storage::read(std::string key, size_t data_size, char *data)

Generated with cgit - Back to sebastiano.tronto.net