for (Tile tile : dev.getTileMap().values()) {
if (tile.getWireHashMap() == null)
continue;
// Create a safe copy to playaround with without messing up any other tiles wires
WireHashMap whm = new WireHashMap(tile.getWireHashMap());
// Traverse all non-PIP wire connections starting at this source wire. If any
// such wire connections lead to a sink wire that is not already a connection of
// the source wire, mark it to be added as a connection
for (Integer wire : whm.keySet()) {
Set<WireConnection> wcToAdd = new HashSet<>();
Set<WireConnection> checkedConnections = new HashSet<>();
Queue<WireConnection> connectionsToFollow = new LinkedList<>();
// Add the wire to prevent building a connection back to itself
checkedConnections.add(new WireConnection(wire, 0, 0, false));
for (WireConnection wc : whm.get(wire)) {
if (!wc.isPIP()) {
checkedConnections.add(wc);
connectionsToFollow.add(wc);
}
}
while (!connectionsToFollow.isEmpty()) {
WireConnection midwc = connectionsToFollow.remove();
Tile midTile = midwc.getTile(tile);
Integer midWire = midwc.getWire();
// Dead end checks
if (midTile.getWireHashMap() == null || midTile.getWireConnections(midWire) == null)
continue;
for (WireConnection sinkwc : midTile.getWireConnections(midWire)) {
if (sinkwc.isPIP()) continue;
Integer sinkWire = sinkwc.getWire();
Tile sinkTile = sinkwc.getTile(midTile);
int colOffset = midwc.getColumnOffset() + sinkwc.getColumnOffset();
int rowOffset = midwc.getRowOffset() + sinkwc.getRowOffset();
// This represents the wire connection from the original source to the sink wire
WireConnection source2sink = new WireConnection(sinkWire, rowOffset, colOffset, false);
boolean wirePreviouslyChecked = !checkedConnections.add(source2sink);
// Check if we've already processed this guy and process him if we haven't
if (wirePreviouslyChecked)
continue;
connectionsToFollow.add(source2sink);
// Only add the connection if the wire is a sink. Other connections are
// useless for wire traversing.
if (wireIsSink(we, sinkTile, sinkWire))
wcToAdd.add(source2sink);
}
}
// If there are wires to add, add them here by creating a new WireConnection array
// combining the old and new wires.
if (!wcToAdd.isEmpty()) {
List<WireConnection> newConnections = new ArrayList<>(Arrays.asList(whm.get(wire)));
newConnections.addAll(wcToAdd);
WireConnection[] arrView = newConnections.toArray(
new WireConnection[newConnections.size()]);
whm.put(wire, arrView);
}
}
}
}