function c = colon(a,d,b)
%:  Colon.
%   C = COLON(A,B) or equivalently C = A:B
%   C = COLON(A,D,B) or equivalently C = A:D:B
%   
%   All inputs must be scalar. The output C is unformatted.
%
%   See also COLON, DLARRAY.

%   Copyright 2019 The MathWorks, Inc.

% Note: Standard colon allows non-scalar inputs (element x(1) is used) and
% even empty inputs (return 1x0). These are not supported for dlarray for
% the moment.

if nargin == 2
    b = d;
    d = 1;
end

if ~isscalar(a) || ~isscalar(d) || ~isscalar(b)
    error(message('deep:colon:NonScalarInput'));
end

% Note: If a and b are not dlarray, c = d, otherwise, if a
% is not dlarray, c = b, otherwise, c = a.
% c and fdc are always initialized, because at least one of
% a, b, d is a dlarray. No properties of either object are used, so it
% doesn't really matter except that we need to call insertData on something.

if isa(d, 'dlarray')
    [fdc, ddata] = extractData(d.FormattedData);
    c = d;
else
    deep.internal.dlarray.validateNonDlarray(d);
    ddata = d;
end

if isa(b, 'dlarray')
    [fdc, bdata] = extractData(b.FormattedData);
    c = b;
else
    deep.internal.dlarray.validateNonDlarray(b);
    bdata = b;
end

if isa(a, 'dlarray')
    [fdc, adata] = extractData(a.FormattedData);
    c = a;
else
    deep.internal.dlarray.validateNonDlarray(a);
    adata = a;
end

% Do not pass along internal permutations or dimension labels
c.DataToViewMap = [];
c.ViewToDataMap = [];
fdc.DimensionMetadata = '';

% Compute a:b or a:d:b on the underlying data
if nargin == 2
    cdata = adata:bdata;
else
    cdata = adata:ddata:bdata;
end

c.FormattedData = insertData(fdc, cdata);

end
