home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / som / somk / c / tp / ll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-24  |  10.8 KB  |  399 lines

  1.  
  2. /*
  3.  *  This file was generated by the SOM Compiler.
  4.  *  Generated using:
  5.  *     SOM incremental update: 10.9
  6.  */
  7.  
  8.  
  9. #ifndef lint
  10. static char *sccsid = "%Z% %I% %W% %G% %U% [%H% %T%] (c)IBM Corp. 1992";
  11. #endif
  12.  
  13. /*
  14.  * This class is adapted from the book
  15.  *   Class Construction in C and C++, Object Oriented Fundamentals
  16.  *   by Roger Sessions, Copyright (c) 1992 Prentice Hall.
  17.  * Reprinted with permission.
  18.  */
  19.  
  20. #define linkedList_Class_Source
  21. #if defined(_WIN32)
  22. #include "statcls.h"
  23. #endif
  24. #include "link.h"
  25. #include "ll.ih"
  26. #define MAX_INT 30000
  27.  
  28. /* ************************************************************ */
  29. /*
  30.  *  Make the head of the list the current link, and
  31.  *  return its contents.
  32.  */
  33.  
  34. SOM_Scope baseType *SOMLINK llHead(linkedList * somSelf)
  35. {
  36.     linkedListData *somThis = linkedListGetData(somSelf);
  37.     linkedListMethodDebug("linkedList", "llHead");
  38.  
  39.     if (!_nlinks)
  40.     return (baseType *) NULL;
  41.     _currentLink = _headLink;
  42.     return (_linkGetContents(_currentLink));
  43. }
  44.  
  45. /* ************************************************************ */
  46. /*
  47.  *  Make the tail of the list the current link, and
  48.  *  returns its contents.
  49.  */
  50.  
  51. SOM_Scope baseType *SOMLINK llTail(linkedList * somSelf)
  52. {
  53.     linkedListData *somThis = linkedListGetData(somSelf);
  54.     linkedListMethodDebug("linkedList", "llTail");
  55.  
  56.     if (!_nlinks)
  57.     return (baseType *) NULL;
  58.     _currentLink = _tailLink;
  59.     return (_linkGetContents(_currentLink));
  60. }
  61.  
  62. /* ************************************************************ */
  63. /*
  64.  *  Return the number of links is the list.
  65.  */
  66.  
  67. SOM_Scope long  SOMLINK llLength(linkedList *somSelf)
  68. {
  69.     linkedListData *somThis = linkedListGetData(somSelf);
  70.     linkedListMethodDebug("linkedList", "llLength");
  71.     return _nlinks;
  72. }
  73.  
  74. /* ************************************************************ */
  75. /*
  76.  *  Set the maximum number of links the list can contain.
  77.  */
  78.  
  79. SOM_Scope void  SOMLINK llSetMax(linkedList *somSelf,
  80.         long newMax)
  81. {
  82.     linkedListData *somThis = linkedListGetData(somSelf);
  83.     linkedListMethodDebug("linkedList", "llSetMax");
  84.     _max = newMax;
  85. }
  86.  
  87. /* ************************************************************ */
  88. /*
  89.  *  Return the number of links that can still be added to the
  90.  *  list.
  91.  */
  92.  
  93. SOM_Scope long  SOMLINK llLeft(linkedList *somSelf)
  94. {
  95.     linkedListData *somThis = linkedListGetData(somSelf);
  96.     linkedListMethodDebug("linkedList", "llLeft");
  97.     return (_max - _llLength(somSelf));
  98. }
  99.  
  100. /* ************************************************************ */
  101. /*
  102.  *  Make the link after current the new current link, and
  103.  *  return its contents.
  104.  */
  105.  
  106. SOM_Scope baseType *SOMLINK llNext(linkedList * somSelf)
  107. {
  108.     linkedListData *somThis = linkedListGetData(somSelf);
  109.     linkedListMethodDebug("linkedList", "llNext");
  110.  
  111.     if (!_nlinks)
  112.     return (baseType *) NULL;
  113.     if (_linkGetNext(_currentLink)) {
  114.     _currentLink = _linkGetNext(_currentLink);
  115.     return (_linkGetContents(_currentLink));
  116.     }
  117.     else {
  118.     return (baseType *) NULL;
  119.     }
  120. }
  121.  
  122. /* ************************************************************ */
  123. /*
  124.  *  Make the link before current the new current link, and
  125.  *  return its contents.
  126.  */
  127.  
  128. SOM_Scope baseType *SOMLINK llPrevious(linkedList * somSelf)
  129. {
  130.     linkedListData *somThis = linkedListGetData(somSelf);
  131.     linkedListMethodDebug("linkedList", "llPrevious");
  132.  
  133.     if (!_nlinks)
  134.     return (baseType *) NULL;
  135.     if (_linkGetPrevious(_currentLink)) {
  136.     _currentLink = _linkGetPrevious(_currentLink);
  137.     return (_linkGetContents(_currentLink));
  138.     }
  139.     else {
  140.     return (baseType *) NULL;
  141.     }
  142. }
  143.  
  144. /* ************************************************************ */
  145. /*
  146.  *  Return the contents of the current link.
  147.  */
  148.  
  149. SOM_Scope baseType *SOMLINK llRetrieve(linkedList * somSelf)
  150. {
  151.     linkedListData *somThis = linkedListGetData(somSelf);
  152.     linkedListMethodDebug("linkedList", "llRetrieve");
  153.  
  154.     if (!_nlinks)
  155.     return (baseType *) NULL;
  156.     return (_linkGetContents(_currentLink));
  157. }
  158.  
  159. /* ************************************************************ */
  160. /*
  161.  *  Replace the contents of the current list but this new
  162.  *  element, and return a pointer to the new contents.
  163.  */
  164.  
  165. SOM_Scope baseType *SOMLINK llReplace(linkedList * somSelf,
  166.                        baseType * newElement)
  167. {
  168.     linkedListData *somThis = linkedListGetData(somSelf);
  169.     linkedListMethodDebug("linkedList", "llReplace");
  170.  
  171.     if (!_nlinks)
  172.     return (baseType *) NULL;
  173.     _linkSetContents(_currentLink, newElement);
  174.     return (_linkGetContents(_currentLink));
  175. }
  176.  
  177. /* ************************************************************ */
  178. /*
  179.  *  Move the tail link to the head of the list, and return
  180.  *  its contents.
  181.  */
  182.  
  183. SOM_Scope baseType *SOMLINK llPromoteTail(linkedList * somSelf)
  184. {
  185.     linkedListData *somThis = linkedListGetData(somSelf);
  186.     link *oldTail;
  187.     linkedListMethodDebug("linkedList", "llPromoteTail");
  188.     if (!_nlinks)
  189.     return (baseType *) NULL;
  190.     if (_nlinks == 1)
  191.     return (_linkGetContents(_headLink));
  192.     oldTail = _tailLink;
  193.     _tailLink = _linkGetPrevious(_tailLink);
  194.     _linkSetNext(_linkGetPrevious(oldTail), 0);
  195.     _linkSetPrevious(oldTail, 0);
  196.     _linkSetNext(oldTail, _headLink);
  197.     _linkSetPrevious(_headLink, oldTail);
  198.     _headLink = oldTail;
  199.     _currentLink = _headLink;
  200. }
  201.  
  202. /* ************************************************************ */
  203. /*
  204.  *  Add a link containing this new Element to the head of the
  205.  *  list, and return a pointer to the new Element.
  206.  */
  207.  
  208. SOM_Scope baseType *SOMLINK llAddHead(linkedList * somSelf,
  209.                        baseType * newElement)
  210. {
  211.     linkedListData *somThis = linkedListGetData(somSelf);
  212.     link *newLink = linkNew();
  213.     linkedListMethodDebug("linkedList", "llAddHead");
  214.  
  215.     _linkSetContents(newLink, newElement);
  216.     if (!_llLeft(somSelf)) {
  217.     _llHead(somSelf);
  218.     return (_llReplace(somSelf, newElement));
  219.     }
  220.     if (_llHead(somSelf)) {
  221.     _linkSetPrevious(_currentLink, newLink);
  222.     }
  223.     else
  224.     _tailLink = newLink;
  225.     _linkSetNext(newLink, _currentLink);
  226.     _headLink = _currentLink = newLink;
  227.     _nlinks++;
  228.     return (_linkGetContents(_currentLink));
  229. }
  230.  
  231. /* ************************************************************ */
  232. /*
  233.  *  Add a link containing this new Element to the tail of the
  234.  *  list, and return a pointer to the new Element.
  235.  */
  236.  
  237. SOM_Scope baseType *SOMLINK llAddTail(linkedList * somSelf,
  238.                        baseType * newElement)
  239. {
  240.     linkedListData *somThis = linkedListGetData(somSelf);
  241.     link *newLink = linkNew();
  242.     linkedListMethodDebug("linkedList", "llAddTail");
  243.  
  244.     _linkSetContents(newLink, newElement);
  245.     if (!_llLeft(somSelf)) {
  246.     _llTail(somSelf);
  247.     return (_llReplace(somSelf, newElement));
  248.     }
  249.     if (_llTail(somSelf)) {
  250.     _linkSetNext(_currentLink, newLink);
  251.     }
  252.     else
  253.     _headLink = newLink;
  254.     _linkSetPrevious(newLink, _currentLink);
  255.     _tailLink = _currentLink = newLink;
  256.     _nlinks++;
  257.     return (_linkGetContents(_currentLink));
  258. }
  259.  
  260. /* ************************************************************ */
  261. /*
  262.  *  Delete the link at the head of the list.
  263.  */
  264.  
  265. SOM_Scope baseType *SOMLINK llRemoveHead(linkedList * somSelf)
  266. {
  267.     linkedListData *somThis = linkedListGetData(somSelf);
  268.     baseType *thisItem;
  269.     linkedListMethodDebug("linkedList", "llRemoveHead");
  270.  
  271.     if (!_nlinks)
  272.     return (baseType *) NULL;
  273.     thisItem = _llHead(somSelf);
  274.     if (_nlinks == 1) {
  275.     _somFree(_headLink);
  276.     _headLink = _tailLink = _currentLink = 0;
  277.     }
  278.     if (_nlinks > 1) {
  279.     _llNext(somSelf);
  280.     _somFree(_headLink);
  281.     _headLink = _currentLink;
  282.     _linkSetPrevious(_headLink, 0);
  283.     }
  284.     _nlinks--;
  285.     return thisItem;
  286. }
  287.  
  288. /* ************************************************************ */
  289. /*
  290.  *  Returns TRUE if the current link is the head of the list,
  291.  *  FALSE otherwise.
  292.  */
  293.  
  294. SOM_Scope long  SOMLINK llIsTail(linkedList *somSelf)
  295. {
  296.     linkedListData *somThis = linkedListGetData(somSelf);
  297.     linkedListMethodDebug("linkedList", "llIsTail");
  298.     return (_currentLink == _tailLink);
  299.  
  300. }
  301.  
  302. /* ************************************************************ */
  303. /*
  304.  *  Frees the entire list, including the contents of each link.
  305.  */
  306.  
  307. SOM_Scope void SOMLINK llFreeContents(linkedList * somSelf)
  308. {
  309.     linkedListData *somThis = linkedListGetData(somSelf);
  310.     baseType *thisItem;
  311.  
  312.     linkedListMethodDebug("linkedList", "llFreeContents");
  313.  
  314.     while (thisItem = _llRemoveHead(somSelf))
  315.     _somFree(thisItem);
  316. }
  317.  
  318. /* ************************************************************ */
  319. SOM_Scope void SOMLINK print(linkedList * somSelf,
  320.                   FILE * outputFile)
  321. {
  322.     linkedListData *somThis = linkedListGetData(somSelf);
  323.     baseType *thisItem;
  324.  
  325.     linkedListMethodDebug("linkedList", "print");
  326.     thisItem = _llHead(somSelf);
  327.     if (thisItem) {
  328.     while (thisItem) {
  329.         _print(thisItem, outputFile);
  330.         thisItem = _llNext(somSelf);
  331.     }
  332.     }
  333. }
  334.  
  335. /* ************************************************************ */
  336. SOM_Scope void SOMLINK somInit(linkedList * somSelf)
  337. {
  338.     linkedListData *somThis = linkedListGetData(somSelf);
  339.     linkedListMethodDebug("linkedList", "somInit");
  340.     parent_somInit(somSelf);
  341.  
  342.     _currentLink = 0;
  343.     _headLink = 0;
  344.     _tailLink = 0;
  345.     _nlinks = 0;
  346.     _max = MAX_INT;
  347. }
  348.  
  349. /* ************************************************************ */
  350. SOM_Scope void SOMLINK somUninit(linkedList * somSelf)
  351. {
  352.     linkedListData *somThis = linkedListGetData(somSelf);
  353.     linkedListMethodDebug("linkedList", "somUninit");
  354.  
  355.     while (_llRemoveHead(somSelf))
  356.     ;
  357.     parent_somUninit(somSelf);
  358. }
  359.  
  360. /* ************************************************************ */
  361.  
  362. /*
  363.  * SOM_Scope void SOMLINK somDumpSelfInt(linkedList * somSelf,
  364.  *                        int level)
  365.  */
  366.  
  367. /*
  368.  * The prototype for somDumpSelfInt was replaced by the following prototype:
  369.  */
  370. SOM_Scope void  SOMLINK somDumpSelfInt(linkedList *somSelf,
  371.         long level)
  372. {
  373.     linkedListData *somThis = linkedListGetData(somSelf);
  374.     linkedListMethodDebug("linkedList", "somDumpSelfInt");
  375.     parent_somDumpSelfInt(somSelf, level);
  376. }
  377.  
  378. /* ************************************************************ */
  379. /*
  380.  *  Trace the linkedList, useful for debugging.
  381.  */
  382.  
  383. SOM_Scope void SOMLINK llTrace(linkedList * somSelf, FILE * output)
  384. {
  385.     linkedListData *somThis = linkedListGetData(somSelf);
  386.     linkedListMethodDebug("linkedList", "llTrace");
  387.     fprintf(output, "\n");
  388.     fprintf(output, " Linked List\n");
  389.     fprintf(output, "        max: %d\n", _max);
  390.     fprintf(output, "     nlinks: %d\n", _nlinks);
  391.     fprintf(output, "currentLink: %x\n", _currentLink);
  392.     fprintf(output, "   headLink: %x\n", _headLink);
  393.     fprintf(output, "   tailLink: %x\n", _tailLink);
  394.  
  395.     _print(_headLink, output);
  396.     _print(_currentLink, output);
  397.     _print(_tailLink, output);
  398. }
  399.