Changeset 1385 for trunk/psLib/src/collections/psVector.c
- Timestamp:
- Aug 4, 2004, 1:37:39 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psVector.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psVector.c
r1360 r1385 8 8 * @author Ross Harman, MHPCC 9 9 * 10 * @version $Revision: 1.1 7$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-0 7-31 02:28:10$10 * @version $Revision: 1.18 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-08-04 23:37:39 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 63 63 psVector * psVec = NULL; 64 64 int elementSize = 0; 65 65 66 66 // Invalid nalloc 67 67 if ( nalloc < 1 ) { 68 psError( __func__, "Invalid value for nalloc. nalloc: %d\n", nalloc );69 return NULL;70 }71 68 psError( __func__, "Invalid value for nalloc. nalloc: %d\n", nalloc ); 69 return NULL; 70 } 71 72 72 elementSize = PSELEMTYPE_SIZEOF( elemType ); 73 73 74 74 // Create vector struct 75 75 psVec = ( psVector * ) psAlloc( sizeof( psVector ) ); 76 76 p_psMemSetDeallocator( psVec, ( psFreeFcn ) vectorFree ); 77 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 84 psVec->data.V = psAlloc( nalloc * elementSize ); 85 85 86 86 return psVec; 87 87 } … … 91 91 int elementSize = 0; 92 92 psElemType elemType; 93 93 94 94 // Invalid nalloc 95 95 if ( nalloc < 1 ) { 96 psError( __func__, "Invalid value for realloc (%d)\n", nalloc );97 return NULL;98 }99 96 psError( __func__, "Invalid value for realloc (%d)\n", nalloc ); 97 return NULL; 98 } 99 100 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 101 psError( __func__, "Null input vector\n" ); 102 return NULL; 103 } else 104 if ( in->nalloc != nalloc ) { // No need to realloc to same size 104 105 elemType = in->type.type; 105 106 elementSize = PSELEMTYPE_SIZEOF( elemType ); 106 107 if ( nalloc < in->n ) { 107 in->n = nalloc;108 }109 108 in->n = nalloc; 109 } 110 110 111 // Realloc after decrementation to avoid accessing freed array elements 111 112 in->data.V = psRealloc( in->data.V, nalloc * elementSize ); 112 113 in->nalloc = nalloc; 113 114 } 114 115 115 116 return in; 116 117 } … … 119 120 { 120 121 psElemType elemType; 121 122 122 123 if ( in == NULL ) { 123 return psVectorAlloc( nalloc, type );124 }125 124 return psVectorAlloc( nalloc, type ); 125 } 126 126 127 elemType = in->type.type; 127 128 128 129 if ( in->nalloc == nalloc && elemType == type ) { 129 // it is proper size/type already130 return in;131 }132 130 // it is proper size/type already 131 return in; 132 } 133 133 134 // Invalid nalloc 134 135 if ( nalloc < 1 ) { 135 psError( __func__, "Invalid value for nalloc (%d)\n", nalloc );136 psFree( in );137 return NULL;138 }139 140 136 psError( __func__, "Invalid value for nalloc (%d)\n", nalloc ); 137 psFree( in ); 138 return NULL; 139 } 140 141 141 142 in->data.V = psRealloc( in->data.V, nalloc * PSELEMTYPE_SIZEOF( type ) ); 142 143 143 144 in->type.type = type; 144 145 in->nalloc = nalloc; 145 146 in->n = nalloc; 146 147 147 148 return in; 148 149 } … … 156 157 void *outVec = NULL; 157 158 psElemType inType = 0; 158 159 159 160 if ( inVector == NULL ) { 160 psError( __func__, " : Line %d - Null input vector\n", __LINE__ );161 return outVector;162 }163 161 psError( __func__, " : Line %d - Null input vector\n", __LINE__ ); 162 return outVector; 163 } 164 164 165 inType = inVector->type.type; 165 166 inN = inVector->n; 166 167 inVec = inVector->data.V; 167 168 elSize = PSELEMTYPE_SIZEOF( inType ); 168 169 169 170 if ( outVector == NULL ) { 170 outVector = psVectorAlloc( inN, inType );171 outVector->n = inVector->n;172 }173 171 outVector = psVectorAlloc( inN, inType ); 172 outVector->n = inVector->n; 173 } 174 174 175 outN = outVector->n; 175 176 outVec = outVector->data.V; 176 177 177 178 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 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 183 184 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 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 189 190 if ( inN == 0 ) { 190 psError( __func__, " : Line %d - No elements in use for input vector\n", __LINE__ );191 return outVector;192 }193 191 psError( __func__, " : Line %d - No elements in use for input vector\n", __LINE__ ); 192 return outVector; 193 } 194 194 195 if ( outN == 0 ) { 195 psError( __func__, " : Line %d - No elements in use for output vector\n", __LINE__ );196 return outVector;197 }198 196 psError( __func__, " : Line %d - No elements in use for output vector\n", __LINE__ ); 197 return outVector; 198 } 199 199 200 // Copy input vector values into output vector 200 201 memcpy( outVec, inVec, elSize * outN ); 201 202 202 203 // Sort output vector 203 204 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 205 case PS_TYPE_U8: 206 qsort( outVec, inN, elSize, psCompareU8 ); 207 break; 208 case PS_TYPE_U16: 209 qsort( outVec, inN, elSize, psCompareU16 ); 210 break; 211 case PS_TYPE_U32: 212 qsort( outVec, inN, elSize, psCompareU32 ); 213 break; 214 case PS_TYPE_U64: 215 qsort( outVec, inN, elSize, psCompareU64 ); 216 break; 217 case PS_TYPE_S8: 218 qsort( outVec, inN, elSize, psCompareS8 ); 219 break; 220 case PS_TYPE_S16: 221 qsort( outVec, inN, elSize, psCompareS16 ); 222 break; 223 case PS_TYPE_S32: 224 qsort( outVec, inN, elSize, psCompareS32 ); 225 break; 226 case PS_TYPE_S64: 227 qsort( outVec, inN, elSize, psCompareS64 ); 228 break; 229 case PS_TYPE_F32: 230 qsort( outVec, inN, elSize, psCompareF32 ); 231 break; 232 case PS_TYPE_F64: 233 qsort( outVec, inN, elSize, psCompareF64 ); 234 break; 235 default: 236 psError( __func__, " : Line %d - Invalid psType\n", __LINE__ ); 237 } 238 238 239 return outVector; 239 240 } … … 241 242 #define SORT_INDICES(TYPE) \ 242 243 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 244 for(j=0; j<inN; j++) { \ 245 diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]); \ 246 if(diff < FLT_EPSILON) { \ 247 outVec[i] = j; \ 248 break; \ 249 } \ 250 } \ 251 } 252 252 253 psVector *psVectorSortIndex( psVector *restrict outVector, const psVector *restrict inVector ) 253 254 { … … 261 262 psVector *tmpVector = NULL; 262 263 psElemType inType = 0; 263 264 264 265 if ( inVector == NULL ) { 265 psError( __func__, " : Line %d - Null input vector\n", __LINE__ );266 return outVector;267 }268 266 psError( __func__, " : Line %d - Null input vector\n", __LINE__ ); 267 return outVector; 268 } 269 269 270 inN = inVector->n; 270 271 inVec = inVector->data.V; 271 272 inType = inVector->type.type; 272 273 273 274 if ( outVector == NULL ) { 274 outVector = psVectorAlloc( inN, PS_TYPE_U32 );275 outVector->n = inN;276 }277 275 outVector = psVectorAlloc( inN, PS_TYPE_U32 ); 276 outVector->n = inN; 277 } 278 278 279 outN = outVector->n; 279 280 outVec = outVector->data.V; 280 281 281 282 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 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 287 288 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 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 293 294 tmpVector = psVectorAlloc( inN, inType ); 294 295 tmpVector->n = inN; 295 296 tmpVector = psVectorSort( tmpVector, inVector ); 296 297 297 298 // Sort output vector 298 299 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 300 case PS_TYPE_U8: 301 SORT_INDICES( U8 ); 302 break; 303 case PS_TYPE_U16: 304 SORT_INDICES( U16 ); 305 break; 306 case PS_TYPE_U32: 307 SORT_INDICES( U32 ); 308 break; 309 case PS_TYPE_U64: 310 SORT_INDICES( U64 ); 311 break; 312 case PS_TYPE_S8: 313 SORT_INDICES( S8 ); 314 break; 315 case PS_TYPE_S16: 316 SORT_INDICES( S16 ); 317 break; 318 case PS_TYPE_S32: 319 SORT_INDICES( S32 ); 320 break; 321 case PS_TYPE_S64: 322 SORT_INDICES( S64 ); 323 break; 324 case PS_TYPE_F32: 325 SORT_INDICES( F32 ); 326 break; 327 case PS_TYPE_F64: 328 SORT_INDICES( F64 ); 329 break; 330 default: 331 psError( __func__, " : Line %d - Invalid psType\n", __LINE__ ); 332 } 333 333 334 // Free temp memory 334 335 psFree( tmpVector ); 335 336 336 337 return outVector; 337 338 } … … 340 341 { 341 342 if ( psVec == NULL ) { 342 return ;343 }344 343 return ; 344 } 345 345 346 psFree( psVec->data.V ); 346 347 }
Note:
See TracChangeset
for help on using the changeset viewer.
