zeroFill method

Float64List zeroFill (Float64List array, int newLength, bool zeroFillExtend)

Zero-fills array as follows and returns the result:

  1. if newLength is different from array.length, the input array is either cut, or extended by zeroes, respectively.
  2. if the array is not a power of 2, it is extended and filled to the next power of 2.
  3. if zeroFillExtend is true, the array is doubled and filled. Returns a new array. Only if none of the above conditions are fulfilled the original array is returned.

Implementation

static Float64List zeroFill(
    Float64List array, int newLength, bool zeroFillExtend) {
  int newsize;

  Float64List result = array;

  int ftsize = array.length;
  if (newLength != null && newLength != ftsize) {
    ftsize = newLength;
    result = Float64List(ftsize); // is autofilled with 0.0
    for (int i = 0; i < array.length; i++) {
      if (i > ftsize - 1) // must cut in this case
        break;
      result[i] = array[i];
    }
  }

  // zero-fill array up to next power of two if array size is not a power of 2
  Float64List zeroFilledValues;
  if (!isPowerOfTwo(ftsize)) {
    newsize = nextPowerOfTwo(ftsize);
    zeroFilledValues = Float64List(newsize); // is autofilled with 0.0
    for (int i = 0; i < result.length; i++) {
      zeroFilledValues[i] = result[i];
    }
    result = zeroFilledValues;
  }

  // zero-filled array to become twice as big.
  // example: for spectrum real part to get the right size
  if (zeroFillExtend) {
    newsize = result.length * 2;
    zeroFilledValues = Float64List(newsize); // auto zero-filled
    for (int i = 0; i < result.length; i++) {
      zeroFilledValues[i] = result[i];
    }
    result = zeroFilledValues;
  }
  return result;
}