home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / OpenDoc / OpenDoc Utilities / Interfaces / LinkList.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  5.1 KB  |  221 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        LinkList.h
  3.  
  4.     Contains:    Primitive linked list class
  5.  
  6.     Owned by:    Richard Rodseth and Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <2>     5/24/96    jpa        1.1MRD: pragma internal eliminates NOPs.
  13.                                     Also inlined methods.
  14.  
  15.     In Progress:
  16.         
  17. */
  18.  
  19. #ifndef _LINKLIST_
  20. #define _LINKLIST_
  21.  
  22. #ifndef _ODTYPES_
  23. #include "ODTypes.h"
  24. #endif
  25.  
  26.  
  27. #ifdef PRAGMA_INTERNAL_SUPPORTED
  28. #pragma internal on
  29. #endif
  30.  
  31. //=====================================================================================
  32. // Theory of Operation
  33. //=====================================================================================
  34.  
  35. /*
  36.  
  37.  Note: These classes are private to the implementation. They are not available
  38.  to part handlers.
  39.  
  40.  Note: These are primitive classes for implementing higher-level collections
  41.  For example, to create a FrameList class, subclass Link to add a field to
  42.  store the frame. The FrameList class would use a LinkedList in its implementation
  43.  and would manufacture the Link objects internally.
  44.  
  45. */
  46.  
  47. //==============================================================================
  48. // Constants
  49. //==============================================================================
  50.  
  51. //==============================================================================
  52. // Scalar Types
  53. //==============================================================================
  54.  
  55. //==============================================================================
  56. // Classes defined in this interface
  57. //==============================================================================
  58.  
  59. class Link;
  60. class LinkedList;
  61. class LinkedListIterator;
  62.  
  63. //==============================================================================
  64. // Classes used by this interface
  65. //==============================================================================
  66.  
  67.  
  68. //==============================================================================
  69. // Link
  70. //==============================================================================
  71.  
  72. class  Link {
  73.     public:
  74.     
  75.         Link();
  76.         
  77.         Link(Link* next, Link* previous);
  78.         
  79.         Link( const Link& );
  80.         
  81.         virtual ~Link();
  82.         
  83.         Link* GetNext() const                    {return fNext;}
  84.         
  85.         Link* GetPrevious() const                {return fPrevious;}
  86.     
  87.     // The following operations are provided for efficiency, but DO NOT USE THEM
  88.     // if there are any iterators active on a list. These operations don't bump
  89.     // the list's fSeed and the iterators will not be able to detect that they
  90.     // are out of sync!
  91.         
  92.         void  Remove( );
  93.         
  94.         void  AddBefore( Link *aLink );
  95.         
  96.         void  AddAfter( Link *aLink );
  97.     
  98.   //private-by-convention:
  99.           
  100.         void  SetNext(Link* aLink)                {fNext = aLink;}
  101.         
  102.         void  SetPrevious(Link* aLink)            {fPrevious = aLink;}
  103.  
  104.     private:
  105.     
  106.         Link*        fNext;
  107.         Link*        fPrevious;
  108. };
  109.  
  110.  
  111. //==============================================================================
  112. // LinkedList
  113. //==============================================================================
  114.  
  115.  
  116. class LinkedList {
  117.  
  118.     public:
  119.     
  120.           LinkedList();
  121.           
  122.           ~LinkedList()    { }
  123.           
  124.           ODBoolean        IsEmpty( )                                        const;
  125.           
  126.           ODULong            Count()                                            const;
  127.           
  128.           ODBoolean        Includes( const Link* )                            const;
  129.           
  130.           void            Remove(Link&);
  131.           
  132.           void            RemoveAll();
  133.  
  134.           void            DeleteAllLinks();
  135.           
  136.           Link*            RemoveFirst();
  137.           
  138.           Link*            RemoveLast();
  139.           
  140.           void            AddBefore(Link& existing, Link* link);
  141.           
  142.           void            AddAfter(Link& existing, Link* link);
  143.           
  144.           void            AddFirst(Link* link);
  145.           
  146.           void            AddLast(Link* link);
  147.           
  148.           void            AddLast( LinkedList &list );
  149.           
  150.           void            AddLastUnique( LinkedList &list );
  151.           
  152.           Link*            After(const Link& link)                            const;
  153.           
  154.           Link*             Before(const Link& link)                        const;
  155.           
  156.           Link*            First()                                            const;
  157.           
  158.           Link*             Last()                                            const;
  159.  
  160. protected:
  161.  
  162.         Link            fSentinel;    // Marks the head & tail
  163.         ODULong            fSeed;        // Used to detect out-of-sync iterators
  164.         
  165.         Link*            GetSentinel( )
  166.                                                 {return &fSentinel;}
  167.         const Link*        GetSentinel( )                                    const
  168.                                                 {return &fSentinel;}
  169.         ODBoolean        IsSentinel( const Link* link )                    const
  170.                                                 {return link==this->GetSentinel();}
  171.         ODBoolean        NotSentinel( const Link* link )                    const
  172.                                                 {return link!=this->GetSentinel();}
  173.  
  174. private:                  
  175.         friend class LinkedListIterator;
  176. };
  177.  
  178.  
  179. //=====================================================================================
  180. // LinkedListIterator
  181. //=====================================================================================
  182.  
  183. class LinkedListIterator {
  184.  
  185.     public:
  186.     
  187.         LinkedListIterator(LinkedList*    list);
  188.         
  189.         ~LinkedListIterator()                { }
  190.         
  191.         Link*            First();
  192.         
  193.         Link*            Next();
  194.         
  195.         Link*            Last();
  196.         
  197.         Link*            Previous();
  198.  
  199.         Link*            Current()            {return fCurrent;}
  200.  
  201.         ODBoolean         IsNotComplete()        {return (fCurrent != kODNULL);}
  202.         
  203.         void            RemoveCurrent();
  204.         
  205.     private:
  206.     
  207.         LinkedList*        fList;
  208.         Link*            fCurrent;
  209.         Link*            fNext;        // Used only when deleting while iterating
  210.         Link*             fPrevious;    // Used only when deleting while iterating
  211.         Link*            fSentinel;
  212.         ODULong            fSeed;        // Used to detect out-of-sync iterators
  213.         
  214. };
  215.  
  216.  
  217. #ifdef PRAGMA_INTERNAL_SUPPORTED
  218. #pragma internal off
  219. #endif
  220.  
  221. #endif // _LINKLIST_