Index: trunk/psLib/src/collections/psCollectionsErrors.dat
===================================================================
--- trunk/psLib/src/collections/psCollectionsErrors.dat	(revision 3690)
+++ trunk/psLib/src/collections/psCollectionsErrors.dat	(revision 3737)
@@ -15,5 +15,5 @@
 psVector_NOT_A_VECTOR                  The input psVector must have a vector dimension type.
 psVector_SORT_NULL                     psVectorSort can not sort a NULL psVector.
-psVector_UNSUPPORTED_TYPE              Input psVector is an unsupported type (%d).
+psVector_UNSUPPORTED_TYPE              Input psVector is an unsupported type (0x%x).
 #
 psBitSet_ALLOC_NEG_SIZE                The number of bit in a psBitSet (%d) must be greater than zero.
Index: trunk/psLib/src/collections/psCollectionsErrors.h
===================================================================
--- trunk/psLib/src/collections/psCollectionsErrors.h	(revision 3690)
+++ trunk/psLib/src/collections/psCollectionsErrors.h	(revision 3737)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-02-17 19:26:23 $
+ *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-04-21 21:18:23 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -36,5 +36,5 @@
 #define PS_ERRORTEXT_psVector_NOT_A_VECTOR "The input psVector must have a vector dimension type."
 #define PS_ERRORTEXT_psVector_SORT_NULL "psVectorSort can not sort a NULL psVector."
-#define PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE "Input psVector is an unsupported type (%d)."
+#define PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE "Input psVector is an unsupported type (0x%x)."
 #define PS_ERRORTEXT_psBitSet_ALLOC_NEG_SIZE "The number of bit in a psBitSet (%d) must be greater than zero."
 #define PS_ERRORTEXT_psBitSet_SET_NULL "Can not operate on a NULL psBitSet."
Index: trunk/psLib/src/collections/psVector.c
===================================================================
--- trunk/psLib/src/collections/psVector.c	(revision 3690)
+++ 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;
Index: trunk/psLib/src/collections/psVector.h
===================================================================
--- trunk/psLib/src/collections/psVector.h	(revision 3690)
+++ trunk/psLib/src/collections/psVector.h	(revision 3737)
@@ -11,6 +11,6 @@
  *  @author Ross Harman, MHPCC
  *
- *  @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-04-08 17:58:57 $
+ *  @version $Revision: 1.32 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2005-04-21 21:18:23 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -25,4 +25,20 @@
 /// @{
 
+///< Union of psVector data types.
+typedef union {
+    psU8* U8;               ///< Unsigned 8-bit integer data.
+    psU16* U16;             ///< Unsigned 16-bit integer data.
+    psU32* U32;             ///< Unsigned 32-bit integer data.
+    psU64* U64;             ///< Unsigned 64-bit integer data.
+    psS8* S8;               ///< Signed 8-bit integer data.
+    psS16* S16;             ///< Signed 16-bit integer data.
+    psS32* S32;             ///< Signed 32-bit integer data.
+    psS64* S64;             ///< Signed 64-bit integer data.
+    psF32* F32;             ///< Single-precision float data.
+    psF64* F64;             ///< Double-precision float data.
+    psC32* C32;             ///< Single-precision complex data.
+    psC64* C64;             ///< Double-precision complex data.
+} p_psVectorData;
+
 /** An vector to support primitive types.
  *
@@ -35,19 +51,5 @@
     int n;                      ///< Number of elements in use.
     const int nalloc;           ///< Total number of elements available.
-
-    union {
-        psU8* U8;               ///< Unsigned 8-bit integer data.
-        psU16* U16;             ///< Unsigned 16-bit integer data.
-        psU32* U32;             ///< Unsigned 32-bit integer data.
-        psU64* U64;             ///< Unsigned 64-bit integer data.
-        psS8* S8;               ///< Signed 8-bit integer data.
-        psS16* S16;             ///< Signed 16-bit integer data.
-        psS32* S32;             ///< Signed 32-bit integer data.
-        psS64* S64;             ///< Signed 64-bit integer data.
-        psF32* F32;             ///< Single-precision float data.
-        psF64* F64;             ///< Double-precision float data.
-        psC32* C32;             ///< Single-precision complex data.
-        psC64* C64;             ///< Double-precision complex data.
-    } data;                     ///< Union for data types.
+    p_psVectorData data;        ///< Union for data types.
 }
 psVector;
