Index: trunk/psLib/src/collections/psVector.c
===================================================================
--- trunk/psLib/src/collections/psVector.c	(revision 3684)
+++ trunk/psLib/src/collections/psVector.c	(revision 3737)
@@ -9,6 +9,6 @@
 *  @author Robert DeSonia, MHPCC
 *
-*  @version $Revision: 1.38 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-04-08 17:58:57 $
+*  @version $Revision: 1.39 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-04-21 21:18:23 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -28,6 +28,12 @@
 #include "psCollectionsErrors.h"
 
+typedef struct
+{
+    p_psVectorData data; // need this first for psVectorSortIndex to work.
+    int index;
+}
+indexedVector;
+
 static void vectorFree(psVector* psVec);
-
 
 static void vectorFree(psVector* psVec)
@@ -297,4 +303,6 @@
                 PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
                 inType);
+        psFree(outVector);
+        return NULL;
     }
 
@@ -305,8 +313,5 @@
 {
     psS32 N = 0;
-    psVector* tmpVector = NULL;
-    psVector* checkVector = NULL;
     psElemType inType = 0;
-    psU32* outVec;
 
     if (inVector == NULL) {
@@ -320,17 +325,4 @@
     N = inVector->n;
 
-    if (outVector == NULL) {
-        outVector = psVectorAlloc(N, PS_TYPE_U32);
-    }
-
-    // check to see if output vector needs to be resized/retyped
-    if ( (N > outVector->nalloc) ||
-            (outVector->type.type != PS_TYPE_U32) ) {
-        // reshape the output vector to match the input vector's size/type.
-        outVector = psVectorRecycle(outVector,N,PS_TYPE_U32);
-    }
-    outVector->n = N;
-    outVec = outVector->data.U32;
-
     if (N == 0) {
         // no need to sort anything, as there are no elements in input vector.
@@ -338,60 +330,45 @@
     }
 
-    tmpVector = psVectorSort(tmpVector, inVector);
-
-    checkVector = psVectorAlloc(N,PS_TYPE_U8);
-    for(psS32 k=0; k<N; k++) {
-        checkVector->data.U8[k] = 0;
-    }
-
-    #define SORT_INDICES(TYPE,absfcn,maxError) {                              \
-        ps##TYPE* inVec = inVector->data.TYPE;                                \
-        ps##TYPE* tmpVec = tmpVector->data.TYPE;                              \
-        ps##TYPE  diff;                                                       \
-        for(psS32 i=0; i<N; i++) {                                              \
-            for(psS32 j=0; j<N; j++) {                                          \
-                if(checkVector->data.U8[j] == 0 ) {                                    \
-                    diff = absfcn(tmpVec[i] - inVec[j]);                          \
-                    if(diff < maxError) {                                         \
-                        outVec[i] = j;                                            \
-                        checkVector->data.U8[j] = 1; \
-                        break;                                                    \
-                    }                                                             \
-                } \
-            }                                                                 \
-        }                                                                     \
-    }
-
-    // Sort output vector
+    // ok, let's create a temporary indexed vector
+    indexedVector* idxVector = psAlloc(sizeof(indexedVector)*N);
+    int elSize = PSELEMTYPE_SIZEOF(inType);
+    for (int i = 0; i < N; i++) {
+        idxVector[i].data.U8 = inVector->data.U8+i*elSize;
+        idxVector[i].index = i;
+    }
+
+    // Sort indexed vector
+    // n.b., since first element in indexedVector is a pointer to the data,
+    // we can use the 'Ptr' version of the standard compare functions
     switch (inType) {
     case PS_TYPE_U8:
-        SORT_INDICES(U8,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU8Ptr);
         break;
     case PS_TYPE_U16:
-        SORT_INDICES(U16,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU16Ptr);
         break;
     case PS_TYPE_U32:
-        SORT_INDICES(U32,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU32Ptr);
         break;
     case PS_TYPE_U64:
-        SORT_INDICES(U64,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareU64Ptr);
         break;
     case PS_TYPE_S8:
-        SORT_INDICES(S8,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS8Ptr);
         break;
     case PS_TYPE_S16:
-        SORT_INDICES(S16,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS16Ptr);
         break;
     case PS_TYPE_S32:
-        SORT_INDICES(S32,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS32Ptr);
         break;
     case PS_TYPE_S64:
-        SORT_INDICES(S64,abs,1);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareS64Ptr);
         break;
     case PS_TYPE_F32:
-        SORT_INDICES(F32,fabsf,FLT_EPSILON);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareF32Ptr);
         break;
     case PS_TYPE_F64:
-        SORT_INDICES(F64,fabs,DBL_EPSILON);
+        qsort(idxVector, N, sizeof(indexedVector), (psCompareFcn)psCompareF64Ptr);
         break;
     default:
@@ -399,9 +376,18 @@
                 PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
                 inType);
+        psFree(idxVector);
+        psFree(outVector);
+        return NULL;
+    }
+
+    // extract the indices to the output vector
+    outVector = psVectorRecycle(outVector, N, PS_TYPE_U32);
+    psU32* outData = outVector->data.U32;
+    for (int i = 0; i < N; i++) {
+        outData[i] = idxVector[i].index;
     }
 
     // Free temp memory
-    psFree(tmpVector);
-    psFree(checkVector);
+    psFree(idxVector);
 
     return outVector;
