flatten method

Float32Array flatten(
  1. List array,
  2. int nBlocks,
  3. int blockSize
)

Implementation

Float32Array flatten(List array, int nBlocks, int blockSize) {
  var firstElem = array[0];

  if (firstElem is num || firstElem is double || firstElem is int) {
    List<double> array2 = [];

    for (var element in array) {
      array2.add(element.toDouble());
    }

    return Float32Array.from(array2);
  }

  // // unoptimized: ! isNaN( firstElem )
  // // see http://jacksondunstan.com/articles/983

  var n = nBlocks * blockSize;
  Float32Array? r = arrayCacheF32[n];

  if (r == null) {
    r = Float32Array(n);
    arrayCacheF32[n] = r;
  }

  if (nBlocks != 0) {
    // firstElem.toArray( r.data, 0 );

    // for ( var i = 1, offset = 0; i != nBlocks; ++ i ) {

    //   offset += blockSize;
    //   array[ i ].toArray( r.data, offset );

    // }

    for (int i = 0; i < nBlocks; i++) {
      // print(" i: ${i} this: ${this} nBlocks: ${nBlocks} ");

      List<num> data = array[i].toJSON();

      data.asMap().forEach((index, element) {
        int idx = i * blockSize + index;
        r![idx] = element.toDouble();
      });
    }

    // bool stringKey = false;

    // if(array[0] == null) {
    //   stringKey = true;
    // }

    // if(!stringKey) {
    //   for ( var i = 0; i < nBlocks; i++ ) {
    //     List<num> _data = array[ i ].toJSON();

    //     _data.asMap().forEach((index, element) {
    //       int _idx = i * blockSize + index;
    //       r[_idx] = element;
    //     });
    //   }
    // } else {
    //   for ( var i = 0; i < nBlocks; i++ ) {
    //     List<num> _data = array[ i.toString() ].toJSON();

    //     _data.asMap().forEach((index, element) {
    //       int _idx = i * blockSize + index;
    //       r[_idx] = element;
    //     });
    //   }
    // }

  }

  return r;
}