IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 10, 2004, 1:15:08 PM (22 years ago)
Author:
harman
Message:

Added more psTypes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/collections/psSort.c

    r831 r989  
    33 *  @brief Sorts vectors
    44 *
    5  *  The psSort functions use the qsort() stdlib function with a user-defined callback to sort an vector of 
    6  *  floats. The qsort function requires the starting point of the vector, number of elements in the vector, size 
     5 *  The psSort functions use the qsort() stdlib function with a user-defined callback to sort an vector of
     6 *  floats. The qsort function requires the starting point of the vector, number of elements in the vector, size
    77 *  of each element, and a pointer to a callback comparison function. Once called, qsort() will sort the vector
    88 *  contents in ascending order according to the comparison function. The comparison function is called with
    9  *  two arguments that point to the objects being compared - in this case two floats. The comparison function 
     9 *  two arguments that point to the objects being compared - in this case two floats. The comparison function
    1010 *  must return an integer less than, equal to, or greater than zero if the first argument is considered to be
    1111 *  respectively less than, equal to, or greater than the second. If two members compare as equal, their order
     
    1313 *
    1414 *  @author Ross Harman, MHPCC
    15  *   
    16  *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-06-02 23:29:14 $
     15 *
     16 *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-06-10 23:15:08 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    6464/** Private callback comparison function to compare two floats.
    6565 *
    66  *  The comparison function is called by qsort() with two arguments that point to the objects being
    67  *  compared. The comparison function must return an integer less than, equal to, or greater than zero if
    68  *  the first argument is considered to be respectively less than, equal to, or greater than the second. If
    69  *  two members compare as equal, their order in the sorted vector is undefined.
     66 *  The comparison function is called by qsort() with two arguments that point to the objects being
     67 *  compared. The comparison function must return an integer less than, equal to, or greater than zero if
     68 *  the first argument is considered to be respectively less than, equal to, or greater than the second.
    7069 *
    7170 *  @return  int: Result of comparsion (-1, 0, or 1).
    7271 */
    73 
    74 static int psCompare(
    75     const void *x,  /**< First float to compare. */
    76     const void *y   /**< Second float to compare. */
    77 )
    78 {
    79     float *item1 = NULL;
    80     float *item2 = NULL;
    81 
    82     if(x == NULL || y == NULL) {
    83         psError(__func__, " : Line %d - Null input argument: x=%d y=%d\n", __LINE__, x, y);
    84     }
    85 
    86     item1 = (float*)x;
    87     item2 = (float*)y;
    88 
    89     if(item1 == NULL || item2 == NULL) {
    90         psError(__func__, " : Line %d - Improper cast to float: item1=%d item2=%d\n", __LINE__, item1, item2);
    91     }
    92 
    93     if(*item1 < *item2) {
    94         return -1;
    95     } else if(*item1 > *item2) {
    96         return 1;
    97     } else {
    98         return 0;
    99     }
    100 }
     72#define PS_COMPARE(TYPE)                                                                                     \
     73static int psCompare##TYPE(const void *x, const void *y)                                                     \
     74{                                                                                                            \
     75    ps##TYPE *item1 = NULL;                                                                                  \
     76    ps##TYPE *item2 = NULL;                                                                                  \
     77    \
     78    if(x == NULL || y == NULL) {                                                                             \
     79        psError(__func__, " : Line %d - Null input argument: x=%d y=%d\n", __LINE__, x, y);                  \
     80    }                                                                                                        \
     81    \
     82    item1 = (ps##TYPE *)x;                                                                                   \
     83    item2 = (ps##TYPE *)y;                                                                                   \
     84    \
     85    if(item1 == NULL || item2 == NULL) {                                                                     \
     86        psError(__func__, " : Line %d - Improper cast to float: item1=%d item2=%d\n", __LINE__, item1,       \
     87                item2);                                                                                      \
     88    }                                                                                                        \
     89    \
     90    if(*item1 < *item2) {                                                                                    \
     91        return -1;                                                                                           \
     92    } else if(*item1 > *item2) {                                                                             \
     93        return 1;                                                                                            \
     94    } else {                                                                                                 \
     95        return 0;                                                                                            \
     96    }                                                                                                        \
     97}
     98
     99PS_COMPARE(U8)
     100PS_COMPARE(U16)
     101PS_COMPARE(F32)
     102PS_COMPARE(F64)
    101103
    102104/*****************************************************************************/
     
    104106/*****************************************************************************/
    105107
    106 psVector *psSort(
    107     psVector *restrict outVector,        /**< Sorted output vector. */
    108     const psVector *restrict inVector    /**< Input vector to sort. */
    109 )
     108psVector *psSort(psVector *restrict outVector, const psVector *restrict inVector)
    110109{
    111110    int inN = 0;
    112111    int outN = 0;
    113     int i = 0;
    114112    int elSize = 0;
    115     float *inVec = NULL;
    116     float *outVec = NULL;
    117 
    118     if(outVector == NULL) {
    119         psError(__func__, " : Line %d - Null output vector\n", __LINE__);
    120         return outVector;
    121     }
     113    void *inVec = NULL;
     114    void *outVec = NULL;
     115    psElemType inType = 0;
    122116
    123117    if(inVector == NULL) {
     
    126120    }
    127121
    128     // Initialize values
     122    inType = inVector->type.type;
    129123    inN = inVector->n;
     124    inVec = inVector->data.V;
     125    elSize = PSELEMTYPE_SIZEOF(inType);
     126
     127    if(outVector == NULL) {
     128        outVector = psVectorAlloc(inN, inType);
     129        outVector->n = inVector->n;
     130    }
     131
    130132    outN = outVector->n;
    131     inVec = inVector->data.F32;
    132     outVec = outVector->data.F32;
    133     elSize = sizeof(float);
     133    outVec = outVector->data.V;
    134134
    135135    if(inN != outN) {
     
    150150
    151151    // Copy input vector values into output vector
    152     for(i=0; i<inN; i++) {
    153         outVec[i] = inVec[i];
    154     }
     152    memcpy(outVec, inVec, elSize*outN);
    155153
    156154    // Sort output vector
    157     qsort((void*)outVec, inN, elSize, psCompare);
     155    switch(inType) {
     156    case PS_TYPE_U8:
     157        qsort(outVec, inN, elSize, psCompareU8);
     158        break;
     159    case PS_TYPE_U16:
     160        qsort(outVec, inN, elSize, psCompareU16);
     161        break;
     162    case PS_TYPE_F32:
     163        qsort(outVec, inN, elSize, psCompareF32);
     164        break;
     165    case PS_TYPE_F64:
     166        qsort(outVec, inN, elSize, psCompareF64);
     167        break;
     168    default:
     169        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
     170    }
    158171
    159172    return outVector;
    160173}
    161174
    162 psVector *psSortIndex(
    163     psVector *restrict outVector,         /**< Output vector of sorted indices. */
    164     const psVector *restrict inVector     /**< Input vector to be sorted. */
    165 )
     175#define SORT_INDICES(TYPE)                                                                                   \
     176for(i=0; i<inN; i++) {                                                                                       \
     177    for(j=0; j<inN; j++) {                                                                                   \
     178        diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]);                                             \
     179        if(diff < FLT_EPSILON) {                                                                             \
     180            outVec[i] = j;                                                                                   \
     181            break;                                                                                           \
     182        }                                                                                                    \
     183    }                                                                                                        \
     184}
     185
     186psVector *psSortIndex(psVector *restrict outVector, const psVector *restrict inVector)
    166187{
    167188    int inN = 0;
     
    169190    int i = 0;
    170191    int j = 0;
    171     float tempVal = 0.0f;
    172192    float *inVec = NULL;
    173193    int *outVec = NULL;
    174     float diff = 0.0f;
    175     psVector *tmpFloatVector = NULL;
    176 
    177     if(outVector == NULL) {
    178         psError(__func__, " : Line %d - Null output vector\n", __LINE__);
    179         return outVector;
    180     }
     194    double diff = 0.0f;
     195    psVector *tmpVector = NULL;
     196    psElemType inType = 0;
    181197
    182198    if(inVector == NULL) {
     
    185201    }
    186202
    187     // Initialize values
    188203    inN = inVector->n;
     204    inVec = inVector->data.V;
     205    inType = inVector->type.type;
     206
     207    if(outVector == NULL) {
     208        outVector = psVectorAlloc(inN, PS_TYPE_U32);
     209        outVector->n = inN;
     210    }
     211
    189212    outN = outVector->n;
    190     inVec = inVector->data.F32;
    191     outVec = outVector->data.S32;
     213    outVec = outVector->data.V;
    192214
    193215    if(inN != outN) {
    194         psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", __LINE__,
    195                 inN, outN);
    196         return outVector;
    197     }
    198 
    199     tmpFloatVector = psVectorAlloc(inN, PS_TYPE_F32);
    200     tmpFloatVector->n = inN;
    201     tmpFloatVector = psSort(tmpFloatVector, inVector);
    202 
    203     for(i=0; i<inN; i++) {
    204         tempVal = tmpFloatVector->data.F32[i];
    205         for(j=0; j<inN; j++) {
    206             diff = fabsf(tempVal - inVec[j]);
    207             if(diff < FLT_EPSILON) {
    208                 outVec[i] = j;
    209                 break;
    210             }
    211         }
     216        psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
     217                __LINE__, inN, outN);
     218        return outVector;
     219    }
     220
     221    tmpVector = psVectorAlloc(inN, inType);
     222    tmpVector->n = inN;
     223    tmpVector = psSort(tmpVector, inVector);
     224
     225    // Sort output vector
     226    switch(inType) {
     227    case PS_TYPE_U8:
     228        SORT_INDICES(U8);
     229        break;
     230    case PS_TYPE_U16:
     231        SORT_INDICES(U16);
     232        break;
     233    case PS_TYPE_F32:
     234        SORT_INDICES(F32);
     235        break;
     236    case PS_TYPE_F64:
     237        SORT_INDICES(F64);
     238        break;
     239    default:
     240        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
    212241    }
    213242
    214243    // Free temp memory
    215     psVectorFree(tmpFloatVector);
     244    psVectorFree(tmpVector);
    216245
    217246    return outVector;
Note: See TracChangeset for help on using the changeset viewer.