uves_tools_body.c

00001 /* $Id: uves_tools_body.c,v 1.2 2007/06/22 09:15:35 amodigli 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: amodigli $
00023  * $Date: 2007/06/22 09:15:35 $
00024  * $Revision: 1.2 $
00025  * $Name: uves-3_4_5 $
00026  */
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 
00032 /*----------------------------------------------------------------------------*/
00056 /*----------------------------------------------------------------------------*/
00057 cpl_error_code
00058 CONCAT2X(xxx_tools_sort_stable_pattern, CPL_TYPE_NAME)(
00059     CPL_TYPE const *pix_arr,
00060     int n,
00061     int reverse,
00062     int stable,
00063     int *sort_pattern)
00064 {
00065     int i;  
00066     
00067     /* Check entries */
00068     cpl_ensure(pix_arr, CPL_ERROR_NULL_INPUT,
00069                CPL_ERROR_NULL_INPUT) ;
00070     
00071     cpl_ensure(sort_pattern, CPL_ERROR_NULL_INPUT,
00072                CPL_ERROR_NULL_INPUT) ;
00073 
00074     if (n == 0) return CPL_ERROR_NONE;
00075 
00076     for (i = 0; i < n; i++) {
00077         sort_pattern[i] = i;
00078     }
00079 
00080     /* 
00081      * Heap sort
00082      */
00083     for (i = n / 2 - 1; i >= 0; i--) {
00084         int done = 0;
00085         int root = i;
00086         int bottom = n - 1;
00087         
00088         while ((root*2 + 1 <= bottom) && (!done)) {
00089             int child = root*2 + 1;
00090 
00091             if (child+1 <= bottom) {
00092                 if ((!reverse && CPL_TOOLS_SORT_LT(
00093                          pix_arr[sort_pattern[child]],
00094                          pix_arr[sort_pattern[child + 1]]))
00095                     ||
00096                     (reverse && CPL_TOOLS_SORT_LT(
00097                         pix_arr[sort_pattern[child + 1]],
00098                         pix_arr[sort_pattern[child]]))
00099                     ) {
00100                     child += 1;
00101                 }
00102             }
00103 
00104             if ((!reverse && CPL_TOOLS_SORT_LT(
00105                     pix_arr[sort_pattern[root]],
00106                     pix_arr[sort_pattern[child]])) 
00107                     ||
00108                     (reverse && CPL_TOOLS_SORT_LT(
00109                     pix_arr[sort_pattern[child]],
00110                     pix_arr[sort_pattern[root]]))
00111                     ) {
00112                 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00113                     root = child;
00114             }
00115             else {
00116                 done = 1;
00117             }
00118         }
00119     }
00120     
00121     for (i = n - 1; i >= 1; i--) {
00122         int done = 0;
00123         int root = 0;
00124         int bottom = i - 1;
00125         CPL_INT_SWAP(sort_pattern[0], sort_pattern[i])
00126         
00127         while ((root*2 + 1 <= bottom) && (!done)) {
00128             int child = root*2 + 1;
00129 
00130             if (child+1 <= bottom) {
00131                 if ((!reverse && CPL_TOOLS_SORT_LT(
00132                          pix_arr[sort_pattern[child]],
00133                          pix_arr[sort_pattern[child + 1]]))
00134                     ||
00135                     (reverse && CPL_TOOLS_SORT_LT(
00136                         pix_arr[sort_pattern[child + 1]],
00137                         pix_arr[sort_pattern[child]]))
00138                     ) {
00139                     child += 1;
00140                 }
00141             }
00142             if ((!reverse && CPL_TOOLS_SORT_LT(
00143                     pix_arr[sort_pattern[root]],
00144                     pix_arr[sort_pattern[child]])) 
00145                     ||
00146                 (reverse && CPL_TOOLS_SORT_LT(
00147                     pix_arr[sort_pattern[child]],
00148                     pix_arr[sort_pattern[root]]))
00149                 ) {
00150                 CPL_INT_SWAP(sort_pattern[root], sort_pattern[child])
00151                     root = child;
00152             }
00153             else {
00154                 done = 1;
00155             }
00156         }
00157     }
00158 
00159     /* 
00160      * Enforce stability
00161      */
00162     if (stable) {
00163         for (i = 0; i < n; i++) {
00164             int j;
00165             j = i + 1;
00166             while(j < n &&
00167                   !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[i]],
00168                                      pix_arr[sort_pattern[j]]) &&
00169                   !CPL_TOOLS_SORT_LT(pix_arr[sort_pattern[j]],
00170                                      pix_arr[sort_pattern[i]])) {
00171                 j++;
00172             }
00173             if (j - i > 1) {
00174                 xxx_tools_sort_int(sort_pattern + i, j - i);
00175             }
00176             i = j - 1; 
00177         }
00178     }     
00179 
00180     return CPL_ERROR_NONE ;
00181 }

Generated on Thu Nov 15 14:32:31 2007 for UVES Pipeline Reference Manual by  doxygen 1.5.1