Changeset 989 for trunk/psLib/src/collections/psSort.c
- Timestamp:
- Jun 10, 2004, 1:15:08 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/collections/psSort.c (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psSort.c
r831 r989 3 3 * @brief Sorts vectors 4 4 * 5 * The psSort functions use the qsort() stdlib function with a user-defined callback to sort an vector of 6 * floats. The qsort function requires the starting point of the vector, number of elements in the vector, size 5 * The psSort functions use the qsort() stdlib function with a user-defined callback to sort an vector of 6 * floats. The qsort function requires the starting point of the vector, number of elements in the vector, size 7 7 * of each element, and a pointer to a callback comparison function. Once called, qsort() will sort the vector 8 8 * contents in ascending order according to the comparison function. The comparison function is called with 9 * two arguments that point to the objects being compared - in this case two floats. The comparison function 9 * two arguments that point to the objects being compared - in this case two floats. The comparison function 10 10 * must return an integer less than, equal to, or greater than zero if the first argument is considered to be 11 11 * respectively less than, equal to, or greater than the second. If two members compare as equal, their order … … 13 13 * 14 14 * @author Ross Harman, MHPCC 15 * 16 * @version $Revision: 1.1 0$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-06- 02 23:29:14$15 * 16 * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-06-10 23:15:08 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 64 64 /** Private callback comparison function to compare two floats. 65 65 * 66 * The comparison function is called by qsort() with two arguments that point to the objects being 67 * compared. The comparison function must return an integer less than, equal to, or greater than zero if 68 * the first argument is considered to be respectively less than, equal to, or greater than the second. If 69 * two members compare as equal, their order in the sorted vector is undefined. 66 * The comparison function is called by qsort() with two arguments that point to the objects being 67 * compared. The comparison function must return an integer less than, equal to, or greater than zero if 68 * the first argument is considered to be respectively less than, equal to, or greater than the second. 70 69 * 71 70 * @return int: Result of comparsion (-1, 0, or 1). 72 71 */ 73 74 static int psCompare( 75 const void *x, /**< First float to compare. */ 76 const void *y /**< Second float to compare. */ 77 ) 78 { 79 float *item1 = NULL; 80 float *item2 = NULL; 81 82 if(x == NULL || y == NULL) { 83 psError(__func__, " : Line %d - Null input argument: x=%d y=%d\n", __LINE__, x, y); 84 } 85 86 item1 = (float*)x; 87 item2 = (float*)y; 88 89 if(item1 == NULL || item2 == NULL) { 90 psError(__func__, " : Line %d - Improper cast to float: item1=%d item2=%d\n", __LINE__, item1, item2); 91 } 92 93 if(*item1 < *item2) { 94 return -1; 95 } else if(*item1 > *item2) { 96 return 1; 97 } else { 98 return 0; 99 } 100 } 72 #define PS_COMPARE(TYPE) \ 73 static int psCompare##TYPE(const void *x, const void *y) \ 74 { \ 75 ps##TYPE *item1 = NULL; \ 76 ps##TYPE *item2 = NULL; \ 77 \ 78 if(x == NULL || y == NULL) { \ 79 psError(__func__, " : Line %d - Null input argument: x=%d y=%d\n", __LINE__, x, y); \ 80 } \ 81 \ 82 item1 = (ps##TYPE *)x; \ 83 item2 = (ps##TYPE *)y; \ 84 \ 85 if(item1 == NULL || item2 == NULL) { \ 86 psError(__func__, " : Line %d - Improper cast to float: item1=%d item2=%d\n", __LINE__, item1, \ 87 item2); \ 88 } \ 89 \ 90 if(*item1 < *item2) { \ 91 return -1; \ 92 } else if(*item1 > *item2) { \ 93 return 1; \ 94 } else { \ 95 return 0; \ 96 } \ 97 } 98 99 PS_COMPARE(U8) 100 PS_COMPARE(U16) 101 PS_COMPARE(F32) 102 PS_COMPARE(F64) 101 103 102 104 /*****************************************************************************/ … … 104 106 /*****************************************************************************/ 105 107 106 psVector *psSort( 107 psVector *restrict outVector, /**< Sorted output vector. */ 108 const psVector *restrict inVector /**< Input vector to sort. */ 109 ) 108 psVector *psSort(psVector *restrict outVector, const psVector *restrict inVector) 110 109 { 111 110 int inN = 0; 112 111 int outN = 0; 113 int i = 0;114 112 int elSize = 0; 115 float *inVec = NULL; 116 float *outVec = NULL; 117 118 if(outVector == NULL) { 119 psError(__func__, " : Line %d - Null output vector\n", __LINE__); 120 return outVector; 121 } 113 void *inVec = NULL; 114 void *outVec = NULL; 115 psElemType inType = 0; 122 116 123 117 if(inVector == NULL) { … … 126 120 } 127 121 128 // Initialize values122 inType = inVector->type.type; 129 123 inN = inVector->n; 124 inVec = inVector->data.V; 125 elSize = PSELEMTYPE_SIZEOF(inType); 126 127 if(outVector == NULL) { 128 outVector = psVectorAlloc(inN, inType); 129 outVector->n = inVector->n; 130 } 131 130 132 outN = outVector->n; 131 inVec = inVector->data.F32; 132 outVec = outVector->data.F32; 133 elSize = sizeof(float); 133 outVec = outVector->data.V; 134 134 135 135 if(inN != outN) { … … 150 150 151 151 // Copy input vector values into output vector 152 for(i=0; i<inN; i++) { 153 outVec[i] = inVec[i]; 154 } 152 memcpy(outVec, inVec, elSize*outN); 155 153 156 154 // Sort output vector 157 qsort((void*)outVec, inN, elSize, psCompare); 155 switch(inType) { 156 case PS_TYPE_U8: 157 qsort(outVec, inN, elSize, psCompareU8); 158 break; 159 case PS_TYPE_U16: 160 qsort(outVec, inN, elSize, psCompareU16); 161 break; 162 case PS_TYPE_F32: 163 qsort(outVec, inN, elSize, psCompareF32); 164 break; 165 case PS_TYPE_F64: 166 qsort(outVec, inN, elSize, psCompareF64); 167 break; 168 default: 169 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 170 } 158 171 159 172 return outVector; 160 173 } 161 174 162 psVector *psSortIndex( 163 psVector *restrict outVector, /**< Output vector of sorted indices. */ 164 const psVector *restrict inVector /**< Input vector to be sorted. */ 165 ) 175 #define SORT_INDICES(TYPE) \ 176 for(i=0; i<inN; i++) { \ 177 for(j=0; j<inN; j++) { \ 178 diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]); \ 179 if(diff < FLT_EPSILON) { \ 180 outVec[i] = j; \ 181 break; \ 182 } \ 183 } \ 184 } 185 186 psVector *psSortIndex(psVector *restrict outVector, const psVector *restrict inVector) 166 187 { 167 188 int inN = 0; … … 169 190 int i = 0; 170 191 int j = 0; 171 float tempVal = 0.0f;172 192 float *inVec = NULL; 173 193 int *outVec = NULL; 174 float diff = 0.0f; 175 psVector *tmpFloatVector = NULL; 176 177 if(outVector == NULL) { 178 psError(__func__, " : Line %d - Null output vector\n", __LINE__); 179 return outVector; 180 } 194 double diff = 0.0f; 195 psVector *tmpVector = NULL; 196 psElemType inType = 0; 181 197 182 198 if(inVector == NULL) { … … 185 201 } 186 202 187 // Initialize values188 203 inN = inVector->n; 204 inVec = inVector->data.V; 205 inType = inVector->type.type; 206 207 if(outVector == NULL) { 208 outVector = psVectorAlloc(inN, PS_TYPE_U32); 209 outVector->n = inN; 210 } 211 189 212 outN = outVector->n; 190 inVec = inVector->data.F32; 191 outVec = outVector->data.S32; 213 outVec = outVector->data.V; 192 214 193 215 if(inN != outN) { 194 psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", __LINE__, 195 inN, outN); 196 return outVector; 197 } 198 199 tmpFloatVector = psVectorAlloc(inN, PS_TYPE_F32); 200 tmpFloatVector->n = inN; 201 tmpFloatVector = psSort(tmpFloatVector, inVector); 202 203 for(i=0; i<inN; i++) { 204 tempVal = tmpFloatVector->data.F32[i]; 205 for(j=0; j<inN; j++) { 206 diff = fabsf(tempVal - inVec[j]); 207 if(diff < FLT_EPSILON) { 208 outVec[i] = j; 209 break; 210 } 211 } 216 psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", 217 __LINE__, inN, outN); 218 return outVector; 219 } 220 221 tmpVector = psVectorAlloc(inN, inType); 222 tmpVector->n = inN; 223 tmpVector = psSort(tmpVector, inVector); 224 225 // Sort output vector 226 switch(inType) { 227 case PS_TYPE_U8: 228 SORT_INDICES(U8); 229 break; 230 case PS_TYPE_U16: 231 SORT_INDICES(U16); 232 break; 233 case PS_TYPE_F32: 234 SORT_INDICES(F32); 235 break; 236 case PS_TYPE_F64: 237 SORT_INDICES(F64); 238 break; 239 default: 240 psError(__func__, " : Line %d - Invalid psType\n", __LINE__); 212 241 } 213 242 214 243 // Free temp memory 215 psVectorFree(tmp FloatVector);244 psVectorFree(tmpVector); 216 245 217 246 return outVector;
Note:
See TracChangeset
for help on using the changeset viewer.
