src/plugin/qsort_utils.c
int sf_numsetup()
{
    ST_retcode rc ;
    ST_double  z ;
    int i, k;

    int kvars_sort_num = sf_get_vector_length("c_qsort_sortmiss");
    if ( kvars_sort_num < 0 ) return (198);

    double mins[kvars_sort_num], maxs[kvars_sort_num], miss[kvars_sort_num];
    int nonmiss[kvars_sort_num];
    size_t in1    = SF_in1();
    size_t in2    = SF_in2();
    size_t N      = in2 - in1 + 1;
    size_t start  = 0;

    for (k = 0; k < kvars_sort_num; k++)
        mins[k] = maxs[k] = miss[k] = nonmiss[k] = 0;

    do {
        for (k = 0; k < kvars_sort_num; k++) {
            if ( (rc = SF_vdata(k + 1, start + in1, &z)) ) return(rc);
            if ( SF_is_missing(z) ) {
                miss[k] = 1;
            }
            else {
                if ( nonmiss[k] == 0 ) {
                    nonmiss[k] = 1;
                    mins[k]    = z;
                    maxs[k]    = z;
                }
            }
        }
        ++start;
    } while ( (mf_sum_signed(nonmiss, kvars_sort_num) < kvars_sort_num) & (start < N) );

    for (i = 0; i < N; i++) {
        for (k = 0; k < kvars_sort_num; k++) {
            if ( (rc = SF_vdata(k + 1, i + in1, &z)) ) return(rc);
            if ( SF_is_missing(z) ) {
                miss[k] = 1;
            }
            else {
                if (mins[k] > z) mins[k] = z;
                if (maxs[k] < z) maxs[k] = z;
            }
        }
    }

    for (k = 0; k < kvars_sort_num; k++)
        sf_printf ("%.4f \t %.4f \t %.4f \n", mins[k], maxs[k], miss[k]);

    for (k = 0; k < kvars_sort_num; k++) {
        if ( (rc = SF_mat_store("c_qsort_sortmiss", 1, k + 1, miss[k])) ) return(rc);
        if ( (rc = SF_mat_store("c_qsort_sortmin",  1, k + 1, mins[k])) ) return(rc);
        if ( (rc = SF_mat_store("c_qsort_sortmax",  1, k + 1, maxs[k])) ) return(rc);
    }

    return(0);
}
/**
 * @brief Min, max, and missing count for numeric sort vars
 *
 * @return Computes min, max, and missing count in Stata
 */
@brief Min, max, and missing count for numeric sort vars
@return Computes min, max, and missing count in Stata
@brief Min max and missing count for numeric sort vars @return Computes min max and missing count in Stata
int sf_numsetup()
{
    ST_retcode rc ;
    ST_double  z ;
    int i, k;
    int kvars_sort_num = sf_get_vector_length("c_qsort_sortmiss");
    if ( kvars_sort_num < 0 ) return (198);
    double mins[kvars_sort_num], maxs[kvars_sort_num], miss[kvars_sort_num];
    int nonmiss[kvars_sort_num];
    size_t in1    = SF_in1();
    size_t in2    = SF_in2();
    size_t N      = in2 - in1 + 1;
    size_t start  = 0;
    for (k = 0; k < kvars_sort_num; k++)
        mins[k] = maxs[k] = miss[k] = nonmiss[k] = 0;
    do {
        for (k = 0; k < kvars_sort_num; k++) {
            if ( (rc = SF_vdata(k + 1, start + in1, &z)) ) return(rc);
            if ( SF_is_missing(z) ) {
                miss[k] = 1;
            }
            else {
                if ( nonmiss[k] == 0 ) {
                    nonmiss[k] = 1;
                    mins[k]    = z;
                    maxs[k]    = z;
                }
            }
        }
        ++start;
    } while ( (mf_sum_signed(nonmiss, kvars_sort_num) < kvars_sort_num) & (start < N) );
    for (i = 0; i < N; i++) {
        for (k = 0; k < kvars_sort_num; k++) {
            if ( (rc = SF_vdata(k + 1, i + in1, &z)) ) return(rc);
            if ( SF_is_missing(z) ) {
                miss[k] = 1;
            }
            else {
                if (mins[k] > z) mins[k] = z;
                if (maxs[k] < z) maxs[k] = z;
            }
        }
    }
    for (k = 0; k < kvars_sort_num; k++)
        sf_printf ("%.4f \t %.4f \t %.4f \n", mins[k], maxs[k], miss[k]);
    for (k = 0; k < kvars_sort_num; k++) {
        if ( (rc = SF_mat_store("c_qsort_sortmiss", 1, k + 1, miss[k])) ) return(rc);
        if ( (rc = SF_mat_store("c_qsort_sortmin",  1, k + 1, mins[k])) ) return(rc);
        if ( (rc = SF_mat_store("c_qsort_sortmax",  1, k + 1, maxs[k])) ) return(rc);
    }
    return(0);
}
int sf_numsetup ( ) { ST_retcode rc ; ST_double z ; int i , k ; int kvars_sort_num = sf_get_vector_length ( " " ) ; if ( kvars_sort_num < 0 ) return ( 198 ) ; double mins [ kvars_sort_num ] , maxs [ kvars_sort_num ] , miss [ kvars_sort_num ] ; int nonmiss [ kvars_sort_num ] ; size_t in1 = SF_in1 ( ) ; size_t in2 = SF_in2 ( ) ; size_t N = in2 - in1 + 1 ; size_t start = 0 ; for ( k = 0 ; k < kvars_sort_num ; k ++ ) mins [ k ] = maxs [ k ] = miss [ k ] = nonmiss [ k ] = 0 ; do { for ( k = 0 ; k < kvars_sort_num ; k ++ ) { if ( ( rc = SF_vdata ( k + 1 , start + in1 , & z ) ) ) return ( rc ) ; if ( SF_is_missing ( z ) ) { miss [ k ] = 1 ; } else { if ( nonmiss [ k ] == 0 ) { nonmiss [ k ] = 1 ; mins [ k ] = z ; maxs [ k ] = z ; } } } ++ start ; } while ( ( mf_sum_signed ( nonmiss , kvars_sort_num ) < kvars_sort_num ) & ( start < N ) ) ; for ( i = 0 ; i < N ; i ++ ) { for ( k = 0 ; k < kvars_sort_num ; k ++ ) { if ( ( rc = SF_vdata ( k + 1 , i + in1 , & z ) ) ) return ( rc ) ; if ( SF_is_missing ( z ) ) { miss [ k ] = 1 ; } else { if ( mins [ k ] > z ) mins [ k ] = z ; if ( maxs [ k ] < z ) maxs [ k ] = z ; } } } for ( k = 0 ; k < kvars_sort_num ; k ++ ) sf_printf ( " \t \t \n " , mins [ k ] , maxs [ k ] , miss [ k ] ) ; for ( k = 0 ; k < kvars_sort_num ; k ++ ) { if ( ( rc = SF_mat_store ( " " , 1 , k + 1 , miss [ k ] ) ) ) return ( rc ) ; if ( ( rc = SF_mat_store ( " " , 1 , k + 1 , mins [ k ] ) ) ) return ( rc ) ; if ( ( rc = SF_mat_store ( " " , 1 , k + 1 , maxs [ k ] ) ) ) return ( rc ) ; } return ( 0 ) ; }
@brief Min, max, and missing count for numeric sort vars
 @return Computes min, max, and missing count in Stata
@brief Min max and missing count for numeric sort vars @return Computes min max and missing count in Stata