Changeset 1807 for trunk/psLib/src/collections/psVector.c
- Timestamp:
- Sep 14, 2004, 10:01:52 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psVector.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psVector.c
r1761 r1807 10 10 * @author Robert DeSonia, MHPCC 11 11 * 12 * @version $Revision: 1.2 3$ $Name: not supported by cvs2svn $13 * @date $Date: 2004-09- 09 21:59:03$12 * @version $Revision: 1.24 $ $Name: not supported by cvs2svn $ 13 * @date $Date: 2004-09-14 20:01:52 $ 14 14 * 15 15 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 25 25 #include "psLogMsg.h" 26 26 #include "psCompare.h" 27 27 28 #include "psCollectionsErrors.h" 28 29 29 30 static void vectorFree(psVector* restrict psVec); 31 32 33 static void vectorFree(psVector* restrict psVec) 34 { 35 if (psVec == NULL) { 36 return; 37 } 38 39 psFree(psVec->data.V); 40 } 30 41 31 42 // FUNCTION IMPLEMENTATION - PUBLIC … … 35 46 psVector* psVec = NULL; 36 47 int elementSize = 0; 37 38 // Invalid nalloc39 if (nalloc < 1) {40 psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);41 return NULL;42 }43 48 44 49 elementSize = PSELEMTYPE_SIZEOF(elemType); … … 64 69 psElemType elemType; 65 70 66 // Invalid nalloc67 if (nalloc < 1) {68 psError(__func__, "Invalid value for realloc (%d)\n", nalloc);69 return NULL;70 }71 72 71 if (in == NULL) { 73 psError(__func__, "Null input vector\n"); 72 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorRealloc", 73 PS_ERR_BAD_PARAMETER_NULL, true, 74 PS_ERRORTEXT_psVector_REALLOC_NULL); 74 75 return NULL; 75 76 } else if (in->nalloc != nalloc) { // No need to realloc to same size … … 101 102 return in; 102 103 } 103 // Invalid nalloc104 if (nalloc < 1) {105 psError(__func__, "Invalid value for nalloc (%d)\n", nalloc);106 psFree(in);107 return NULL;108 }109 104 110 105 in->data.V = psRealloc(in->data.V, nalloc * PSELEMTYPE_SIZEOF(type)); … … 119 114 psVector* psVectorSort(psVector* restrict outVector, const psVector* restrict inVector) 120 115 { 121 int inN = 0; 122 int outN = 0; 116 int N = 0; 123 117 int elSize = 0; 124 118 void *inVec = NULL; … … 127 121 128 122 if (inVector == NULL) { 129 psError(__func__, " : Line %d - Null input vector\n", __LINE__); 130 return outVector; 123 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort", 124 PS_ERR_BAD_PARAMETER_NULL, true, 125 PS_ERRORTEXT_psVector_SORT_NULL); 126 psFree(outVector); 127 return NULL; 131 128 } 132 129 133 130 inType = inVector->type.type; 134 inN = inVector->n;131 N = inVector->n; 135 132 inVec = inVector->data.V; 136 133 elSize = PSELEMTYPE_SIZEOF(inType); 137 134 138 135 if (outVector == NULL) { 139 outVector = psVectorAlloc(inN, inType); 140 outVector->n = inVector->n; 141 } 142 143 outN = outVector->n; 136 outVector = psVectorAlloc(N, inType); 137 } 138 139 // check to see if output vector needs to be resized/retyped 140 if ( (N > outVector->nalloc) || 141 (inType != outVector->type.type) ) { 142 // reshape the output vector to match the input vector's size/type. 143 outVector = psVectorRecycle(outVector,N,inType); 144 } 145 outVector->n = N; 144 146 outVec = outVector->data.V; 145 147 146 if (inN != outN) { 147 psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", 148 __LINE__, inN, outN); 148 if (N == 0) { 149 // no need to sort anything, as there are no elements in input vector. 149 150 return outVector; 150 151 } 151 152 152 if (inType != outVector->type.type) { 153 psError(__func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__, 154 inType, outVector->type.type); 155 return outVector; 156 } 157 158 if (inN == 0) { 159 psError(__func__, " : Line %d - No elements in use for input vector\n", __LINE__); 160 return outVector; 161 } 162 163 if (outN == 0) { 164 psError(__func__, " : Line %d - No elements in use for output vector\n", __LINE__); 165 return outVector; 166 } 167 // Copy input vector values into output vector 168 memcpy(outVec, inVec, elSize * outN); 153 // Copy input vector values into output vector if not in-place sorting 154 if (inVector != outVector) { 155 memcpy(outVec, inVec, elSize * N); 156 } 169 157 170 158 // Sort output vector 171 159 switch (inType) { 172 160 case PS_TYPE_U8: 173 qsort(outVec, inN, elSize, psCompareU8);161 qsort(outVec, N, elSize, psCompareU8); 174 162 break; 175 163 case PS_TYPE_U16: 176 qsort(outVec, inN, elSize, psCompareU16);164 qsort(outVec, N, elSize, psCompareU16); 177 165 break; 178 166 case PS_TYPE_U32: 179 qsort(outVec, inN, elSize, psCompareU32);167 qsort(outVec, N, elSize, psCompareU32); 180 168 break; 181 169 case PS_TYPE_U64: 182 qsort(outVec, inN, elSize, psCompareU64);170 qsort(outVec, N, elSize, psCompareU64); 183 171 break; 184 172 case PS_TYPE_S8: 185 qsort(outVec, inN, elSize, psCompareS8);173 qsort(outVec, N, elSize, psCompareS8); 186 174 break; 187 175 case PS_TYPE_S16: 188 qsort(outVec, inN, elSize, psCompareS16);176 qsort(outVec, N, elSize, psCompareS16); 189 177 break; 190 178 case PS_TYPE_S32: 191 qsort(outVec, inN, elSize, psCompareS32);179 qsort(outVec, N, elSize, psCompareS32); 192 180 break; 193 181 case PS_TYPE_S64: 194 qsort(outVec, inN, elSize, psCompareS64);182 qsort(outVec, N, elSize, psCompareS64); 195 183 break; 196 184 case PS_TYPE_F32: 197 qsort(outVec, inN, elSize, psCompareF32);185 qsort(outVec, N, elSize, psCompareF32); 198 186 break; 199 187 case PS_TYPE_F64: 200 qsort(outVec, inN, elSize, psCompareF64);188 qsort(outVec, N, elSize, psCompareF64); 201 189 break; 202 190 default: 203 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 191 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort", 192 PS_ERR_BAD_PARAMETER_TYPE, true, 193 PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE, 194 inType); 204 195 } 205 196 … … 207 198 } 208 199 209 #define SORT_INDICES(TYPE) \210 for(i=0; i<inN; i++) { \211 for(j=0; j<inN; j++) { \212 diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]); \213 if(diff < FLT_EPSILON) { \214 outVec[i] = j; \215 break; \216 } \217 } \218 }219 220 200 psVector* psVectorSortIndex(psVector* restrict outVector, const psVector* restrict inVector) 221 201 { 222 int inN = 0; 223 int outN = 0; 224 int i = 0; 225 int j = 0; 226 float *inVec = NULL; 227 int *outVec = NULL; 228 double diff = 0.0f; 202 int N = 0; 229 203 psVector* tmpVector = NULL; 230 204 psElemType inType = 0; 205 psU32* outVec; 231 206 232 207 if (inVector == NULL) { 233 psError(__func__, " : Line %d - Null input vector\n", __LINE__); 208 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex", 209 PS_ERR_BAD_PARAMETER_NULL, true, 210 PS_ERRORTEXT_psVector_SORT_NULL); 211 psFree(outVector); 212 return NULL; 213 } 214 215 N = inVector->n; 216 217 if (outVector == NULL) { 218 outVector = psVectorAlloc(N, PS_TYPE_U32); 219 } 220 221 // check to see if output vector needs to be resized/retyped 222 if ( (N > outVector->nalloc) || 223 (outVector->type.type != PS_TYPE_U32) ) { 224 // reshape the output vector to match the input vector's size/type. 225 outVector = psVectorRecycle(outVector,N,PS_TYPE_U32); 226 } 227 outVector->n = N; 228 outVec = outVector->data.U32; 229 230 if (N == 0) { 231 // no need to sort anything, as there are no elements in input vector. 234 232 return outVector; 235 233 } 236 234 237 inN = inVector->n;238 inVec = inVector->data.V;239 inType = inVector->type.type;240 241 if (outVector == NULL) {242 outVector = psVectorAlloc(inN, PS_TYPE_U32);243 outVector->n = inN;244 }245 246 outN = outVector->n;247 outVec = outVector->data.V;248 249 if (inN != outN) {250 psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",251 __LINE__, inN, outN);252 return outVector;253 }254 255 if (outVector->type.type != PS_TYPE_U32) {256 psError(__func__, " : Line %d - Output vector is not of type U32: out=%d\n",257 __LINE__, outVector->type.type);258 return outVector;259 }260 261 tmpVector = psVectorAlloc(inN, inType);262 tmpVector->n = inN;263 235 tmpVector = psVectorSort(tmpVector, inVector); 236 237 #define SORT_INDICES(TYPE,absfcn,maxError) { \ 238 ps##TYPE* inVec = inVector->data.TYPE; \ 239 ps##TYPE* tmpVec = tmpVector->data.TYPE; \ 240 ps##TYPE diff; \ 241 for(int i=0; i<N; i++) { \ 242 for(int j=0; j<N; j++) { \ 243 diff = absfcn(tmpVec[i] - inVec[j]); \ 244 if(diff < maxError) { \ 245 outVec[i] = j; \ 246 break; \ 247 } \ 248 } \ 249 } \ 250 } 264 251 265 252 // Sort output vector 266 253 switch (inType) { 267 254 case PS_TYPE_U8: 268 SORT_INDICES(U8 );255 SORT_INDICES(U8,/* no absfcn needed */,1); 269 256 break; 270 257 case PS_TYPE_U16: 271 SORT_INDICES(U16 );258 SORT_INDICES(U16,/* no absfcn needed */,1); 272 259 break; 273 260 case PS_TYPE_U32: 274 SORT_INDICES(U32 );261 SORT_INDICES(U32,/* no absfcn needed */,1); 275 262 break; 276 263 case PS_TYPE_U64: 277 SORT_INDICES(U64 );264 SORT_INDICES(U64,/* no absfcn needed */,1); 278 265 break; 279 266 case PS_TYPE_S8: 280 SORT_INDICES(S8 );267 SORT_INDICES(S8,/* no absfcn needed */,1); 281 268 break; 282 269 case PS_TYPE_S16: 283 SORT_INDICES(S16 );270 SORT_INDICES(S16,/* no absfcn needed */,1); 284 271 break; 285 272 case PS_TYPE_S32: 286 SORT_INDICES(S32 );273 SORT_INDICES(S32,/* no absfcn needed */,1); 287 274 break; 288 275 case PS_TYPE_S64: 289 SORT_INDICES(S64 );276 SORT_INDICES(S64,/* no absfcn needed */,1); 290 277 break; 291 278 case PS_TYPE_F32: 292 SORT_INDICES(F32 );279 SORT_INDICES(F32,fabsf,FLT_EPSILON); 293 280 break; 294 281 case PS_TYPE_F64: 295 SORT_INDICES(F64); 282 SORT_INDICES(F64,fabs,DBL_EPSILON); 283 break; 284 case PS_TYPE_C32: 285 SORT_INDICES(F64,cabsf,FLT_EPSILON); 286 break; 287 case PS_TYPE_C64: 288 SORT_INDICES(F64,cabs,DBL_EPSILON); 296 289 break; 297 290 default: 298 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 291 psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex", 292 PS_ERR_BAD_PARAMETER_TYPE, true, 293 PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE, 294 inType); 299 295 } 300 296 … … 304 300 return outVector; 305 301 } 306 307 static void vectorFree(psVector* restrict psVec)308 {309 if (psVec == NULL) {310 return;311 }312 313 psFree(psVec->data.V);314 }
Note:
See TracChangeset
for help on using the changeset viewer.
