splitArray method
Makes a 2D array from array
. Each row of the 2D array will have
size
elements. The last row may be smaller if the length of array
can't be divided by size
without remainder.
If size
is 0 or equal or greater than the length of array
, the result
will contain array
as its single row.
Implementation
static List<Float64List> splitArray(Float64List array, int size) {
int len = array.length;
if (size >= len || size == 0) return [array];
int nrows = len ~/ size;
int lastsubrowLength = 0;
if (len.remainder(size) > 0) {
nrows++;
lastsubrowLength = len.remainder(size);
}
List<Float64List> result = new List<Float64List>(nrows);
int arrix = 0;
for (int i = 0; i < nrows; i++) {
int rowlen = size;
if (i == nrows - 1 && lastsubrowLength > 0) rowlen = lastsubrowLength;
Float64List row = new Float64List(rowlen);
for (int k = 0; k < rowlen; k++) {
row[k] = array[arrix++];
}
result[i] = row;
}
return result;
}