uves_tools_body.c

00001 /* $Id: uves_tools_body.c,v 1.1 2007/06/06 06:27:38 jmlarsen Exp $
00002  *
00003  * This file is part of the ESO Common Pipeline Library
00004  * Copyright (C) 2001-2004 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: jmlarsen $
00023  * $Date: 2007/06/06 06:27:38 $
00024  * $Revision: 1.1 $
00025  * $Name: uves-3_3_1 $
00026  */
00027 
00028 /*----------------------------------------------------------------------------*/
00052 /*----------------------------------------------------------------------------*/
00053 cpl_error_code
00054 CONCAT2X(xxx_tools_sort_stable_pattern, CPL_TYPE_NAME)(
00055     CPL_TYPE const *pix_arr,
00056     int n,
00057     int reverse,
00058     int stable,
00059     int *sort_pattern)
00060 {
00061     int i;  
00062     
00063     /* Check entries */
00064     cpl_ensure(pix_arr, CPL_ERROR_NULL_INPUT,
00065                CPL_ERROR_NULL_INPUT) ;
00066     
00067     cpl_ensure(sort_pattern, CPL_ERROR_NULL_INPUT,
00068                CPL_ERROR_NULL_INPUT) ;
00069 
00070     if (n == 0) return CPL_ERROR_NONE;
00071 
00072     for (i = 0; i < n; i++) {
00073         sort_pattern[i] = i;
00074     }
00075 
00076     /* 
00077      * Heap sort
00078      */
00079     for (i = n / 2 - 1; i >= 0; i--) {
00080         int done = 0;
00081         int root = i;
00082         int bottom = n - 1;
00083         
00084         while ((root*2 + 1 <= bottom) && (!done)) {
00085             int child = root*2 + 1;
00086 
00087             if (child+1 <= bottom) {
00088                 if ((!reverse && CPL_TOOLS_SORT_LT(
00089                          pix_arr[sort_pattern[child]],
00090                          pix_arr[sort_pattern[child + 1]]))
00091                     ||
00092                     (reverse && CPL_TOOLS_SORT_LT(
00093                         pix_arr[sort_pattern[child + 1]],
00094                         pix_arr[sort_pattern[child]]))
00095                     ) {
00096                     child += 1;
00097                 }
00098             }
00099 
00100             if ((!reverse && CPL_TOOLS_SORT_LT(
00101                     pix_arr[sort_pattern[root]],
00102                     pix_arr[sort_pattern[child]])) 
00103                     ||
00104                     (reverse && CPL_TOOLS_SORT_LT(
00105                     pix_arr[sort_pattern[child]],
00106                     pix_arr[sort_pattern[root]]))
00107                     ) {
00108                 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00109                     root = child;
00110             }
00111             else {
00112                 done = 1;
00113             }
00114         }
00115     }
00116     
00117     for (i = n - 1; i >= 1; i--) {
00118         int done = 0;
00119         int root = 0;
00120         int bottom = i - 1;
00121         CPL_INT_SWAP(sort_pattern[0], sort_pattern[i])
00122         
00123         while ((root*2 + 1 <= bottom) && (!done)) {
00124             int child = root*2 + 1;
00125 
00126             if (child+1 <= bottom) {
00127                 if ((!reverse && CPL_TOOLS_SORT_LT(
00128                          pix_arr[sort_pattern[child]],
00129                          pix_arr[sort_pattern[child + 1]]))
00130                     ||
00131                     (reverse && CPL_TOOLS_SORT_LT(
00132                         pix_arr[sort_pattern[child + 1]],
00133                         pix_arr[sort_pattern[child]]))
00134                     ) {
00135                     child += 1;
00136                 }
00137             }
00138             if ((!reverse && CPL_TOOLS_SORT_LT(
00139                     pix_arr[sort_pattern[root]],
00140                     pix_arr[sort_pattern[child]])) 
00141                     ||
00142                 (reverse && CPL_TOOLS_SORT_LT(
00143                     pix_arr[sort_pattern[child]],
00144                     pix_arr[sort_pattern[root]]))
00145                 ) {
00146                 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00147                     root = child;
00148             }
00149             else {
00150                 done = 1;
00151             }
00152         }
00153     }
00154 
00155     /* 
00156      * Enforce stability
00157      */
00158     if (stable) {
00159         for (i = 0; i < n; i++) {
00160             int j;
00161             j = i + 1;
00162             while(j < n &&
00163                   !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[i]],
00164                                      pix_arr[sort_pattern[j]]) &&
00165                   !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[j]],
00166                                      pix_arr[sort_pattern[i]])) {
00167                 j++;
00168             }
00169             if (j - i > 1) {
00170                 xxx_tools_sort_int(sort_pattern + i, j - i);
00171             }
00172             i = j - 1; 
00173         }
00174     }     
00175 
00176     return CPL_ERROR_NONE ;
00177 }

Generated on Tue Jun 19 14:39:17 2007 for UVES Pipeline Reference Manual by  doxygen 1.4.6