//
// MATLAB Compiler: 2.2
// Date: Wed Mar 10 16:44:48 2004
// Arguments: "-B" "macro_default" "-O" "all" "-O" "fold_scalar_mxarrays:on"
// "-O" "fold_non_scalar_mxarrays:on" "-O" "optimize_integer_for_loops:on" "-O"
// "array_indexing:on" "-O" "optimize_conditionals:on" "-p" "-W" "main" "-L"
// "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-B" "sglcpp" "-p" "-W"
// "main" "-L" "Cpp" "-t" "-T" "link:exe" "-h" "libmmfile.mlib" "-W" "mainhg"
// "libmwsglm.mlib" "hier_community.m" "Default.m" "Stop_Simulation.m"
// "Call_SaveLoad_Fig.m" "SaveLoad_Fig.m" "SaveLoad.m" "Load_Population_Data.m"
// "Save_Population_Data.m" "Start_hier_community.m" "MyInline.m" "My_Eval.m"
// "isnumber.m" "Find_Var.m" "Change_Number_of_Populations.m"
// "Switch_Populations.m" "Store_Parameters.m" 
//
#include "Find_Var.hpp"
#include "libmatlbm.hpp"

static mxChar _array1_[134] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'F', 'i', 'n', 'd', '_',
                                'V', 'a', 'r', ' ', 'L', 'i', 'n', 'e', ':',
                                ' ', '1', ' ', 'C', 'o', 'l', 'u', 'm', 'n',
                                ':', ' ', '1', ' ', 'T', 'h', 'e', ' ', 'f',
                                'u', 'n', 'c', 't', 'i', 'o', 'n', ' ', '"',
                                'F', 'i', 'n', 'd', '_', 'V', 'a', 'r', '"',
                                ' ', 'w', 'a', 's', ' ', 'c', 'a', 'l', 'l',
                                'e', 'd', ' ', 'w', 'i', 't', 'h', ' ', 'm',
                                'o', 'r', 'e', ' ', 't', 'h', 'a', 'n', ' ',
                                't', 'h', 'e', ' ', 'd', 'e', 'c', 'l', 'a',
                                'r', 'e', 'd', ' ', 'n', 'u', 'm', 'b', 'e',
                                'r', ' ', 'o', 'f', ' ', 'o', 'u', 't', 'p',
                                'u', 't', 's', ' ', '(', '2', ')', '.' };
static mwArray _mxarray0_ = mclInitializeString(134, _array1_);

static mxChar _array3_[133] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'F', 'i', 'n', 'd', '_',
                                'V', 'a', 'r', ' ', 'L', 'i', 'n', 'e', ':',
                                ' ', '1', ' ', 'C', 'o', 'l', 'u', 'm', 'n',
                                ':', ' ', '1', ' ', 'T', 'h', 'e', ' ', 'f',
                                'u', 'n', 'c', 't', 'i', 'o', 'n', ' ', '"',
                                'F', 'i', 'n', 'd', '_', 'V', 'a', 'r', '"',
                                ' ', 'w', 'a', 's', ' ', 'c', 'a', 'l', 'l',
                                'e', 'd', ' ', 'w', 'i', 't', 'h', ' ', 'm',
                                'o', 'r', 'e', ' ', 't', 'h', 'a', 'n', ' ',
                                't', 'h', 'e', ' ', 'd', 'e', 'c', 'l', 'a',
                                'r', 'e', 'd', ' ', 'n', 'u', 'm', 'b', 'e',
                                'r', ' ', 'o', 'f', ' ', 'i', 'n', 'p', 'u',
                                't', 's', ' ', '(', '2', ')', '.' };
static mwArray _mxarray2_ = mclInitializeString(133, _array3_);
static mwArray _mxarray4_ = mclInitializeDoubleVector(0, 0, (double *)NULL);
static mwArray _mxarray5_ = mclInitializeDouble(1.0);

void InitializeModule_Find_Var() {
}

void TerminateModule_Find_Var() {
}

static mwArray MFind_Var(mwArray * pos,
                         int nargout_,
                         mwArray expr,
                         mwArray var);

_mexLocalFunctionTable _local_function_table_Find_Var
  = { 0, (mexFunctionTableEntry *)NULL };

