home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / ITBSEQ.H < prev    next >
Text File  |  1993-09-22  |  11KB  |  278 lines

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