Skip to content

Commit

Permalink
Não usar Isolate
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagohm committed Aug 21, 2020
1 parent ee8f179 commit 3b6a976
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 48 deletions.
2 changes: 1 addition & 1 deletion lib/src/kociemba_solver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class KociembaSolver extends Solver {
// increment axis
if (++search.ax[n] > 5) {
if (n == 0) {
if (sw.elapsedMilliseconds > timeout.inMilliseconds) {
if (sw.elapsed > timeout) {
return null;
}

Expand Down
68 changes: 21 additions & 47 deletions lib/src/solver.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:isolate';

import 'package:cuber/src/cube.dart';
import 'package:cuber/src/solution.dart';

Expand Down Expand Up @@ -31,54 +29,30 @@ abstract class Solver {
Cube cube, {
Duration timeout = defaultTimeout,
}) async* {
final receiver = ReceivePort();

await Isolate.spawn(_solveDeeply, [
receiver.sendPort,
cube,
timeout?.inMilliseconds,
this,
]);

await for (final data in receiver) {
if (data is Solution) {
yield data;
timeout ??= Solver.defaultTimeout;

var maxDepth = Solver.defaultMaxDepth;
final solutions = <Solution>{};
final sw = Stopwatch()..start();

while (sw.elapsed < timeout) {
final s = solve(
cube,
maxDepth: maxDepth,
timeout: timeout - sw.elapsed,
);

if (maxDepth > 0 && s != null && s.isNotEmpty) {
if (!solutions.contains(s)) {
solutions.add(s);
yield s;
maxDepth = s.length - 1;
} else {
maxDepth--;
}
} else {
break;
}
}
}
}

void _solveDeeply(List data) {
final SendPort sender = data[0];
final Cube cube = data[1];
final int timeout = data[2] ?? Solver.defaultTimeout.inMilliseconds;
final Solver solver = data[3];

var maxDepth = Solver.defaultMaxDepth;
final solutions = <Solution>{};
final sw = Stopwatch()..start();

while (sw.elapsedMilliseconds < timeout) {
final s = solver.solve(
cube,
maxDepth: maxDepth,
timeout: Duration(milliseconds: timeout - sw.elapsedMilliseconds),
);

if (maxDepth > 0 && s != null && s.isNotEmpty) {
if (!solutions.contains(s)) {
solutions.add(s);
sender.send(s);
maxDepth = s.length - 1;
} else {
maxDepth--;
}
} else {
break;
}
}

sender.send(null);
}

0 comments on commit 3b6a976

Please sign in to comment.