Skip to content

Commit 4bcd4a7

Browse files
committed
[squash] working with bugfix in _canconnect
1 parent bb1e303 commit 4bcd4a7

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/benchmark/quicksort-sibling.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
App,
1010
TimeValue,
1111
Origin,
12-
Log
12+
Log,
13+
PrecedenceGraph
1314
} from "../core/internal";
1415

1516
// This is the thrshold for the quicksort algorithm, feeding sorters below this number will use Array.prototype.sort()
@@ -32,7 +33,7 @@ class QuickSorter extends Reactor {
3233
leftReactor: Reactor | undefined;
3334
rightReactor: Reactor | undefined;
3435

35-
constructor(parent: Reactor, name = "Innocent QuickSorter") {
36+
constructor(parent: Reactor, name = "root") {
3637
super(parent, name);
3738
this.parentReadPort = new InPort<number[]>(this);
3839
this.parentWritePort = new OutPort<number[]>(this);
@@ -47,33 +48,46 @@ class QuickSorter extends Reactor {
4748
this.addMutation(
4849
[this.parentReadPort],
4950
[this.parentReadPort,
50-
this.parentWritePort,
51+
this.writable(this.parentWritePort),
5152
this.leftWritePort,
52-
this.rightWritePort
53+
this.rightWritePort,
54+
this.leftReadPort,
55+
this.rightReadPort
5356
],
54-
function (this, parentReadPort, parentWritePort, leftWritePort, rightWritePort) {
57+
function (this, parentReadPort, parentWritePort, leftWritePort, rightWritePort, leftread, rightread) {
5558
const fullarr = parentReadPort.get();
5659
if (fullarr == null) {
5760
throw Error("Received null from port")
5861
}
5962
if (fullarr.length <= T) {
6063
const sorted = [...fullarr].sort((a, b) => (a - b));
61-
this.getReactor().writable(parentWritePort).set(sorted);
64+
parentWritePort.set(sorted);
6265
return;
6366
}
6467
const pivot = fullarr[0];
6568
const leftToSort = fullarr.filter((val) => (val <= pivot));
6669
const righttoSort = fullarr.filter((val) => (val > pivot));
70+
// Hack: if either of them is empty, this is not a good pivot.
71+
// Instead of choosing another pivot, we simply sort it.
72+
if (leftToSort.length === 0 || righttoSort.length === 0) {
73+
const sorted = [...fullarr].sort((a, b) => (a - b));
74+
parentWritePort.set(sorted);
75+
return;
76+
}
6777

6878
console.log(`I received a request! ${fullarr}! Pivot is ${pivot}, so I divided it into ${leftToSort} and ${righttoSort}`);
6979

7080
// First, create 2 new reactors
71-
const leftReactor = this.getReactor()._uncheckedAddSibling(QuickSorter);
72-
const rightReactor = this.getReactor()._uncheckedAddSibling(QuickSorter);
81+
const leftReactor = this.getReactor()._uncheckedAddSibling(QuickSorter, `${this.getReactor()._name}/l`);
82+
const rightReactor = this.getReactor()._uncheckedAddSibling(QuickSorter, `${this.getReactor()._name}/r`);
7383

7484
// Connect ports accoringly
7585
this.connect(leftWritePort, leftReactor.parentReadPort);
7686
this.connect(rightWritePort, rightReactor.parentReadPort);
87+
console.log("000", arb["_getPrecedenceGraph"]().toMermaidString([[leftReactor.parentWritePort, leftread]]));
88+
89+
this.connect(leftReactor.parentWritePort, leftread);
90+
this.connect(rightReactor.parentWritePort, rightread);
7791

7892
this.getReactor().writable(leftWritePort).set(leftToSort);
7993
this.getReactor().writable(rightWritePort).set(righttoSort);
@@ -179,8 +193,8 @@ class Arbiter extends App {
179193
fail?: () => void
180194
) {
181195
super(timeout, keepAlive, fast, success, fail, name);
182-
this.rootSorter = new QuickSorter(this, "rootroot");
183-
this.supplier = new Supplier(this, [5, 1, 4, 1, 1, 4, 8, 1, 0, 1, 9, 1, 9]);
196+
this.rootSorter = new QuickSorter(this, "root");
197+
this.supplier = new Supplier(this, [578, 530, 482, 105, 400, 787, 563, 613, 483, 888, 439, 928, 857, 404, 949, 736, 68, 761, 951, 432, 799, 212, 108, 937, 562, 616, 436, 358, 221, 315, 423, 539, 215, 795, 409, 227, 715, 847, 66, 242, 168, 637, 572, 468, 116, 668, 213, 859, 880, 291, 609, 502, 486, 710, 662, 172, 991, 631, 120, 905, 751, 293, 411, 503, 901, 53, 774, 145, 831, 140, 592, 184, 228, 111, 907, 640, 553, 519, 579, 389, 735, 545, 975, 255, 83, 449, 673, 427, 369, 854, 86, 33, 885, 940, 904, 764, 834, 250, 183, 191]);
184198
this._connect(this.supplier.rootWritePort, this.rootSorter.parentReadPort);
185199
this._connect(this.rootSorter.parentWritePort, this.supplier.rootReadPort);
186200
}

0 commit comments

Comments
 (0)