function mustBeMember(A,B)
%MATLAB Code Generation Library Function
%
%   Limitations:
%   * Uses a simple O(numel(A)*numel(B)) algorithm.
%
%   Copyright 2016-2017 The MathWorks, Inc.
%#codegen

coder.internal.allowEnumInputs;
coder.internal.prefer_const(A,B);
if ~allMembers(A,B)
    if coder.internal.isConst(A) && coder.internal.isConst(B)
        % Run the check again in MATLAB to issue a compile-time error with
        % MATLAB's full error message.
        coder.const(@feval,'mustBeMember',A,B);
    elseif coder.internal.get_eml_option('MXArrayCodeGen')
        % Run the check again in MATLAB to issue a run-time error with
        % MATLAB's full error message.
        feval('mustBeMember',A,B);
    else
        % Issue a simplified run-time error (no listing of set members).
        coder.internal.error('Coder:toolbox:mustBeMember');
    end
end

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

function p = allMembers(A,B)

coder.internal.prefer_const(A,B);
SHORTCIRC = ~coder.target('HDL');
TEXTA = coder.internal.isTextRow(A);
TEXTB = coder.internal.isTextRow(B);
if isenum(A) && TEXTB
    % In this case we do not want to treat B as an array.
    % Example: mustBeMember(myEnum.name1,'name1')
    p = coder.internal.enumEq(A,B);
elseif TEXTA && ~TEXTB
    % In this case we do not want to treat A as an array.
    % Example: mustBeMember('abc',{'abc','def'})
    idx = coder.internal.findInArray(A,B);
    p = idx >= 1;
else
    p = true;
    na = coder.internal.indexInt(numel(A));
    coder.unroll(iscell(A) && ~coder.internal.isHomogeneousCell(A));
    for k = 1:na
        idx = coder.internal.findInArray(coder.internal.getElement(A,k),B);
        if idx == 0
            p = false;
            if SHORTCIRC
                break
            end
        end
    end
end

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