function [e,f] = partitionIndices(part,workerIdx)
%PARTITIONINDICES   Local span of a partition
%   The local span of a partition is the index range in the distributed
%   dimension for the associated codistributed array on a particular worker.
%
%   With one input argument and one output argument
%      K = PARTITIONINDICES(PART)
%   returns a vector K so that LOCALPART(D) = D(...,K,...) on the current
%   worker, where PART = a nonnegative vector of length spmdSize.
%
%   With one input argument and two output arguments
%      [E,F] = PARTITIONINDICES(PART)
%   returns two integers E and F so that LOCALPART(D) = D(...,E:F,...) on the
%   current worker, where PART = a nonnegative vector of length spmdSize.
%
%   With two input arguments and one output argument
%      K = PARTITIONINDICES(PART,WORKERIDX)
%   returns a vector K so that LOCALPART(D) = D(...,K,...) on the specified
%   worker, where PART = a nonnegative vector of length spmdSize.
%
%   With two input arguments and two output arguments
%      [E,F] = PARTITIONINDICES(PART,WORKERIDX)
%   returns two integers E and F so that getLocalPart(D) = D(...,E:F,...) on the
%   specified worker, where PART = a nonnegative vector of length spmdSize.
%
%   In all of the above syntaxes, if the partition is unspecified, then K,
%   E and F are -1.
%
%   Example: with spmdSize = 4
%      part = [6 6 5 5]
%      On worker 1, K = partitionIndices(part) returns K = 1:6.
%      On worker 2, [E,F] = partitionIndices(part) returns E = 7, F = 12.
%      K = partitionIndices(part,3) returns K = 13:17.
%      [E,F] = partitionIndices(part,4) returns E = 18, F = 22.

%   Copyright 2007-2022 The MathWorks, Inc.

if nargin < 2
   workerIdx = spmdIndex;
end
if isscalar(part) && part==-1
    e = -1;
    f = -1;
    return;
end
if workerIdx < 1
   f = 0;
   e = 1;
elseif workerIdx > spmdSize
   f = sum(part);
   e = f+1;
else
   f = sum(part(1:workerIdx));
   e = f-part(workerIdx)+1;
end
if nargout < 2
   e = e:f;
end
