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