function [fillPattern, numNonZero] = getFillPattern(A)
% Common implementation to get the fill pattern from A. Can optionally
% return the number of non-zeros too since we have to calculate that along
% the way! Discovered values are cached to avoid recalculation.

%   Copyright 2016-2019 The MathWorks, Inc.

if A.Metadata.FillPattern.isKnown()
    % Get the answer from metadata
    fillPattern = A.Metadata.FillPattern.get();
    numNonZero = A.Metadata.Nnz.get();
else
    % Calculate the fill pattern, check for NaNs or Infs, and NNZ
    codistr = getCodistributor(A);
    LP = getLocalPart(A);
    [fillPattern, bands, hasNaN, hasInf, numNonZero] = codistr.hGetFillPatternNaNInfImpl(LP);
    % store back into metadata so we don't have to calculate it again
    A.Metadata.FillPattern.set(fillPattern);
    A.Metadata.Bandwidth.set(bands);
    A.Metadata.HasNaN.set(hasNaN);
    A.Metadata.HasInf.set(hasInf);
    A.Metadata.Nnz.set(numNonZero);
end

end

