home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / bbxxj / itbseq.h__ / ITBSEQ.H
Encoding:
C/C++ Source or Header  |  1992-10-26  |  9.0 KB  |  253 lines

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