Skip to content

Commit

Permalink
fixed reset bug by changing recursive propogate to breath first using…
Browse files Browse the repository at this point in the history
… queue
  • Loading branch information
sr14978 committed Nov 28, 2016
1 parent 78841e9 commit ff748ad
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/com/modsim/simulator/Sim.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

import javax.swing.JOptionPane;

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

private Queue<QueueItem> propagationQueue;

/**
* Begin deferring propagation operations (preventing errors during large-scale operations)
*/
Expand Down Expand Up @@ -103,7 +107,7 @@ public void newSim() {
filePath = "";
Main.ui.updateTitle();
}

propagationQueue = new LinkedList<QueueItem>();
Main.ui.view.flagStaticRedraw();
}

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

// Add link to visited - remove after propagation
visited[id] = true;
doPropagate(p.link.targ.owner, visited);
visited[id] = false;
boolean[] clone = visited.clone();
clone[id] = true;
propagationQueue.add(new QueueItem(p.link.targ.owner, clone));
}
p.updated = false;
}
Expand All @@ -343,7 +347,21 @@ private void doPropagate(BaseModule m, boolean[] visited) {
*/
public void propagate(BaseModule m) {
synchronized (lock) {
doPropagate(m, new boolean[1024]);
propagationQueue.add(new QueueItem(m, new boolean[1024]));
while(!propagationQueue.isEmpty()){
QueueItem it = propagationQueue.remove();
doPropagate(it.baseModule, it.visited);
}
}
}

class QueueItem {
private BaseModule baseModule;
private boolean[] visited;
public QueueItem(BaseModule m, boolean[] v){
baseModule = m;
visited = v;
}
}

}

0 comments on commit ff748ad

Please sign in to comment.