home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / warphead.zip / H / LINKLIST.H < prev    next >
C/C++ Source or Header  |  1997-02-28  |  6KB  |  237 lines

  1. //====START_GENERATED_PROLOG======================================
  2. //
  3. //
  4. //   COMPONENT_NAME: odutils
  5. //
  6. //   CLASSES:   Link
  7. //        LinkedList
  8. //        LinkedListIterator
  9. //
  10. //   ORIGINS: 82,27
  11. //
  12. //
  13. //   (C) COPYRIGHT International Business Machines Corp. 1995,1996
  14. //   All Rights Reserved
  15. //   Licensed Materials - Property of IBM
  16. //   US Government Users Restricted Rights - Use, duplication or
  17. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  18. //       
  19. //   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  20. //   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  21. //   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  22. //   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  23. //   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  24. //   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
  25. //   OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. //====END_GENERATED_PROLOG========================================
  28. //
  29. // @(#) 1.3 com/src/utils/include/LinkList.h, odutils, od96os2, odos29646d 7/15/96 18:00:37 [ 11/15/96 15:29:32 ]
  30. /*
  31.     File:        LinkList.h
  32.  
  33.     Contains:    Primitive linked list class
  34.  
  35.     Owned by:    Richard Rodseth and Jens Alfke
  36.  
  37.     Copyright:    ⌐ 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  38.  
  39.     
  40. */
  41.  
  42. #ifndef _LINKLIST_
  43. #define _LINKLIST_
  44.  
  45. #ifndef _ODTYPES_
  46. #include "ODTypes.h"
  47. #endif
  48.  
  49.  
  50. //=====================================================================================
  51. // Theory of Operation
  52. //=====================================================================================
  53.  
  54. /*
  55.  
  56.  Note: These classes are private to the implementation. They are not available
  57.  to part handlers.
  58.  
  59.  Note: These are primitive classes for implementing higher-level collections
  60.  For example, to create a FrameList class, subclass Link to add a field to
  61.  store the frame. The FrameList class would use a LinkedList in its implementation
  62.  and would manufacture the Link objects internally.
  63.  
  64. */
  65.  
  66. //==============================================================================
  67. // Constants
  68. //==============================================================================
  69.  
  70. //==============================================================================
  71. // Scalar Types
  72. //==============================================================================
  73.  
  74. //==============================================================================
  75. // Classes defined in this interface
  76. //==============================================================================
  77.  
  78. class Link;
  79. class LinkedList;
  80. class LinkedListIterator;
  81.  
  82. //==============================================================================
  83. // Classes used by this interface
  84. //==============================================================================
  85.  
  86.  
  87. //==============================================================================
  88. // Link
  89. //==============================================================================
  90.  
  91. class  Link {
  92.     public:
  93.     
  94.         Link();
  95.         
  96.         Link(Link* next, Link* previous);
  97.         
  98.         Link( const Link& );
  99.         
  100.         virtual ~Link();
  101.         
  102.         Link* GetNext() const                    {return fNext;}
  103.         
  104.         Link* GetPrevious() const                {return fPrevious;}
  105.     
  106.     // The following operations are provided for efficiency, but DO NOT USE THEM
  107.     // if there are any iterators active on a list. These operations don't bump
  108.     // the list's fSeed and the iterators will not be able to detect that they
  109.     // are out of sync!
  110.         
  111.         void  Remove( );
  112.         
  113.         void  AddBefore( Link *aLink );
  114.         
  115.         void  AddAfter( Link *aLink );
  116.     
  117.   //private-by-convention:
  118.           
  119.         void  SetNext(Link* aLink)                {fNext = aLink;}
  120.         
  121.         void  SetPrevious(Link* aLink)            {fPrevious = aLink;}
  122.  
  123.     private:
  124.     
  125.         Link*        fNext;
  126.         Link*        fPrevious;
  127. };
  128.  
  129.  
  130. //==============================================================================
  131. // LinkedList
  132. //==============================================================================
  133.  
  134.  
  135. class LinkedList {
  136.  
  137.     public:
  138.     
  139.           LinkedList();
  140.           
  141.           ~LinkedList();
  142.           
  143.           ODBoolean        IsEmpty( )                                        const;
  144.           
  145.           ODULong            Count()                                            const;
  146.           
  147.           ODBoolean        Includes( const Link* )                            const;
  148.           
  149.           void            Remove(Link&);
  150.           
  151.           void            RemoveAll();
  152.  
  153.           void            DeleteAllLinks();
  154.           
  155.           Link*            RemoveFirst();
  156.           
  157.           Link*            RemoveLast();
  158.           
  159.           void            AddBefore(Link& existing, Link* link);
  160.           
  161.           void            AddAfter(Link& existing, Link* link);
  162.           
  163.           void            AddFirst(Link* link);
  164.           
  165.           void            AddLast(Link* link);
  166.           
  167.           void            AddLast( LinkedList &list );
  168.           
  169.           void            AddLastUnique( LinkedList &list );
  170.           
  171.           Link*            After(const Link& link)                            const;
  172.           
  173.           Link*             Before(const Link& link)                        const;
  174.           
  175.           Link*            First()                                            const;
  176.           
  177.           Link*             Last()                                            const;
  178.  
  179. protected:
  180.  
  181.         Link            fSentinel;    // Marks the head & tail
  182.         ODULong            fSeed;        // Used to detect out-of-sync iterators
  183.         
  184.         Link*            GetSentinel( )
  185.                                                 {return &fSentinel;}
  186.         const Link*        GetSentinel( )                                    const
  187.                                                 {return &fSentinel;}
  188.         ODBoolean        IsSentinel( const Link* link )                    const
  189.                                                 {return link==this->GetSentinel();}
  190.         ODBoolean        NotSentinel( const Link* link )                    const
  191.                                                 {return link!=this->GetSentinel();}
  192.  
  193. private:                  
  194.         friend class LinkedListIterator;
  195. };
  196.  
  197.  
  198. //=====================================================================================
  199. // LinkedListIterator
  200. //=====================================================================================
  201.  
  202. class LinkedListIterator {
  203.  
  204.     public:
  205.     
  206.         LinkedListIterator(LinkedList*    list);
  207.         
  208.         ~LinkedListIterator();
  209.         
  210.         Link*            First();
  211.         
  212.         Link*            Next();
  213.         
  214.         Link*            Last();
  215.         
  216.         Link*            Previous();
  217.  
  218.         Link*            Current();
  219.  
  220.         ODBoolean         IsNotComplete();
  221.         
  222.         void            RemoveCurrent();
  223.         
  224.     private:
  225.     
  226.         LinkedList*        fList;
  227.         Link*            fCurrent;
  228.         Link*            fNext;        // Used only when deleting while iterating
  229.         Link*             fPrevious;    // Used only when deleting while iterating
  230.         Link*            fSentinel;
  231.         ODULong            fSeed;        // Used to detect out-of-sync iterators
  232.         
  233. };
  234.  
  235.  
  236. #endif // _LINKLIST_
  237.