00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00014
00015
00016
00017
00018
00019
00020
00021 #include "pdyn.h"
00022
00023
00024
00025
00026 static int n_pdyn_mem = 0;
00027
00028
00029
00030
00031 static pdyn *pdyn_mem = NULL;
00032 static bool *pdyn_free = NULL;
00033 static int *pdyn_ptr = NULL;
00034
00035
00036
00037 static int n_alloc = 0;
00038 static int *pdyn_alloc = NULL;
00039
00040
00041
00042
00043
00044 void dealloc_all()
00045 {
00046
00047
00048 n_alloc = 0;
00049 for (int i = 0; i < n_pdyn_mem; i++) {
00050 pdyn_free[i] = true;
00051 pdyn_ptr[i] = i;
00052 pdyn_alloc[i] = i;
00053 }
00054 }
00055
00056 bool initialize_pdyn(int n,
00057 hbpfp the_hbpfp,
00058 sbpfp the_sbpfp,
00059 bool use_stories)
00060 {
00061
00062
00063
00064 n_pdyn_mem = 0;
00065 pdyn_mem = new pdyn[n](the_hbpfp, the_sbpfp, use_stories);
00066 pdyn_free = new bool[n];
00067 pdyn_ptr = new int[n];
00068 pdyn_alloc = new int[n];
00069
00070 if (pdyn_mem && pdyn_free && pdyn_ptr && pdyn_alloc) {
00071 n_pdyn_mem = n;
00072 dealloc_all();
00073
00074 cerr << "initialize_pdyn: created " << n
00075 << " pdyn elements; pdyn_mem = " << pdyn_mem << endl;
00076
00077 return true;
00078
00079 } else
00080 return false;
00081 }
00082
00083
00084
00085
00086 pdyn *alloc_pdyn(hbpfp the_hbpfp,
00087 sbpfp the_sbpfp,
00088 bool use_stories,
00089 bool reinit)
00090 {
00091 if (pdyn_mem) {
00092
00093
00094
00095
00096
00097
00098 if (n_alloc >= n_pdyn_mem) err_exit("pdyn array overflow");
00099
00100
00101
00102
00103 int i = pdyn_alloc[n_alloc];
00104 pdyn_free[i] = false;
00105 pdyn_ptr[i] = n_alloc++;
00106
00107 pdyn *p = pdyn_mem + i;
00108
00109 if (reinit) {
00110
00111
00112
00113 p->pdyn_init();
00114 p->dyn_init();
00115 p->node_init();
00116
00117
00118
00119 if (!use_stories)
00120 p->rmstory();
00121 else
00122 p->node_set_stories(use_stories);
00123
00124 if (!the_hbpfp)
00125 p->rmhydrobase();
00126 else
00127 p->node_set_hbase(the_hbpfp);
00128
00129 if (!the_sbpfp)
00130 p->rmstarbase();
00131 else
00132 p->node_set_sbase(the_sbpfp);
00133 }
00134
00135 return p;
00136
00137 } else
00138
00139
00140
00141 return new pdyn(the_hbpfp, the_sbpfp, use_stories);
00142 }
00143
00144
00145
00146
00147 void dealloc_pdyn(pdyn *p)
00148 {
00149 if (p) {
00150
00151 if (pdyn_mem) {
00152
00153
00154
00155
00156 int i = p - pdyn_mem;
00157 pdyn_free[i] = true;
00158 int j = pdyn_ptr[i];
00159
00160
00161
00162
00163
00164 int itmp = pdyn_alloc[n_alloc-1];
00165
00166 pdyn_alloc[--n_alloc] = i;
00167 pdyn_ptr[i] = n_alloc;
00168
00169 pdyn_alloc[j] = itmp;
00170 pdyn_ptr[itmp] = j;
00171
00172
00173
00174
00175
00176
00177
00178
00179 p->rmkepler();
00180
00181
00182
00183 p->set_invalid();
00184 p->clear_name();
00185 if (p->is_root()) p->set_root(NULL);
00186 p->rmstory();
00187 p->rmhydrobase();
00188 p->rmstarbase();
00189
00190 } else
00191
00192
00193
00194 delete p;
00195 }
00196 }
00197
00198
00199
00200 node * new_pdyn(hbpfp the_hbpfp,
00201 sbpfp the_sbpfp,
00202 bool use_stories)
00203 {
00204 return (node *) new pdyn(the_hbpfp, the_sbpfp, use_stories);
00205 }
00206
00207
00208
00209 node * new_pdyn2(hbpfp the_hbpfp,
00210 sbpfp the_sbpfp,
00211 bool use_stories)
00212 {
00213 return alloc_pdyn(the_hbpfp, the_sbpfp, use_stories);
00214 }