00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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 #include <memory.h>
00077 #include "udanax.h"
00078
00086
00087
00088 bool
00089 splitcrumupwards(
00090 typecuc *father)
00091 {
00092 bool splitsomething = false;
00093
00094 if (father->height <= 0)
00095 assert(0);
00096
00097 for (; toomanysons(father); father = (typecuc *) findfather((typecorecrum *) father)) {
00098 if (isfullcrum((typecorecrum *) father)) {
00099 levelpush(father);
00100 splitcrum((typecuc *) findleftson(father));
00101
00102 #ifndef DISTRIBUTION
00103 L("splitcrumupwards split something\n");
00104 asserttreeisok((typecorecrum *) father);
00105 #endif
00106 return true;
00107 }
00108 splitcrum(father);
00109 splitsomething = true;
00110 }
00111
00112 #ifndef DISTRIBUTION
00113 asserttreeisok((typecorecrum *) father);
00114 #endif
00115
00116 return splitsomething;
00117 }
00118
00126
00127 static void
00128 splitcrumseq(
00129 typecuc *father)
00130 {
00131 typecorecrum *newcc, *ptr, *next;
00132 int i, halfsons;
00133
00134 ivemodified((typecorecrum *) father);
00135 newcc = createcrum((int) father->height, (int) father->cenftype);
00136 reserve(newcc);
00137 adopt(newcc, RIGHTBRO, (typecorecrum *) father);
00138 rejuvinate(newcc);
00139 ivemodified(newcc);
00140 halfsons = father->numberofsons / 2;
00141 for (i = 0, ptr = findrightmostson(father); i < halfsons && ptr; ++i, ptr = next) {
00142 next = findleftbro(ptr);
00143 disown(ptr);
00144 adopt(ptr, LEFTMOSTSON, newcc);
00145 rejuvinate(ptr);
00146 ivemodified(ptr);
00147 }
00148
00149 setwispupwards(father, 0);
00150 setwispupwards((typecuc *) newcc, 0);
00151 }
00152
00160 static void
00161 splitcrumsp(
00162 typecuc *father)
00163 {
00164 typecorecrum *ptr, *correctone;
00165
00166 for (correctone = ptr = findleftson(father); ptr; ptr = findrightbro(ptr)) {
00167
00168 if (comparecrumsdiagonally(ptr, correctone) == GREATER)
00169 correctone = ptr;
00170 }
00171 peelcrumoffnd(correctone);
00172 }
00173
00181 static void
00182 splitcrumpm(
00183 typecuc *father)
00184 {
00185 typecorecrum *ptr, *correctone;
00186
00187 for (correctone = ptr = findleftson(father); ptr; ptr = findrightbro(ptr)) {
00188 if (tumblercmp(&ptr->cdsp.dsas[SPANRANGE], &correctone->cdsp.dsas[SPANRANGE]) == GREATER)
00189
00190 correctone = ptr;
00191 }
00192 peelcrumoffnd(correctone);
00193 }
00194
00202
00203 void
00204 splitcrum(
00205 typecuc *father)
00206 {
00207 switch (father->cenftype) {
00208 case GRAN: splitcrumseq(father); break;
00209 case POOM: splitcrumpm(father); break;
00210 case SPAN: splitcrumsp(father); break;
00211 default:
00212 assert(0);
00213 }
00214 setwispupwards(father, 0);
00215 }
00216
00224 void
00225 peelcrumoffnd(
00226 typecorecrum *ptr)
00227 {
00228 if (isfullcrum(ptr))
00229 assert(0);
00230
00231 typecuc *father = findfather(ptr);
00232 int ofatherage = father->age;
00233 int optrage = ptr->age;
00234
00235 father->age = NEW;
00236 ptr->age = NEW;
00237
00238 reserve((typecorecrum *) father);
00239 father->modified = true;
00240 reserve(ptr);
00241 disown(ptr);
00242
00243 typecorecrum *newcc = createcrum((int) father->height, (int) father->cenftype);
00244 adopt(newcc, RIGHTBRO, (typecorecrum *) father);
00245 movewisp(&father->cdsp, &newcc->cdsp);
00246 adopt(ptr, LEFTMOSTSON, newcc);
00247 rejuvinate(newcc);
00248 rejuvinate(ptr);
00249 rejuvinate((typecorecrum *) father);
00250
00251 if (ofatherage == RESERVED)
00252 father->age = RESERVED;
00253
00254 if (optrage == RESERVED)
00255 ptr->age = RESERVED;
00256
00257 ivemodified(ptr);
00258 setwispupwards(father, 0);
00259 setwispupwards((typecuc *) newcc, 0);
00260 setwispupwards((typecuc *) ptr, 1);
00261 }