Changeset 1233 for trunk/psLib/src/collections/psVector.c
- Timestamp:
- Jul 15, 2004, 1:52:34 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psVector.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psVector.c
r1228 r1233 8 8 * @author Ross Harman, MHPCC 9 9 * 10 * @version $Revision: 1.1 3$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-07-15 2 2:18:02$10 * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-07-15 23:52:33 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 17 17 /* INCLUDE FILES */ 18 18 /******************************************************************************/ 19 #include <string.h> // for memcpy 20 #include <stdlib.h> 21 #include <math.h> 22 19 23 #include "psMemory.h" 20 24 #include "psError.h" 21 25 #include "psVector.h" 22 26 #include "psLogMsg.h" 27 #include "psCompare.h" 23 28 24 29 /******************************************************************************/ … … 143 148 } 144 149 150 psVector *psVectorSort(psVector *restrict outVector, const psVector *restrict inVector) 151 { 152 int inN = 0; 153 int outN = 0; 154 int elSize = 0; 155 void *inVec = NULL; 156 void *outVec = NULL; 157 psElemType inType = 0; 158 159 if(inVector == NULL) { 160 psError(__func__, " : Line %d - Null input vector\n", __LINE__); 161 return outVector; 162 } 163 164 inType = inVector->type.type; 165 inN = inVector->n; 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 174 outN = outVector->n; 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(inN == 0) { 184 psError(__func__, " : Line %d - No elements in use for input vector\n", __LINE__); 185 return outVector; 186 } 187 188 if(outN == 0) { 189 psError(__func__, " : Line %d - No elements in use for output vector\n", __LINE__); 190 return outVector; 191 } 192 193 // Copy input vector values into output vector 194 memcpy(outVec, inVec, elSize*outN); 195 196 // Sort output vector 197 switch(inType) { 198 case PS_TYPE_U8: 199 qsort(outVec, inN, elSize, psCompareU8); 200 break; 201 case PS_TYPE_U16: 202 qsort(outVec, inN, elSize, psCompareU16); 203 break; 204 case PS_TYPE_U32: 205 qsort(outVec, inN, elSize, psCompareU32); 206 break; 207 case PS_TYPE_U64: 208 qsort(outVec, inN, elSize, psCompareU64); 209 break; 210 case PS_TYPE_S8: 211 qsort(outVec, inN, elSize, psCompareS8); 212 break; 213 case PS_TYPE_S16: 214 qsort(outVec, inN, elSize, psCompareS16); 215 break; 216 case PS_TYPE_S32: 217 qsort(outVec, inN, elSize, psCompareS32); 218 break; 219 case PS_TYPE_S64: 220 qsort(outVec, inN, elSize, psCompareS64); 221 break; 222 case PS_TYPE_F32: 223 qsort(outVec, inN, elSize, psCompareF32); 224 break; 225 case PS_TYPE_F64: 226 qsort(outVec, inN, elSize, psCompareF64); 227 break; 228 default: 229 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 230 } 231 232 return outVector; 233 } 234 235 #define SORT_INDICES(TYPE) \ 236 for(i=0; i<inN; i++) { \ 237 for(j=0; j<inN; j++) { \ 238 diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]); \ 239 if(diff < FLT_EPSILON) { \ 240 outVec[i] = j; \ 241 break; \ 242 } \ 243 } \ 244 } 245 246 psVector *psVectorSortIndex(psVector *restrict outVector, const psVector *restrict inVector) 247 { 248 int inN = 0; 249 int outN = 0; 250 int i = 0; 251 int j = 0; 252 float *inVec = NULL; 253 int *outVec = NULL; 254 double diff = 0.0f; 255 psVector *tmpVector = NULL; 256 psElemType inType = 0; 257 258 if(inVector == NULL) { 259 psError(__func__, " : Line %d - Null input vector\n", __LINE__); 260 return outVector; 261 } 262 263 inN = inVector->n; 264 inVec = inVector->data.V; 265 inType = inVector->type.type; 266 267 if(outVector == NULL) { 268 outVector = psVectorAlloc(inN, PS_TYPE_U32); 269 outVector->n = inN; 270 } 271 272 outN = outVector->n; 273 outVec = outVector->data.V; 274 275 if(inN != outN) { 276 psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", 277 __LINE__, inN, outN); 278 return outVector; 279 } 280 281 tmpVector = psVectorAlloc(inN, inType); 282 tmpVector->n = inN; 283 tmpVector = psVectorSort(tmpVector, inVector); 284 285 // Sort output vector 286 switch(inType) { 287 case PS_TYPE_U8: 288 SORT_INDICES(U8); 289 break; 290 case PS_TYPE_U16: 291 SORT_INDICES(U16); 292 break; 293 case PS_TYPE_U32: 294 SORT_INDICES(U32); 295 break; 296 case PS_TYPE_U64: 297 SORT_INDICES(U64); 298 break; 299 case PS_TYPE_S8: 300 SORT_INDICES(S8); 301 break; 302 case PS_TYPE_S16: 303 SORT_INDICES(S16); 304 break; 305 case PS_TYPE_S32: 306 SORT_INDICES(S32); 307 break; 308 case PS_TYPE_S64: 309 SORT_INDICES(S64); 310 break; 311 case PS_TYPE_F32: 312 SORT_INDICES(F32); 313 break; 314 case PS_TYPE_F64: 315 SORT_INDICES(F64); 316 break; 317 default: 318 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 319 } 320 321 // Free temp memory 322 psFree(tmpVector); 323 324 return outVector; 325 } 326 145 327 static void vectorFree(psVector *restrict psVec) 146 328 {
Note:
See TracChangeset
for help on using the changeset viewer.
