IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 4, 2008, 2:10:40 PM (18 years ago)
Author:
eugene
Message:

some efficiency improvements and cleanups to the footprint code

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/pmFootprintsAssignPeaks.c

    r17443 r17516  
    88psErrorCode
    99pmFootprintsAssignPeaks(psArray *footprints,    // the pmFootprints
    10                           const psArray *peaks) { // the pmPeaks
     10                        const psArray *peaks) { // the pmPeaks
    1111    assert (footprints != NULL);
    1212    assert (footprints->n == 0 || pmFootprintTest(footprints->data[0]));
     
    5454   
    5555    psFree(ids);
    56     //
     56
    5757    // Make sure that peaks within each footprint are sorted and unique
    58     //
    5958    for (int i = 0; i < footprints->n; i++) {
     59       
    6060        pmFootprint *fp = footprints->data[i];
     61
     62        // XXX are we allowed to have peak-less footprints??
     63        if (!fp->peaks->n) continue;
     64
    6165        fp->peaks = psArraySort(fp->peaks, pmPeakSortBySN);
    6266
    63         for (int j = 1; j < fp->peaks->n; j++) { // check for duplicates
    64             if (fp->peaks->data[j] == fp->peaks->data[j-1]) {
    65                 (void)psArrayRemoveIndex(fp->peaks, j);
    66                 j--;                    // we moved everything down one
     67        // XXX EAM : the algorithm below should be much faster than using psArrayRemove if
     68        // the number of peaks in the footprint is large, or if there are no duplicates.
     69        // if we have a lot of small-number peak arrays with duplicates, this may be
     70        // slower.
     71
     72        // track the number of good peaks in the footprint
     73        int nGood = 1;
     74
     75        // check for duplicates
     76        // on first pass, we set the index to NULL if peak is a duplicate
     77        // XXX EAM : this can leave behind duplicates of the same S/N
     78        // (if sorted list has A, B, A, B ...)
     79        for (int j = 1; j < fp->peaks->n; j++) {
     80            if (fp->peaks->data[j] == fp->peaks->data[nGood]) {
     81                // everything on the array has its own mem reference; free and drop this one
     82                psFree (fp->peaks->data[j]);
     83                fp->peaks->data[j] = NULL;
     84            } else {
     85                nGood ++;
    6786            }
    6887        }
     88
     89        // no deleted peaks, go to next footprint
     90        if (nGood == fp->peaks->n) continue;
     91
     92        int nKeep = 0;
     93
     94        psArray *goodPeaks = psArrayAlloc (nGood);
     95        // on second pass, save the good peaks
     96        for (int j = 0; j < fp->peaks->n; j++) { // check for duplicates
     97            if (fp->peaks->data[j] == NULL) continue;
     98            // transfer the data (NULL to avoid double free)
     99            // this is only slightly sleazy
     100            goodPeaks->data[nKeep] = fp->peaks->data[j];
     101            fp->peaks->data[j] = NULL;
     102            nKeep ++;
     103        }
     104        psAssert (nGood == nKeep, "mis-counted nKeep or nGood");
     105
     106        // free the old (now NULL-filled) array
     107        psFree (fp->peaks);
     108        fp->peaks = goodPeaks;
    69109    }
     110
     111    // (void)psArrayRemoveIndex(fp->peaks, j);
     112
    70113
    71114    return PS_ERR_NONE;
Note: See TracChangeset for help on using the changeset viewer.