function tf = isValidIndexVector(idx)
% Check whether an input index is a valid type and has valid values. True
% if OK to use an index. False otherwise.
    
%   Copyright 2017-2019 The MathWorks, Inc.

if parallel.internal.indexing.isColonLiteral(idx)
    % Colon literal is always OK.
    tf = true;
elseif parallel.internal.indexing.isColonDescriptor(idx)
    % ColonDescriptor doesn't allow inf or nan end-points so we
    % just need to check for integer, positive values.
    tf = ~idx.hasNonIntegerIndex & iGetMinIndex(idx)>0;
elseif isnumeric(idx)
    % Indices could be in any order, so need to check every element.
    tf = isreal(idx) && all(isfinite(idx) & (idx == floor(idx)) & (idx>0), "all");
elseif ischar(idx)
    % Guaranteed integer, finite, but need to ensure positive.
    tf = all(idx>0, "all");
elseif islogical(idx)
    % Guaranteed integer, finite, and positive.
    tf = true;
else
    % Unsupported indices.
    tf = false;
end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function mx = iGetMinIndex(idx)
% Get the lowest index value from an index argument from a ColonDescriptor.
if idx.Stride>=0
    mx = idx.Start;
else
    lastElement = prod(size(idx)); %#ok<PSIZE> Seems to be only way to get length of ColonDescriptor
    mx = idx(lastElement);
end
end
