reshape<T> method

List reshape<T>(
  1. List<int> shape
)

Implementation

List<dynamic> reshape<T>(List<int> shape) {
  final int dimsSize = shape.length;
  int numElements = 1;
  for (int i = 0; i < dimsSize; i++) {
    numElements *= shape[i];
  }

  if (numElements != computeNumElements) {
    throw ArgumentError('Total elements mismatch expected: $numElements elements for shape: $shape but found $computeNumElements');
  }

  if (dimsSize <= 5) {
    switch (dimsSize) {
      case 2:
        return _reshape2<T>(shape);
      case 3:
        return _reshape3<T>(shape);
      case 4:
        return _reshape4<T>(shape);
      case 5:
        return _reshape5<T>(shape);
    }
  }

  List<dynamic> reshapedList = flatten<dynamic>();
  for (int i = dimsSize - 1; i > 0; i--) {
    final List<dynamic> temp = <dynamic>[];
    for (int start = 0; start + shape[i] <= reshapedList.length; start += shape[i]) {
      temp.add(reshapedList.sublist(start, start + shape[i]));
    }
    reshapedList = temp;
  }
  return reshapedList;
}