00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
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
00150
00151
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