Skip to content

Commit

Permalink
Make sure that exchanging ports does not lead to bugs by using a scalar.
Browse files Browse the repository at this point in the history
  • Loading branch information
soerendomroes committed Aug 19, 2024
1 parent 1af5370 commit 72b658b
Showing 1 changed file with 52 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ public int compare(final LPort originalP1, final LPort originalP2) {
if (p1.getSide() != p2.getSide()) {
int result = new PortSideComparator().compare(p1.getSide(), p2.getSide());
if (result > 0) {
updateBiggerAndSmallerAssociations(p1, p2);
updateBiggerAndSmallerAssociations(p1, p2, 1);
} else {
updateBiggerAndSmallerAssociations(p2, p1);
updateBiggerAndSmallerAssociations(p2, p1, 1);
}
return result;
}
int reverseOrder = 1;

// Sort incoming edges by sorting their ports by the order of the nodes they connect to.
if (!p1.getIncomingEdges().isEmpty() && !p2.getIncomingEdges().isEmpty()) {
Expand All @@ -131,9 +132,7 @@ public int compare(final LPort originalP1, final LPort originalP2) {
|| p1.getSide() == PortSide.SOUTH && p2.getSide() == PortSide.SOUTH) {
// Some ports are ordered in the way around.
// Previously this did not matter, since the north south processor did override the ordering.
LPort temp = p1;
p1 = p2;
p2 = temp;
reverseOrder = -reverseOrder;
}

LPort p1SourcePort = p1.getIncomingEdges().get(0).getSource();
Expand All @@ -145,11 +144,11 @@ public int compare(final LPort originalP1, final LPort originalP2) {
// first and should hence be sorted.
for (LPort port : p1Node.getPorts()) {
if (p1SourcePort.equals(port)) {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
} else if (p2SourcePort.equals(port)) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
}
}
}
Expand All @@ -171,14 +170,15 @@ public int compare(final LPort originalP1, final LPort originalP2) {
if (p1.getSide() == PortSide.EAST && p2.getSide() == PortSide.EAST) {
// Some ports are ordered in the way around.
// Previously this did not matter, since the north south processor did override the ordering.
inPreviousLayer = -inPreviousLayer;
reverseOrder = -reverseOrder;
}
if (inPreviousLayer > 0) {
updateBiggerAndSmallerAssociations(p1, p2);
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
return inPreviousLayer;
}
}

Expand All @@ -187,11 +187,11 @@ public int compare(final LPort originalP1, final LPort originalP2) {
int inPreviousLayer = checkReferenceLayer(Arrays.stream(previousLayer).collect(Collectors.toList()), p1Node, p2Node, p1, p2);
if (inPreviousLayer != 0) {
if (inPreviousLayer > 0) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
}
// If both ports do not connect to the previous layer, use the port model order.
Expand All @@ -201,11 +201,11 @@ public int compare(final LPort originalP1, final LPort originalP2) {
int result = checkPortModelOrder(p1, p2);
if (result != 0) {
if (result > 0) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
}
}
Expand All @@ -231,11 +231,11 @@ public int compare(final LPort originalP1, final LPort originalP2) {
int p1MO = p1TargetNode.getProperty(InternalProperties.MODEL_ORDER);
int p2MO = p2TargetNode.getProperty(InternalProperties.MODEL_ORDER);
if (p1MO > p2MO) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
}

Expand All @@ -245,11 +245,11 @@ public int compare(final LPort originalP1, final LPort originalP2) {
int result = checkPortModelOrder(p1, p2);
if (result != 0) {
if (result > 0) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
}
// If one of the ports has no model order, find something else to compare them which is the edge order.
Expand All @@ -268,11 +268,11 @@ public int compare(final LPort originalP1, final LPort originalP2) {
if (p1TargetNode != null && p1TargetNode.equals(p2TargetNode)) {
// If both edges are reversed or not reversed, just use their model order.
if (p1Order > p2Order) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
}
// Use precomputed ordering value if possible to utilize order inheritence of edges connected to a node.
Expand All @@ -287,36 +287,36 @@ public int compare(final LPort originalP1, final LPort originalP2) {
}
// If the nodes have different targets just use their order.
if (p1Order > p2Order) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}

}
// Sort outgoing ports before incoming ports.
if (!p1.getIncomingEdges().isEmpty() && !p2.getOutgoingEdges().isEmpty()) {
updateBiggerAndSmallerAssociations(p1, p2);
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return 1;
} else if (!p1.getOutgoingEdges().isEmpty() && !p2.getIncomingEdges().isEmpty()) {
updateBiggerAndSmallerAssociations(p2, p1);
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -1;
} else if (p1.hasProperty(InternalProperties.MODEL_ORDER) && p2.hasProperty(InternalProperties.MODEL_ORDER)) {
// The ports have no edges.
// Use the port model order to compare them.
int p1MO = p1.getProperty(InternalProperties.MODEL_ORDER);
int p2MO = p2.getProperty(InternalProperties.MODEL_ORDER);
if (p1MO > p2MO) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
updateBiggerAndSmallerAssociations(p1, p2, reverseOrder);
return reverseOrder;
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
} else {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
updateBiggerAndSmallerAssociations(p2, p1, reverseOrder);
return -reverseOrder;
}
}

Expand All @@ -338,7 +338,13 @@ public int checkPortModelOrder(final LPort p1, final LPort p2) {
return 0;
}

private void updateBiggerAndSmallerAssociations(final LPort bigger, final LPort smaller) {
private void updateBiggerAndSmallerAssociations(final LPort biggerOri, final LPort smallerOri, int reverseOrder) {
LPort bigger = biggerOri;
LPort smaller = smallerOri;
if (reverseOrder < 0) {
bigger = smallerOri;
smaller = biggerOri;
}
HashSet<LPort> biggerPortBiggerThan = biggerThan.get(bigger);
HashSet<LPort> smallerPortBiggerThan = biggerThan.get(smaller);
HashSet<LPort> biggerPortSmallerThan = smallerThan.get(bigger);
Expand Down

0 comments on commit 72b658b

Please sign in to comment.