home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / ITDSEQ.H < prev    next >
Text File  |  1993-09-22  |  11KB  |  287 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 _ITDSEQ_H
  13. #define _ITDSEQ_H
  14.  
  15. #include <iglobals.h>
  16. #include <icursor.h>
  17. #include <iitdseq.h>
  18.  
  19. template < class Element, class ElementOps >
  20. class IGDilutedSequence {
  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 IGDilutedSequence < Element, ElementOps >;
  37.     friend class Cursor;
  38.     friend class Operations;
  39.  
  40.     Element ivElement;
  41.     IBoolean ivIsRemoved;
  42.  
  43. #if defined (__IBMCPP__) && defined (__DEBUG_ALLOC__)
  44.     void*            operator new        (size_t s, char const* fn, size_t ln)
  45.                      { return elementOps().allocate (s, fn, ln); }
  46.  
  47.     void             operator delete     (void* p, char const* fn, size_t ln)
  48.                      { elementOps().deallocate (p, fn, ln); }
  49. #else
  50.     void*            operator new        (size_t s)
  51.                      { return elementOps().allocate (s); }
  52.  
  53.     void             operator delete     (void* p)
  54.                      { elementOps().deallocate (p); }
  55. #endif // __IBMCPP__ && __DEBUG_ALLOC__
  56.   };
  57.  
  58.   class Operations : public IDilutedSequenceImpl::Operations {
  59.     friend class IGDilutedSequence < Element, ElementOps >;
  60.  
  61.     void        swap                     (INumber index1,
  62.                                           INumber index2) const;
  63.     void        copyFrom                 (INumber index,
  64.                                           void const* element) const;
  65.     void const* elementAt                (INumber index) const;
  66.  
  67.     IBoolean     isRemoved                (INumber index) const;
  68.     void        remove                   (INumber index);
  69.     IBoolean     setToNext                (INumber& index) const;
  70.     IBoolean     setToPrevious            (INumber& index) const;
  71.  
  72.     IBoolean     constantFunctionIteration (void *function,
  73.                                            void* env,
  74.                                            INumber index) const;
  75.     IBoolean     functionIteration         (void *function,
  76.                                            void* env,
  77.                                            INumber index);
  78.     IBoolean     constantIteratorIteration (void* iterator,
  79.                                            INumber index) const;
  80.     IBoolean     iteratorIteration         (void* iterator,
  81.                                            INumber index);
  82.  
  83.     long        functionComparison        (void *compareFunction,
  84.                                            void const* element1,
  85.                                            void const* element2);
  86.     long        functionComparison        (void *compareFunction,
  87.                                            INumber index1,
  88.                                            INumber index2);
  89.  
  90.     void        blockLeft                 (INumber target, INumber moveCount);
  91.     void        blockRight                (INumber target, INumber moveCount);
  92.  
  93.     void        copy                      (IDilutedSequenceImpl::Table const&);
  94.     void        expand                    (INumber n);
  95.     void        makeFree                  (INumber&);
  96.   protected:
  97.     void        compress                  ();
  98.   };
  99.  
  100.   // ivOps must be initialized (and therefore declared) before ivImpl
  101.   // since the constructor of ivImpl uses ivOps.
  102.   Operations ivOps;
  103.   IDilutedSequenceImpl ivImpl;
  104.  
  105. public:
  106.  
  107.   class Cursor : public ICursor {
  108.   protected:
  109.     IGDilutedSequence < Element, ElementOps > const* ivCollection;
  110.     IDilutedSequenceImpl::Cursor ivImpl;
  111.     friend class IGDilutedSequence < Element, ElementOps >;
  112.     IBoolean isFor (IGDilutedSequence < Element, ElementOps > const& c) const
  113.     { return ivCollection == &c; }
  114.   public:
  115.     Cursor (IGDilutedSequence < Element, ElementOps > const& c)
  116.     : ivCollection (&c) {};
  117.  
  118.     IBoolean setToFirst ();
  119.     IBoolean setToNext  ();
  120.     IBoolean isValid    () const;
  121.     void    invalidate ();
  122.  
  123.     Element const& element      () const
  124.     { return ivCollection->elementAt (*this); }
  125.  
  126.     IBoolean setToLast     ()
  127.     { return ivCollection->setToLast (*this); }
  128.     IBoolean setToPrevious ()
  129.     { return ivCollection->setToPrevious (*this); }
  130.  
  131.     IBoolean operator== (Cursor const& cursor) const
  132.     { return ivCollection == cursor.ivCollection &&
  133.              ivImpl == cursor.ivImpl; }
  134.     IBoolean operator!= (Cursor const& cursor) const
  135.     { return ! operator== (cursor); }
  136.   };
  137.  
  138. public:
  139.  
  140.                   IGDilutedSequence             (INumber
  141.                                                numberOfElements = 100);
  142.  
  143.                   IGDilutedSequence             (IGDilutedSequence < Element, ElementOps > const&);
  144.  
  145.    IGDilutedSequence < Element, ElementOps >&
  146.                   operator =                  (IGDilutedSequence < Element, ElementOps > const&);
  147.  
  148.                  ~IGDilutedSequence             ();
  149.  
  150.    IBoolean       add                         (Element const&);
  151.  
  152.    IBoolean       add                         (Element const&,
  153.                                                ICursor&);
  154.  
  155.    void           addAllFrom                  (IGDilutedSequence < Element, ElementOps > const&);
  156.  
  157.    Element const& elementAt                   (ICursor const&) const;
  158.  
  159.    Element&       elementAt                   (ICursor const&);
  160.  
  161.    Element const& anyElement                  () const;
  162.  
  163.    void           removeAt                    (ICursor const&);
  164.  
  165.    INumber        removeAll                   (IBoolean (*property)
  166.                                                (Element const&, void*),
  167.                                                void* additionalArgument = 0);
  168.  
  169.    void           replaceAt                   (ICursor const&,
  170.                                                Element const&);
  171.  
  172.    void           removeAll                   ();
  173.  
  174.    IBoolean       isBounded                   () const;
  175.  
  176.    INumber        maxNumberOfElements         () const;
  177.  
  178.    INumber        numberOfElements            () const;
  179.  
  180.    IBoolean       isEmpty                     () const;
  181.  
  182.    IBoolean       isFull                      () const;
  183.  
  184.    ICursor*       newCursor                   () const;
  185.  
  186.    IBoolean       setToFirst                  (ICursor&) const;
  187.  
  188.    IBoolean       setToNext                   (ICursor&) const;
  189.  
  190.    IBoolean       allElementsDo               (IBoolean (*function)
  191.                                                   (Element&, void*),
  192.                                                void* additionalArgument = 0);
  193.  
  194.    IBoolean       allElementsDo               (IIterator <Element>&);
  195.  
  196.    IBoolean       allElementsDo               (IBoolean (*function)
  197.                                                (Element const&, void*),
  198.                                                void* additionalArgument = 0)
  199.                                                const;
  200.  
  201.    IBoolean       allElementsDo               (IConstantIterator
  202.                                                   <Element>&) const;
  203.  
  204.    IBoolean       isConsistent                () const;
  205.  
  206.    void           removeFirst                 ();
  207.  
  208.    void           removeLast                  ();
  209.  
  210.    void           removeAtPosition            (IPosition);
  211.  
  212.    Element const& firstElement                () const;
  213.  
  214.    Element const& lastElement                 () const;
  215.  
  216.    Element const& elementAtPosition           (IPosition) const;
  217.  
  218.    IBoolean       setToLast                   (ICursor&) const;
  219.  
  220.    IBoolean       setToPrevious               (ICursor&) const;
  221.  
  222.    void           setToPosition               (IPosition,
  223.                                                ICursor&) const;
  224.  
  225.    IBoolean       isFirst                     (ICursor const&) const;
  226.  
  227.    IBoolean       isLast                      (ICursor const&) const;
  228.  
  229.    long           compare                     (IGDilutedSequence < Element, ElementOps > const&,
  230.                                                long (*comparisonFunction)
  231.                                                   (Element const&,
  232.                                                   Element const&)) const;
  233.  
  234.    void           addAsFirst                  (Element const&);
  235.  
  236.    void           addAsFirst                  (Element const&,
  237.                                                ICursor&);
  238.  
  239.    void           addAsLast                   (Element const&);
  240.  
  241.    void           addAsLast                   (Element const&,
  242.                                                ICursor&);
  243.  
  244.    void           addAsNext                   (Element const&,
  245.                                                ICursor&);
  246.  
  247.    void           addAsPrevious               (Element const&,
  248.                                                ICursor&);
  249.  
  250.    void           addAtPosition               (IPosition,
  251.                                                Element const&);
  252.  
  253.    void           addAtPosition               (IPosition,
  254.                                                Element const&,
  255.                                                ICursor&);
  256.  
  257.    void           sort                        (long (*comparisonFunction)
  258.                                                (Element const&,
  259.                                                Element const&));
  260.  
  261. protected:
  262.  
  263.   void                checkNotEmpty        () const;
  264.   void                checkCursorIsForThis (ICursor const&) const;
  265.   void                checkCursor          (ICursor const&) const;
  266.   void                checkPositionExists  (IPosition) const;
  267.   void                checkPositionForAdd  (IPosition) const;
  268. };
  269.  
  270. template < class Element >
  271. class IDilutedSequence :
  272.   public IGDilutedSequence < Element, IStdOps < Element > > {
  273. public:
  274.   IDilutedSequence (INumber n = 100) :
  275.     IGDilutedSequence < Element, IStdOps < Element > > (n) {}
  276. };
  277.  
  278. #include <itdseq.if>
  279.  
  280. #ifdef __IBMCPP__
  281. #ifndef __TEMPINC__
  282. #include <itdseq.c>
  283. #endif
  284. #endif
  285.  
  286. #endif
  287.