IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 15076


Ignore:
Timestamp:
Sep 28, 2007, 11:03:24 AM (19 years ago)
Author:
eugene
Message:

added psArraySortIndex

Location:
trunk/psLib/src/types
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/types/psArray.c

    r14927 r15076  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.62 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2007-09-20 23:56:57 $
     11 *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2007-09-28 21:03:24 $
    1313 *
    1414 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3131#include "psArray.h"
    3232#include "psLogMsg.h"
     33#include "psAbort.h"
    3334#include "psAssert.h"
    3435
     
    243244}
    244245
     246// Heap sort of the index array
     247psVector *psArraySortIndex (psVector *out, psArray *in, psCompareFunc func) {
     248
     249    if (in == NULL) {
     250        return NULL;
     251    }
     252
     253    // XXX isn't this line a waste of space?
     254    out = psVectorRecycle(out, in->n, PS_TYPE_S32); // Vector for output
     255    out = psVectorCreate(out, 0, in->n, 1, PS_TYPE_S32);
     256    long N = out->n;                    // Number of elements
     257    if (N < 2) {
     258        return out;
     259    }
     260
     261    long I = N >> 1;
     262    long J = N - 1;
     263
     264    int result;
     265
     266    psS32 temp;
     267    psS32 *index = out->data.S32;
     268    while (1) {
     269        if (I > 0) {
     270            // I--;
     271            temp = index[--I];
     272        } else {
     273            temp = index[J];
     274            index[J] = index[0];
     275            // J--;
     276            if (--J == 0) {
     277                index[0] = temp;
     278                return out;
     279            }
     280        }
     281        long i = I;
     282        long j = (I << 1) + 1;
     283        while (j <= J) {
     284            result = func (in->data[index[j]], in->data[index[j+1]]);
     285            if ((j < J) && (result < 0)) {
     286                j++;
     287            }
     288            result = func (in->data[temp], in->data[index[j]]);
     289            if (result < 0) {
     290                index[i]=index[j];
     291                j += (i=j) + 1;
     292            } else {
     293                j = J + 1;
     294            }
     295        }
     296        index[i] = temp;
     297    }
     298    psAbort ("impossible to reach here");
     299}
     300
    245301psArray* psArraySort(psArray* array,
    246302                     psComparePtrFunc func)
  • trunk/psLib/src/types/psArray.h

    r14927 r15076  
    1010 *  @author Joshua Hoblitt, University of Hawaii
    1111 *
    12  *  @version $Revision: 1.50 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2007-09-20 23:56:57 $
     12 *  @version $Revision: 1.51 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2007-09-28 21:03:24 $
    1414 *
    1515 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    2121#include "psType.h"
    2222#include "psCompare.h"
     23#include "psVector.h"
    2324
    2425/// @addtogroup DataContainer Data Containers
     
    220221);
    221222
     223// return the index which sorts the array
     224psVector *psArraySortIndex (psVector *outIndex, psArray *in, psCompareFunc func);
    222225
    223226/** Set an element in the array.  If the current element is non-NULL, the old
Note: See TracChangeset for help on using the changeset viewer.