//
// The function "Find_Var" contains the normal interface for the "Find_Var"
// M-function from file "C:\shu\hier_community_exe\Find_Var.m" (lines 1-45).
// This function processes any input arguments and passes them to the
// implementation version of the function, appearing above.
//
mwArray Find_Var(mwArray * pos, mwArray expr, mwArray var) {
    int nargout(1);
    mwArray var_order(mclGetUninitializedArray());
    mwArray pos__(mclGetUninitializedArray());
    if (pos != NULL) {
        ++nargout;
    }
    var_order = MFind_Var(&pos__, nargout, expr, var);
    if (pos != NULL) {
        *pos = pos__;
    }
    return var_order;
}

//
// The function "mlxFind_Var" contains the feval interface for the "Find_Var"
// M-function from file "C:\shu\hier_community_exe\Find_Var.m" (lines 1-45).
// The feval function calls the implementation version of Find_Var through this
// function. This function processes any input arguments and passes them to the
// implementation version of the function, appearing above.
//
void mlxFind_Var(int nlhs, mxArray * plhs[], int nrhs, mxArray * prhs[]) {
    MW_BEGIN_MLX();
    {
        mwArray mprhs[2];
        mwArray mplhs[2];
        int i;
        mclCppUninitializeArrays(2, mplhs);
        if (nlhs > 2) {
            error(_mxarray0_);
        }
        if (nrhs > 2) {
            error(_mxarray2_);
        }
        for (i = 0; i < 2 && i < nrhs; ++i) {
            mprhs[i] = mwArray(prhs[i], 0);
        }
        for (; i < 2; ++i) {
            mprhs[i].MakeDIN();
        }
        mplhs[0] = MFind_Var(&mplhs[1], nlhs, mprhs[0], mprhs[1]);
        plhs[0] = mplhs[0].FreezeData();
        for (i = 1; i < 2 && i < nlhs; ++i) {
            plhs[i] = mplhs[i].FreezeData();
        }
    }
    MW_END_MLX();
}

