int main()
{
	part0();
	part1();
	part2();
	part3();
	part4();
	part5();
	part6();	
}
/* Gateway of minkmex */
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[]) {
    
    mwSignedIndex l, i, kout, nelem, p0, nz, nansplit, npos;
    mwSize columns, rows;
    int sparseflag; /* sparse */
    
    /* Check arguments */
    if (nrhs<2)
        mexErrMsgTxt("MINKMEX: Two input arguments required.");
    
    if (!mxIsNumeric(prhs[0]))
        mexErrMsgTxt("MINKMEX: First input LIST argument must be numeric.");
    
    sparseflag = mxIsSparse(prhs[0]); /* sparse */
    
    if (!mxIsNumeric(prhs[1]))                              
        mexErrMsgTxt("MINKMEX: Second input K must be numeric.");

    rows =  mxGetM(prhs[0]);
    columns = mxGetN(prhs[0]);
    nelem = rows*columns;
    /* Get the number of elements of the list of subindexes */
    if (sparseflag)
    {
        /* Search for the number of non-zero in sparse */      
        l = *(mxGetJc(prhs[0]) + columns);
    }
    else if (mxGetNumberOfDimensions(prhs[0])==2) /* Check for array */
        l = nelem;
    else
        mexErrMsgTxt("MINKMEX: First input LIST must be a 2D array.");
    
    if (mxGetClassID(prhs[0]) != mxDOUBLE_CLASS)
        mexErrMsgTxt("MINKMEX: First input LIST must be a double.");
    
    /* Get the number of elements of the list of subindexes */
    if (mxGetM(prhs[1])!=1 || mxGetN(prhs[1])!=1)
        mexErrMsgTxt("MINKMEX: Second input K must be a scalar.");
    
    if (mxGetClassID(prhs[1]) != mxDOUBLE_CLASS)
        mexErrMsgTxt("MINKMEX: Second input K must be a double.");
    
    kout = k = (int)(*mxGetPr(prhs[1]));
    if (k<0)
        mexErrMsgTxt("MINKMEX: K must be non-negative integer.");
    
    /* Get a data pointer */
    list = mxGetPr(prhs[0]);

    /* Clip k */
    if (k>l) k=l;
    
    /* Clip kout */
    if (kout>nelem) kout=nelem;

    /* Clean programming */
    pos=NULL;
    
    /* Work for non-empty array */
    if (l>0) {
         /* Vector of index */
        pos = mxMalloc(sizeof(mwSize)*l);
        if (pos==NULL)
            mexErrMsgTxt("Out of memory.");
        /* Initialize the array of position (zero-based index) */
        for (i=0; i<l; i++) pos[i]=i;
        
        /* Call the recursive engine */
        k--; /* because we work on zero-based */
        nansplit = partNaN(0, l-1); /* Push NaN at the end */
        if (k<nansplit && nansplit>=0)
            findFirstK(0, nansplit);
        
        /* Look for the split of positive/negative numbers */
        if (sparseflag) {
            p0 = part0(0, k); /* number of strict negative elements */
            if (p0 < k) /* There are at least two positive elements */
            {
                /* Number of implicite zeros */
                nz = nelem-l;
                if (nz) /* in case the positive set is unordered */
                {
                    k -= nz;
                    findFirstK(p0, nansplit);
                    k += nz;
                }
            }
            /* ++ to restore one-based Matlab index */
            k++; 
        }
        else
            /* ++ to Restore one-based Matlab index */
            p0 = ++k;
    } /* if (l>0) */
    else p0 = 0;
    
    /* Number of implicite zero in (sparse) */
    nz = nelem-l;
    /* Create the Matrix result (first output) */
    plhs[0] = MinMaxResult(k, p0, nz, kout);
    
     /* Create the Matrix position (second output) */
    if (nlhs>=2)
    {
        if (sparseflag)
            SpLocResult(k, p0, nz, kout, prhs[0], &(plhs[1]), &(plhs[2]));
        else
            plhs[1] = LocResult(k, p0, nz, kout);
    }
    
    /* Free the array of position */
    if (pos) mxFree(pos);
    pos = NULL; /* clean programming */
    
    return;

} /* Gateway of minkmex.c */