home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1996 January / macformat-033.iso / mac / Shareware City / Developers / ABox.v1.8 / CPlus Files / ABLinkedList.c next >
Encoding:
C/C++ Source or Header  |  1995-05-23  |  9.3 KB  |  425 lines  |  [TEXT/MMCC]

  1. /*    
  2.     Copyright © 1991-1995 by TopSoft Inc.  All rights reserved.
  3.  
  4.     You may distribute this file under the terms of the TopSoft
  5.     Artistic License, accompanying this package.
  6.     
  7.     This file was developed by George (ty) Tempel in connection with TopSoft, Inc..
  8.     See the Modification History for more details.
  9.  
  10. Product
  11.     About Box
  12.  
  13. FILE
  14.     ABLinkedList.c
  15.  
  16. NAME
  17.     ABLinkedList.c, part of the ABox project source code,
  18.     responsible for handling the AboutBox linked list class stuff.
  19.  
  20. DESCRIPTION
  21.     This file contains defines for the about box modules.
  22.     
  23. DEVELOPED BY
  24.     George (ty) Tempel                netromancr@aol.com
  25.     All code in this file, and its associated header file was
  26.     Created by George (ty) Tempel in connection with the TopSoft, Inc.
  27.     "FilterTop" application development, except where noted.
  28.  
  29. CARETAKER - George (ty) Tempel <netromancr@aol.com>
  30.      Please consult this person for any changes or suggestions to this file.
  31.  
  32. MODIFICATION HISTORY
  33.  
  34.     dd mmm yy    -    xxx    -    patchxx: description of patch
  35.     9 June 94    -    ty    -    Initial Version Created
  36.     20-july-94    -    ty    -    initial version released
  37.     23-may-95    -    ty    -    changes for compatibility with the CodeWarrior CW6
  38.                             release and the associated Universal Headers from Apple:
  39.                             most methods that returned references now have "Ref" at
  40.                             the end of their methods names to prevent possible collisions
  41.                             with datatypes and classes of the same name (older versions
  42.                             of the compiler didn't have a problem with this).
  43.  
  44. */
  45.  
  46. /*===========================================================================*/
  47.  
  48. /*======= Segmentation directives ========*/
  49.  
  50. #ifdef USE_MANUAL_SEGMENTATION
  51. #pragma segment ty
  52. #endif
  53.  
  54. /*============ Header files ==============*/
  55.     
  56. #include     "ABLinkedList.h"
  57.  
  58. /*=============== Globals ================*/
  59.  
  60. /*================ CODE ==================*/
  61.  
  62. /*=============================== ABLink::ABLink ================================*/
  63.  
  64. ABLink::ABLink(void)
  65. {
  66.     mPreviousLink = NULL;
  67.     mThisLink = this;
  68.     mNextLink = NULL;
  69.  
  70. }    // end ABLink
  71.  
  72.  
  73. /*=============================== ABLink::~ABLink ================================*/
  74.  
  75. ABLink::~ABLink(void)
  76. {
  77.     this->Unlink();
  78. }    // end ~ABLink
  79.  
  80.  
  81. /*=============================== ABLink::Data ================================*/
  82. void    *ABLink::Data(void)
  83. {
  84.     //    this function should be overridden!!!
  85.     return NULL;
  86. }    // end Data
  87.  
  88.  
  89.         
  90.  
  91. /*=============================== ABLink::FindHead ================================*/
  92. ABLink    *ABLink::FindHead(void)
  93. {
  94.     ABLink    *thing = this;
  95.     
  96.     while (thing->HasPreviousLink())
  97.     {
  98.         thing = thing->PreviousLink();
  99.     }    // end while block
  100.     
  101.     if (thing->DoesntHaveThisLink())
  102.         //    we've found the ABLinkedList "head" element,
  103.         //    so back off once and find the first element, if
  104.         //    one does indeed exist
  105.         return thing->NextLink();
  106.     else
  107.         //    nope, this must be a disembodied link item,
  108.         //    without the proper "head" or "tail" elements
  109.         //    present in an ABLinkedList
  110.         return thing;
  111. } // end FindHead
  112.  
  113.  
  114.  
  115.  
  116. /*=============================== ABLink::FindTail ================================*/
  117. ABLink    *ABLink::FindTail(void)
  118. {
  119.     ABLink    *thing = this;
  120.     
  121.     while (thing->HasNextLink())
  122.     {
  123.         thing = thing->NextLink();
  124.     }
  125.     
  126.     if (thing->DoesntHaveThisLink())
  127.         //    we've found the ABLinkedList "tail" element,
  128.         //    so back off once and find the last element, if
  129.         //    one does indeed exist
  130.         return thing->PreviousLink();
  131.     else
  132.         //    nope, this must be a disembodied link item,
  133.         //    without the proper "head" or "tail" elements
  134.         //    present in an ABLinkedList
  135.         return thing;
  136. } // end FindTail
  137.  
  138.  
  139.  
  140. /*=============================== ABLink::Unlink ================================*/
  141. void    ABLink::Unlink(void)
  142. {
  143.     if (this->HasPreviousLink())
  144.         this->PreviousLink()->NextLink() = this->NextLink();
  145.     
  146.     if (this->HasNextLink())
  147.         this->NextLink()->PreviousLink() = this->PreviousLink();
  148.     
  149. } // end Unlink
  150.  
  151.  
  152.  
  153.  
  154. /*=============================== ABLink::ExchangeWith ================================*/
  155. //
  156. //    This method will return the _OLD_ link, the one that you are replacing.
  157. //
  158. ABLink    *ABLink::ExchangeWith(ABLink *newGuy)
  159. {
  160.     if (newGuy)
  161.     {
  162.         ABLink    *link;
  163.     
  164.         //    previousNode    <---+                            <----+
  165.         //        previous        |                                 |
  166.         //        next        --+ |                            ---+ |
  167.         //                      | |                               | |
  168.         //    this            <-+    |  <+        newGuy            <--+ | <+
  169.         //        previous    ----+   |            previous    -----+  |
  170.         //        next        --+     |            next        ---+    |
  171.         //                      |     |                           |    |
  172.         //    nextNode        <-+     |                        <--+    |
  173.         //        previous    --------+                        --------+
  174.         //        next        
  175.         //
  176.         if (this->HasPreviousLink())
  177.             this->PreviousLink()->NextLink() = newGuy;
  178.         link = this->PreviousLink();
  179.         this->PreviousLink() = newGuy->PreviousLink();
  180.         newGuy->PreviousLink() = link;
  181.         
  182.         if (this->HasNextLink())
  183.             this->NextLink()->PreviousLink() = newGuy;
  184.         link = this->NextLink();
  185.         this->NextLink() = newGuy->NextLink();
  186.         newGuy->NextLink() = link;
  187.     } // end if block
  188.     
  189.     return this;
  190.     
  191. } // end ExchangeWith
  192.  
  193.  
  194.  
  195.  
  196. /*=============================== ABLink::Ordinal ================================*/
  197. ABIndex    ABLink::Ordinal(void)
  198. {
  199.     ABLink    *head;
  200.     ABLink    *tail;
  201.     ABLink    *t;
  202.     
  203.     ABIndex    marker;
  204.     
  205.     //    begin here...
  206.     
  207.     if (!(this->HasPreviousLink() && this->HasNextLink()))
  208.     {
  209.         return 1;
  210.     } else {
  211.         head = this->FindHead();
  212.         tail = this->FindTail();
  213.  
  214.         marker = 1;
  215.         t = head;
  216.         while ((t != this) && (t != tail))
  217.         {
  218.             t = t->NextLink();
  219.             ++marker;
  220.         } // end while
  221.         
  222.             
  223.         if (t == this)
  224.             return marker;
  225.         else
  226.             return 0;
  227.     } // end if else block
  228. } // end Ordinal
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237. /*=============================== ABLinkedList::ABLinkedList ================================*/
  238. ABLinkedList::ABLinkedList(void)
  239. {
  240.     mListCount = 0;
  241.     mHead.PreviousLink() = mHead.ThisLink() = NULL;
  242.     mHead.NextLink() = NULL;
  243.     mTail.PreviousLink() = NULL;
  244.     mTail.NextLink() = mTail.ThisLink() = NULL;
  245.     mCurrentLink = NULL;
  246. } // end ABLinkedList
  247.  
  248.  
  249. /*=============================== ABLinkedList::~ABLinkedList ================================*/
  250. ABLinkedList::~ABLinkedList(void)
  251. {
  252.     ABLink    *link = NULL;
  253.     
  254.     while (this->IsntEmpty())
  255.     {
  256.         link = this->NthLink(this->Count());
  257.         if (link)
  258.             link->Unlink();
  259.         delete link;
  260.         --(this->ListCount());
  261.     } // end while
  262.     
  263. }    // end ~ABLinkedList
  264.  
  265.  
  266.  
  267.  
  268.  
  269. /*=============================== ABLinkedList::Append ================================*/
  270. ABLinkedList    *ABLinkedList::Append(ABLink *item)
  271. {
  272.     if (this->Tail().HasPreviousLink())
  273.     {
  274.         //    point the last item to this new item
  275.         this->Tail().PreviousLink()->NextLink() = item;
  276.     } // end if block
  277.     
  278.     //    make the new item the last item
  279.     item->NextLink() = &this->Tail();
  280.     //    point new item back to the former last item
  281.     item->PreviousLink() = this->Tail().PreviousLink();
  282.     this->Tail().PreviousLink() = item;
  283.     
  284.     if (this->Head().DoesntHaveNextLink())
  285.     {
  286.         //    make the new item the first item
  287.         this->Head().NextLink() = item;
  288.         item->PreviousLink() = &this->Head();
  289.     } // end if block
  290.         
  291.     ++(this->ListCount());
  292.     this->CurrentLink() = item;
  293.     
  294.     return this;
  295. } // end Append
  296.  
  297.  
  298.  
  299.  
  300. /*=============================== ABLinkedList::Delete ================================*/
  301. ABLinkedList    *ABLinkedList::Detach(ABLink *item)
  302. {
  303.     if (item)
  304.     {
  305.         if (item == this->CurrentLink())
  306.             this->CurrentLink() = item->PreviousLink();
  307.         if (this->DoesntHaveCurrentLink())
  308.             this->CurrentLink() = & this->Head();
  309.             
  310.         item->Unlink();
  311.     }
  312.      // end if block
  313.      
  314.     return this;
  315.     
  316. } // end Detach
  317.  
  318.  
  319.  
  320.  
  321. /*=============================== ABLinkedList::NthLink ================================*/
  322. ABLink    *ABLinkedList::NthLink(ABIndex n)
  323. {
  324.     ABIndex    index;
  325.     ABLink    *link;
  326.     
  327.     //    begin here...
  328.     
  329.     if ((this->Count() < 1) || (n > this->Count())) 
  330.         return NULL;
  331.     
  332.     link = &this->Head();
  333.     for (index = 1; (index <= n) && link; ++index)
  334.     {
  335.         link = link->NextLink();
  336.     } // end for loop
  337.     
  338.     if (link == &this->Tail())
  339.         return NULL;
  340.     else
  341.         return link;
  342. } // end NthLink
  343.  
  344.  
  345.  
  346. /*=============================== ABLinkedList::GotoLink ================================*/
  347. ABLink    *ABLinkedList::GotoLink(ABIndex n)
  348. {
  349.     ABLink    *link;
  350.     
  351.     //    begin here...
  352.     
  353.     link = this->NthLink(n);
  354.     if (link)
  355.         this->CurrentLink() = link;
  356.         
  357.     return link;
  358. } // end GotoLink
  359.  
  360.  
  361.  
  362. /*=============================== ABLinkedList::PreviousLink ================================*/
  363. ABLink    *ABLinkedList::PreviousLink(void)
  364. {
  365.     if (this->DoesntHaveCurrentLink())
  366.         return NULL;
  367.         
  368.     if (this->CurrentLink()->PreviousLink() != &this->Head())
  369.         this->CurrentLink() = this->CurrentLink()->PreviousLink();
  370.  
  371.     return this->CurrentLink();
  372. } // end PreviousLink
  373.  
  374.  
  375.  
  376. /*=============================== ABLinkedList::NextLink ================================*/
  377. ABLink    *ABLinkedList::NextLink(void)
  378. {
  379.     if (this->DoesntHaveCurrentLink())
  380.         return NULL;
  381.         
  382.     if (this->CurrentLink()->NextLink() != &this->Tail())
  383.         this->CurrentLink() = this->CurrentLink()->NextLink();
  384.  
  385.     return this->CurrentLink();
  386. } // end NextLink
  387.  
  388.  
  389.  
  390. /*=============================== ABLinkedList::FirstLink ================================*/
  391. ABLink    *ABLinkedList::FirstLink(void)
  392. {
  393.     if (this->IsntEmpty())
  394.         return this->Head().NextLink();
  395.     else
  396.         return NULL;
  397. } // end FirstLink
  398.  
  399.  
  400.  
  401. /*=============================== ABLinkedList::LastLink ================================*/
  402. ABLink    *ABLinkedList::LastLink(void)
  403. {
  404.     if (this->IsntEmpty())
  405.         return this->Tail().PreviousLink();
  406.     else
  407.         return NULL;
  408. } // end LastLink
  409.  
  410.  
  411.  
  412. /*=============================== ABLinkedList::Concatenate ================================*/
  413. //
  414. //    ForEach is an internal method used to do something to all items in
  415. //    the list.
  416. OSErr    ABLinkedList::ForEach (ABMessage /* message */, void* /* data*/)
  417. {
  418.     //    OVERRIDE THIS METHOD...
  419.     return noErr;
  420. } /// end ForEach
  421.  
  422.  
  423.  
  424.  
  425. // end of file.