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 #include "econfig.h"
00094 #include "machdefs.h"
00095 #include "dbl_priority.h"
00096 #include "allocrus.h"
00097 #include "except.h"
00098 #ifdef USEDMALLOC
00099 #include "dmalloc.h"
00100 #endif
00101
00102
00103 int dbl_ILLutil_priority_init (
00104 dbl_ILLpriority * pri,
00105 int k)
00106 {
00107 int i;
00108 int list;
00109 int rval = 0;
00110
00111 pri->space = k;
00112 ILL_SAFE_MALLOC (pri->pri_info, k, union dbl_ILLpri_data);
00113
00114 rval = dbl_ILLutil_dheap_init (&pri->dbl_heap, k);
00115 ILL_CLEANUP_IF (rval);
00116
00117 list = -1;
00118 for (i = k - 1; i >= 0; i--)
00119 {
00120 pri->pri_info[i].next = list;
00121 list = i;
00122 }
00123 pri->freelist = list;
00124
00125 CLEANUP:
00126
00127 if (rval)
00128 {
00129 ILL_IFFREE (pri->pri_info, union dbl_ILLpri_data);
00130 }
00131 return rval;
00132 }
00133
00134 void dbl_ILLutil_priority_free (
00135 dbl_ILLpriority * pri)
00136 {
00137 dbl_ILLutil_dheap_free (&pri->dbl_heap);
00138 ILL_IFFREE (pri->pri_info, union dbl_ILLpri_data);
00139
00140 pri->space = 0;
00141 }
00142
00143 void dbl_ILLutil_priority_findmin (
00144 dbl_ILLpriority * pri,
00145 double * keyval,
00146 void **en)
00147 {
00148 int handle;
00149
00150 dbl_ILLutil_dheap_findmin (&pri->dbl_heap, &handle);
00151
00152 if (handle < 0)
00153 {
00154 *en = (void *) NULL;
00155 }
00156 else
00157 {
00158 if (keyval)
00159 dbl_EGlpNumCopy (*keyval, pri->dbl_heap.key[handle]);
00160 *en = pri->pri_info[handle].data;
00161 }
00162 }
00163
00164 int dbl_ILLutil_priority_insert (
00165 dbl_ILLpriority * pri,
00166 void *data,
00167 double * keyval,
00168 int *handle)
00169 {
00170 int newsize;
00171 int i;
00172 int list;
00173 int rval = 0;
00174
00175 if (pri->freelist == -1)
00176 {
00177
00178 newsize = pri->space + (pri->space / 3);
00179 if (newsize < pri->space + 1000)
00180 newsize = pri->space + 1000;
00181 rval = dbl_ILLutil_dheap_resize (&pri->dbl_heap, newsize);
00182 ILL_CLEANUP_IF (rval);
00183
00184 pri->pri_info =
00185 EGrealloc (pri->pri_info, sizeof (union dbl_ILLpri_data) * newsize);
00186
00187
00188
00189
00190 list = -1;
00191 for (i = newsize - 1; i >= pri->space; i--)
00192 {
00193 pri->pri_info[i].next = list;
00194 list = i;
00195 }
00196 pri->space = newsize;
00197 pri->freelist = list;
00198 }
00199
00200 i = pri->freelist;
00201 pri->freelist = pri->pri_info[i].next;
00202 pri->pri_info[i].data = data;
00203 dbl_EGlpNumCopy (pri->dbl_heap.key[i], *keyval);
00204 rval = dbl_ILLutil_dheap_insert (&pri->dbl_heap, i);
00205 ILL_CLEANUP_IF (rval);
00206
00207 if (handle)
00208 *handle = i;
00209
00210 CLEANUP:
00211
00212 return rval;
00213 }
00214
00215 void dbl_ILLutil_priority_delete (
00216 dbl_ILLpriority * pri,
00217 int handle)
00218 {
00219 dbl_ILLutil_dheap_delete (&pri->dbl_heap, handle);
00220 pri->pri_info[handle].next = pri->freelist;
00221 pri->freelist = handle;
00222 }
00223
00224 void dbl_ILLutil_priority_deletemin (
00225 dbl_ILLpriority * pri,
00226 double * keyval,
00227 void **en)
00228 {
00229 int handle;
00230 void *data;
00231
00232 dbl_ILLutil_dheap_deletemin (&pri->dbl_heap, &handle);
00233
00234 if (handle < 0)
00235 {
00236 *en = (void *) NULL;
00237 }
00238 else
00239 {
00240 if (keyval)
00241 dbl_EGlpNumCopy (*keyval, pri->dbl_heap.key[handle]);
00242 data = pri->pri_info[handle].data;
00243 pri->pri_info[handle].next = pri->freelist;
00244 pri->freelist = handle;
00245 *en = data;
00246 }
00247 }
00248
00249 void dbl_ILLutil_priority_changekey (
00250 dbl_ILLpriority * pri,
00251 int handle,
00252 double * newkey)
00253 {
00254 dbl_ILLutil_dheap_changekey (&pri->dbl_heap, handle, newkey);
00255 }