function a = ipermute(b,order)
%MATLAB Code Generation Library Function
%
% Limitations:
% 
%   1. Cell input for first argument is not supported

%   Copyright 1984-2019 The MathWorks, Inc.
%#codegen

coder.internal.allowEnumInputs;
coder.internal.allowHalfInputs;
eml_invariant(nargin == 2, ...
    'MATLAB:minrhs');
eml_invariant(~iscell(b), ...
    'Coder:toolbox:CellArraysNotSupported', mfilename);
eml_invariant(~coder.isenum(order), ...
    'Coder:toolbox:ipermute_2', ...
    'IfNotConst','Fail');
coder.internal.prefer_const(order);
eml_invariant(eml_is_const(order) || eml_option('VariableSizing'), ...
    'Coder:toolbox:ipermute_3', ...
    'IfNotConst','Fail');
if ~isreal(order) || ~isvector(order)
    a = ipermute(b,real(order(:)));
    return
end
eml_invariant(numel(order) >= ndims(b), ...
    'Coder:toolbox:ipermute_orderNeedsNElements');
eml_invariant(eml_is_permutation(real(order)), ...
    'Coder:toolbox:ipermute_invalidPermutation');
a = permute(b,invperm(real(order)));

%--------------------------------------------------------------------------

function q = invperm(p)
% Function to invert permutation
% p(p) = 1:numel(p);
q = zeros(1,numel(p),coder.internal.indexIntClass);
for k = 1:numel(p)
    q(p(k)) = k;
end

%--------------------------------------------------------------------------
