//
// 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 "MyInline.hpp"
#include "libmatlbm.hpp"

static mxChar _array1_[134] = { 'R', 'u', 'n', '-', 't', 'i', 'm', 'e', ' ',
                                'E', 'r', 'r', 'o', 'r', ':', ' ', 'F', 'i',
                                'l', 'e', ':', ' ', 'M', 'y', 'I', 'n', 'l',
                                'i', 'n', 'e', ' ', 'L', 'i', 'n', 'e', ':',
                                ' ', '1', ' ', 'C', 'o', 'l', 'u', 'm', 'n',
                                ':', ' ', '1', ' ', 'T', 'h', 'e', ' ', 'f',
                                'u', 'n', 'c', 't', 'i', 'o', 'n', ' ', '"',
                                'M', 'y', 'I', 'n', 'l', 'i', 'n', 'e', '"',
                                ' ', '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', ' ', '(', '1', ')', '.' };
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', ':', ' ', 'M', 'y', 'I', 'n', 'l',
                                'i', 'n', 'e', ' ', 'L', 'i', 'n', 'e', ':',
                                ' ', '1', ' ', 'C', 'o', 'l', 'u', 'm', 'n',
                                ':', ' ', '1', ' ', 'T', 'h', 'e', ' ', 'f',
                                'u', 'n', 'c', 't', 'i', 'o', 'n', ' ', '"',
                                'M', 'y', 'I', 'n', 'l', 'i', 'n', 'e', '"',
                                ' ', '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', ' ', '(', '5', ')', '.' };
static mwArray _mxarray2_ = mclInitializeString(133, _array3_);
static mwArray _mxarray4_ = mclInitializeDouble(1.0);
static mwArray _mxarray5_ = mclInitializeDouble(-1.0);
static mwArray _mxarray6_ = mclInitializeDouble(2.0);

void InitializeModule_MyInline() {
}

void TerminateModule_MyInline() {
}

static mwArray MMyInline(int nargout_,
                         mwArray expr,
                         mwArray var,
                         mwArray value,
                         mwArray var_order,
                         mwArray pos);

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

//
// The function "MyInline" contains the normal interface for the "MyInline"
// M-function from file "C:\shu\hier_community_exe\MyInline.m" (lines 1-25).
// This function processes any input arguments and passes them to the
// implementation version of the function, appearing above.
//
mwArray MyInline(mwArray expr,
                 mwArray var,
                 mwArray value,
                 mwArray var_order,
                 mwArray pos) {
    int nargout(1);
    mwArray answer(mclGetUninitializedArray());
    answer = MMyInline(nargout, expr, var, value, var_order, pos);
    return answer;
}

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

