Skip to content

Commit ff748ad

Browse files
committed
fixed reset bug by changing recursive propogate to breath first using queue
1 parent 78841e9 commit ff748ad

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/com/modsim/simulator/Sim.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.LinkedList;
56
import java.util.List;
7+
import java.util.Queue;
68

79
import javax.swing.JOptionPane;
810

@@ -40,6 +42,8 @@ public class Sim implements Runnable {
4042
private List<BaseModule> deferredPropagators = new ArrayList<>();
4143
private int deferring = 0;
4244

45+
private Queue<QueueItem> propagationQueue;
46+
4347
/**
4448
* Begin deferring propagation operations (preventing errors during large-scale operations)
4549
*/
@@ -103,7 +107,7 @@ public void newSim() {
103107
filePath = "";
104108
Main.ui.updateTitle();
105109
}
106-
110+
propagationQueue = new LinkedList<QueueItem>();
107111
Main.ui.view.flagStaticRedraw();
108112
}
109113

@@ -328,9 +332,9 @@ private void doPropagate(BaseModule m, boolean[] visited) {
328332
p.link.targ.setVal(p.getVal());
329333

330334
// Add link to visited - remove after propagation
331-
visited[id] = true;
332-
doPropagate(p.link.targ.owner, visited);
333-
visited[id] = false;
335+
boolean[] clone = visited.clone();
336+
clone[id] = true;
337+
propagationQueue.add(new QueueItem(p.link.targ.owner, clone));
334338
}
335339
p.updated = false;
336340
}
@@ -343,7 +347,21 @@ private void doPropagate(BaseModule m, boolean[] visited) {
343347
*/
344348
public void propagate(BaseModule m) {
345349
synchronized (lock) {
346-
doPropagate(m, new boolean[1024]);
350+
propagationQueue.add(new QueueItem(m, new boolean[1024]));
351+
while(!propagationQueue.isEmpty()){
352+
QueueItem it = propagationQueue.remove();
353+
doPropagate(it.baseModule, it.visited);
354+
}
347355
}
348356
}
357+
358+
class QueueItem {
359+
private BaseModule baseModule;
360+
private boolean[] visited;
361+
public QueueItem(BaseModule m, boolean[] v){
362+
baseModule = m;
363+
visited = v;
364+
}
365+
}
366+
349367
}

0 commit comments

Comments
 (0)