home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / bbxxk / ihshkb.h__ / IHSHKB.H
Encoding:
C/C++ Source or Header  |  1992-10-26  |  8.8 KB  |  249 lines

  1. /* Copyright (c) IBM Corp. 1992 */
  2.  
  3. #ifndef _IHSHKB_H
  4. #define _IHSHKB_H
  5.  
  6. #include <iglobals.h>
  7. #include <icursor.h>
  8. #include <stdlib.h>
  9. #include <iihshkb.h>
  10.  
  11. template < class Element, class Key, class ElementOps >
  12. class IGHashKeyBag {
  13.  
  14.   static ElementOps cvElementOps;
  15.  
  16.   class Node;
  17.   class Operations;
  18.   class Cursor;
  19.  
  20.   friend class Node;
  21.   friend class Operations;
  22.   friend class Cursor;
  23.  
  24.   class Node : public IHashKeyBagImpl::Node {
  25.     friend class IGHashKeyBag < Element, Key, ElementOps >;
  26.     friend class Cursor;
  27.     friend class Operations;
  28.  
  29.     Element ivElement;
  30.  
  31.                      Node                (Element const& element)
  32.              : ivElement (element) {};
  33.  
  34.     void*            operator new        (size_t s)
  35.              { return cvElementOps.allocate (s); }
  36.  
  37.     void             operator delete     (void* p)
  38.                      { cvElementOps.deallocate (p); }
  39.   };
  40.  
  41.   class Operations : public IHashKeyBagImpl::Operations {
  42.  
  43.     IHashKeyBagImpl::Node*
  44.         newNode                   (void const* element) const;
  45.     IHashKeyBagImpl::Node*
  46.         copyNode                  (void const* node) const;
  47.     void        deleteNode                (void* node) const;
  48.  
  49.     void*       newBlock                  (size_t size) const;
  50.     void        removeBlock               (void*, size_t size) const;
  51.  
  52.     INumber     getHashvalue              (void const* key, INumber) const;
  53.  
  54.     void*       elementAt                 (void *node) const;
  55.     void const* constElementAt            (void const* node) const;
  56.     void const* keyAt                     (void const* node) const;
  57.     Boolean     isKeyEqualTo              (void const* node1,
  58.                                            void const* node2) const;
  59.     Boolean     isKeyEqualToElement       (void const* node,
  60.                                            void const* element) const;
  61.     Boolean     isKeyEqualToKey           (void const* node,
  62.                                            void const* key) const;
  63.     Boolean     isEqualTo                 (void const* node1,
  64.                                            void const* node2) const;
  65.     Boolean     isEqualToElement          (void const* node,
  66.                                            void const* element) const;
  67.     void        copyFrom                  (void* node,
  68.                                            void const* element) const;
  69.  
  70.     Boolean     constantFunctionIteration (void *function,
  71.                                            void* env,
  72.                                            void const* node);
  73.     Boolean     functionIteration         (void *function,
  74.                                            void* env,
  75.                                            void* node);
  76.     Boolean     constantIteratorIteration (void* iterator,
  77.                                            void const* node);
  78.     Boolean     iteratorIteration         (void* iterator,
  79.                                            void* node);
  80.  
  81.     long        functionComparison        (void *compareFunction,
  82.                                            void const* node1,
  83.                                            void const* node2);
  84.   };
  85.  
  86.   // ivOps must be initialized (and therefore declared) before ivImpl
  87.   // since the constructor of ivImpl uses ivOps.
  88.   Operations          ivOps;
  89.   IHashKeyBagImpl      ivImpl;
  90.  
  91. public:
  92.  
  93.   class Cursor : public ICursor {
  94.   protected:
  95.     IGHashKeyBag < Element, Key, ElementOps > const* ivCollection;
  96.     IHashKeyBagImpl::Cursor ivImpl;
  97.     friend class IGHashKeyBag < Element, Key, ElementOps >;
  98.  
  99.   public:
  100.     Cursor (IGHashKeyBag < Element, Key, ElementOps > const& collection)
  101.     : ivCollection (&collection) {};
  102.  
  103.     Boolean setToFirst ();
  104.     Boolean setToNext  ();
  105.     Boolean isValid    () const;
  106.     void    invalidate ();
  107.  
  108.     Element const& element      ()
  109.     { return ivCollection->elementAt (*this); }
  110.  
  111.     Boolean operator== (Cursor const& cursor)
  112.     { return ivCollection == cursor.ivCollection &&
  113.              ivImpl == cursor.ivImpl; }
  114.     Boolean operator!= (Cursor const& cursor)
  115.     { return ! operator== (cursor); }
  116.   };
  117.  
  118. private:
  119.  
  120.   void                checkNotEmpty        () const;
  121.   void                checkCursorIsForThis (ICursor const&) const;
  122.   void                checkCursor          (ICursor const&) const;
  123.  
  124. public:
  125.  
  126.                   IGHashKeyBag             (INumber
  127.                                                numberOfElements = 100,
  128.                                                IBoundIndicator =
  129.                                                IUnbounded);
  130.  
  131.                   IGHashKeyBag             (IGHashKeyBag < Element, Key, ElementOps > const&);
  132.  
  133.    IGHashKeyBag < Element, Key, ElementOps >&    operator =                  (IGHashKeyBag < Element, Key, ElementOps > const&);
  134.  
  135.                  ~IGHashKeyBag             ();
  136.  
  137.    Boolean        add                         (Element const&);
  138.  
  139.    Boolean        add                         (Element const&,
  140.                                                ICursor&);
  141.  
  142.    void           addAllFrom                  (IGHashKeyBag < Element, Key, ElementOps > const&);
  143.  
  144.    Element const& elementAt                   (ICursor const&) const;
  145.  
  146.    Element&       elementAt                   (ICursor const&);
  147.  
  148.    Element const& anyElement                  () const;
  149.  
  150.    void           removeAt                    (ICursor const&);
  151.  
  152.    INumber        removeAll                   (Boolean (*property)
  153.                                                (Element const&, void*),
  154.                                                void* additionalArgument = 0);
  155.  
  156.    void           replaceAt                   (ICursor const&,
  157.                                                Element const&);
  158.  
  159.    void           removeAll                   ();
  160.  
  161.    Boolean        isBounded                   () const;
  162.  
  163.    INumber        maxNumberOfElements         () const;
  164.  
  165.    INumber        numberOfElements            () const;
  166.  
  167.    Boolean        isEmpty                     () const;
  168.  
  169.    Boolean        isFull                      () const;
  170.  
  171.    ICursor*       newCursor                   () const;
  172.  
  173.    Boolean        setToFirst                  (ICursor&) const;
  174.  
  175.    Boolean        setToNext                   (ICursor&) const;
  176.  
  177.    Boolean        allElementsDo               (Boolean (*function)
  178.                                                   (Element&, void*),
  179.                                                void* additionalArgument = 0);
  180.  
  181.    Boolean        allElementsDo               (IIterator <Element>&);
  182.  
  183.    Boolean        allElementsDo               (Boolean (*function)
  184.                                                (Element const&, void*),
  185.                                                void* additionalArgument = 0)
  186.                                                const;
  187.  
  188.    Boolean        allElementsDo               (IConstantIterator
  189.                                                   <Element>&) const;
  190.  
  191.    Key const&     key                         (Element const&) const;
  192.  
  193.    Boolean        containsElementWithKey      (Key const&) const;
  194.  
  195.    Boolean        containsAllKeysFrom         (IGHashKeyBag < Element, Key, ElementOps > const&) const;
  196.  
  197.    Boolean        locateElementWithKey        (Key const&, ICursor&)
  198.                                                const;
  199.  
  200.    Boolean        replaceElementWithKey       (Element const&);
  201.  
  202.    Boolean        replaceElementWithKey       (Element const&,
  203.                                                ICursor&);
  204.  
  205.    Boolean        locateOrAddElementWithKey   (Element const&);
  206.  
  207.    Boolean        locateOrAddElementWithKey   (Element const&,
  208.                                                ICursor&);
  209.  
  210.    Boolean        addOrReplaceElementWithKey  (Element const&);
  211.  
  212.    Boolean        addOrReplaceElementWithKey  (Element const&,
  213.                                                ICursor&);
  214.  
  215.    Boolean        removeElementWithKey        (Key const&);
  216.  
  217.    Element const& elementWithKey              (Key const&) const;
  218.  
  219.    Element&       elementWithKey              (Key const&);
  220.  
  221.    INumber        numberOfElementsWithKey     (Key const&) const;
  222.  
  223.    Boolean        locateNextElementWithKey    (Key const&,
  224.                                                ICursor&) const;
  225.  
  226.    INumber        removeAllElementsWithKey    (Key const&);
  227.  
  228.    INumber        numberOfDifferentKeys       () const;
  229.  
  230.    Boolean        setToNextWithDifferentKey   (ICursor&) const;
  231.  
  232. };
  233.  
  234. template < class Element, class Key >
  235. class IHashKeyBag :
  236.   public IGHashKeyBag < Element, Key, IKEHOps < Element, Key > > {
  237. public:
  238.   IHashKeyBag (INumber n = 100, IBoundIndicator b = IUnbounded) :
  239.     IGHashKeyBag < Element, Key, IKEHOps < Element, Key > > (n, b) {}
  240. };
  241.  
  242. #include <ihshkb.if>
  243.  
  244. #ifdef __IBMCPP__
  245. #include <ihshkb.c>
  246. #endif
  247.  
  248. #endif
  249.