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