function [LPA, codistr] = hPermuteImpl(codistr, LPA, order)
% hPermuteImpl: implementation of permute for arrays codistributed 1d

% Copyright 2012 The MathWorks, Inc.

% Get new codistributor
    lenOrder = length(order);
    szA = codistr.Cached.GlobalSize;
    % Size of A needs to be as long as order for constructing new codistr
    paddedSize = [szA ones(1, lenOrder - length(szA))];

    % Find new distribution dimension
    % order is extended if old distribution dimension is a higher dimension
    generalOrder = [order (lenOrder+1):codistr.Dimension];
    % use inverse permutation to find new dimension that corresponds to old
    inverseOrder(generalOrder) = 1:length(generalOrder);
    newDim = inverseOrder(codistr.Dimension);
    codistr = codistributor1d(newDim, codistr.Partition, paddedSize(order));

    % Permute local part or create new if empty
    if ~isempty(LPA)
        LPA = permute(LPA, order);
    else
        LPA = distributedutil.Allocator.create(codistr.hLocalSize(), LPA);
    end
end
