Index: trunk/psLib/src/collections/psVector.c
===================================================================
--- trunk/psLib/src/collections/psVector.c	(revision 1761)
+++ trunk/psLib/src/collections/psVector.c	(revision 1807)
@@ -10,6 +10,6 @@
 *  @author Robert DeSonia, MHPCC
 *
-*  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2004-09-09 21:59:03 $
+*  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2004-09-14 20:01:52 $
 *
 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -25,7 +25,18 @@
 #include "psLogMsg.h"
 #include "psCompare.h"
+
 #include "psCollectionsErrors.h"
 
 static void vectorFree(psVector* restrict psVec);
+
+
+static void vectorFree(psVector* restrict psVec)
+{
+    if (psVec == NULL) {
+        return;
+    }
+
+    psFree(psVec->data.V);
+}
 
 // FUNCTION IMPLEMENTATION - PUBLIC
@@ -35,10 +46,4 @@
     psVector* psVec = NULL;
     int elementSize = 0;
-
-    // Invalid nalloc
-    if (nalloc < 1) {
-        psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);
-        return NULL;
-    }
 
     elementSize = PSELEMTYPE_SIZEOF(elemType);
@@ -64,12 +69,8 @@
     psElemType elemType;
 
-    // Invalid nalloc
-    if (nalloc < 1) {
-        psError(__func__, "Invalid value for realloc (%d)\n", nalloc);
-        return NULL;
-    }
-
     if (in == NULL) {
-        psError(__func__, "Null input vector\n");
+        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorRealloc",
+                   PS_ERR_BAD_PARAMETER_NULL, true,
+                   PS_ERRORTEXT_psVector_REALLOC_NULL);
         return NULL;
     } else if (in->nalloc != nalloc) {     // No need to realloc to same size
@@ -101,10 +102,4 @@
         return in;
     }
-    // Invalid nalloc
-    if (nalloc < 1) {
-        psError(__func__, "Invalid value for nalloc (%d)\n", nalloc);
-        psFree(in);
-        return NULL;
-    }
 
     in->data.V = psRealloc(in->data.V, nalloc * PSELEMTYPE_SIZEOF(type));
@@ -119,6 +114,5 @@
 psVector* psVectorSort(psVector* restrict outVector, const psVector* restrict inVector)
 {
-    int inN = 0;
-    int outN = 0;
+    int N = 0;
     int elSize = 0;
     void *inVec = NULL;
@@ -127,79 +121,76 @@
 
     if (inVector == NULL) {
-        psError(__func__, " : Line %d - Null input vector\n", __LINE__);
-        return outVector;
+        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort",
+                   PS_ERR_BAD_PARAMETER_NULL, true,
+                   PS_ERRORTEXT_psVector_SORT_NULL);
+        psFree(outVector);
+        return NULL;
     }
 
     inType = inVector->type.type;
-    inN = inVector->n;
+    N = inVector->n;
     inVec = inVector->data.V;
     elSize = PSELEMTYPE_SIZEOF(inType);
 
     if (outVector == NULL) {
-        outVector = psVectorAlloc(inN, inType);
-        outVector->n = inVector->n;
-    }
-
-    outN = outVector->n;
+        outVector = psVectorAlloc(N, inType);
+    }
+
+    // check to see if output vector needs to be resized/retyped
+    if ( (N > outVector->nalloc) ||
+            (inType != outVector->type.type) ) {
+        // reshape the output vector to match the input vector's size/type.
+        outVector = psVectorRecycle(outVector,N,inType);
+    }
+    outVector->n = N;
     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);
+    if (N == 0) {
+        // no need to sort anything, as there are no elements in input vector.
         return outVector;
     }
 
-    if (inType != outVector->type.type) {
-        psError(__func__, " : Line %d - Input and output vectors are not same type: in=%d out=%d\n", __LINE__,
-                inType, outVector->type.type);
-        return outVector;
-    }
-
-    if (inN == 0) {
-        psError(__func__, " : Line %d - No elements in use for input vector\n", __LINE__);
-        return outVector;
-    }
-
-    if (outN == 0) {
-        psError(__func__, " : Line %d - No elements in use for output vector\n", __LINE__);
-        return outVector;
-    }
-    // Copy input vector values into output vector
-    memcpy(outVec, inVec, elSize * outN);
+    // Copy input vector values into output vector if not in-place sorting
+    if (inVector != outVector) {
+        memcpy(outVec, inVec, elSize * N);
+    }
 
     // Sort output vector
     switch (inType) {
     case PS_TYPE_U8:
-        qsort(outVec, inN, elSize, psCompareU8);
+        qsort(outVec, N, elSize, psCompareU8);
         break;
     case PS_TYPE_U16:
-        qsort(outVec, inN, elSize, psCompareU16);
+        qsort(outVec, N, elSize, psCompareU16);
         break;
     case PS_TYPE_U32:
-        qsort(outVec, inN, elSize, psCompareU32);
+        qsort(outVec, N, elSize, psCompareU32);
         break;
     case PS_TYPE_U64:
-        qsort(outVec, inN, elSize, psCompareU64);
+        qsort(outVec, N, elSize, psCompareU64);
         break;
     case PS_TYPE_S8:
-        qsort(outVec, inN, elSize, psCompareS8);
+        qsort(outVec, N, elSize, psCompareS8);
         break;
     case PS_TYPE_S16:
-        qsort(outVec, inN, elSize, psCompareS16);
+        qsort(outVec, N, elSize, psCompareS16);
         break;
     case PS_TYPE_S32:
-        qsort(outVec, inN, elSize, psCompareS32);
+        qsort(outVec, N, elSize, psCompareS32);
         break;
     case PS_TYPE_S64:
-        qsort(outVec, inN, elSize, psCompareS64);
+        qsort(outVec, N, elSize, psCompareS64);
         break;
     case PS_TYPE_F32:
-        qsort(outVec, inN, elSize, psCompareF32);
+        qsort(outVec, N, elSize, psCompareF32);
         break;
     case PS_TYPE_F64:
-        qsort(outVec, inN, elSize, psCompareF64);
+        qsort(outVec, N, elSize, psCompareF64);
         break;
     default:
-        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
+        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSort",
+                   PS_ERR_BAD_PARAMETER_TYPE, true,
+                   PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
+                   inType);
     }
 
