Changeset 1360 for trunk/psLib/src/collections/psVector.c
- Timestamp:
- Jul 30, 2004, 4:28:29 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psVector.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psVector.c
r1305 r1360 1 1 /** @file psVector.c 2 *3 * @brief Contains support for basic vector types4 *5 * This file defines the basic type for a vector struct and functions useful6 * in manupulating vectors.7 *8 * @author Ross Harman, MHPCC9 *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 Hawaii14 */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 */ 15 15 16 16 /******************************************************************************/ … … 54 54 /* FUNCTION IMPLEMENTATION - LOCAL */ 55 55 /*****************************************************************************/ 56 static void vectorFree( psVector *restrict psVec);56 static void vectorFree( psVector *restrict psVec ); 57 57 58 58 /*****************************************************************************/ 59 59 /* FUNCTION IMPLEMENTATION - PUBLIC */ 60 60 /*****************************************************************************/ 61 psVector* psVectorAlloc( unsigned int nalloc, psElemType elemType)62 { 63 psVector * psVec = NULL;61 psVector* psVectorAlloc( unsigned int nalloc, psElemType elemType ) 62 { 63 psVector * psVec = NULL; 64 64 int elementSize = 0; 65 65 66 66 // 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 74 74 // 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 78 78 psVec->type.dimen = PS_DIMEN_VECTOR; 79 79 psVec->type.type = elemType; 80 80 psVec->nalloc = nalloc; 81 81 psVec->n = nalloc; 82 82 83 83 // Create vector data array 84 psVec->data.V = psAlloc( nalloc*elementSize);85 84 psVec->data.V = psAlloc( nalloc * elementSize ); 85 86 86 return psVec; 87 87 } 88 88 89 psVector *psVectorRealloc( unsigned int nalloc, psVector *restrict in)89 psVector *psVectorRealloc( unsigned int nalloc, psVector *restrict in ) 90 90 { 91 91 int elementSize = 0; 92 92 psElemType elemType; 93 93 94 94 // 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 size104 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 elements111 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 115 115 return in; 116 116 } 117 117 118 psVector *psVectorRecycle( psVector *restrict in, psElemType type, unsigned int nalloc)118 psVector *psVectorRecycle( psVector *restrict in, psElemType type, unsigned int nalloc ) 119 119 { 120 120 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 126 126 elemType = in->type.type; 127 128 if ( in->nalloc == nalloc && elemType == type) {129 // it is proper size/type already130 return in;131 }132 127 128 if ( in->nalloc == nalloc && elemType == type ) { 129 // it is proper size/type already 130 return in; 131 } 132 133 133 // 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 144 143 in->type.type = type; 145 144 in->nalloc = nalloc; 146 145 in->n = nalloc; 146 147 147 return in; 148 148 } 149 149 150 psVector *psVectorSort( psVector *restrict outVector, const psVector *restrict inVector)150 psVector *psVectorSort( psVector *restrict outVector, const psVector *restrict inVector ) 151 151 { 152 152 int inN = 0; … … 156 156 void *outVec = NULL; 157 157 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 164 164 inType = inVector->type.type; 165 165 inN = inVector->n; 166 166 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 174 174 outN = outVector->n; 175 175 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 199 199 // Copy input vector values into output vector 200 memcpy( outVec, inVec, elSize*outN);201 200 memcpy( outVec, inVec, elSize * outN ); 201 202 202 // 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 238 238 return outVector; 239 239 } … … 241 241 #define SORT_INDICES(TYPE) \ 242 242 for(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 252 psVector *psVectorSortIndex( psVector *restrict outVector, const psVector *restrict inVector ) 253 253 { 254 254 int inN = 0; … … 261 261 psVector *tmpVector = NULL; 262 262 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 269 269 inN = inVector->n; 270 270 inVec = inVector->data.V; 271 271 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 278 278 outN = outVector->n; 279 279 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 ); 294 294 tmpVector->n = inN; 295 tmpVector = psVectorSort( tmpVector, inVector);296 295 tmpVector = psVectorSort( tmpVector, inVector ); 296 297 297 // 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 333 333 // Free temp memory 334 psFree( tmpVector);335 334 psFree( tmpVector ); 335 336 336 return outVector; 337 337 } 338 338 339 static void vectorFree( psVector *restrict psVec)340 { 341 if ( psVec == NULL) {342 return;343 }344 345 psFree( psVec->data.V);346 } 339 static 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.
