Index: trunk/psLib/src/collections/psSort.c
===================================================================
--- trunk/psLib/src/collections/psSort.c	(revision 986)
+++ trunk/psLib/src/collections/psSort.c	(revision 989)
@@ -3,9 +3,9 @@
  *  @brief Sorts vectors
  *
- *  The psSort functions use the qsort() stdlib function with a user-defined callback to sort an vector of 
- *  floats. The qsort function requires the starting point of the vector, number of elements in the vector, size 
+ *  The psSort functions use the qsort() stdlib function with a user-defined callback to sort an vector of
+ *  floats. The qsort function requires the starting point of the vector, number of elements in the vector, size
  *  of each element, and a pointer to a callback comparison function. Once called, qsort() will sort the vector
  *  contents in ascending order according to the comparison function. The comparison function is called with
- *  two arguments that point to the objects being compared - in this case two floats. The comparison function 
+ *  two arguments that point to the objects being compared - in this case two floats. The comparison function
  *  must return an integer less than, equal to, or greater than zero if the first argument is considered to be
  *  respectively less than, equal to, or greater than the second. If two members compare as equal, their order
@@ -13,7 +13,7 @@
  *
  *  @author Ross Harman, MHPCC
- *   
- *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2004-06-02 23:29:14 $
+ *
+ *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2004-06-10 23:15:08 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -64,39 +64,41 @@
 /** Private callback comparison function to compare two floats.
  *
- *  The comparison function is called by qsort() with two arguments that point to the objects being 
- *  compared. The comparison function must return an integer less than, equal to, or greater than zero if 
- *  the first argument is considered to be respectively less than, equal to, or greater than the second. If 
- *  two members compare as equal, their order in the sorted vector is undefined. 
+ *  The comparison function is called by qsort() with two arguments that point to the objects being
+ *  compared. The comparison function must return an integer less than, equal to, or greater than zero if
+ *  the first argument is considered to be respectively less than, equal to, or greater than the second.
  *
  *  @return  int: Result of comparsion (-1, 0, or 1).
  */
