transpose function

Assuming even length 2D matrix colsRows, return it's transpose copy.

Implementation

List<List<StackableValuePoint>> transpose(List<List<StackableValuePoint>> colsInRows) {
  int nRows = colsInRows.length;
  if (colsInRows.isEmpty) return colsInRows;

  int nCols = colsInRows[0].length;
  if (nCols == 0) throw StateError('Degenerate matrix');

  // Init the transpose to make sure the size is right
  List<List<StackableValuePoint>> rowsInCols = List.filled(nCols, []);
  for (int col = 0; col < nCols; col++) {
    rowsInCols[col] = List.filled(nRows, StackableValuePoint.initial());
  }

  // Transpose
  for (int row = 0; row < nRows; row++) {
    for (int col = 0; col < nCols; col++) {
      rowsInCols[col][row] = colsInRows[row][col];
    }
  }
  return rowsInCols;
}