dbl_sortrus.c

Go to the documentation of this file.
00001 /****************************************************************************/
00002 /*                                                                          */
00003 /*  This file is part of QSopt_ex.                                          */
00004 /*                                                                          */
00005 /*  (c) Copyright 2006 by David Applegate, William Cook, Sanjeeb Dash,      */
00006 /*  and Daniel Espinoza                                                     */
00007 /*                                                                          */
00008 /*  Sanjeeb Dash ownership of copyright in QSopt_ex is derived from his     */
00009 /*  copyright in QSopt.                                                     */
00010 /*                                                                          */
00011 /*  This code may be used under the terms of the GNU General Public License */
00012 /*  (Version 2.1 or later) as published by the Free Software Foundation.    */
00013 /*                                                                          */
00014 /*  Alternatively, use is granted for research purposes only.               */
00015 /*                                                                          */
00016 /*  It is your choice of which of these two licenses you are operating      */
00017 /*  under.                                                                  */
00018 /*                                                                          */
00019 /*  We make no guarantees about the correctness or usefulness of this code. */
00020 /*                                                                          */
00021 /****************************************************************************/
00022 
00023 /* RCSINFO $Id: dbl_sortrus.c,v 1.2 2003/11/05 16:47:22 meven Exp $ */
00024 /****************************************************************************/
00025 /*                                                                          */
00026 /*  This file is part of CONCORDE                                           */
00027 /*                                                                          */
00028 /*  (c) Copyright 1995--1999 by David Applegate, Robert Bixby,              */
00029 /*  Vasek Chvatal, and William Cook                                         */
00030 /*                                                                          */
00031 /*  Permission is granted for academic research use.  For other uses,       */
00032 /*  contact the authors for licensing options.                              */
00033 /*                                                                          */
00034 /*  Use at your own risk.  We make no guarantees about the                  */
00035 /*  correctness or usefulness of this code.                                 */
00036 /*                                                                          */
00037 /****************************************************************************/
00038 
00039 /****************************************************************************/
00040 /*                                                                          */
00041 /*                         SORTING ROUTINES                                 */
00042 /*                                                                          */
00043 /*                             TSP CODE                                     */
00044 /*                                                                          */
00045 /*   Written by:  Applegate, Bixby, Chvatal, and Cook                       */
00046 /*   DATE:  February 24, 1994                                               */
00047 /*                                                                          */
00048 /*    EXPORTED FUNCTIONS:                                                   */
00049 /*                                                                          */
00050 /*  char *ILLutil_linked_radixsort (char *data, char *datanext,              */
00051 /*      char *dataval, int valsize)                                         */
00052 /*    USAGE:                                                                */
00053 /*      head = (bar *) ILLutil_linked_radixsort ((char *) head,              */
00054 /*         (char *) &(head->next), (char *) &(head->val), sizeof (int));    */
00055 /*    Then head is the start of the linked list in increasing order of      */
00056 /*    val, with next as the field that links the bars.                      */
00057 /*    WARNING: DOES NOT HANDLE NEGATIVE NUMBERS PROPERLY.                   */
00058 /*                                                                          */
00059 /*  void ILLutil_int_array_quicksort (int *len, int n)                       */
00060 /*    len - the array to be sorted                                          */
00061 /*    n - the number of elements in len                                     */
00062 /*    Uses quicksort to put len in increasing order.                        */
00063 /*                                                                          */
00064 /*  void ILLutil_int_perm_quicksort (int *perm, int *len, int n)             */
00065 /*    perm - must be allocated and initialized by the calling routine,      */
00066 /*           it will be arranged in increasing order of len.                */
00067 /*    n - the number of elements in perm and len.                           */
00068 /*                                                                          */
00069 /*  void ILLutil_double_perm_quicksort (int *perm, double *len, int n)       */
00070 /*    perm - must be allocated and initialized by the calling routine,      */
00071 /*           it will be arranged in increasing order of len.                */
00072 /*    n - the number of elements in perm and len.                           */
00073 /*                                                                          */
00074 /*  void ILLutil_rselect (int *arr, int l, int r, int m,                     */
00075 /*      double *coord, ILLrandstate *rstate)                                 */
00076 /*    arr - permutation that will be rearranged                             */
00077 /*    l,r - specify the range of arr that we are interested in              */
00078 /*    m - is the index into l,r that is the break point for the perm        */
00079 /*    coord - gives the keys that determine the ordering                    */
00080 /*                                                                          */
00081 /****************************************************************************/
00082 
00083 #include "qs_config.h"
00084 #include "machdefs.h"
00085 #include "dbl_util.h"
00086 #include "except.h"
00087 #ifdef USEDMALLOC
00088 #include "dmalloc.h"
00089 #endif
00090 
00091 
00092 #define dbl_BITS_PER_PASS (8)
00093 
00094 #define dbl_NBINS (1<<dbl_BITS_PER_PASS)
00095 
00096 
00097 static void dbl_select_EGlpNum_split (
00098   int *arr,
00099   int n,
00100   double * v,
00101   int *start,
00102   int *end,
00103   double * coord),
00104   dbl_select_EGlpNum_sort (
00105   int *arr,
00106   int n,
00107   double * coord),
00108   dbl_select_EGlpNum_sort_dsample (
00109   double * samp,
00110   int n);
00111 
00112 void dbl_ILLutil_EGlpNum_perm_quicksort (
00113   int *perm,
00114   double * len,
00115   int n)
00116 {
00117   int i, j, temp;
00118   double t;
00119 
00120   if (n <= 1)
00121     return;
00122 
00123   dbl_EGlpNumInitVar (t);
00124   dbl_ILL_SWAP (perm[0], perm[(n - 1) / 2], temp);
00125 
00126   i = 0;
00127   j = n;
00128   dbl_EGlpNumCopy (t, len[perm[0]]);
00129 
00130   for (;;)
00131   {
00132     do
00133       i++;
00134     while (i < n && dbl_EGlpNumIsLess (len[perm[i]], t));
00135     do
00136       j--;
00137     while (dbl_EGlpNumIsLess (t, len[perm[j]]));
00138     if (j < i)
00139       break;
00140     dbl_ILL_SWAP (perm[i], perm[j], temp);
00141   }
00142   dbl_ILL_SWAP (perm[0], perm[j], temp);
00143 
00144   dbl_EGlpNumClearVar (t);
00145   dbl_ILLutil_EGlpNum_perm_quicksort (perm, len, j);
00146   dbl_ILLutil_EGlpNum_perm_quicksort (perm + i, len, n - i);
00147 }
00148 
00149 /**********  Median - Select Routines **********/
00150 
00151 /* dbl_NSAMPLES should be odd */
00152 #define dbl_NSAMPLES 3
00153 #define dbl_SORTSIZE 20
00154 
00155 
00156 void dbl_ILLutil_EGlpNum_rselect (
00157   int *arr,
00158   int l,
00159   int r,
00160   int m,
00161   double * coord,
00162   ILLrandstate * rstate)
00163 {
00164   double *samplevals = dbl_EGlpNumAllocArray (dbl_NSAMPLES);
00165   int i;
00166   int st, en;
00167   int n;
00168 
00169   arr += l;
00170   n = r - l + 1;
00171   m -= l;
00172 
00173   while (n > dbl_SORTSIZE)
00174   {
00175     for (i = 0; i < dbl_NSAMPLES; i++)
00176     {
00177       dbl_EGlpNumCopy (samplevals[i], coord[arr[ILLutil_lprand (rstate) % n]]);
00178     }
00179     dbl_select_EGlpNum_sort_dsample (samplevals, dbl_NSAMPLES);
00180     dbl_select_EGlpNum_split (arr, n, &(samplevals[(dbl_NSAMPLES - 1) / 2]),
00181                           &st, &en, coord);
00182     if (st > m)
00183     {
00184       n = st;
00185     }
00186     else if (en <= m)
00187     {
00188       arr += en;
00189       n -= en;
00190       m -= en;
00191     }
00192     else
00193     {
00194       return;
00195     }
00196   }
00197 
00198   dbl_select_EGlpNum_sort (arr, n, coord);
00199   dbl_EGlpNumFreeArray (samplevals);
00200   return;
00201 }
00202 
00203 static void dbl_select_EGlpNum_split (
00204   int *arr,
00205   int n,
00206   double * v,
00207   int *start,
00208   int *end,
00209   double * coord)
00210 {
00211   int i, j, k;
00212   int t;
00213 
00214   i = 0;
00215   j = k = n;
00216 
00217   while (i < j)
00218   {
00219     if (dbl_EGlpNumIsLess (coord[arr[i]], *v))
00220     {
00221       i++;
00222     }
00223     else if (dbl_EGlpNumIsEqqual (coord[arr[i]], *v))
00224     {
00225       j--;
00226       dbl_ILL_SWAP (arr[i], arr[j], t);
00227     }
00228     else
00229     {
00230       j--;
00231       k--;
00232       t = arr[i];
00233       arr[i] = arr[j];
00234       arr[j] = arr[k];
00235       arr[k] = t;
00236     }
00237   }
00238   *start = j;
00239   *end = k;
00240   return;
00241 }
00242 
00243 static void dbl_select_EGlpNum_sort (
00244   int *arr,
00245   int n,
00246   double * coord)
00247 {
00248   int i, j;
00249   int t;
00250 
00251   for (i = 1; i < n; i++)
00252   {
00253     t = arr[i];
00254     for (j = i; j > 0 && dbl_EGlpNumIsLess (coord[t], coord[arr[j - 1]]); j--)
00255     {
00256       arr[j] = arr[j - 1];
00257     }
00258     arr[j] = t;
00259   }
00260 }
00261 
00262 static void dbl_select_EGlpNum_sort_dsample (
00263   double * samp,
00264   int n)
00265 {
00266   int i, j;
00267   double t;
00268 
00269   dbl_EGlpNumInitVar (t);
00270 
00271   for (i = 1; i < n; i++)
00272   {
00273     dbl_EGlpNumCopy (t, samp[i]);
00274     for (j = i; j > 0 && dbl_EGlpNumIsLess (t, samp[j - 1]); j--)
00275     {
00276       dbl_EGlpNumCopy (samp[j], samp[j - 1]);
00277     }
00278     dbl_EGlpNumCopy (samp[j], t);
00279   }
00280   dbl_EGlpNumClearVar (t);
00281 }
00282 
00283 
00284 
00285 

Generated on Thu Mar 29 09:32:29 2012 for QSopt_ex by  doxygen 1.4.7