IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 14, 2004, 10:01:52 AM (22 years ago)
Author:
desonia
Message:

Updated files in src/collections to use new psError functionality. Also
cleaned up the code where needed, removing unnecessary error conditions,
etc.

File:
1 edited

Legend:

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

    r1761 r1807  
    1010*  @author Robert DeSonia, MHPCC
    1111*
    12 *  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2004-09-09 21:59:03 $
     12*  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2004-09-14 20:01:52 $
    1414*
    1515*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2525#include "psLogMsg.h"
    2626#include "psCompare.h"
     27
    2728#include "psCollectionsErrors.h"
    2829
    2930static void vectorFree(psVector* restrict psVec);
     31
     32
     33static void vectorFree(psVector* restrict psVec)
     34{
     35    if (psVec == NULL) {
     36        return;
     37    }
     38
     39    psFree(psVec->data.V);
     40}
    3041
    3142// FUNCTION IMPLEMENTATION - PUBLIC
     
    3546    psVector* psVec = NULL;
    3647    int elementSize = 0;
    37 
    38     // Invalid nalloc
    39     if (nalloc < 1) {
    40         psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);
    41         return NULL;
    42     }
    4348
    4449    elementSize = PSELEMTYPE_SIZEOF(elemType);
     
    6469    psElemType elemType;
    6570
    66     // Invalid nalloc
    67     if (nalloc < 1) {
    68         psError(__func__, "Invalid value for realloc (%d)\n", nalloc);
    69         return NULL;
    70     }
    71 
    7271    if (in == NULL) {
    73         psError(__func__, "Null input vector\n");
     72        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorRealloc",
     73                   PS_ERR_BAD_PARAMETER_NULL, true,
     74                   PS_ERRORTEXT_psVector_REALLOC_NULL);
    7475        return NULL;
    7576    } else if (in->nalloc != nalloc) {     // No need to realloc to same size
     
    101102        return in;
    102103    }
    103     // Invalid nalloc
    104     if (nalloc < 1) {
    105         psError(__func__, "Invalid value for nalloc (%d)\n", nalloc);
    106         psFree(in);
    107         return NULL;
    108     }
    109104
    110105    in->data.V = psRealloc(in->data.V, nalloc * PSELEMTYPE_SIZEOF(type));
     
    119114psVector* psVectorSort(psVector* restrict outVector, const psVector* restrict inVector)
    120115{
    121     int inN = 0;
    122     int outN = 0;
     116    int N = 0;
    123117    int elSize = 0;
    124118    void *inVec = NULL;
     
    127121
    128122    if (inVector == NULL) {
    129         psError(__func__, " : Line %d - Null input vector\n", __LINE__);
    130         return outVector;
     123        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort",
     124                   PS_ERR_BAD_PARAMETER_NULL, true,
     125                   PS_ERRORTEXT_psVector_SORT_NULL);
     126        psFree(outVector);
     127        return NULL;
    131128    }
    132129
    133130    inType = inVector->type.type;
    134     inN = inVector->n;
     131    N = inVector->n;
    135132    inVec = inVector->data.V;
    136133    elSize = PSELEMTYPE_SIZEOF(inType);
    137134
    138135    if (outVector == NULL) {
    139         outVector = psVectorAlloc(inN, inType);
    140         outVector->n = inVector->n;
    141     }
    142 
    143     outN = outVector->n;
     136        outVector = psVectorAlloc(N, inType);
     137    }
     138
     139    // check to see if output vector needs to be resized/retyped
     140    if ( (N > outVector->nalloc) ||
     141            (inType != outVector->type.type) ) {
     142        // reshape the output vector to match the input vector's size/type.
     143        outVector = psVectorRecycle(outVector,N,inType);
     144    }
     145    outVector->n = N;
    144146    outVec = outVector->data.V;
    145147
    146     if (inN != outN) {
    147         psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
    148                 __LINE__, inN, outN);
     148    if (N == 0) {
     149        // no need to sort anything, as there are no elements in input vector.
    149150        return outVector;
    150151    }
    151152
    152     if (inType != outVector->type.type) {
    153         psError(__func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__,
    154                 inType, outVector->type.type);
    155         return outVector;
    156     }
    157 
    158     if (inN == 0) {
    159         psError(__func__, " : Line %d - No elements in use for input vector\n", __LINE__);
    160         return outVector;
    161     }
    162 
    163     if (outN == 0) {
    164         psError(__func__, " : Line %d - No elements in use for output vector\n", __LINE__);
    165         return outVector;
    166     }
    167     // Copy input vector values into output vector
    168     memcpy(outVec, inVec, elSize * outN);
     153    // Copy input vector values into output vector if not in-place sorting
     154    if (inVector != outVector) {
     155        memcpy(outVec, inVec, elSize * N);
     156    }
    169157
    170158    // Sort output vector
    171159    switch (inType) {
    172160    case PS_TYPE_U8:
    173         qsort(outVec, inN, elSize, psCompareU8);
     161        qsort(outVec, N, elSize, psCompareU8);
    174162        break;
    175163    case PS_TYPE_U16:
    176         qsort(outVec, inN, elSize, psCompareU16);
     164        qsort(outVec, N, elSize, psCompareU16);
    177165        break;
    178166    case PS_TYPE_U32:
    179         qsort(outVec, inN, elSize, psCompareU32);
     167        qsort(outVec, N, elSize, psCompareU32);
    180168        break;
    181169    case PS_TYPE_U64:
    182         qsort(outVec, inN, elSize, psCompareU64);
     170        qsort(outVec, N, elSize, psCompareU64);
    183171        break;
    184172    case PS_TYPE_S8:
    185         qsort(outVec, inN, elSize, psCompareS8);
     173        qsort(outVec, N, elSize, psCompareS8);
    186174        break;
    187175    case PS_TYPE_S16:
    188         qsort(outVec, inN, elSize, psCompareS16);
     176        qsort(outVec, N, elSize, psCompareS16);
    189177        break;
    190178    case PS_TYPE_S32:
    191         qsort(outVec, inN, elSize, psCompareS32);
     179        qsort(outVec, N, elSize, psCompareS32);
    192180        break;
    193181    case PS_TYPE_S64:
    194         qsort(outVec, inN, elSize, psCompareS64);
     182        qsort(outVec, N, elSize, psCompareS64);
    195183        break;
    196184    case PS_TYPE_F32:
    197         qsort(outVec, inN, elSize, psCompareF32);
     185        qsort(outVec, N, elSize, psCompareF32);
    198186        break;
    199187    case PS_TYPE_F64:
    200         qsort(outVec, inN, elSize, psCompareF64);
     188        qsort(outVec, N, elSize, psCompareF64);
    201189        break;
    202190    default:
    203         psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
     191        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort",
     192                   PS_ERR_BAD_PARAMETER_TYPE, true,
     193                   PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
     194                   inType);
    204195    }
    205196
     
    207198}
    208199
    209 #define SORT_INDICES(TYPE)                                                                                   \
    210 for(i=0; i<inN; i++) {                                                                                       \
    211     for(j=0; j<inN; j++) {                                                                                   \
    212         diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]);                                             \
    213         if(diff < FLT_EPSILON) {                                                                             \
    214             outVec[i] = j;                                                                                   \
    215             break;                                                                                           \
    216         }                                                                                                    \
    217     }                                                                                                        \
    218 }
    219 
    220200psVector* psVectorSortIndex(psVector* restrict outVector, const psVector* restrict inVector)
    221201{
    222     int inN = 0;
    223     int outN = 0;
    224     int i = 0;
    225     int j = 0;
    226     float *inVec = NULL;
    227     int *outVec = NULL;
    228     double diff = 0.0f;
     202    int N = 0;
    229203    psVector* tmpVector = NULL;
    230204    psElemType inType = 0;
     205    psU32* outVec;
    231206
    232207    if (inVector == NULL) {
    233         psError(__func__, " : Line %d - Null input vector\n", __LINE__);
     208        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex",
     209                   PS_ERR_BAD_PARAMETER_NULL, true,
     210                   PS_ERRORTEXT_psVector_SORT_NULL);
     211        psFree(outVector);
     212        return NULL;
     213    }
     214
     215    N = inVector->n;
     216
     217    if (outVector == NULL) {
     218        outVector = psVectorAlloc(N, PS_TYPE_U32);
     219    }
     220
     221    // check to see if output vector needs to be resized/retyped
     222    if ( (N > outVector->nalloc) ||
     223            (outVector->type.type != PS_TYPE_U32) ) {
     224        // reshape the output vector to match the input vector's size/type.
     225        outVector = psVectorRecycle(outVector,N,PS_TYPE_U32);
     226    }
     227    outVector->n = N;
     228    outVec = outVector->data.U32;
     229
     230    if (N == 0) {
     231        // no need to sort anything, as there are no elements in input vector.
    234232        return outVector;
    235233    }
    236234
    237     inN = inVector->n;
    238     inVec = inVector->data.V;
    239     inType = inVector->type.type;
    240 
    241     if (outVector == NULL) {
    242         outVector = psVectorAlloc(inN, PS_TYPE_U32);
    243         outVector->n = inN;
    244     }
    245 
    246     outN = outVector->n;
    247     outVec = outVector->data.V;
    248 
    249     if (inN != outN) {
    250         psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
    251                 __LINE__, inN, outN);
    252         return outVector;
    253     }
    254 
    255     if (outVector->type.type != PS_TYPE_U32) {
    256         psError(__func__, " : Line %d - Output vector is not of type U32: out=%d\n",
    257                 __LINE__, outVector->type.type);
    258         return outVector;
    259     }
    260 
    261     tmpVector = psVectorAlloc(inN, inType);
    262     tmpVector->n = inN;
    263235    tmpVector = psVectorSort(tmpVector, inVector);
     236
     237    #define SORT_INDICES(TYPE,absfcn,maxError) {                              \
     238        ps##TYPE* inVec = inVector->data.TYPE;                                \
     239        ps##TYPE* tmpVec = tmpVector->data.TYPE;                              \
     240        ps##TYPE  diff;                                                       \
     241        for(int i=0; i<N; i++) {                                              \
     242            for(int j=0; j<N; j++) {                                          \
     243                diff = absfcn(tmpVec[i] - inVec[j]);                          \
     244                if(diff < maxError) {                                         \
     245                    outVec[i] = j;                                            \
     246                    break;                                                    \
     247                }                                                             \
     248            }                                                                 \
     249        }                                                                     \
     250    }
    264251
    265252    // Sort output vector
    266253    switch (inType) {
    267254    case PS_TYPE_U8:
    268         SORT_INDICES(U8);
     255        SORT_INDICES(U8,/* no absfcn needed */,1);
    269256        break;
    270257    case PS_TYPE_U16:
    271         SORT_INDICES(U16);
     258        SORT_INDICES(U16,/* no absfcn needed */,1);
    272259        break;
    273260    case PS_TYPE_U32:
    274         SORT_INDICES(U32);
     261        SORT_INDICES(U32,/* no absfcn needed */,1);
    275262        break;
    276263    case PS_TYPE_U64:
    277         SORT_INDICES(U64);
     264        SORT_INDICES(U64,/* no absfcn needed */,1);
    278265        break;
    279266    case PS_TYPE_S8:
    280         SORT_INDICES(S8);
     267        SORT_INDICES(S8,/* no absfcn needed */,1);
    281268        break;
    282269    case PS_TYPE_S16:
    283         SORT_INDICES(S16);
     270        SORT_INDICES(S16,/* no absfcn needed */,1);
    284271        break;
    285272    case PS_TYPE_S32:
    286         SORT_INDICES(S32);
     273        SORT_INDICES(S32,/* no absfcn needed */,1);
    287274        break;
    288275    case PS_TYPE_S64:
    289         SORT_INDICES(S64);
     276        SORT_INDICES(S64,/* no absfcn needed */,1);
    290277        break;
    291278    case PS_TYPE_F32:
    292         SORT_INDICES(F32);
     279        SORT_INDICES(F32,fabsf,FLT_EPSILON);
    293280        break;
    294281    case PS_TYPE_F64:
    295         SORT_INDICES(F64);
     282        SORT_INDICES(F64,fabs,DBL_EPSILON);
     283        break;
     284    case PS_TYPE_C32:
     285        SORT_INDICES(F64,cabsf,FLT_EPSILON);
     286        break;
     287    case PS_TYPE_C64:
     288        SORT_INDICES(F64,cabs,DBL_EPSILON);
    296289        break;
    297290    default:
    298         psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
     291        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex",
     292                   PS_ERR_BAD_PARAMETER_TYPE, true,
     293                   PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
     294                   inType);
    299295    }
    300296
     
    304300    return outVector;
    305301}
    306 
    307 static void vectorFree(psVector* restrict psVec)
    308 {
    309     if (psVec == NULL) {
    310         return;
    311     }
    312 
    313     psFree(psVec->data.V);
    314 }
Note: See TracChangeset for help on using the changeset viewer.