@@ -207,94 +198,99 @@
 }
 
-#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* psVectorSortIndex(psVector* restrict outVector, const psVector* restrict inVector)
 {
-    int inN = 0;
-    int outN = 0;
-    int i = 0;
-    int j = 0;
-    float *inVec = NULL;
-    int *outVec = NULL;
-    double diff = 0.0f;
+    int N = 0;
     psVector* tmpVector = NULL;
     psElemType inType = 0;
+    psU32* outVec;
 
     if (inVector == NULL) {
-        psError(__func__, " : Line %d - Null input vector\n", __LINE__);
+        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex",
+                   PS_ERR_BAD_PARAMETER_NULL, true,
+                   PS_ERRORTEXT_psVector_SORT_NULL);
+        psFree(outVector);
+        return NULL;
+    }
+
+    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.
         return outVector;
     }
 
-    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;
-    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;
-    }
-
-    if (outVector->type.type != PS_TYPE_U32) {
-        psError(__func__, " : Line %d - Output vector is not of type U32: out=%d\n",
-                __LINE__, outVector->type.type);
-        return outVector;
-    }
-
-    tmpVector = psVectorAlloc(inN, inType);
-    tmpVector->n = inN;
     tmpVector = psVectorSort(tmpVector, inVector);
+
+    #define SORT_INDICES(TYPE,absfcn,maxError) {                              \
+        ps##TYPE* inVec = inVector->data.TYPE;                                \
+        ps##TYPE* tmpVec = tmpVector->data.TYPE;                              \
+        ps##TYPE  diff;                                                       \
+        for(int i=0; i<N; i++) {                                              \
+            for(int j=0; j<N; j++) {                                          \
+                diff = absfcn(tmpVec[i] - inVec[j]);                          \
+                if(diff < maxError) {                                         \
+                    outVec[i] = j;                                            \
+                    break;                                                    \
+                }                                                             \
+            }                                                                 \
+        }                                                                     \
+    }
 
     // Sort output vector
     switch (inType) {
     case PS_TYPE_U8:
-        SORT_INDICES(U8);
+        SORT_INDICES(U8,/* no absfcn needed */,1);
         break;
     case PS_TYPE_U16:
-        SORT_INDICES(U16);
+        SORT_INDICES(U16,/* no absfcn needed */,1);
         break;
     case PS_TYPE_U32:
-        SORT_INDICES(U32);
+        SORT_INDICES(U32,/* no absfcn needed */,1);
         break;
     case PS_TYPE_U64:
-        SORT_INDICES(U64);
+        SORT_INDICES(U64,/* no absfcn needed */,1);
         break;
     case PS_TYPE_S8:
-        SORT_INDICES(S8);
+        SORT_INDICES(S8,/* no absfcn needed */,1);
         break;
     case PS_TYPE_S16:
-        SORT_INDICES(S16);
+        SORT_INDICES(S16,/* no absfcn needed */,1);
         break;
     case PS_TYPE_S32:
-        SORT_INDICES(S32);
+        SORT_INDICES(S32,/* no absfcn needed */,1);
         break;
     case PS_TYPE_S64:
-        SORT_INDICES(S64);
+        SORT_INDICES(S64,/* no absfcn needed */,1);
         break;
     case PS_TYPE_F32:
-        SORT_INDICES(F32);
+        SORT_INDICES(F32,fabsf,FLT_EPSILON);
         break;
     case PS_TYPE_F64:
-        SORT_INDICES(F64);
+        SORT_INDICES(F64,fabs,DBL_EPSILON);
+        break;
+    case PS_TYPE_C32:
+        SORT_INDICES(F64,cabsf,FLT_EPSILON);
+        break;
+    case PS_TYPE_C64:
+        SORT_INDICES(F64,cabs,DBL_EPSILON);
         break;
     default:
-        psError(__func__, " : Line %d - Invalid psType\n", __LINE__);
+        psErrorMsg(PS_ERRORNAME_DOMAIN "psVectorSortIndex",
+                   PS_ERR_BAD_PARAMETER_TYPE, true,
+                   PS_ERRORTEXT_psVector_UNSUPPORTED_TYPE,
+                   inType);
     }
 
@@ -304,11 +300,2 @@
     return outVector;
 }
-
-static void vectorFree(psVector* restrict psVec)
-{
-    if (psVec == NULL) {
-        return;
-    }
-
-    psFree(psVec->data.V);
-}
