.size()];
Iterator<mxGraphAbstractHierarchyCell> iter = rank.iterator();
for (int j = 0; j < orderedCells.length; j++)
{
mxGraphAbstractHierarchyCell cell = iter
.next();
orderedCells[cell.getGeneralPurposeVariable(i)] = cell;
}
List<mxGraphAbstractHierarchyCell> leftCellAboveConnections = null;
List<mxGraphAbstractHierarchyCell> leftCellBelowConnections = null;
List<mxGraphAbstractHierarchyCell> rightCellAboveConnections = null;
List<mxGraphAbstractHierarchyCell> rightCellBelowConnections = null;
int[] leftAbovePositions = null;
int[] leftBelowPositions = null;
int[] rightAbovePositions = null;
int[] rightBelowPositions = null;
mxGraphAbstractHierarchyCell leftCell = null;
mxGraphAbstractHierarchyCell rightCell = null;
for (int j = 0; j < (rank.size() - 1); j++)
{
// For each intra-rank adjacent pair of cells
// see if swapping them around would reduce the
// number of edges crossing they cause in total
// On every cell pair except the first on each rank, we
// can save processing using the previous values for the
// right cell on the new left cell
if (j == 0)
{
leftCell = orderedCells[j];
leftCellAboveConnections = leftCell
.getNextLayerConnectedCells(i);
leftCellBelowConnections = leftCell
.getPreviousLayerConnectedCells(i);
leftAbovePositions = new int[leftCellAboveConnections
.size()];
leftBelowPositions = new int[leftCellBelowConnections
.size()];
for (int k = 0; k < leftAbovePositions.length; k++)
{
leftAbovePositions[k] = leftCellAboveConnections
.get(k).getGeneralPurposeVariable(i + 1);
}
for (int k = 0; k < leftBelowPositions.length; k++)
{
leftBelowPositions[k] = (leftCellBelowConnections
.get(k)).getGeneralPurposeVariable(i - 1);
}
}
else
{
leftCellAboveConnections = rightCellAboveConnections;
leftCellBelowConnections = rightCellBelowConnections;
leftAbovePositions = rightAbovePositions;
leftBelowPositions = rightBelowPositions;
leftCell = rightCell;
}
rightCell = orderedCells[j + 1];
rightCellAboveConnections = rightCell
.getNextLayerConnectedCells(i);
rightCellBelowConnections = rightCell
.getPreviousLayerConnectedCells(i);
rightAbovePositions = new int[rightCellAboveConnections
.size()];
rightBelowPositions = new int[rightCellBelowConnections
.size()];
for (int k = 0; k < rightAbovePositions.length; k++)
{
rightAbovePositions[k] = (rightCellAboveConnections
.get(k)).getGeneralPurposeVariable(i + 1);
}
for (int k = 0; k < rightBelowPositions.length; k++)
{
rightBelowPositions[k] = (rightCellBelowConnections
.get(k)).getGeneralPurposeVariable(i - 1);
}
int totalCurrentCrossings = 0;
int totalSwitchedCrossings = 0;
for (int k = 0; k < leftAbovePositions.length; k++)
{
for (int ik = 0; ik < rightAbovePositions.length; ik++)
{
if (leftAbovePositions[k] > rightAbovePositions[ik])
{
totalCurrentCrossings++;
}
if (leftAbovePositions[k] < rightAbovePositions[ik])
{
totalSwitchedCrossings++;
}
}
}
for (int k = 0; k < leftBelowPositions.length; k++)
{
for (int ik = 0; ik < rightBelowPositions.length; ik++)
{
if (leftBelowPositions[k] > rightBelowPositions[ik])
{
totalCurrentCrossings++;
}
if (leftBelowPositions[k] < rightBelowPositions[ik])
{
totalSwitchedCrossings++;
}
}
}
if ((totalSwitchedCrossings < totalCurrentCrossings)
|| (totalSwitchedCrossings == totalCurrentCrossings && nudge))
{
int temp = leftCell.getGeneralPurposeVariable(i);
leftCell.setGeneralPurposeVariable(i, rightCell
.getGeneralPurposeVariable(i));
rightCell.setGeneralPurposeVariable(i, temp);
// With this pair exchanged we have to switch all of
// values for the left cell to the right cell so the
// next iteration for this rank uses it as the left
// cell again
rightCellAboveConnections = leftCellAboveConnections;