function X = hGatherImpl(codistr, LP, destWorker)
; %#ok<NOSEM> % Undocumented
% Implementation of hGatherImpl for codistributor1d.

%   Copyright 2009-2022 The MathWorks, Inc.

if destWorker == 0
    % Gather into a replicated result.
    X = spmdCat(LP, codistr.Dimension);
else
    % Gather to a single worker. If the target worker already has all of the data
    % then just return the local part.
    otherParts = codistr.Partition(setdiff(1:spmdSize, destWorker));
    if all(otherParts == 0)
        % No communication required. Result is just local part on
        % destWorker and [] on all others.
        if spmdIndex == destWorker
            X = LP;
        else
            X = [];
        end
    else
        % Communicate all local parts to the target worker. Other workers get [].
        X = spmdCat(LP, codistr.Dimension, destWorker);
    end
end
