Changeset 1407 for trunk/psLib/src/collections/psVector.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psVector.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psVector.c
r1406 r1407 1 1 2 /** @file psVector.c 2 3 * … … 8 9 * @author Ross Harman, MHPCC 9 10 * 10 * @version $Revision: 1.2 0$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-08-0 6 22:34:05$11 * @version $Revision: 1.21 $ $Name: not supported by cvs2svn $ 12 * @date $Date: 2004-08-07 00:06:06 $ 12 13 * 13 14 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 15 16 16 17 /******************************************************************************/ 18 17 19 /* INCLUDE FILES */ 18 /******************************************************************************/ 19 #include <string.h> // for memcpy 20 21 /******************************************************************************/ 22 #include <string.h> // for memcpy 20 23 #include <stdlib.h> 21 24 #include <math.h> … … 28 31 29 32 /******************************************************************************/ 33 30 34 /* DEFINE STATEMENTS */ 35 31 36 /******************************************************************************/ 32 37 … … 34 39 35 40 /******************************************************************************/ 41 36 42 /* TYPE DEFINITIONS */ 43 37 44 /******************************************************************************/ 38 45 … … 40 47 41 48 /*****************************************************************************/ 49 42 50 /* GLOBAL VARIABLES */ 51 43 52 /*****************************************************************************/ 44 53 … … 46 55 47 56 /*****************************************************************************/ 57 48 58 /* FILE STATIC VARIABLES */ 59 49 60 /*****************************************************************************/ 50 61 … … 52 63 53 64 /*****************************************************************************/ 65 54 66 /* FUNCTION IMPLEMENTATION - LOCAL */ 55 /*****************************************************************************/ 56 static void vectorFree( psVector *restrict psVec ); 57 58 /*****************************************************************************/ 67 68 /*****************************************************************************/ 69 static void vectorFree(psVector * restrict psVec); 70 71 /*****************************************************************************/ 72 59 73 /* FUNCTION IMPLEMENTATION - PUBLIC */ 60 /*****************************************************************************/ 61 psVector* psVectorAlloc( unsigned int nalloc, psElemType elemType ) 62 { 63 psVector * psVec = NULL; 74 75 /*****************************************************************************/ 76 psVector *psVectorAlloc(unsigned int nalloc, psElemType elemType) 77 { 78 psVector *psVec = NULL; 64 79 int elementSize = 0; 65 80 66 81 // 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); 69 84 return NULL; 70 85 } 71 86 72 elementSize = PSELEMTYPE_SIZEOF( elemType);87 elementSize = PSELEMTYPE_SIZEOF(elemType); 73 88 74 89 // 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); 77 92 78 93 psVec->type.dimen = PS_DIMEN_VECTOR; … … 82 97 83 98 // Create vector data array 84 psVec->data.V = psAlloc( nalloc * elementSize);99 psVec->data.V = psAlloc(nalloc * elementSize); 85 100 86 101 return psVec; 87 102 } 88 103 89 psVector *psVectorRealloc( unsigned int nalloc, psVector *restrict in)104 psVector *psVectorRealloc(unsigned int nalloc, psVector * restrict in) 90 105 { 91 106 int elementSize = 0; … … 93 108 94 109 // 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); 97 112 return NULL; 98 113 } 99 114 100 if ( in == NULL) {101 psError( __func__, "Null input vector\n");115 if (in == NULL) { 116 psError(__func__, "Null input vector\n"); 102 117 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; 114 123 } 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 } 115 128 116 129 return in; 117 130 } 118 131 119 psVector *psVectorRecycle( psVector *restrict in, unsigned int nalloc, psElemType type)132 psVector *psVectorRecycle(psVector * restrict in, unsigned int nalloc, psElemType type) 120 133 { 121 134 psElemType elemType; 122 135 123 if ( in == NULL) {124 return psVectorAlloc( nalloc, type);136 if (in == NULL) { 137 return psVectorAlloc(nalloc, type); 125 138 } 126 139 127 140 elemType = in->type.type; 128 141 129 if ( in->nalloc == nalloc && elemType == type) {142 if (in->nalloc == nalloc && elemType == type) { 130 143 // it is proper size/type already 131 144 return in; 132 145 } 133 134 146 // 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); 138 150 return NULL; 139 151 } 140 152 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)); 143 154 144 155 in->type.type = type; … … 149 160 } 150 161 151 psVector *psVectorSort( psVector *restrict outVector, const psVector *restrict inVector)162 psVector *psVectorSort(psVector * restrict outVector, const psVector * restrict inVector) 152 163 { 153 164 int inN = 0; … … 158 169 psElemType inType = 0; 159 170 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__); 162 173 return outVector; 163 174 } … … 166 177 inN = inVector->n; 167 178 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); 172 183 outVector->n = inVector->n; 173 184 } … … 176 187 outVec = outVector->data.V; 177 188 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 } 200 210 // Copy input vector values into output vector 201 memcpy( outVec, inVec, elSize * outN);211 memcpy(outVec, inVec, elSize * outN); 202 212 203 213 // Sort output vector 204 switch ( inType) {214 switch (inType) { 205 215 case PS_TYPE_U8: 206 qsort( outVec, inN, elSize, psCompareU8);216 qsort(outVec, inN, elSize, psCompareU8); 207 217 break; 208 218 case PS_TYPE_U16: 209 qsort( outVec, inN, elSize, psCompareU16);219 qsort(outVec, inN, elSize, psCompareU16); 210 220 break; 211 221 case PS_TYPE_U32: 212 qsort( outVec, inN, elSize, psCompareU32);222 qsort(outVec, inN, elSize, psCompareU32); 213 223 break; 214 224 case PS_TYPE_U64: 215 qsort( outVec, inN, elSize, psCompareU64);225 qsort(outVec, inN, elSize, psCompareU64); 216 226 break; 217 227 case PS_TYPE_S8: 218 qsort( outVec, inN, elSize, psCompareS8);228 qsort(outVec, inN, elSize, psCompareS8); 219 229 break; 220 230 case PS_TYPE_S16: 221 qsort( outVec, inN, elSize, psCompareS16);231 qsort(outVec, inN, elSize, psCompareS16); 222 232 break; 223 233 case PS_TYPE_S32: 224 qsort( outVec, inN, elSize, psCompareS32);234 qsort(outVec, inN, elSize, psCompareS32); 225 235 break; 226 236 case PS_TYPE_S64: 227 qsort( outVec, inN, elSize, psCompareS64);237 qsort(outVec, inN, elSize, psCompareS64); 228 238 break; 229 239 case PS_TYPE_F32: 230 qsort( outVec, inN, elSize, psCompareF32);240 qsort(outVec, inN, elSize, psCompareF32); 231 241 break; 232 242 case PS_TYPE_F64: 233 qsort( outVec, inN, elSize, psCompareF64);243 qsort(outVec, inN, elSize, psCompareF64); 234 244 break; 235 245 default: 236 psError( __func__, " : Line %d - Invalid psType\n", __LINE__);246 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 237 247 } 238 248 … … 251 261 } 252 262 253 psVector *psVectorSortIndex( psVector *restrict outVector, const psVector *restrict inVector)263 psVector *psVectorSortIndex(psVector * restrict outVector, const psVector * restrict inVector) 254 264 { 255 265 int inN = 0; … … 263 273 psElemType inType = 0; 264 274 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__); 267 277 return outVector; 268 278 } … … 272 282 inType = inVector->type.type; 273 283 274 if ( outVector == NULL) {275 outVector = psVectorAlloc( inN, PS_TYPE_U32);284 if (outVector == NULL) { 285 outVector = psVectorAlloc(inN, PS_TYPE_U32); 276 286 outVector->n = inN; 277 287 } … … 280 290 outVec = outVector->data.V; 281 291 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); 295 305 tmpVector->n = inN; 296 tmpVector = psVectorSort( tmpVector, inVector);306 tmpVector = psVectorSort(tmpVector, inVector); 297 307 298 308 // Sort output vector 299 switch ( inType) {309 switch (inType) { 300 310 case PS_TYPE_U8: 301 SORT_INDICES( U8);311 SORT_INDICES(U8); 302 312 break; 303 313 case PS_TYPE_U16: 304 SORT_INDICES( U16);314 SORT_INDICES(U16); 305 315 break; 306 316 case PS_TYPE_U32: 307 SORT_INDICES( U32);317 SORT_INDICES(U32); 308 318 break; 309 319 case PS_TYPE_U64: 310 SORT_INDICES( U64);320 SORT_INDICES(U64); 311 321 break; 312 322 case PS_TYPE_S8: 313 SORT_INDICES( S8);323 SORT_INDICES(S8); 314 324 break; 315 325 case PS_TYPE_S16: 316 SORT_INDICES( S16);326 SORT_INDICES(S16); 317 327 break; 318 328 case PS_TYPE_S32: 319 SORT_INDICES( S32);329 SORT_INDICES(S32); 320 330 break; 321 331 case PS_TYPE_S64: 322 SORT_INDICES( S64);332 SORT_INDICES(S64); 323 333 break; 324 334 case PS_TYPE_F32: 325 SORT_INDICES( F32);335 SORT_INDICES(F32); 326 336 break; 327 337 case PS_TYPE_F64: 328 SORT_INDICES( F64);338 SORT_INDICES(F64); 329 339 break; 330 340 default: 331 psError( __func__, " : Line %d - Invalid psType\n", __LINE__);341 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 332 342 } 333 343 334 344 // Free temp memory 335 psFree( tmpVector);345 psFree(tmpVector); 336 346 337 347 return outVector; 338 348 } 339 349 340 static void vectorFree( psVector *restrict psVec)341 { 342 if ( psVec == NULL) {343 return ;344 } 345 346 psFree( psVec->data.V);347 } 350 static 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.
