home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / runnable / ibmc / ibmclass / ilnseq.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-26  |  8.5 KB  |  241 lines

  1. /* Copyright (c) IBM Corp. 1992 */
  2.  
  3. #ifndef _ILNSEQ_H
  4. #define _ILNSEQ_H
  5.  
  6. #include <iglobals.h>
  7. #include <icursor.h>
  8. #include <iilnseq.h>
  9.  
  10. template < class Element, class ElementOps >
  11. class IGLinkedSequence {
  12.  
  13.   static ElementOps cvElementOps;
  14.  
  15.   class Node;
  16.   class Operations;
  17.   class Cursor;
  18.  
  19.   friend class Node;
  20.   friend class Operations;
  21.   friend class Cursor;
  22.  
  23.   class Node : public ILinkedSequenceImpl::Node
  24.   { friend class IGLinkedSequence < Element, ElementOps >;
  25.     friend class Cursor;
  26.     friend class Operations;
  27.  
  28.     Element ivElement;
  29.                      Node                (Element const& element)
  30.              : ivElement (element) {};
  31.  
  32.     void*            operator new        (size_t s)
  33.              { return cvElementOps.allocate (s); }
  34.  
  35.     void             operator delete     (void* p)
  36.                      { cvElementOps.deallocate (p); }
  37.   };
  38.  
  39.   class Operations : public ILinkedSequenceImpl::Operations
  40.   { ILinkedSequenceImpl::Node*
  41.         newNode                   (void const* node) const;
  42.     void        deleteNode                (void* node) const;
  43.  
  44.     Boolean     constantFunctionIteration (void *function,
  45.                                            void* env,
  46.                                            void const* node);
  47.     Boolean     functionIteration         (void *function,
  48.                                            void* env,
  49.                                            void* node);
  50.     Boolean     constantIteratorIteration (void* iterator,
  51.                                            void const* node);
  52.     Boolean     iteratorIteration         (void* iterator,
  53.                                            void* node);
  54.  
  55.     long        functionComparison        (void *compareFunction,
  56.                                            void const* node1,
  57.                                            void const* node2);
  58.   };
  59.  
  60.   // ivOps must be initialized (and therefore declared) before ivImpl
  61.   // since the constructor of ivImpl uses ivOps.
  62.   Operations ivOps;
  63.   ILinkedSequenceImpl ivImpl;
  64.  
  65. public:
  66.  
  67.   class Cursor : public ICursor
  68.   {
  69.   protected:
  70.     IGLinkedSequence < Element, ElementOps > const* ivCollection;
  71.     ILinkedSequenceImpl::Node *ivNode;
  72.     friend class IGLinkedSequence < Element, ElementOps >;
  73.   public:
  74.     Cursor (IGLinkedSequence < Element, ElementOps > const& collection)
  75.     : ivCollection (&collection), ivNode (0) {};
  76.  
  77.     Boolean setToFirst ();
  78.     Boolean setToNext  ();
  79.     Boolean isValid    () const;
  80.     void    invalidate ();
  81.  
  82.     Element const& element      ()
  83.     { return ivCollection->elementAt (*this); }
  84.     Boolean setToLast     ()
  85.     { return ivCollection->setToLast (*this); }
  86.     Boolean setToPrevious ()
  87.     { return ivCollection->setToPrevious (*this); }
  88.     Boolean operator== (Cursor const& cursor)
  89.     { return ivCollection == cursor.ivCollection &&
  90.              ivNode == cursor.ivNode; }
  91.     Boolean operator!= (Cursor const& cursor)
  92.     { return ! operator== (cursor); }
  93.   };
  94.  
  95. public:
  96.  
  97.                   IGLinkedSequence             (INumber
  98.                                                numberOfElements = 100,
  99.                                                IBoundIndicator =
  100.                                                IUnbounded);
  101.  
  102.                   IGLinkedSequence             (IGLinkedSequence < Element, ElementOps > const&);
  103.  
  104.    IGLinkedSequence < Element, ElementOps >&    operator =                  (IGLinkedSequence < Element, ElementOps > const&);
  105.  
  106.                  ~IGLinkedSequence             ();
  107.  
  108.    Boolean        add                         (Element const&);
  109.  
  110.    Boolean        add                         (Element const&,
  111.                                                ICursor&);
  112.  
  113.    void           addAllFrom                  (IGLinkedSequence < Element, ElementOps > const&);
  114.  
  115.    Element const& elementAt                   (ICursor const&) const;
  116.  
  117.    Element&       elementAt                   (ICursor const&);
  118.  
  119.    Element const& anyElement                  () const;
  120.  
  121.    void           removeAt                    (ICursor const&);
  122.  
  123.    INumber        removeAll                   (Boolean (*property)
  124.                                                (Element const&, void*),
  125.                                                void* additionalArgument = 0);
  126.  
  127.    void           replaceAt                   (ICursor const&,
  128.                                                Element const&);
  129.  
  130.    void           removeAll                   ();
  131.  
  132.    Boolean        isBounded                   () const;
  133.  
  134.    INumber        maxNumberOfElements         () const;
  135.  
  136.    INumber        numberOfElements            () const;
  137.  
  138.    Boolean        isEmpty                     () const;
  139.  
  140.    Boolean        isFull                      () const;
  141.  
  142.    ICursor*       newCursor                   () const;
  143.  
  144.    Boolean        setToFirst                  (ICursor&) const;
  145.  
  146.    Boolean        setToNext                   (ICursor&) const;
  147.  
  148.    Boolean        allElementsDo               (Boolean (*function)
  149.                                                   (Element&, void*),
  150.                                                void* additionalArgument = 0);
  151.  
  152.    Boolean        allElementsDo               (IIterator <Element>&);
  153.  
  154.    Boolean        allElementsDo               (Boolean (*function)
  155.                                                (Element const&, void*),
  156.                                                void* additionalArgument = 0)
  157.                                                const;
  158.  
  159.    Boolean        allElementsDo               (IConstantIterator
  160.                                                   <Element>&) const;
  161.  
  162.    void           removeFirst                 ();
  163.  
  164.    void           removeLast                  ();
  165.  
  166.    void           removeAtPosition            (IPosition);
  167.  
  168.    Element const& firstElement                () const;
  169.  
  170.    Element const& lastElement                 () const;
  171.  
  172.    Element const& elementAtPosition           (IPosition) const;
  173.  
  174.    Boolean        setToLast                   (ICursor&) const;
  175.  
  176.    Boolean        setToPrevious               (ICursor&) const;
  177.  
  178.    void           setToPosition               (IPosition,
  179.                                                ICursor&) const;
  180.  
  181.    Boolean        isFirst                     (ICursor const&) const;
  182.  
  183.    Boolean        isLast                      (ICursor const&) const;
  184.  
  185.    long           compare                     (IGLinkedSequence < Element, ElementOps > const&,
  186.                                                long (*comparisonFunction)
  187.                                                   (Element const&,
  188.                                                   Element const&)) const;
  189.  
  190.    void           addAsFirst                  (Element const&);
  191.  
  192.    void           addAsFirst                  (Element const&,
  193.                                                ICursor&);
  194.  
  195.    void           addAsLast                   (Element const&);
  196.  
  197.    void           addAsLast                   (Element const&,
  198.                                                ICursor&);
  199.  
  200.    void           addAsNext                   (Element const&,
  201.                                                ICursor&);
  202.  
  203.    void           addAsPrevious               (Element const&,
  204.                                                ICursor&);
  205.  
  206.    void           addAtPosition               (IPosition,
  207.                                                Element const&);
  208.  
  209.    void           addAtPosition               (IPosition,
  210.                                                Element const&,
  211.                                                ICursor&);
  212.  
  213.    void           sort                        (long (*comparisonFunction)
  214.                                                (Element const&,
  215.                                                Element const&));
  216.  
  217. protected:
  218.  
  219.   void                checkNotEmpty        () const;
  220.   void                checkCursorIsForThis (ICursor const&) const;
  221.   void                checkCursor          (ICursor const&) const;
  222.   void                checkPositionExists  (IPosition) const;
  223.   void                checkPositionForAdd  (IPosition) const;
  224. };
  225.  
  226. template < class Element >
  227. class ILinkedSequence :
  228.   public IGLinkedSequence < Element, IStdOps < Element > > {
  229. public:
  230.   ILinkedSequence (INumber n = 100, IBoundIndicator b = IUnbounded) :
  231.     IGLinkedSequence < Element, IStdOps < Element > > (n, b) {}
  232. };
  233.  
  234. #include <ilnseq.if>
  235.  
  236. #ifdef __IBMCPP__
  237. #include <ilnseq.c>
  238. #endif
  239.  
  240. #endif
  241.