@Override
    public Tensor decode(Optional<TensorType> optionalType, GrowableByteBuffer buffer) {
        TensorType type;
        DimensionSizes sizes;
        if (optionalType.isPresent()) {
            type = optionalType.get();
            if (type.valueType() != this.serializationValueType) {
                throw new IllegalArgumentException("Tensor value type mismatch. Value type " + type.valueType() +
                                                   " is not " + this.serializationValueType);
            }
            TensorType serializedType = decodeType(buffer);
            if ( ! serializedType.isAssignableTo(type))
                throw new IllegalArgumentException("Type/instance mismatch: A tensor of type " + serializedType +
                                                   " cannot be assigned to type " + type);
            sizes = sizesFromType(serializedType);
        }
        else {
            type = decodeType(buffer);
            sizes = sizesFromType(type);
        }
        Tensor.Builder builder = Tensor.Builder.of(type, sizes);
        decodeCells(sizes, buffer, (IndexedTensor.BoundBuilder)builder);
        return builder.build();
    }
Implementation of a dense binary format for a tensor on the form:

Sorted dimensions = num_dimensions [dimension_str_len dimension_str_bytes dimension_size_int]*
Cell_values = [double, double, double, ...]*
where values are encoded in order of increasing indexes in each dimension, increasing
indexes of later dimensions in the dimension type before earlier.

@author bratseth
vespajlib/src/main/java/com/yahoo/tensor/serialization/DenseBinaryFormat.java