//
// The function "MMyInline" is the implementation version of the "MyInline"
// M-function from file "C:\shu\hier_community_exe\MyInline.m" (lines 1-25). 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 answer = MyInline(expr,var,value,var_order,pos)
//
static mwArray MMyInline(int nargout_,
                         mwArray expr,
                         mwArray var,
                         mwArray value,
                         mwArray var_order,
                         mwArray pos) {
    mwLocalFunctionTable save_local_function_table_
      (&_local_function_table_MyInline);
    mwArray answer(mclGetUninitializedArray());
    mwArray p(mclGetUninitializedArray());
    mwArray i(mclGetUninitializedArray());
    mwArray L_expr(mclGetUninitializedArray());
    mwArray L_pos(mclGetUninitializedArray());
    //
    // 
    // if(~isempty(var_order))
    //
    if (mclNotBool(mwVe(isempty(mwVa(var_order, "var_order"))))) {
        //
        // 
        // L_pos = length(pos(1,:));
        //
        L_pos
          = mclLengthInt(
              mwVe(mclArrayRef(mwVsa(pos, "pos"), _mxarray4_, colon())));
        //
        // L_expr = length(expr);
        //
        L_expr = mclLengthInt(mwVa(expr, "expr"));
        //
        // 
        // for i = L_pos:-1:1
        //
        {
            mwForLoopIterator viter__;
            for (viter__.Start(mwVv(L_pos, "L_pos"), _mxarray5_, _mxarray4_);
                 viter__.Next(&i);
                 ) {
                //
                // p = find(strcmp(var_order{i},var));
                //
                p
                  = find(
                      mwVe(
                        feval(
                          mwValueVarargout(),
                          mlxStrcmp,
                          mwVarargin(
                            mwVe(
                              mwVsa(var_order, "var_order").cell(
                                mwVsv(i, "i"))),
                            mwVa(var, "var")))));
                //
                // pos(:,i) = pos(:,i);
                //
                mclArrayAssign(
                  &pos,
                  mclArrayRef(mwVsa(pos, "pos"), colon(), mwVsv(i, "i")),
                  colon(),
                  mwVsv(i, "i"));
                //
                // if(pos(1,i)==1 & pos(2,i)<L_expr)
                //
                {
                    mwArray a_
                      (mwVe(
                         mclArrayRef(
                           mwVsa(pos, "pos"), _mxarray4_, mwVsv(i, "i")))
                       == _mxarray4_);
                    if (tobool(a_)
                        && tobool(
                             a_
                             & mwVe(
                                 mclArrayRef(
                                   mwVsa(pos, "pos"),
                                   _mxarray6_,
                                   mwVsv(i, "i")))
                               < mwVv(L_expr, "L_expr"))) {
                        //
                        // expr = [value{p},expr(pos(2,i)+1:end)];
                        //
                        expr
                          = horzcat(
                              mwVarargin(
                                mwVe(
                                  mwVsa(value, "value").cell(mwVsv(p, "p"))),
                                mwVe(
                                  mclArrayRef(
                                    mwVsa(expr, "expr"),
                                    colon(
                                      mwVe(
                                        mclArrayRef(
                                          mwVsa(pos, "pos"),
                                          _mxarray6_,
                                          mwVsv(i, "i")))
                                      + _mxarray4_,
                                      end(
                                        mwVa(expr, "expr"),
                                        _mxarray4_,
                                        _mxarray4_))))));
                    //
                    // elseif(pos(1,i)>1 & pos(2,i)<L_expr)
                    //
                    } else {
                        mwArray a_0
                          (mwVe(
                             mclArrayRef(
                               mwVsa(pos, "pos"), _mxarray4_, mwVsv(i, "i")))
                           > _mxarray4_);
                        if (tobool(a_0)
                            && tobool(
                                 a_0
                                 & mwVe(
                                     mclArrayRef(
                                       mwVsa(pos, "pos"),
                                       _mxarray6_,
                                       mwVsv(i, "i")))
                                   < mwVv(L_expr, "L_expr"))) {
                            //
                            // expr = [expr(1:pos(1,i)-1),value{p},expr(pos(2,i)+1:end)];
                            //
                            expr
                              = horzcat(
                                  mwVarargin(
                                    mwVe(
                                      mclArrayRef(
                                        mwVsa(expr, "expr"),
                                        colon(
                                          _mxarray4_,
                                          mwVe(
                                            mclArrayRef(
                                              mwVsa(pos, "pos"),
                                              _mxarray4_,
                                              mwVsv(i, "i")))
                                          - _mxarray4_))),
                                    mwVe(
                                      mwVsa(value, "value").cell(
                                        mwVsv(p, "p"))),
                                    mwVe(
                                      mclArrayRef(
                                        mwVsa(expr, "expr"),
                                        colon(
                                          mwVe(
                                            mclArrayRef(
                                              mwVsa(pos, "pos"),
                                              _mxarray6_,
                                              mwVsv(i, "i")))
                                          + _mxarray4_,
                                          end(
                                            mwVa(expr, "expr"),
                                            _mxarray4_,
                                            _mxarray4_))))));
                        //
                        // elseif(pos(1,i)>1 & pos(2,i)==L_expr)
                        //
                        } else {
                            mwArray a_1
                              (mwVe(
                                 mclArrayRef(
                                   mwVsa(pos, "pos"),
                                   _mxarray4_,
                                   mwVsv(i, "i")))
                               > _mxarray4_);
                            if (tobool(a_1)
                                && tobool(
                                     a_1
                                     & mwVe(
                                         mclArrayRef(
                                           mwVsa(pos, "pos"),
                                           _mxarray6_,
                                           mwVsv(i, "i")))
                                       == mwVv(L_expr, "L_expr"))) {
                                //
                                // expr = [expr(1:pos(1,i)-1),value{p}];
                                //
                                expr
                                  = horzcat(
                                      mwVarargin(
                                        mwVe(
                                          mclArrayRef(
                                            mwVsa(expr, "expr"),
                                            colon(
                                              _mxarray4_,
                                              mwVe(
                                                mclArrayRef(
                                                  mwVsa(pos, "pos"),
                                                  _mxarray4_,
                                                  mwVsv(i, "i")))
                                              - _mxarray4_))),
                                        mwVe(
                                          mwVsa(value, "value").cell(
                                            mwVsv(p, "p")))));
                            //
                            // else
                            //
                            } else {
                                //
                                // expr = value{p};
                                //
                                expr
                                  = mwVsa(value, "value").cell(mwVsv(p, "p"));
                            }
                        }
                    }
                //
                // end
                //
                }
            //
            // 
            // end
            //
            }
        }
    //
    // 
    // end
    //
    }
    //
    // 
    // answer = eval(expr);
    //
    answer = eval(mwValueVarargout(), mwVa(expr, "expr"));
    mwValidateOutput(answer, 1, nargout_, "answer", "MyInline");
    return answer;
}
