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
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098 #include "econfig.h"
00099 #include "dbl_dheaps_i.h"
00100 #include "allocrus.h"
00101 #include "machdefs.h"
00102 #include "except.h"
00103 #include "trace.h"
00104 #ifdef USEDMALLOC
00105 #include "dmalloc.h"
00106 #endif
00107
00108 static int TRACE = 0;
00109
00110 #define dbl_HEAP_D 3
00111 #define dbl_HEAP_UP(x) (((x)-1)/dbl_HEAP_D)
00112 #define dbl_HEAP_DOWN(x) (((x)*dbl_HEAP_D)+1)
00113
00114
00115 static void dbl_dheap_siftup (
00116 dbl_ILLdheap * h,
00117 int i,
00118 int x),
00119 dbl_dheap_siftdown (
00120 dbl_ILLdheap * h,
00121 int i,
00122 int x);
00123
00124 static int dbl_dheap_minchild (
00125 int x,
00126 dbl_ILLdheap * h);
00127
00128
00129 int dbl_ILLutil_dheap_init (
00130 dbl_ILLdheap * h,
00131 int k)
00132 {
00133 int rval = 0;
00134
00135 h->entry = (int *) NULL;
00136 h->loc = (int *) NULL;
00137 h->key = 0;
00138
00139
00140 ILL_SAFE_MALLOC (h->entry, k, int);
00141 ILL_SAFE_MALLOC (h->loc, k, int);
00142
00143 h->key = dbl_EGlpNumAllocArray (k);
00144 h->size = 0;
00145 h->total_space = k;
00146
00147 CLEANUP:
00148
00149 if (rval)
00150 {
00151 dbl_ILLutil_dheap_free (h);
00152 }
00153
00154 ILL_RETURN (rval, "dbl_ILLutil_dheap_init");
00155 }
00156
00157 void dbl_ILLutil_dheap_free (
00158 dbl_ILLdheap * h)
00159 {
00160 ILL_IFFREE (h->entry, int);
00161 ILL_IFFREE (h->loc, int);
00162
00163 dbl_EGlpNumFreeArray (h->key);
00164 }
00165
00166 int dbl_ILLutil_dheap_resize (
00167 dbl_ILLdheap * h,
00168 int newsize)
00169 {
00170 int rval = 0;
00171
00172 if (newsize < h->size || newsize < h->total_space)
00173 {
00174 ILL_CLEANUP;
00175 }
00176
00177 h->key = EGrealloc (h->key, sizeof (double) * newsize);
00178
00179
00180
00181 h->entry = EGrealloc (h->entry, sizeof (int) * newsize);
00182
00183
00184
00185 h->loc = EGrealloc (h->loc, sizeof (int) * newsize);
00186
00187
00188 h->total_space = newsize;
00189
00190 CLEANUP:
00191
00192 ILL_RETURN (rval, "dbl_ILLutil_dheap_resize");
00193 }
00194
00195 void dbl_ILLutil_dheap_findmin (
00196 dbl_ILLdheap * h,
00197 int *i)
00198 {
00199 if (h->size == 0)
00200 *i = -1;
00201 else
00202 *i = h->entry[0];
00203 }
00204
00205 int dbl_ILLutil_dheap_insert (
00206 dbl_ILLdheap * h,
00207 int i)
00208 {
00209 if (h->size >= h->total_space)
00210 {
00211 fprintf (stderr, "Error - dbl_heap already full\n");
00212 return 1;
00213 }
00214 h->size++;
00215 dbl_dheap_siftup (h, i, h->size - 1);
00216
00217 return 0;
00218 }
00219
00220 void dbl_ILLutil_dheap_delete (
00221 dbl_ILLdheap * h,
00222 int i)
00223 {
00224 int j;
00225
00226 h->size--;
00227 j = h->entry[h->size];
00228 h->entry[h->size] = -1;
00229
00230 if (j != i)
00231 {
00232 if (dbl_EGlpNumIsLeq (h->key[j], h->key[i]))
00233 {
00234 dbl_dheap_siftup (h, j, h->loc[i]);
00235 }
00236 else
00237 {
00238 dbl_dheap_siftdown (h, j, h->loc[i]);
00239 }
00240 }
00241 }
00242
00243 void dbl_ILLutil_dheap_deletemin (
00244 dbl_ILLdheap * h,
00245 int *i)
00246 {
00247 int j;
00248
00249 if (h->size == 0)
00250 *i = -1;
00251 else
00252 {
00253 j = h->entry[0];
00254 dbl_ILLutil_dheap_delete (h, j);
00255 *i = j;
00256 }
00257 }
00258
00259 void dbl_ILLutil_dheap_changekey (
00260 dbl_ILLdheap * h,
00261 int i,
00262 double * newkey)
00263 {
00264 if (dbl_EGlpNumIsLess (*newkey, h->key[i]))
00265 {
00266 dbl_EGlpNumCopy (h->key[i], *newkey);
00267 dbl_dheap_siftup (h, i, h->loc[i]);
00268 }
00269 else if (dbl_EGlpNumIsLess (h->key[i], *newkey))
00270 {
00271 dbl_EGlpNumCopy (h->key[i], *newkey);
00272 dbl_dheap_siftdown (h, i, h->loc[i]);
00273 }
00274 }
00275
00276 static void dbl_dheap_siftup (
00277 dbl_ILLdheap * h,
00278 int i,
00279 int x)
00280 {
00281 int p;
00282
00283 p = dbl_HEAP_UP (x);
00284 while (x && dbl_EGlpNumIsLess (h->key[i], h->key[h->entry[p]]))
00285 {
00286 h->entry[x] = h->entry[p];
00287 h->loc[h->entry[p]] = x;
00288 x = p;
00289 p = dbl_HEAP_UP (p);
00290 }
00291 h->entry[x] = i;
00292 h->loc[i] = x;
00293 }
00294
00295 static void dbl_dheap_siftdown (
00296 dbl_ILLdheap * h,
00297 int i,
00298 int x)
00299 {
00300 int c;
00301
00302 c = dbl_dheap_minchild (x, h);
00303
00304 while (c >= 0 && dbl_EGlpNumIsLess (h->key[h->entry[c]], h->key[i]))
00305 {
00306 h->entry[x] = h->entry[c];
00307 h->loc[h->entry[c]] = x;
00308 x = c;
00309 c = dbl_dheap_minchild (c, h);
00310 }
00311 h->entry[x] = i;
00312 h->loc[i] = x;
00313 }
00314
00315 static int dbl_dheap_minchild (
00316 int x,
00317 dbl_ILLdheap * h)
00318 {
00319 int c = dbl_HEAP_DOWN (x);
00320 int cend;
00321 double minval;
00322 int minloc;
00323
00324 if (c >= h->size)
00325 return -1;
00326
00327 dbl_EGlpNumInitVar (minval);
00328 dbl_EGlpNumCopy (minval, h->key[h->entry[c]]);
00329 minloc = c;
00330 cend = c + dbl_HEAP_D;
00331 if (h->size < cend)
00332 cend = h->size;
00333 for (c++; c < cend; c++)
00334 {
00335 if (dbl_EGlpNumIsLess (h->key[h->entry[c]], minval))
00336 {
00337 dbl_EGlpNumCopy (minval, h->key[h->entry[c]]);
00338 minloc = c;
00339 }
00340 }
00341 dbl_EGlpNumClearVar (minval);
00342 return minloc;
00343 }