function [LP, codistr] = hCatImpl(obj, catdim, codistrs, LPs, templ, targetSize)
; %#ok<NOSEM> % Undocumented
%   Implementation of hCatImpl for codistributor2dbc.  

%   Copyright 2009-2022 The MathWorks, Inc.
    
% Since hCatCheck has returned true, we can trivially concatenate the local
% parts to form the resulting array.  
% First, convert the replicated arrays into the corresponding local parts.
isReplicated = cellfun(@isempty, codistrs); 
LPs(isReplicated) = cellfun(@(lp) iConvertToLocalPart(obj, lp), ...
                            LPs(isReplicated), ...
                            'UniformOutput', false);

codistr = codistributor2dbc(obj.WorkerGrid, obj.BlockSize, obj.Orientation, targetSize);
LP = distributedutil.Allocator.create(codistr.hLocalSize(), templ);
LP(:) = cat(catdim, LPs{:});
if issparse(templ) && ~issparse(LP)
    LP = sparse(LP);
end
end % End of hCatImpl.

function LP = iConvertToLocalPart(obj, X)
    codistr =  codistributor2dbc(obj.WorkerGrid, obj.BlockSize, obj.Orientation, size(X));
    srcWorker = 0;
    LP = codistr.hBuildFromReplicatedImpl(srcWorker, X);
end % End of iConvertToLocalPart.
