IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 6, 2004, 2:06:06 PM (22 years ago)
Author:
desonia
Message:

another attempt to get astyle to get it right.

File:
1 edited

Legend:

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

    r1406 r1407  
     1
    12/** @file  psVector.c
    23*
     
    89*  @author Ross Harman, MHPCC
    910*
    10 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
    11 *  @date $Date: 2004-08-06 22:34:05 $
     11*  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2004-08-07 00:06:06 $
    1213*
    1314*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1516
    1617/******************************************************************************/
     18
    1719/*  INCLUDE FILES                                                             */
    18 /******************************************************************************/
    19 #include <string.h>        // for memcpy
     20
     21/******************************************************************************/
     22#include <string.h>                        // for memcpy
    2023#include <stdlib.h>
    2124#include <math.h>
     
    2831
    2932/******************************************************************************/
     33
    3034/*  DEFINE STATEMENTS                                                         */
     35
    3136/******************************************************************************/
    3237
     
    3439
    3540/******************************************************************************/
     41
    3642/*  TYPE DEFINITIONS                                                          */
     43
    3744/******************************************************************************/
    3845
     
    4047
    4148/*****************************************************************************/
     49
    4250/*  GLOBAL VARIABLES                                                         */
     51
    4352/*****************************************************************************/
    4453
     
    4655
    4756/*****************************************************************************/
     57
    4858/*  FILE STATIC VARIABLES                                                    */
     59
    4960/*****************************************************************************/
    5061
     
    5263
    5364/*****************************************************************************/
     65
    5466/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    55 /*****************************************************************************/
    56 static void vectorFree( psVector *restrict psVec );
    57 
    58 /*****************************************************************************/
     67
     68/*****************************************************************************/
     69static void vectorFree(psVector * restrict psVec);
     70
     71/*****************************************************************************/
     72
    5973/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    60 /*****************************************************************************/
    61 psVector* psVectorAlloc( unsigned int nalloc, psElemType elemType )
    62 {
    63     psVector * psVec = NULL;
     74
     75/*****************************************************************************/
     76psVector *psVectorAlloc(unsigned int nalloc, psElemType elemType)
     77{
     78    psVector *psVec = NULL;
    6479    int elementSize = 0;
    6580
    6681    // Invalid nalloc
    67     if ( nalloc < 1 ) {
    68         psError( __func__, "Invalid value for nalloc. nalloc: %d\n", nalloc );
     82    if (nalloc < 1) {
     83        psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);
    6984        return NULL;
    7085    }
    7186
    72     elementSize = PSELEMTYPE_SIZEOF( elemType );
     87    elementSize = PSELEMTYPE_SIZEOF(elemType);
    7388
    7489    // Create vector struct
    75     psVec = ( psVector * ) psAlloc( sizeof( psVector ) );
    76     p_psMemSetDeallocator( psVec, ( psFreeFcn ) vectorFree );
     90    psVec = (psVector *) psAlloc(sizeof(psVector));
     91    p_psMemSetDeallocator(psVec, (psFreeFcn) vectorFree);
    7792
    7893    psVec->type.dimen = PS_DIMEN_VECTOR;
     
    8297
    8398    // Create vector data array
    84     psVec->data.V = psAlloc( nalloc * elementSize );
     99    psVec->data.V = psAlloc(nalloc * elementSize);
    85100
    86101    return psVec;
    87102}
    88103
    89 psVector *psVectorRealloc( unsigned int nalloc, psVector *restrict in )
     104psVector *psVectorRealloc(unsigned int nalloc, psVector * restrict in)
    90105{
    91106    int elementSize = 0;
     
    93108
    94109    // Invalid nalloc
    95     if ( nalloc < 1 ) {
    96         psError( __func__, "Invalid value for realloc (%d)\n", nalloc );
     110    if (nalloc < 1) {
     111        psError(__func__, "Invalid value for realloc (%d)\n", nalloc);
    97112        return NULL;
    98113    }
    99114
    100     if ( in == NULL ) {
    101         psError( __func__, "Null input vector\n" );
     115    if (in == NULL) {
     116        psError(__func__, "Null input vector\n");
    102117        return NULL;
    103     } else
    104         if ( in->nalloc != nalloc ) {                    // No need to realloc to same size
    105             elemType = in->type.type;
    106             elementSize = PSELEMTYPE_SIZEOF( elemType );
    107             if ( nalloc < in->n ) {
    108                 in->n = nalloc;
    109             }
    110 
    111             // Realloc after decrementation to avoid accessing freed array elements
    112             in->data.V = psRealloc( in->data.V, nalloc * elementSize );
    113             in->nalloc = nalloc;
     118    } else if (in->nalloc != nalloc) {     // No need to realloc to same size
     119        elemType = in->type.type;
     120        elementSize = PSELEMTYPE_SIZEOF(elemType);
     121        if (nalloc < in->n) {
     122            in->n = nalloc;
    114123        }
     124        // Realloc after decrementation to avoid accessing freed array elements
     125        in->data.V = psRealloc(in->data.V, nalloc * elementSize);
     126        in->nalloc = nalloc;
     127    }
    115128
    116129    return in;
    117130}
    118131
    119 psVector *psVectorRecycle( psVector *restrict in, unsigned int nalloc, psElemType type )
     132psVector *psVectorRecycle(psVector * restrict in, unsigned int nalloc, psElemType type)
    120133{
    121134    psElemType elemType;
    122135
    123     if ( in == NULL ) {
    124         return psVectorAlloc( nalloc, type );
     136    if (in == NULL) {
     137        return psVectorAlloc(nalloc, type);
    125138    }
    126139
    127140    elemType = in->type.type;
    128141
    129     if ( in->nalloc == nalloc && elemType == type ) {
     142    if (in->nalloc == nalloc && elemType == type) {
    130143        // it is proper size/type already
    131144        return in;
    132145    }
    133 
    134146    // Invalid nalloc
    135     if ( nalloc < 1 ) {
    136         psError( __func__, "Invalid value for nalloc (%d)\n", nalloc );
    137         psFree( in );
     147    if (nalloc < 1) {
     148        psError(__func__, "Invalid value for nalloc (%d)\n", nalloc);
     149        psFree(in);
    138150        return NULL;
    139151    }
    140152
    141 
    142     in->data.V = psRealloc( in->data.V, nalloc * PSELEMTYPE_SIZEOF( type ) );
     153    in->data.V = psRealloc(in->data.V, nalloc * PSELEMTYPE_SIZEOF(type));
    143154
    144155    in->type.type = type;
     
    149160}
    150161
    151 psVector *psVectorSort( psVector *restrict outVector, const psVector *restrict inVector )
     162psVector *psVectorSort(psVector * restrict outVector, const psVector * restrict inVector)
    152163{
    153164    int inN = 0;
     
    158169    psElemType inType = 0;
    159170
    160     if ( inVector == NULL ) {
    161         psError( __func__, " : Line %d - Null input vector\n", __LINE__ );
     171    if (inVector == NULL) {
     172        psError(__func__, " : Line %d - Null input vector\n", __LINE__);
    162173        return outVector;
    163174    }
     
    166177    inN = inVector->n;
    167178    inVec = inVector->data.V;
    168     elSize = PSELEMTYPE_SIZEOF( inType );
    169 
    170     if ( outVector == NULL ) {
    171         outVector = psVectorAlloc( inN, inType );
     179    elSize = PSELEMTYPE_SIZEOF(inType);
     180
     181    if (outVector == NULL) {
     182        outVector = psVectorAlloc(inN, inType);
    172183        outVector->n = inVector->n;
    173184    }
     
    176187    outVec = outVector->data.V;
    177188
    178     if ( inN != outN ) {
    179         psError( __func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", __LINE__,
    180                  inN, outN );
    181         return outVector;
    182     }
    183 
    184     if ( inType != outVector->type.type ) {
    185         psError( __func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__,
    186                  inType, outVector->type.type );
    187         return outVector;
    188     }
    189 
    190     if ( inN == 0 ) {
    191         psError( __func__, " : Line %d - No elements in use for input vector\n", __LINE__ );
    192         return outVector;
    193     }
    194 
    195     if ( outN == 0 ) {
    196         psError( __func__, " : Line %d - No elements in use for output vector\n", __LINE__ );
    197         return outVector;
    198     }
    199 
     189    if (inN != outN) {
     190        psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
     191                __LINE__, inN, outN);
     192        return outVector;
     193    }
     194
     195    if (inType != outVector->type.type) {
     196        psError(__func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__,
     197                inType, outVector->type.type);
     198        return outVector;
     199    }
     200
     201    if (inN == 0) {
     202        psError(__func__, " : Line %d - No elements in use for input vector\n", __LINE__);
     203        return outVector;
     204    }
     205
     206    if (outN == 0) {
     207        psError(__func__, " : Line %d - No elements in use for output vector\n", __LINE__);
     208        return outVector;
     209    }
    200210    // Copy input vector values into output vector
    201     memcpy( outVec, inVec, elSize * outN );
     211    memcpy(outVec, inVec, elSize * outN);
    202212
    203213    // Sort output vector
    204     switch ( inType ) {
     214    switch (inType) {
    205215    case PS_TYPE_U8:
    206         qsort( outVec, inN, elSize, psCompareU8 );
     216        qsort(outVec, inN, elSize, psCompareU8);
    207217        break;
    208218    case PS_TYPE_U16:
    209         qsort( outVec, inN, elSize, psCompareU16 );
     219        qsort(outVec, inN, elSize, psCompareU16);
    210220        break;
    211221    case PS_TYPE_U32:
    212         qsort( outVec, inN, elSize, psCompareU32 );
     222        qsort(outVec, inN, elSize, psCompareU32);
    213223        break;
    214224    case PS_TYPE_U64:
    215         qsort( outVec, inN, elSize, psCompareU64 );
     225        qsort(outVec, inN, elSize, psCompareU64);
    216226        break;
    217227    case PS_TYPE_S8:
    218         qsort( outVec, inN, elSize, psCompareS8 );
     228        qsort(outVec, inN, elSize, psCompareS8);
    219229        break;
    220230    case PS_TYPE_S16:
    221         qsort( outVec, inN, elSize, psCompareS16 );
     231        qsort(outVec, inN, elSize, psCompareS16);
    222232        break;
    223233    case PS_TYPE_S32:
    224         qsort( outVec, inN, elSize, psCompareS32 );
     234        qsort(outVec, inN, elSize, psCompareS32);
    225235        break;
    226236    case PS_TYPE_S64:
    227         qsort( outVec, inN, elSize, psCompareS64 );
     237        qsort(outVec, inN, elSize, psCompareS64);
    228238        break;
    229239    case PS_TYPE_F32:
    230         qsort( outVec, inN, elSize, psCompareF32 );
     240        qsort(outVec, inN, elSize, psCompareF32);
    231241        break;
    232242    case PS_TYPE_F64:
    233         qsort( outVec, inN, elSize, psCompareF64 );
     243        qsort(outVec, inN, elSize, psCompareF64);
    234244        break;
    235245    default:
    236         psError( __func__, " : Line %d - Invalid psType\n", __LINE__ );
     246        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
    237247    }
    238248
     
    251261}
    252262
    253 psVector *psVectorSortIndex( psVector *restrict outVector, const psVector *restrict inVector )
     263psVector *psVectorSortIndex(psVector * restrict outVector, const psVector * restrict inVector)
    254264{
    255265    int inN = 0;
     
    263273    psElemType inType = 0;
    264274
    265     if ( inVector == NULL ) {
    266         psError( __func__, " : Line %d - Null input vector\n", __LINE__ );
     275    if (inVector == NULL) {
     276        psError(__func__, " : Line %d - Null input vector\n", __LINE__);
    267277        return outVector;
    268278    }
     
    272282    inType = inVector->type.type;
    273283
    274     if ( outVector == NULL ) {
    275         outVector = psVectorAlloc( inN, PS_TYPE_U32 );
     284    if (outVector == NULL) {
     285        outVector = psVectorAlloc(inN, PS_TYPE_U32);
    276286        outVector->n = inN;
    277287    }
     
    280290    outVec = outVector->data.V;
    281291
    282     if ( inN != outN ) {
    283         psError( __func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
    284                  __LINE__, inN, outN );
    285         return outVector;
    286     }
    287 
    288     if ( outVector->type.type != PS_TYPE_U32 ) {
    289         psError( __func__, " : Line %d - Output vector is not of type U32: out=%d\n",
    290                  __LINE__, outVector->type.type );
    291         return outVector;
    292     }
    293 
    294     tmpVector = psVectorAlloc( inN, inType );
     292    if (inN != outN) {
     293        psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
     294                __LINE__, inN, outN);
     295        return outVector;
     296    }
     297
     298    if (outVector->type.type != PS_TYPE_U32) {
     299        psError(__func__, " : Line %d - Output vector is not of type U32: out=%d\n",
     300                __LINE__, outVector->type.type);
     301        return outVector;
     302    }
     303
     304    tmpVector = psVectorAlloc(inN, inType);
    295305    tmpVector->n = inN;
    296     tmpVector = psVectorSort( tmpVector, inVector );
     306    tmpVector = psVectorSort(tmpVector, inVector);
    297307
    298308    // Sort output vector
    299     switch ( inType ) {
     309    switch (inType) {
    300310    case PS_TYPE_U8:
    301         SORT_INDICES( U8 );
     311        SORT_INDICES(U8);
    302312        break;
    303313    case PS_TYPE_U16:
    304         SORT_INDICES( U16 );
     314        SORT_INDICES(U16);
    305315        break;
    306316    case PS_TYPE_U32:
    307         SORT_INDICES( U32 );
     317        SORT_INDICES(U32);
    308318        break;
    309319    case PS_TYPE_U64:
    310         SORT_INDICES( U64 );
     320        SORT_INDICES(U64);
    311321        break;
    312322    case PS_TYPE_S8:
    313         SORT_INDICES( S8 );
     323        SORT_INDICES(S8);
    314324        break;
    315325    case PS_TYPE_S16:
    316         SORT_INDICES( S16 );
     326        SORT_INDICES(S16);
    317327        break;
    318328    case PS_TYPE_S32:
    319         SORT_INDICES( S32 );
     329        SORT_INDICES(S32);
    320330        break;
    321331    case PS_TYPE_S64:
    322         SORT_INDICES( S64 );
     332        SORT_INDICES(S64);
    323333        break;
    324334    case PS_TYPE_F32:
    325         SORT_INDICES( F32 );
     335        SORT_INDICES(F32);
    326336        break;
    327337    case PS_TYPE_F64:
    328         SORT_INDICES( F64 );
     338        SORT_INDICES(F64);
    329339        break;
    330340    default:
    331         psError( __func__, " : Line %d - Invalid psType\n", __LINE__ );
     341        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
    332342    }
    333343
    334344    // Free temp memory
    335     psFree( tmpVector );
     345    psFree(tmpVector);
    336346
    337347    return outVector;
    338348}
    339349
    340 static void vectorFree( psVector *restrict psVec )
    341 {
    342     if ( psVec == NULL ) {
    343         return ;
    344     }
    345 
    346     psFree( psVec->data.V );
    347 }
     350static void vectorFree(psVector * restrict psVec)
     351{
     352    if (psVec == NULL) {
     353        return;
     354    }
     355
     356    psFree(psVec->data.V);
     357}
Note: See TracChangeset for help on using the changeset viewer.