IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 30, 2004, 4:28:29 PM (22 years ago)
Author:
desonia
Message:

* empty log message *

File:
1 edited

Legend:

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

    r1305 r1360  
    11/** @file  psVector.c
    2  *
    3  *  @brief Contains support for basic vector types
    4  *
    5  *  This file defines the basic type for a vector struct and functions useful
    6  *  in manupulating vectors.
    7  *
    8  *  @author Ross Harman, MHPCC
    9  *
    10  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-07-28 00:06:13 $
    12  *
    13  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    14  */
     2*
     3*  @brief Contains support for basic vector types
     4*
     5*  This file defines the basic type for a vector struct and functions useful
     6*  in manupulating vectors.
     7*
     8*  @author Ross Harman, MHPCC
     9*
     10*  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
     11*  @date $Date: 2004-07-31 02:28:10 $
     12*
     13*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     14*/
    1515
    1616/******************************************************************************/
     
    5454/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    5555/*****************************************************************************/
    56 static void vectorFree(psVector *restrict psVec);
     56static void vectorFree( psVector *restrict psVec );
    5757
    5858/*****************************************************************************/
    5959/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    6060/*****************************************************************************/
    61 psVector* psVectorAlloc(unsigned int nalloc, psElemType elemType)
    62 {
    63     psVector *psVec = NULL;
     61psVector* psVectorAlloc( unsigned int nalloc, psElemType elemType )
     62{
     63    psVector * psVec = NULL;
    6464    int elementSize = 0;
    65 
     65   
    6666    // Invalid nalloc
    67     if(nalloc < 1) {
    68         psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);
    69         return NULL;
    70     }
    71 
    72     elementSize = PSELEMTYPE_SIZEOF(elemType);
    73 
     67    if ( nalloc < 1 ) {
     68            psError( __func__, "Invalid value for nalloc. nalloc: %d\n", nalloc );
     69            return NULL;
     70        }
     71       
     72    elementSize = PSELEMTYPE_SIZEOF( elemType );
     73   
    7474    // Create vector struct
    75     psVec = (psVector *)psAlloc(sizeof(psVector));
    76     p_psMemSetDeallocator(psVec,(psFreeFcn)vectorFree);
    77 
     75    psVec = ( psVector * ) psAlloc( sizeof( psVector ) );
     76    p_psMemSetDeallocator( psVec, ( psFreeFcn ) vectorFree );
     77   
    7878    psVec->type.dimen = PS_DIMEN_VECTOR;
    7979    psVec->type.type = elemType;
    8080    psVec->nalloc = nalloc;
    8181    psVec->n = nalloc;
    82 
     82   
    8383    // Create vector data array
    84     psVec->data.V = psAlloc(nalloc*elementSize);
    85 
     84    psVec->data.V = psAlloc( nalloc * elementSize );
     85   
    8686    return psVec;
    8787}
    8888
    89 psVector *psVectorRealloc(unsigned int nalloc, psVector *restrict in)
     89psVector *psVectorRealloc( unsigned int nalloc, psVector *restrict in )
    9090{
    9191    int elementSize = 0;
    9292    psElemType elemType;
    93 
     93   
    9494    // Invalid nalloc
    95     if(nalloc < 1) {
    96         psError(__func__, "Invalid value for realloc (%d)\n", nalloc);
    97         return NULL;
    98     }
    99 
    100     if(in == NULL) {
    101         psError(__func__, "Null input vector\n");
    102         return NULL;
    103     } else if(in->nalloc != nalloc) {                    // No need to realloc to same size
    104         elemType = in->type.type;
    105         elementSize = PSELEMTYPE_SIZEOF(elemType);
    106         if(nalloc < in->n) {
    107             in->n = nalloc;
    108         }
    109 
    110         // Realloc after decrementation to avoid accessing freed array elements
    111         in->data.V = psRealloc(in->data.V,nalloc*elementSize);
    112         in->nalloc = nalloc;
    113     }
    114 
     95    if ( nalloc < 1 ) {
     96            psError( __func__, "Invalid value for realloc (%d)\n", nalloc );
     97            return NULL;
     98        }
     99       
     100    if ( in == NULL ) {
     101            psError( __func__, "Null input vector\n" );
     102            return NULL;
     103        } else if ( in->nalloc != nalloc ) {                    // No need to realloc to same size
     104            elemType = in->type.type;
     105            elementSize = PSELEMTYPE_SIZEOF( elemType );
     106            if ( nalloc < in->n ) {
     107                    in->n = nalloc;
     108                }
     109               
     110            // Realloc after decrementation to avoid accessing freed array elements
     111            in->data.V = psRealloc( in->data.V, nalloc * elementSize );
     112            in->nalloc = nalloc;
     113        }
     114       
    115115    return in;
    116116}
    117117
    118 psVector *psVectorRecycle(psVector *restrict in, psElemType type, unsigned int nalloc)
     118psVector *psVectorRecycle( psVector *restrict in, psElemType type, unsigned int nalloc )
    119119{
    120120    psElemType elemType;
    121 
    122     if (in == NULL) {
    123         return psVectorAlloc(nalloc, type);
    124     }
    125 
     121   
     122    if ( in == NULL ) {
     123            return psVectorAlloc( nalloc, type );
     124        }
     125       
    126126    elemType = in->type.type;
    127 
    128     if (in->nalloc == nalloc && elemType == type) {
    129         // it is proper size/type already
    130         return in;
    131     }
    132 
     127   
     128    if ( in->nalloc == nalloc && elemType == type ) {
     129            // it is proper size/type already
     130            return in;
     131        }
     132       
    133133    // Invalid nalloc
    134     if(nalloc < 1) {
    135         psError(__func__, "Invalid value for nalloc (%d)\n", nalloc);
    136         psFree(in);
    137         return NULL;
    138     }
    139 
    140 
    141     in->data.V = psRealloc(in->data.V,nalloc*PSELEMTYPE_SIZEOF(type));
    142 
    143     in->n = 0;
     134    if ( nalloc < 1 ) {
     135            psError( __func__, "Invalid value for nalloc (%d)\n", nalloc );
     136            psFree( in );
     137            return NULL;
     138        }
     139       
     140       
     141    in->data.V = psRealloc( in->data.V, nalloc * PSELEMTYPE_SIZEOF( type ) );
     142   
    144143    in->type.type = type;
    145144    in->nalloc = nalloc;
    146 
     145    in->n = nalloc;
     146   
    147147    return in;
    148148}
    149149
    150 psVector *psVectorSort(psVector *restrict outVector, const psVector *restrict inVector)
     150psVector *psVectorSort( psVector *restrict outVector, const psVector *restrict inVector )
    151151{
    152152    int inN = 0;
     
    156156    void *outVec = NULL;
    157157    psElemType inType = 0;
    158 
    159     if(inVector == NULL) {
    160         psError(__func__, " : Line %d - Null input vector\n", __LINE__);
    161         return outVector;
    162     }
    163 
     158   
     159    if ( inVector == NULL ) {
     160            psError( __func__, " : Line %d - Null input vector\n", __LINE__ );
     161            return outVector;
     162        }
     163       
    164164    inType = inVector->type.type;
    165165    inN = inVector->n;
    166166    inVec = inVector->data.V;
    167     elSize = PSELEMTYPE_SIZEOF(inType);
    168 
    169     if(outVector == NULL) {
    170         outVector = psVectorAlloc(inN, inType);
    171         outVector->n = inVector->n;
    172     }
    173 
     167    elSize = PSELEMTYPE_SIZEOF( inType );
     168   
     169    if ( outVector == NULL ) {
     170            outVector = psVectorAlloc( inN, inType );
     171            outVector->n = inVector->n;
     172        }
     173       
    174174    outN = outVector->n;
    175175    outVec = outVector->data.V;
    176 
    177     if(inN != outN) {
    178         psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", __LINE__,
    179                 inN, outN);
    180         return outVector;
    181     }
    182 
    183     if(inType != outVector->type.type) {
    184         psError(__func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__,
    185                 inType, outVector->type.type);
    186         return outVector;
    187     }
    188 
    189     if(inN == 0) {
    190         psError(__func__, " : Line %d - No elements in use for input vector\n", __LINE__);
    191         return outVector;
    192     }
    193 
    194     if(outN == 0) {
    195         psError(__func__, " : Line %d - No elements in use for output vector\n", __LINE__);
    196         return outVector;
    197     }
    198 
     176   
     177    if ( inN != outN ) {
     178            psError( __func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", __LINE__,
     179                     inN, outN );
     180            return outVector;
     181        }
     182       
     183    if ( inType != outVector->type.type ) {
     184            psError( __func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__,
     185                     inType, outVector->type.type );
     186            return outVector;
     187        }
     188       
     189    if ( inN == 0 ) {
     190            psError( __func__, " : Line %d - No elements in use for input vector\n", __LINE__ );
     191            return outVector;
     192        }
     193       
     194    if ( outN == 0 ) {
     195            psError( __func__, " : Line %d - No elements in use for output vector\n", __LINE__ );
     196            return outVector;
     197        }
     198       
    199199    // Copy input vector values into output vector
    200     memcpy(outVec, inVec, elSize*outN);
    201 
     200    memcpy( outVec, inVec, elSize * outN );
     201   
    202202    // Sort output vector
    203     switch(inType) {
    204     case PS_TYPE_U8:
    205         qsort(outVec, inN, elSize, psCompareU8);
    206         break;
    207     case PS_TYPE_U16:
    208         qsort(outVec, inN, elSize, psCompareU16);
    209         break;
    210     case PS_TYPE_U32:
    211         qsort(outVec, inN, elSize, psCompareU32);
    212         break;
    213     case PS_TYPE_U64:
    214         qsort(outVec, inN, elSize, psCompareU64);
    215         break;
    216     case PS_TYPE_S8:
    217         qsort(outVec, inN, elSize, psCompareS8);
    218         break;
    219     case PS_TYPE_S16:
    220         qsort(outVec, inN, elSize, psCompareS16);
    221         break;
    222     case PS_TYPE_S32:
    223         qsort(outVec, inN, elSize, psCompareS32);
    224         break;
    225     case PS_TYPE_S64:
    226         qsort(outVec, inN, elSize, psCompareS64);
    227         break;
    228     case PS_TYPE_F32:
    229         qsort(outVec, inN, elSize, psCompareF32);
    230         break;
    231     case PS_TYPE_F64:
    232         qsort(outVec, inN, elSize, psCompareF64);
    233         break;
    234     default:
    235         psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
    236     }
    237 
     203    switch ( inType ) {
     204            case PS_TYPE_U8:
     205            qsort( outVec, inN, elSize, psCompareU8 );
     206            break;
     207            case PS_TYPE_U16:
     208            qsort( outVec, inN, elSize, psCompareU16 );
     209            break;
     210            case PS_TYPE_U32:
     211            qsort( outVec, inN, elSize, psCompareU32 );
     212            break;
     213            case PS_TYPE_U64:
     214            qsort( outVec, inN, elSize, psCompareU64 );
     215            break;
     216            case PS_TYPE_S8:
     217            qsort( outVec, inN, elSize, psCompareS8 );
     218            break;
     219            case PS_TYPE_S16:
     220            qsort( outVec, inN, elSize, psCompareS16 );
     221            break;
     222            case PS_TYPE_S32:
     223            qsort( outVec, inN, elSize, psCompareS32 );
     224            break;
     225            case PS_TYPE_S64:
     226            qsort( outVec, inN, elSize, psCompareS64 );
     227            break;
     228            case PS_TYPE_F32:
     229            qsort( outVec, inN, elSize, psCompareF32 );
     230            break;
     231            case PS_TYPE_F64:
     232            qsort( outVec, inN, elSize, psCompareF64 );
     233            break;
     234            default:
     235            psError( __func__, " : Line %d - Invalid psType\n", __LINE__ );
     236        }
     237       
    238238    return outVector;
    239239}
     
    241241#define SORT_INDICES(TYPE)                                                                                   \
    242242for(i=0; i<inN; i++) {                                                                                       \
    243     for(j=0; j<inN; j++) {                                                                                   \
    244         diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]);                                             \
    245         if(diff < FLT_EPSILON) {                                                                             \
    246             outVec[i] = j;                                                                                   \
    247             break;                                                                                           \
    248         }                                                                                                    \
    249     }                                                                                                        \
    250 }
    251 
    252 psVector *psVectorSortIndex(psVector *restrict outVector, const psVector *restrict inVector)
     243        for(j=0; j<inN; j++) {                                                                                   \
     244                diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]);                                             \
     245                if(diff < FLT_EPSILON) {                                                                             \
     246                        outVec[i] = j;                                                                                   \
     247                        break;                                                                                           \
     248                    }                                                                                                    \
     249            }                                                                                                        \
     250    }
     251   
     252psVector *psVectorSortIndex( psVector *restrict outVector, const psVector *restrict inVector )
    253253{
    254254    int inN = 0;
     
    261261    psVector *tmpVector = NULL;
    262262    psElemType inType = 0;
    263 
    264     if(inVector == NULL) {
    265         psError(__func__, " : Line %d - Null input vector\n", __LINE__);
    266         return outVector;
    267     }
    268 
     263   
     264    if ( inVector == NULL ) {
     265            psError( __func__, " : Line %d - Null input vector\n", __LINE__ );
     266            return outVector;
     267        }
     268       
    269269    inN = inVector->n;
    270270    inVec = inVector->data.V;
    271271    inType = inVector->type.type;
    272 
    273     if(outVector == NULL) {
    274         outVector = psVectorAlloc(inN, PS_TYPE_U32);
    275         outVector->n = inN;
    276     }
    277 
     272   
     273    if ( outVector == NULL ) {
     274            outVector = psVectorAlloc( inN, PS_TYPE_U32 );
     275            outVector->n = inN;
     276        }
     277       
    278278    outN = outVector->n;
    279279    outVec = outVector->data.V;
    280 
    281     if(inN != outN) {
    282         psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
    283                 __LINE__, inN, outN);
    284         return outVector;
    285     }
    286 
    287     if(outVector->type.type != PS_TYPE_U32) {
    288         psError(__func__, " : Line %d - Output vector is not of type U32: out=%d\n",
    289                 __LINE__, outVector->type.type);
    290         return outVector;
    291     }
    292 
    293     tmpVector = psVectorAlloc(inN, inType);
     280   
     281    if ( inN != outN ) {
     282            psError( __func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
     283                     __LINE__, inN, outN );
     284            return outVector;
     285        }
     286       
     287    if ( outVector->type.type != PS_TYPE_U32 ) {
     288            psError( __func__, " : Line %d - Output vector is not of type U32: out=%d\n",
     289                     __LINE__, outVector->type.type );
     290            return outVector;
     291        }
     292       
     293    tmpVector = psVectorAlloc( inN, inType );
    294294    tmpVector->n = inN;
    295     tmpVector = psVectorSort(tmpVector, inVector);
    296 
     295    tmpVector = psVectorSort( tmpVector, inVector );
     296   
    297297    // Sort output vector
    298     switch(inType) {
    299     case PS_TYPE_U8:
    300         SORT_INDICES(U8);
    301         break;
    302     case PS_TYPE_U16:
    303         SORT_INDICES(U16);
    304         break;
    305     case PS_TYPE_U32:
    306         SORT_INDICES(U32);
    307         break;
    308     case PS_TYPE_U64:
    309         SORT_INDICES(U64);
    310         break;
    311     case PS_TYPE_S8:
    312         SORT_INDICES(S8);
    313         break;
    314     case PS_TYPE_S16:
    315         SORT_INDICES(S16);
    316         break;
    317     case PS_TYPE_S32:
    318         SORT_INDICES(S32);
    319         break;
    320     case PS_TYPE_S64:
    321         SORT_INDICES(S64);
    322         break;
    323     case PS_TYPE_F32:
    324         SORT_INDICES(F32);
    325         break;
    326     case PS_TYPE_F64:
    327         SORT_INDICES(F64);
    328         break;
    329     default:
    330         psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
    331     }
    332 
     298    switch ( inType ) {
     299            case PS_TYPE_U8:
     300            SORT_INDICES( U8 );
     301            break;
     302            case PS_TYPE_U16:
     303            SORT_INDICES( U16 );
     304            break;
     305            case PS_TYPE_U32:
     306            SORT_INDICES( U32 );
     307            break;
     308            case PS_TYPE_U64:
     309            SORT_INDICES( U64 );
     310            break;
     311            case PS_TYPE_S8:
     312            SORT_INDICES( S8 );
     313            break;
     314            case PS_TYPE_S16:
     315            SORT_INDICES( S16 );
     316            break;
     317            case PS_TYPE_S32:
     318            SORT_INDICES( S32 );
     319            break;
     320            case PS_TYPE_S64:
     321            SORT_INDICES( S64 );
     322            break;
     323            case PS_TYPE_F32:
     324            SORT_INDICES( F32 );
     325            break;
     326            case PS_TYPE_F64:
     327            SORT_INDICES( F64 );
     328            break;
     329            default:
     330            psError( __func__, " : Line %d - Invalid psType\n", __LINE__ );
     331        }
     332       
    333333    // Free temp memory
    334     psFree(tmpVector);
    335 
     334    psFree( tmpVector );
     335   
    336336    return outVector;
    337337}
    338338
    339 static void vectorFree(psVector *restrict psVec)
    340 {
    341     if (psVec == NULL) {
    342         return;
    343     }
    344 
    345     psFree(psVec->data.V);
    346 }
     339static void vectorFree( psVector *restrict psVec )
     340{
     341    if ( psVec == NULL ) {
     342            return ;
     343        }
     344       
     345    psFree( psVec->data.V );
     346}
Note: See TracChangeset for help on using the changeset viewer.