build method Null safety

  1. @override
Float32Tensor build(
  1. {bool recycle = false}
)
override

Builds tensor and resets elements to empty list.

If recycle is true, elements is not reset and the tensor will be recycled the next build is called (only if tensorShape is equal and elements is identical).

Implementation

@override
Float32Tensor build({bool recycle = false}) {
  final tensorShape = this.tensorShape;
  var elements = this.elements;
  final recycled = _recycled;
  if (recycled != null &&
      recycled.tensorShape == tensorShape &&
      identical(_recycledElements, elements)) {
    if (!recycle) {
      _tensorShape = TensorShape.scalar;
      _elements = _emptyData;
    }
    return recycled;
  }
  if (!recycle) {
    _tensorShape = TensorShape.scalar;
    _elements = _emptyData;
  }
  final length = tensorShape.numberOfElements;
  if (elements.length != length) {
    elements = elements.sublist(0, length);
  }
  switch (tensorShape.numberOfDimensions) {
    case 1:
      final result = Float32Vector.withFloat32List(elements);
      if (recycle) {
        _recycled = result;
        _recycledElements = elements;
      }
      return result;
    case 2:
      final result = Float32Matrix.withFloat32List(
        elements,
        width: tensorShape.x,
      );
      if (recycle) {
        _recycled = result;
        _recycledElements = elements;
      }
      return result;
    default:
      throw UnimplementedError();
  }
}