function LP = triLowerUpperImpl(triFun, codistr, LP, k)
%triLowerUpperImpl  Common implementation of  TRIU and TRIL for Codistributor1d.

%   Copyright 2020-2022 The MathWorks, Inc.

if isempty(LP)  % required to handle 1d codistributors partitioned with dim > 2
    % Ensure empties are always returned as real
    if ~isreal(LP)
        LP = real(LP);
    end
else
    offsets = [0 cumsum(codistr.Partition)];
    if codistr.Dimension == 1
        % Shift the columns: positive offset.
        LP = triFun(LP, offsets(spmdIndex) + k);
    else % codistr.Dimension == 2
        % Shift the rows: negative offset.
        LP = triFun(LP, -offsets(spmdIndex) + k);
    end
end