//
// The function "MFind_Var" is the implementation version of the "Find_Var"
// M-function from file "C:\shu\hier_community_exe\Find_Var.m" (lines 1-45). It
// contains the actual compiled code for that M-function. It is a static
// function and must only be called from one of the interface functions,
// appearing below.
//
//
// function [var_order,pos] = Find_Var(expr,var)
//
static mwArray MFind_Var(mwArray * pos,
                         int nargout_,
                         mwArray expr,
                         mwArray var) {
    mwLocalFunctionTable save_local_function_table_
      (&_local_function_table_Find_Var);
    mwArray var_order(mclGetUninitializedArray());
    mwArray L(mclGetUninitializedArray());
    mwArray j(mclGetUninitializedArray());
    mwArray i(mclGetUninitializedArray());
    mwArray L_var(mclGetUninitializedArray());
    mwArray L_expr(mclGetUninitializedArray());
    //
    // 
    // pos = [];
    //
    *pos = _mxarray4_;
    //
    // var_order = [];
    //
    var_order = _mxarray4_;
    //
    // 
    // L_expr = length(expr);
    //
    L_expr = mclLengthInt(mwVa(expr, "expr"));
    //
    // L_var = length(var);
    //
    L_var = mclLengthInt(mwVa(var, "var"));
    //
    // 
    // for i = 1:L_expr
    //
    {
        int v_(mclForIntStart(1));
        int e_(mclForIntEnd(mwVv(L_expr, "L_expr")));
        if (v_ > e_) {
            i = _mxarray4_;
        } else {
            //
            // j=1;
            // while(j<=L_var)
            // L = length(var{j})-1;
            // if(i==1 & i+L<L_expr)
            // if(strcmp(expr(i:i+L),var{j}) & ~isletter(expr(i+L+1)))
            // pos = [pos,[i;i+L]];
            // var_order = [var_order,{var{j}}];
            // j = L_expr;
            // i = i+L+1;
            // end
            // elseif(i>1 & i+L<L_expr)
            // if(strcmp(expr(i:i+L),var{j}) & ~isletter(expr(i-1)) & ~isletter(expr(i+L+1)))
            // pos = [pos,[i;i+L]];
            // var_order = [var_order,{var{j}}];
            // j = L_expr;     
            // i = i+L+1;
            // end            
            // elseif(i> 1 & i+L==L_expr)
            // if(strcmp(expr(i:i+L),var{j}) & ~isletter(expr(i-1)))
            // pos = [pos,[i;i+L]];
            // var_order = [var_order,{var{j}}];
            // j = L_expr;     
            // i = i+L+1;
            // end         
            // elseif(i==1 & i+L==L_expr)
            // if(strcmp(expr(i:i+L),var{j}))
            // pos = [pos,[i;i+L]];
            // var_order = [var_order,{var{j}}];
            // j = L_expr;
            // i = i+L+1;
            // end  
            // end
            // j = j+1;
            // end
            // end
            //
            for (; ; ) {
                i = v_;
                j = _mxarray5_;
                while (mclLeBool(mwVv(j, "j"), mwVv(L_var, "L_var"))) {
                    L
                      = mwVe(
                          feval(
                            mwValueVarargout(),
                            mlxLength,
                            mwVarargin(
                              mwVe(mwVsa(var, "var").cell(mwVsv(j, "j"))))))
                        - _mxarray5_;
                    {
                        mwArray a_(mwVv(i, "i") == _mxarray5_);
                        if (tobool(a_)
                            && tobool(
                                 a_
                                 & mwVv(i, "i") + mwVv(L, "L")
                                   < mwVv(L_expr, "L_expr"))) {
                            mwArray a_0
                              (mwVe(
                                 feval(
                                   mwValueVarargout(),
                                   mlxStrcmp,
                                   mwVarargin(
                                     mwVe(
                                       mclArrayRef(
                                         mwVsa(expr, "expr"),
                                         colon(
                                           mwVv(i, "i"),
                                           mwVv(i, "i") + mwVv(L, "L")))),
                                     mwVe(
                                       mwVsa(var, "var").cell(
                                         mwVsv(j, "j")))))));
                            if (tobool(a_0)
                                && tobool(
                                     a_0
                                     & ~ mwVe(
                                           isletter(
                                             mwVe(
                                               mclArrayRef(
                                                 mwVsa(expr, "expr"),
                                                 mwVv(i, "i") + mwVv(L, "L")
                                                 + _mxarray5_)))))) {
                                *pos
                                  = horzcat(
                                      mwVarargin(
                                        mwVv(*pos, "pos"),
                                        vertcat(
                                          mwVarargin(
                                            mwVv(i, "i"),
                                            mwVv(i, "i") + mwVv(L, "L")))));
                                var_order
                                  = horzcat(
                                      mwVarargin(
                                        mwVv(var_order, "var_order"),
                                        cellhcat(
                                          mwVarargin(
                                            mwVe(
                                              mwVsa(var, "var").cell(
                                                mwVsv(j, "j")))))));
                                j = mwVsv(L_expr, "L_expr");
                                i = mwVv(i, "i") + mwVv(L, "L") + _mxarray5_;
                            } else {
                            }
                        } else {
                            mwArray a_1(mwVv(i, "i") > _mxarray5_);
                            if (tobool(a_1)
                                && tobool(
                                     a_1
                                     & mwVv(i, "i") + mwVv(L, "L")
                                       < mwVv(L_expr, "L_expr"))) {
                                mwArray a_2
                                  (mwVe(
                                     feval(
                                       mwValueVarargout(),
                                       mlxStrcmp,
                                       mwVarargin(
                                         mwVe(
                                           mclArrayRef(
                                             mwVsa(expr, "expr"),
                                             colon(
                                               mwVv(i, "i"),
                                               mwVv(i, "i") + mwVv(L, "L")))),
                                         mwVe(
                                           mwVsa(var, "var").cell(
                                             mwVsv(j, "j")))))));
                                if (tobool(a_2)) {
                                    a_2
                                      = a_2
                                        & ~ mwVe(
                                              isletter(
                                                mwVe(
                                                  mclArrayRef(
                                                    mwVsa(expr, "expr"),
                                                    mwVv(i, "i")
                                                    - _mxarray5_))));
                                } else {
                                    a_2 = 0;
                                }
                                if (tobool(a_2)
                                    && tobool(
                                         a_2
                                         & ~ mwVe(
                                               isletter(
                                                 mwVe(
                                                   mclArrayRef(
                                                     mwVsa(expr, "expr"),
                                                     mwVv(i, "i")
                                                     + mwVv(L, "L")
                                                     + _mxarray5_)))))) {
                                    *pos
                                      = horzcat(
                                          mwVarargin(
                                            mwVv(*pos, "pos"),
                                            vertcat(
                                              mwVarargin(
                                                mwVv(i, "i"),
                                                mwVv(i, "i")
                                                + mwVv(L, "L")))));
                                    var_order
                                      = horzcat(
                                          mwVarargin(
                                            mwVv(var_order, "var_order"),
                                            cellhcat(
                                              mwVarargin(
                                                mwVe(
                                                  mwVsa(var, "var").cell(
                                                    mwVsv(j, "j")))))));
                                    j = mwVsv(L_expr, "L_expr");
                                    i
                                      = mwVv(i, "i") + mwVv(L, "L")
                                        + _mxarray5_;
                                } else {
                                }
                            } else {
                                mwArray a_3(mwVv(i, "i") > _mxarray5_);
                                if (tobool(a_3)
                                    && tobool(
                                         a_3
                                         & mwVv(i, "i") + mwVv(L, "L")
                                           == mwVv(L_expr, "L_expr"))) {
                                    mwArray a_4
                                      (mwVe(
                                         feval(
                                           mwValueVarargout(),
                                           mlxStrcmp,
                                           mwVarargin(
                                             mwVe(
                                               mclArrayRef(
                                                 mwVsa(expr, "expr"),
                                                 colon(
                                                   mwVv(i, "i"),
                                                   mwVv(i, "i")
                                                   + mwVv(L, "L")))),
                                             mwVe(
                                               mwVsa(var, "var").cell(
                                                 mwVsv(j, "j")))))));
                                    if (tobool(a_4)
                                        && tobool(
                                             a_4
                                             & ~ mwVe(
                                                   isletter(
                                                     mwVe(
                                                       mclArrayRef(
                                                         mwVsa(expr, "expr"),
                                                         mwVv(i, "i")
                                                         - _mxarray5_)))))) {
                                        *pos
                                          = horzcat(
                                              mwVarargin(
                                                mwVv(*pos, "pos"),
                                                vertcat(
                                                  mwVarargin(
                                                    mwVv(i, "i"),
                                                    mwVv(i, "i")
                                                    + mwVv(L, "L")))));
                                        var_order
                                          = horzcat(
                                              mwVarargin(
                                                mwVv(var_order, "var_order"),
                                                cellhcat(
                                                  mwVarargin(
                                                    mwVe(
                                                      mwVsa(var, "var").cell(
                                                        mwVsv(j, "j")))))));
                                        j = mwVsv(L_expr, "L_expr");
                                        i
                                          = mwVv(i, "i") + mwVv(L, "L")
                                            + _mxarray5_;
                                    } else {
                                    }
                                } else {
                                    mwArray a_5(mwVv(i, "i") == _mxarray5_);
                                    if (tobool(a_5)
                                        && tobool(
                                             a_5
                                             & mwVv(i, "i") + mwVv(L, "L")
                                               == mwVv(L_expr, "L_expr"))) {
                                        if (tobool(
                                              mwVe(
                                                feval(
                                                  mwValueVarargout(),
                                                  mlxStrcmp,
                                                  mwVarargin(
                                                    mwVe(
                                                      mclArrayRef(
                                                        mwVsa(expr, "expr"),
                                                        colon(
                                                          mwVv(i, "i"),
                                                          mwVv(i, "i")
                                                          + mwVv(L, "L")))),
                                                    mwVe(
                                                      mwVsa(var, "var").cell(
                                                        mwVsv(j, "j")))))))) {
                                            *pos
                                              = horzcat(
                                                  mwVarargin(
                                                    mwVv(*pos, "pos"),
                                                    vertcat(
                                                      mwVarargin(
                                                        mwVv(i, "i"),
                                                        mwVv(i, "i")
                                                        + mwVv(L, "L")))));
                                            var_order
                                              = horzcat(
                                                  mwVarargin(
                                                    mwVv(
                                                      var_order, "var_order"),
                                                    cellhcat(
                                                      mwVarargin(
                                                        mwVe(
                                                          mwVsa(
                                                            var, "var").cell(
                                                            mwVsv(
                                                              j, "j")))))));
                                            j = mwVsv(L_expr, "L_expr");
                                            i
                                              = mwVv(i, "i") + mwVv(L, "L")
                                                + _mxarray5_;
                                        }
                                    } else {
                                    }
                                }
                            }
                        }
                    }
                    j = mwVv(j, "j") + _mxarray5_;
                }
                if (v_ == e_) {
                    break;
                }
                ++v_;
            }
        }
    }
    mwValidateOutput(var_order, 1, nargout_, "var_order", "Find_Var");
    mwValidateOutput(*pos, 2, nargout_, "pos", "Find_Var");
    return var_order;
}
