function localInd = hGlobalToLocalIndices(codistr, globalInd, dim, workerIdx)
    ; %#ok<NOSEM> % Undocumented

    %   Implementation of hGlobalToLocalIndices for codistributor1d.
    % The result is always a numeric vector of local indices, but the input
    % globalIndices can be:
    % * literal colon (':')
    % * matlab.internal.ColonDescriptor
    % * logical vector
    % * numeric vector

    %   Copyright 2011-2022 The MathWorks, Inc.

    if nargin < 4
        workerIdx = spmdIndex;
    elseif ~distributedutil.CodistParser.isValidSPMDIndex(workerIdx)
        error(message('parallel:codistributors:InvalidSpmdIndex'));
    end

    if ~isPositiveIntegerValuedNumeric(dim) || ...
            dim > length(codistr.Cached.GlobalSize)
        error(message('parallel:codistributors:InvalidDimInput'));
    end

    if ~parallel.internal.indexing.isValidIndexVector(globalInd)
        error(message('parallel:codistributors:InvalidGlobalIndex'));
    end

    % Get the partition limits
    [minInd, maxInd] = codistr.hGlobalIndicesImpl(dim, workerIdx);

    % Handle each type of index
    if parallel.internal.indexing.isColonLiteral(globalInd)
        localInd = 1:(maxInd-minInd+1);
        
    elseif isa(globalInd, "matlab.internal.ColonDescriptor")
        localInd = parallel.internal.indexing.limitColonDescriptorToRange(globalInd, minInd, maxInd);
        localInd = localInd(:) - minInd + 1;
        
    elseif islogical(globalInd)
        % Keep the portion of the loigcal array for this partition
        localInd = find(globalInd(minInd:maxInd));
        
    else
        % Numeric
        if  ~isPositiveIntegerValuedNumeric(globalInd) || ...
                any(globalInd > codistr.Cached.GlobalSize(dim) | globalInd < 1, "all")
            error(message('parallel:codistributors:InvalidGlobalIndex'));
        end

        localInd = globalInd - minInd + 1;
        indicesNotOwnedByThisWorker = globalInd < minInd | globalInd > maxInd;
        % 0 is chosen to indicate non-local indices because 0 and abs(0) are
        % always invalid indices.
        localInd(indicesNotOwnedByThisWorker) = 0;
        % Make sure the result is a vector
        localInd = localInd(:);
    end
end
