libsrc/ndcuts.cxx

Go to the documentation of this file.
00001 /**********************************************************************
00002  * Copyright 2002 Jeff Rush <jrush@taupro.com>
00003  * Original Copyright 1979-2002 Udanax.com
00004  *
00005  * This file is part of the Udanax xanalogical storage system.
00006  *
00007  * Udanax is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * Udanax is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with Udanax; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  **********************************************************************/
00021 
00030 /* Modification History:
00031  * $Log: ndcuts.cxx,v $
00032  * Revision 1.12  2004/09/11 13:59:21  jrush
00033  * Changed all fprintf's to stderr to use the Nana library L() macro.  Also
00034  * removed a 2-3 minor compiler warnings.
00035  *
00036  * Revision 1.11  2004/09/04 16:02:17  jrush
00037  * Added Doxygen headers before each and every function definition.
00038  *
00039  * Revision 1.10  2002/07/26 04:32:01  jrush
00040  * Replaced gerror() with assert()
00041  *
00042  * Revision 1.9  2002/05/28 04:22:29  jrush
00043  * Adjusted source files to comply with GPL licensing.
00044  *
00045  * Revision 1.8  2002/05/28 02:51:11  jrush
00046  * Made static any functions and global variables private to this source file
00047  * and commented out any unused functions.
00048  *
00049  * Revision 1.7  2002/04/12 11:56:43  jrush
00050  * Reorganized include file layout, renamed xanadu.h to udanax.h and
00051  * typecontext/typecrumcontext to C++ class Context/CrumContext.
00052  *
00053  * Revision 1.6  2002/04/09 21:45:46  jrush
00054  * Renamed class 'tumbler' to 'Tumbler', for consistency with Python sources,
00055  * and changed typeisa from typedef to a subclass, in preparation for cleaning
00056  * up the type/class tree.
00057  *
00058  * Revision 1.5  2002/04/08 18:55:55  jrush
00059  * Switched from using global variable 'user' to object 'Session' as a
00060  * connection for tracking open document descriptors in the bert table.
00061  *
00062  * Revision 1.4  2002/04/06 19:51:30  jrush
00063  * Renamed TRUE/FALSE constant use to the C++ standard of true/false.
00064  *
00065  * Revision 1.3  2002/04/06 15:01:17  jrush
00066  * Changed INT to just 'int'.
00067  *
00068  * Revision 1.2  2002/02/14 09:27:43  jrush
00069  * Cleaned up source:
00070  *
00071  * 1. ran thru the indent tool to achieve a standard look,
00072  * 2. added structured comments at top for use with DOxygen reporting
00073  *    as well as CVS keywords,
00074  * 3. fixed compiler warnings re ambiguous assign/compares,
00075  *    needed casts and unused/uninitialized variables,
00076  * 4. fixed funcs that didn't specify a return type,
00077  * 5. centralized prototypes in protos.h, removing incomplete ones,
00078  * 6. cleaned up use of bool/BOOLEAN type to suit C++ type,
00079  * 7. fixed initializer nesting in tumbler constants,
00080  * 8. renamed vars that conflict with C++ keywords (new, this),
00081  * 9. fixed global/extern confusion re some global vars.
00082  *
00083  */
00084 
00085 #include <memory.h>
00086 #include "udanax.h"
00087 
00088 static void makecutsbackuptohere(typecuc *ptr, typewid *offset, typeknives *knives);
00089 
00097     static bool
00098 crumiscut(
00099     typecuc    *ptr,
00100     typewid    *offset,
00101     typeknives *knives)
00102 {
00103     int i;
00104     for (i = 0; i < knives->nblades; ++i) {
00105         if (whereoncrum((typecorecrum *) ptr, offset, &knives->blades[i], knives->dimension) == THRUME)
00106             return true;
00107     }
00108     return false;
00109 }
00110 
00118     static bool
00119 sonsarecut(
00120     typecuc    *ptr,
00121     typewid    *offset,
00122     typeknives *knives)
00123 {
00124     typecuc *son;
00125     typewid grasp;
00126 
00127     prologuend((typecorecrum *) ptr, offset, &grasp, (typedsp *) NULL);
00128     for (son = (typecuc *) findleftson(ptr); son; son = (typecuc *) findrightbro((typecorecrum *) son)) {
00129         if (crumiscut(son, &grasp, knives))
00130             return true;
00131     }
00132 
00133     return false;
00134 }
00135 
00143     static void
00144 makecutsdownnd(
00145     typecuc    *fullcrumptr,
00146     typewid    *offset,
00147     typeknives *knives)
00148 {
00149     typecuc *ptr;
00150     typecorecrum *sonptr, *son;
00151     int n;
00152 
00153     ptr = fullcrumptr;
00154     for (; (knives->nblades > 1) && false && ptr->height;) {
00155         for (n = 0, son = findleftson(ptr); son; son = findrightbro(son)) {
00156             if (crumiscut((typecuc *) son, offset, knives)) {
00157                 n++;
00158                 sonptr = son;
00159             }
00160         }
00161 
00162         if (n == 1) {
00163             prologuend((typecorecrum *) ptr, offset, offset, (typedsp *) NULL);
00164             ptr = (typecuc *) sonptr;
00165         } else
00166             break;
00167     }                                  /* now we're at intersection */
00168 
00169     makecutsbackuptohere(ptr, offset, knives);
00170     if (toomanysons(ptr)) {
00171         /* L("found toomany sons in makecutsdownnd\n"); */
00172         if (isfullcrum((typecorecrum *) ptr)) {
00173             /* L("and was fullcrum\n"); */
00174             /* return; */
00175 
00176             levelpush(ptr);
00177             makecutsnd(fullcrumptr, knives);
00178             /* L("returningfrom makecutsnd\n"); */
00179         } else
00180             makecutsnd(fullcrumptr, knives);
00181     }
00182 }
00183 
00191     static void
00192 cutsons(
00193     typecuc    *ptr,
00194     typewid    *offset,
00195     typeknives *knives)
00196 {
00197     typewid grasp;
00198     typecorecrum *son, *nextson;
00199 
00200     while (sonsarecut(ptr, offset, knives)) {
00201           setwispupwards(ptr, 1);
00202           for (son = findleftson(ptr); son; son = nextson) {
00203             nextson = findrightbro(son);
00204             prologuend((typecorecrum *) ptr, offset, &grasp, (typedsp *) NULL);
00205             for (; crumiscut((typecuc *) son, &grasp, knives);
00206                  prologuend((typecorecrum *) ptr, offset, &grasp, (typedsp *) NULL)) {
00207                 makecutsbackuptohere((typecuc *) son, &grasp, knives);
00208             }
00209         }
00210     }
00211 }
00212 
00220     static bool
00221 crumleftofithcut(
00222     typecorecrum *ptr,
00223     typewid      *offset,
00224     typeknives   *knives,
00225     int           i)
00226 {
00227     if (whereoncrum(ptr, offset, &knives->blades[i], knives->dimension) > THRUME)
00228         return false;
00229     else
00230         return true;
00231 }
00232 
00240     static void
00241 newpeelcrumoffnd(
00242     typecorecrum *ptr,
00243     typecorecrum *newuncle)
00244 {
00245     typedsp temp;
00246     typecuc *father;
00247     typedsp grasp;
00248     typewid origin;
00249     typedsp offset;
00250 
00251     if (!roomformoresons((typecuc *) newuncle))
00252         assert(0); // noroomfor more  sons for newuncle
00253 
00254     if (isfullcrum(ptr))
00255         assert(0); // peeloffcurmnd called with fullcrum
00256 
00257     ivemodified(ptr);                  /* to set father->modified */
00258     father = findfather(ptr);
00259     clear(&offset, sizeof(offset));
00260     dspadd(&father->cdsp, &ptr->cdsp, &origin, (int) father->cenftype);
00261 /* 1 ptr->cdsp */
00262 /* 
00263  * L("in newpeelcrumoffnd 1 \n"); dumpwid(&father->cdsp);
00264  * dumpwid(&ptr->cdsp); dumpwid(&origin); */
00265     (void)tumblercheckptr((Tumbler *) & origin, (int *) ptr);
00266 
00267     makeroomonleftnd((typecuc *) newuncle, &offset, &origin, &grasp);
00268     disown(ptr);
00269     adopt(ptr, LEFTMOSTSON, newuncle);
00270 /* dspsub(&father->cdsp,&newuncle->cdsp,&temp,ptr->cenftype); */
00271 /* 2 &temp <- father->cdsp - newuncle->cdsp check this father temp stuff4/6/85 */
00272     dspadd(&father->cdsp, &ptr->cdsp, &temp, (int) ptr->cenftype);
00273 /* 
00274  * dumpwid(&newuncle->cdsp); L("in newpeelcrumoffnd 2 \n");
00275  * dumpwid(&temp); */
00276     (void)tumblercheckptr((Tumbler *) & temp, (int *) ptr);
00277 
00278 /* dspadd(&ptr->cdsp,&temp,&ptr->cdsp, ptr->cenftype); */
00279     dspsub(&temp, &newuncle->cdsp, &ptr->cdsp, (int) ptr->cenftype);
00280 /* 3 ptr->cdsp <- temp + ptr->cdsp */
00281 /* 
00282  * L("in newpeelcrumoffnd 3 \n"); dumpwid(&ptr->cdsp);
00283  * dump(ptr); */
00284     (void)tumblercheckptr((Tumbler *) & ptr->cdsp, (int *) ptr);
00285 
00286     ivemodified(ptr);
00287     ivemodified(newuncle);
00288 /* setwispsofsons(newuncle);// to fix makeroomonleftnd// */
00289     setwispupwards((typecuc *) ptr, 0);
00290     setwispupwards((typecuc *) newuncle, 0);
00291     setwispupwards(father, 1);
00292 
00293     if (toomanysons((typecuc *) newuncle))
00294         assert(0); // toomany sons for newuncle
00295 
00296     /* L("calling fixincoresubtreewids\n"); */
00297     /* fixincoresubtreewids(findfullcrum(ptr)); */
00298 }
00299 
00307     static void
00308 peelsoncorrectly(
00309     typecorecrum *ptr,
00310     typewid      *offset,
00311     typecorecrum *son,
00312     typewid      *grasp,
00313     typeknives   *knives,
00314     int           i)
00315 {                                      /* put son in leftest uncle with room that is less than cut */
00316     typecuc *uncle;
00317 
00318     uncle = (typecuc *) findleftmostbro(ptr);
00319     for (; uncle; uncle = (typecuc *) findrightbro((typecorecrum *) uncle)) {
00320         if (uncle == (typecuc *) ptr) {
00321             continue;
00322         }
00323         if (roomformoresons(uncle)) {
00324             if (crumleftofithcut((typecorecrum *) uncle, offset, knives, i)) {
00325                 newpeelcrumoffnd((typecorecrum *) son, (typecorecrum *) uncle);
00326                 return;
00327             }
00328         }
00329     }
00330     uncle = (typecuc *) createcrum((int) ptr->height, (int) ptr->cenftype);
00331     adopt((typecorecrum *) uncle, RIGHTBRO, findrightmostbro(ptr));
00332     movewisp(&ptr->cdsp, &uncle->cdsp);
00333     newpeelcrumoffnd((typecorecrum *) son, (typecorecrum *) uncle);
00334 }
00335 
00343     static bool
00344 crumiscutbyithknife(
00345     typecuc    *ptr,
00346     typewid    *offset,
00347     typeknives *knives,
00348     int         i)
00349 {
00350     if (whereoncrum((typecorecrum *) ptr, offset, &knives->blades[i], knives->dimension) == THRUME)
00351         return true;
00352 
00353     return false;
00354 }
00355 
00363     static void
00364 makeithcutonson(
00365     typecorecrum *ptr,
00366     typewid      *offset,
00367     typecorecrum *son,
00368     typewid      *grasp,
00369     typeknives   *knives,
00370     int           i)
00371 {
00372     int temp;
00373 
00374     if (!crumiscutbyithknife((typecuc *) ptr, offset, knives, i)) {
00375 #ifndef DISTRIBUTION
00376         L("foo return in makeithcutonson\n");
00377 #endif
00378         return;
00379     }
00380     if ((temp = whereoncrum(son, grasp, &knives->blades[i], knives->dimension)) < THRUME) {
00381         peelsoncorrectly(ptr, offset, son, grasp, knives, i);
00382           setwispupwards((typecuc *) ptr, 1);
00383           if (!crumiscutbyithknife((typecuc *) ptr, offset, knives, i)) {
00384               setwispupwards((typecuc *) ptr, 1);
00385              
00386 /* prologuend(ptr,offset,grasp,NULL); */
00387                     if (((typecuc *) ptr)->numberofsons == 0) {
00388 #ifndef DISTRIBUTION
00389                 displaycutspm(knives);
00390                 dumpwid(offset, ptr->cenftype);
00391                 L("sons went away\n");
00392 /* dump(ptr); *//*dumpwholetree(ptr); */
00393                 check((typecuc *) ptr);
00394 #endif
00395                 return;
00396             }
00397             return;
00398         }
00399         (void)findleftson((typecuc *) ptr);     /* make sure its in core */
00400         if (((typecuc *) ptr)->numberofsons == 0) {
00401 #ifndef DISTRIBUTION
00402             displaycutspm(knives);
00403             dumpwid(offset, ptr->cenftype);
00404             L("____sons went away\n");
00405             dump(ptr);
00406             check((typecuc *) ptr);
00407             assert(0); // sons went away
00408 #else
00409             assert(0);
00410 #endif
00411         }
00412     } else if (temp == THRUME)
00413         assert(0); // makecutsbackuptohere crum not cut
00414 }
00415 
00423     static bool
00424 peeloffcorrectson(
00425     typecorecrum *ptr,
00426     typeknives   *knives)
00427 {
00428     typecorecrum *bro, *uncle;
00429 
00430     if (toomanysons((typecuc *) ptr)) {
00431         for (bro = findrightbro(ptr); bro; ptr = findrightbro(ptr)) {
00432             if (roomformoresons /* !toomanysons */ ((typecuc *) bro)) {
00433                 newpeelcrumoffnd(findleftson((typecuc *) ptr), bro);
00434                 return true;
00435             }
00436         }
00437         uncle = createcrum((int) ptr->height, (int) ptr->cenftype);
00438         adopt(uncle, RIGHTBRO, ptr);
00439         movewisp(&ptr->cdsp, &uncle->cdsp);
00440         newpeelcrumoffnd(findleftson((typecuc *) ptr), uncle);
00441     }
00442     return true;
00443 }
00444 
00452     static void
00453 slicecbcpm(
00454     typecorecrum *ptr,
00455     typewid      *offset,
00456     typecorecrum *newcc,
00457     Tumbler      *cut,
00458     int           index)
00459 {
00460     typedsp grasp /* , reach */ ;
00461     Tumbler localcut;
00462     typewid newwid;
00463     int i;
00464 
00465 /* L("entering slicecbcpm \n"); */
00466     int enftype = ptr->cenftype;
00467 
00468     prologuend(ptr, offset, &grasp, /* &reach */ (typedsp *) NULL);
00469     if (whereoncrum(ptr, offset, cut, index) != THRUME)
00470         assert(0); // Why are you trying to slice me?
00471 
00472     if (!lockis1story(ptr->cwid.dsas, (unsigned)widsize(enftype)))
00473         assert(0); // Not one story in POOM wid
00474 
00475     tumblersub(cut, &grasp.dsas[index], &localcut);
00476 
00477     if (localcut.exp != ptr->cwid.dsas[index].exp) {
00478 #ifndef DISTRIBUTION
00479         dump(ptr);
00480         dump(newcc);
00481         dumptumbler(cut);
00482         dumptumbler(&localcut);
00483         dumpwholetree(ptr);
00484         assert(0); // Oh well, I thought I understood this1
00485 #else
00486         assert(0);
00487 #endif
00488     }
00489     if (!is1story(&localcut)) {
00490 #ifndef DISTRIBUTION
00491         L("\nin slicecbcpm  ptr \n");
00492         dump(ptr);
00493         L("\nin slicecbcpm newcc  \n");
00494         dump(newcc);
00495         L("\nin slicecbcpm  offset \n");
00496         dumpwid(offset, ptr->cenftype);
00497         L("\nin slicecbcpm  grasp \n");
00498         dumpwid(&grasp, ptr->cenftype);
00499         L("\nin slicecbcpm  cut \n");
00500         dumptumbler(cut);
00501         L("\nin slicecbcpm  localcut \n");
00502         dumptumbler(&localcut);
00503         L("\nin slicecbcpm wholedamn tree  \n");
00504         dumpwholetree(ptr);
00505         assert(0); // Oh well, I thought I understood this2
00506 #else
00507         assert(0);
00508 #endif
00509     }
00510 
00511     if (tumblerlength(cut) != tumblerlength(&ptr->cwid.dsas[index]))
00512         assert(0); // level mismatch
00513 
00514     movewisp(&ptr->cwid, &newwid);
00515     for (i = 0; i < widsize(enftype); i++) {    /* I really don't understand this loop */
00516         newwid.dsas[i].mantissa[0] = localcut.mantissa[0];
00517         tumblerjustify(&newwid.dsas[i]);
00518     }
00519 
00520 /* locksubtract (&ptr->cwid, &newwid, &newcc->cwid, widsize (enftype)); */
00521     locksubtract((Tumbler *) & ptr->cwid, (Tumbler *) & newwid, (Tumbler *) & newcc->cwid, (unsigned)widsize(enftype));
00522 
00523     movewisp(&newwid, &ptr->cwid);
00524     dspadd(&ptr->cdsp, &ptr->cwid, &newcc->cdsp, enftype);
00525     move2dinfo(&((type2dcbc *) ptr)->c2dinfo, &((type2dcbc *) newcc)->c2dinfo);
00526     adopt(newcc, RIGHTBRO, ptr);
00527 /* L("leaving slicecpcpm \n"); */
00528 }
00529 
00537     void
00538 makecutsnd(
00539     typecuc    *fullcrumptr,
00540     typeknives *knives)
00541 {
00542     typewid offset;
00543 
00544     //FIX: needs access to Session from higher-level code     logbertmodifiedforcrum(sess, fullcrumptr);
00545 /* (L("\nmakecutsnd knives = \n"); displaycutspm(knives); */
00546     clear(&offset, sizeof(offset));
00547     makecutsdownnd(fullcrumptr, &offset, knives);
00548     clear(&offset, sizeof(offset));    /* clears ARE redundant */
00549     for (fullcrumptr = findfullcrum((typecorecrum *) fullcrumptr); sonsarecut(fullcrumptr, &offset, knives);
00550          fullcrumptr = findfullcrum((typecorecrum *) fullcrumptr)) {
00551         clear(&offset, sizeof(offset));
00552         makecutsdownnd(fullcrumptr, &offset, knives);
00553     }
00554 #ifndef DISTRIBUTION
00555     asserttreeisok((typecorecrum *) fullcrumptr);
00556 #endif
00557 }
00558 
00566     static void
00567 makecutsbackuptohere(
00568     typecuc    *ptr,
00569     typewid    *offset,
00570     typeknives *knives)
00571 {
00572     typewid grasp;
00573     int i;
00574     typecuc *son, *newcc, *nextson;
00575 
00576     if (ptr->height == 0) {
00577         for (i = 0; i < knives->nblades; i++) {
00578             if (whereoncrum((typecorecrum *) ptr, offset, &knives->blades[i], knives->dimension) == THRUME) {
00579                 newcc = (typecuc *) createcrum((int) ptr->height, (int) ptr->cenftype);
00580                 if (ptr->cenftype == GRAN) {
00581                     ((typecbc *) newcc)->cinfo.infotype = ((typecbc *) ptr)->cinfo.infotype;
00582                 }
00583                 slicecbcpm((typecorecrum *) ptr, offset, (typecorecrum *) newcc, &knives->blades[i], knives->dimension);
00584                 ivemodified((typecorecrum *) ptr);
00585                 ivemodified((typecorecrum *) newcc);
00586                   setwisp((typecorecrum *) ptr);
00587                  
00588 /* asserttreeisok(ptr); */
00589             }
00590         }
00591         return;
00592     } else {
00593         for (i = 0; i < knives->nblades; ++i) {
00594             cutsons(ptr, offset, knives);
00595 
00596             if (!isfullcrum((typecorecrum *) ptr)) {
00597                   setwispupwards(ptr, 0);
00598                   if (crumiscutbyithknife(ptr, offset, knives, i)) {
00599                     for (son = (typecuc *) findleftson(ptr); son; son = nextson) {
00600                         nextson = (typecuc *) findrightbro((typecorecrum *)
00601                                                            son);
00602                         prologuend((typecorecrum *) ptr, offset, &grasp, (typedsp *) NULL);
00603 
00604                         makeithcutonson((typecorecrum *) ptr, offset, (typecorecrum *) son, &grasp, knives, i);
00605                         if (!crumiscutbyithknife(ptr, offset, knives, i)) {
00606                             if (ptr->numberofsons == 0) {
00607                                 return;
00608                             }
00609                             break;
00610                         }
00611                     }
00612                 }
00613                  setwispupwards(ptr, 0);
00614              }
00615 /* asserttreeisok(ptr); */
00616         }
00617     }
00618     if (!isfullcrum((typecorecrum *) ptr)) {
00619         if (toomanysons(ptr)) {
00620             while (toomanysons(ptr)) {
00621 #ifndef DISTRIBUTION
00622                 L("setwispupwards 4\n" /*BUG: too many args, 1*/);
00623 #endif
00624                   setwispupwards(ptr, 0);
00625                  
00626 #ifndef DISTRIBUTION
00627                         L("toomanysons peel\n");
00628 #endif
00629                 peeloffcorrectson((typecorecrum *) ptr, knives);
00630             }
00631 #ifndef DISTRIBUTION
00632             L("calling makecutsbackuptohere\n");
00633 #endif
00634             makecutsbackuptohere(ptr, offset, knives);
00635         }
00636     } else if (toomanysons(ptr)) {
00637 #ifndef DISTRIBUTION
00638         L("________________toomanysons in fullcrum\n");
00639 #endif
00640     }
00641      setwispupwards(ptr, 1);
00642  
00643 }

Generated on Sun Aug 21 04:18:14 2005 for Udanax-Green by doxygen1.3.4