src/ptra.c
NUMA *
numaGetBinSortIndex(NUMA    *nas,
                    l_int32  sortorder)
{
l_int32    i, n, isize, ival, imax;
l_float32  size;
NUMA      *na, *nai, *nad;
L_PTRA    *paindex;

    PROCNAME("numaGetBinSortIndex");

    if (!nas)
        return (NUMA *)ERROR_PTR("nas not defined", procName, NULL);
    if (sortorder != L_SORT_INCREASING && sortorder != L_SORT_DECREASING)
        return (NUMA *)ERROR_PTR("invalid sort order", procName, NULL);

        /* Set up a ptra holding numa at indices for which there
         * are values in nas.  This effectively sorts the input
         * numbers. */
    numaGetMax(nas, &size, NULL);
    isize = (l_int32)size;
    if (isize > 50000)
        L_WARNING_INT("large array: %d elements", procName, isize);
    paindex = ptraCreate(isize + 1);
    n = numaGetCount(nas);
    for (i = 0; i < n; i++) {
        numaGetIValue(nas, i, &ival);
        nai = (NUMA *)ptraGetHandle(paindex, ival);
        if (!nai) {  /* make it; no shifting will occur */
            nai = numaCreate(1);
            ptraInsert(paindex, ival, nai, L_MIN_DOWNSHIFT);
        }
        numaAddNumber(nai, i);
    }

        /* Sort by pulling the numbers out of the numas, taken
         * successively in requested order. */
    ptraGetMaxIndex(paindex, &imax);
    nad = numaCreate(0);
    if (sortorder == L_SORT_INCREASING) {
        for (i = 0; i <= imax; i++) {
            na = (NUMA *)ptraRemove(paindex, i, L_NO_COMPACTION);
            numaJoin(nad, na, 0, 0);
            numaDestroy(&na);
        }
    } else {  /* L_SORT_DECREASING */
        for (i = imax; i >= 0; i--) {
            na = (NUMA *)ptraRemove(paindex, i, L_NO_COMPACTION);
            numaJoin(nad, na, 0, 0);
            numaDestroy(&na);
        }
    }

    ptraDestroy(&paindex, FALSE, FALSE);
    return nad;
}
/*!
 *  numaGetBinSortIndex()
 *
 *      Input:  na (of non-negative integers with a max that is typically
 *                  less than 50,000)
 *              sortorder (L_SORT_INCREASING or L_SORT_DECREASING)
 *      Return: na (sorted), or null on error
 *
 *  Notes:
 *      (1) This creates an array (or lookup table) that gives the
 *          sorted position of the elements in the input Numa.
 *      (2) Because it uses a bin sort with buckets of size 1, it
 *          is not appropriate for sorting either small arrays or
 *          arrays containing very large integer values.  For such
 *          arrays, use a standard general sort function like
 *          numaGetSortIndex().
 */
numaGetBinSortIndex()
Input:  na (of non-negative integers with a max that is typically
less than 50,000)
sortorder (L_SORT_INCREASING or L_SORT_DECREASING)
Return: na (sorted), or null on error

