transpose method

bool transpose(
  1. List<List<Node>> layers
)

Implementation

bool transpose(List<List<Node>> layers) {
  var changed = false;
  var improved = true;

  while (improved) {
    improved = false;
    for (var l = 0; l < layers.length - 1; l++) {
      final northernNodes = layers[l];
      final southernNodes = layers[l + 1];

      // Create a map that holds the index of every [Node]. Key is the [Node] and value is the index of the item.
      final indexMap = HashMap.of(northernNodes.asMap().map((key, value) => MapEntry(value, key)));

      for (var i = 0; i < southernNodes.length - 1; i++) {
        final v = southernNodes[i];
        final w = southernNodes[i + 1];
        if (crossingCount(indexMap, v, w) > crossingCount(indexMap, w, v)) {
          improved = true;
          exchange(southernNodes, v, w);
          changed = true;
        }
      }
    }
  }
  return changed;
}