-
-static int psCompare(
-    const void *x,  /**< First float to compare. */
-    const void *y   /**< Second float to compare. */
-)
-{
-    float *item1 = NULL;
-    float *item2 = NULL;
-
-    if(x == NULL || y == NULL) {
-        psError(__func__, " : Line %d - Null input argument: x=%d y=%d\n", __LINE__, x, y);
-    }
-
-    item1 = (float*)x;
-    item2 = (float*)y;
-
-    if(item1 == NULL || item2 == NULL) {
-        psError(__func__, " : Line %d - Improper cast to float: item1=%d item2=%d\n", __LINE__, item1, item2);
-    }
-
-    if(*item1 < *item2) {
-        return -1;
-    } else if(*item1 > *item2) {
-        return 1;
-    } else {
-        return 0;
-    }
-}
+#define PS_COMPARE(TYPE)                                                                                     \
+static int psCompare##TYPE(const void *x, const void *y)                                                     \
+{                                                                                                            \
+    ps##TYPE *item1 = NULL;                                                                                  \
+    ps##TYPE *item2 = NULL;                                                                                  \
+    \
+    if(x == NULL || y == NULL) {                                                                             \
+        psError(__func__, " : Line %d - Null input argument: x=%d y=%d\n", __LINE__, x, y);                  \
+    }                                                                                                        \
+    \
+    item1 = (ps##TYPE *)x;                                                                                   \
+    item2 = (ps##TYPE *)y;                                                                                   \
+    \
+    if(item1 == NULL || item2 == NULL) {                                                                     \
+        psError(__func__, " : Line %d - Improper cast to float: item1=%d item2=%d\n", __LINE__, item1,       \
+                item2);                                                                                      \
+    }                                                                                                        \
+    \
+    if(*item1 < *item2) {                                                                                    \
+        return -1;                                                                                           \
+    } else if(*item1 > *item2) {                                                                             \
+        return 1;                                                                                            \
+    } else {                                                                                                 \
+        return 0;                                                                                            \
+    }                                                                                                        \
+}
+
+PS_COMPARE(U8)
+PS_COMPARE(U16)
+PS_COMPARE(F32)
+PS_COMPARE(F64)
 
 /*****************************************************************************/
@@ -104,20 +106,12 @@
 /*****************************************************************************/
 
-psVector *psSort(
-    psVector *restrict outVector,        /**< Sorted output vector. */
-    const psVector *restrict inVector    /**< Input vector to sort. */
-)
+psVector *psSort(psVector *restrict outVector, const psVector *restrict inVector)
 {
     int inN = 0;
     int outN = 0;
-    int i = 0;
     int elSize = 0;
-    float *inVec = NULL;
-    float *outVec = NULL;
-
-    if(outVector == NULL) {
-        psError(__func__, " : Line %d - Null output vector\n", __LINE__);
-        return outVector;
-    }
+    void *inVec = NULL;
+    void *outVec = NULL;
+    psElemType inType = 0;
 
     if(inVector == NULL) {
@@ -126,10 +120,16 @@
     }
 
-    // Initialize values
+    inType = inVector->type.type;
     inN = inVector->n;
+    inVec = inVector->data.V;
+    elSize = PSELEMTYPE_SIZEOF(inType);
+
+    if(outVector == NULL) {
+        outVector = psVectorAlloc(inN, inType);
+        outVector->n = inVector->n;
+    }
+
     outN = outVector->n;
-    inVec = inVector->data.F32;
-    outVec = outVector->data.F32;
-    elSize = sizeof(float);
+    outVec = outVector->data.V;
 
     if(inN != outN) {
@@ -150,18 +150,39 @@
 
     // Copy input vector values into output vector
-    for(i=0; i<inN; i++) {
-        outVec[i] = inVec[i];
-    }
+    memcpy(outVec, inVec, elSize*outN);
 
     // Sort output vector
-    qsort((void*)outVec, inN, elSize, psCompare);
+    switch(inType) {
+    case PS_TYPE_U8:
+        qsort(outVec, inN, elSize, psCompareU8);
+        break;
+    case PS_TYPE_U16:
+        qsort(outVec, inN, elSize, psCompareU16);
+        break;
+    case PS_TYPE_F32:
+        qsort(outVec, inN, elSize, psCompareF32);
+        break;
+    case PS_TYPE_F64:
+        qsort(outVec, inN, elSize, psCompareF64);
+        break;
+    default:
+        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
+    }
 
     return outVector;
 }
 
-psVector *psSortIndex(
-    psVector *restrict outVector,         /**< Output vector of sorted indices. */
-    const psVector *restrict inVector     /**< Input vector to be sorted. */
-)
+#define SORT_INDICES(TYPE)                                                                                   \
+for(i=0; i<inN; i++) {                                                                                       \
+    for(j=0; j<inN; j++) {                                                                                   \
+        diff = fabs((double)tmpVector->data.TYPE[i] - inVec[j]);                                             \
+        if(diff < FLT_EPSILON) {                                                                             \
+            outVec[i] = j;                                                                                   \
+            break;                                                                                           \
+        }                                                                                                    \
+    }                                                                                                        \
+}
+
+psVector *psSortIndex(psVector *restrict outVector, const psVector *restrict inVector)
 {
     int inN = 0;
@@ -169,14 +190,9 @@
     int i = 0;
     int j = 0;
-    float tempVal = 0.0f;
     float *inVec = NULL;
     int *outVec = NULL;
-    float diff = 0.0f;
-    psVector *tmpFloatVector = NULL;
-
-    if(outVector == NULL) {
-        psError(__func__, " : Line %d - Null output vector\n", __LINE__);
-        return outVector;
-    }
+    double diff = 0.0f;
+    psVector *tmpVector = NULL;
+    psElemType inType = 0;
 
     if(inVector == NULL) {
@@ -185,33 +201,46 @@
     }
 
-    // Initialize values
     inN = inVector->n;
+    inVec = inVector->data.V;
+    inType = inVector->type.type;
+
+    if(outVector == NULL) {
+        outVector = psVectorAlloc(inN, PS_TYPE_U32);
+        outVector->n = inN;
+    }
+
     outN = outVector->n;
-    inVec = inVector->data.F32;
-    outVec = outVector->data.S32;
+    outVec = outVector->data.V;
 
     if(inN != outN) {
-        psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n", __LINE__,
-                inN, outN);
-        return outVector;
-    }
-
-    tmpFloatVector = psVectorAlloc(inN, PS_TYPE_F32);
-    tmpFloatVector->n = inN;
-    tmpFloatVector = psSort(tmpFloatVector, inVector);
-
-    for(i=0; i<inN; i++) {
-        tempVal = tmpFloatVector->data.F32[i];
-        for(j=0; j<inN; j++) {
-            diff = fabsf(tempVal - inVec[j]);
-            if(diff < FLT_EPSILON) {
-                outVec[i] = j;
-                break;
-            }
-        }
+        psError(__func__, " : Line %d - Input and output vector sizes are not equal: in=%d out=%d\n",
+                __LINE__, inN, outN);
+        return outVector;
+    }
+
+    tmpVector = psVectorAlloc(inN, inType);
+    tmpVector->n = inN;
+    tmpVector = psSort(tmpVector, inVector);
+
+    // Sort output vector
+    switch(inType) {
+    case PS_TYPE_U8:
+        SORT_INDICES(U8);
+        break;
+    case PS_TYPE_U16:
+        SORT_INDICES(U16);
+        break;
+    case PS_TYPE_F32:
+        SORT_INDICES(F32);
+        break;
+    case PS_TYPE_F64:
+        SORT_INDICES(F64);
+        break;
+    default:
+        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
     }
 
     // Free temp memory
-    psVectorFree(tmpFloatVector);
+    psVectorFree(tmpVector);
 
     return outVector;
