Skip to content

Commit

Permalink
cache results in getConnection and hasGroundConnection to speed up an…
Browse files Browse the repository at this point in the history
…alyzeCircuit (sharpie7#967)
  • Loading branch information
pfalstad committed Nov 2, 2024
1 parent 82b6186 commit 30e6184
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/com/lushprojects/circuitjs1/client/CompositeElm.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public String dumpElements(int mask) {
}

// are n1 and n2 connected internally somehow?
public boolean getConnection(int n1, int n2) {
public boolean getConnectionSlow(int n1, int n2) {
Vector<Integer> connectedNodes = new Vector<Integer>();

// keep list of nodes connected to n1
Expand Down Expand Up @@ -242,8 +242,35 @@ public boolean getConnection(int n1, int n2) {
return false;
}

HashMap<IntPair, Boolean> connectionMap;
HashMap<Integer, Boolean> groundConnectionMap;

public boolean getConnection(int n1, int n2) {
if (connectionMap == null)
connectionMap = new HashMap<IntPair, Boolean>();
IntPair key = new IntPair(n1, n2);
Boolean result = connectionMap.get(key);
if (result != null)
return result;
result = getConnectionSlow(n1, n2);
connectionMap.put(key, result);
return result;
}

// is n1 connected to ground somehow?
public boolean hasGroundConnection(int n1) {
if (groundConnectionMap == null)
groundConnectionMap = new HashMap<Integer, Boolean>();
Integer key = n1;
Boolean result = groundConnectionMap.get(key);
if (result != null)
return result;
result = hasGroundConnectionSlow(n1);
groundConnectionMap.put(key, result);
return result;
}

public boolean hasGroundConnectionSlow(int n1) {
Vector<Integer> connectedNodes = new Vector<Integer>();

// keep list of nodes connected to n1
Expand Down

0 comments on commit 30e6184

Please sign in to comment.