(1) This creates an array (or lookup table) that gives the
sorted position of the elements in the input Numa.
(2) Because it uses a bin sort with buckets of size 1, it
is not appropriate for sorting either small arrays or
arrays containing very large integer values.  For such
arrays, use a standard general sort function like
numaGetSortIndex().
numaGetBinSortIndex () Input : na ( of non - negative integers with a max that is typically less than 50 000 ) sortorder ( L_SORT_INCREASING or L_SORT_DECREASING ) Return : na ( sorted ) or null on error ( 1 ) This creates an array ( or lookup table ) that gives the sorted position of the elements in the input Numa . ( 2 ) Because it uses a bin sort with buckets of size 1 it is not appropriate for sorting either small arrays or arrays containing very large integer values . For such arrays use a standard general sort function like numaGetSortIndex () .
NUMA *
numaGetBinSortIndex(NUMA    *nas,
                    l_int32  sortorder)
{
l_int32    i, n, isize, ival, imax;
l_float32  size;
NUMA      *na, *nai, *nad;
L_PTRA    *paindex;
    PROCNAME("numaGetBinSortIndex");
    if (!nas)
        return (NUMA *)ERROR_PTR("nas not defined", procName, NULL);
    if (sortorder != L_SORT_INCREASING && sortorder != L_SORT_DECREASING)
        return (NUMA *)ERROR_PTR("invalid sort order", procName, NULL);
    numaGetMax(nas, &size, NULL);
    isize = (l_int32)size;
    if (isize > 50000)
        L_WARNING_INT("large array: %d elements", procName, isize);
    paindex = ptraCreate(isize + 1);
    n = numaGetCount(nas);
    for (i = 0; i < n; i++) {
        numaGetIValue(nas, i, &ival);
        nai = (NUMA *)ptraGetHandle(paindex, ival);
        if (!nai) {  
            nai = numaCreate(1);
            ptraInsert(paindex, ival, nai, L_MIN_DOWNSHIFT);
        }
        numaAddNumber(nai, i);
    }
    ptraGetMaxIndex(paindex, &imax);
    nad = numaCreate(0);
    if (sortorder == L_SORT_INCREASING) {
        for (i = 0; i <= imax; i++) {
            na = (NUMA *)ptraRemove(paindex, i, L_NO_COMPACTION);
            numaJoin(nad, na, 0, 0);
            numaDestroy(&na);
        }
    } else {  
        for (i = imax; i >= 0; i--) {
            na = (NUMA *)ptraRemove(paindex, i, L_NO_COMPACTION);
            numaJoin(nad, na, 0, 0);
            numaDestroy(&na);
        }
    }
    ptraDestroy(&paindex, FALSE, FALSE);
    return nad;
}
NUMA * numaGetBinSortIndex ( NUMA * nas , l_int32 sortorder ) { l_int32 i , n , isize , ival , imax ; l_float32 size ; NUMA * na , * nai , * nad ; L_PTRA * paindex ; PROCNAME ( " " ) ; if ( ! nas ) return ( NUMA * ) ERROR_PTR ( " " , procName , NULL ) ; if ( sortorder != L_SORT_INCREASING && sortorder != L_SORT_DECREASING ) return ( NUMA * ) ERROR_PTR ( " " , procName , NULL ) ; numaGetMax ( nas , & size , NULL ) ; isize = ( l_int32 ) size ; if ( isize > 50000 ) L_WARNING_INT ( " " , procName , isize ) ; paindex = ptraCreate ( isize + 1 ) ; n = numaGetCount ( nas ) ; for ( i = 0 ; i < n ; i ++ ) { numaGetIValue ( nas , i , & ival ) ; nai = ( NUMA * ) ptraGetHandle ( paindex , ival ) ; if ( ! nai ) { nai = numaCreate ( 1 ) ; ptraInsert ( paindex , ival , nai , L_MIN_DOWNSHIFT ) ; } numaAddNumber ( nai , i ) ; } ptraGetMaxIndex ( paindex , & imax ) ; nad = numaCreate ( 0 ) ; if ( sortorder == L_SORT_INCREASING ) { for ( i = 0 ; i <= imax ; i ++ ) { na = ( NUMA * ) ptraRemove ( paindex , i , L_NO_COMPACTION ) ; numaJoin ( nad , na , 0 , 0 ) ; numaDestroy ( & na ) ; } } else { for ( i = imax ; i >= 0 ; i -- ) { na = ( NUMA * ) ptraRemove ( paindex , i , L_NO_COMPACTION ) ; numaJoin ( nad , na , 0 , 0 ) ; numaDestroy ( & na ) ; } } ptraDestroy ( & paindex , FALSE , FALSE ) ; return nad ; }
numaGetBinSortIndex()
      Input:  na (of non-negative integers with a max that is typically
                  less than 50,000)
              sortorder (L_SORT_INCREASING or L_SORT_DECREASING)
      Return: na (sorted), or null on error
numaGetBinSortIndex () Input : na ( of non - negative integers with a max that is typically less than 50 000 ) sortorder ( L_SORT_INCREASING or L_SORT_DECREASING ) Return : na ( sorted ) or null on error
/* Set up a ptra holding numa at indices for which there
         * are values in nas.  This effectively sorts the input
         * numbers. */ /* make it; no shifting will occur */ /* Sort by pulling the numbers out of the numas, taken
         * successively in requested order. */ /* L_SORT_DECREASING */