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

  1. /* Copyright (c) IBM Corp. 1992 */
  2.  
  3. #ifndef _IHSHKS_H
  4. #define _IHSHKS_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 IGHashKeySet {
  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 IGHashKeySet < 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.     IGHashKeySet < Element, Key, ElementOps > const* ivCollection;
  96.     IHashKeyBagImpl::Cursor ivImpl;
  97.     friend class IGHashKeySet < Element, Key, ElementOps >;
  98.  
  99.   public:
  100.     Cursor (IGHashKeySet < 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.     Boolean operator== (Cursor const& cursor)
  111.     { return ivCollection == cursor.ivCollection &&
  112.              ivImpl == cursor.ivImpl; }
  113.     Boolean operator!= (Cursor const& cursor)
  114.     { return ! operator== (cursor); }
  115.   };
  116.  
  117. private:
  118.  
  119.   void                checkNotEmpty        () const;
  120.   void                checkCursorIsForThis (ICursor const&) const;
  121.   void                checkCursor          (ICursor const&) const;
  122.  
  123. public:
  124.  
  125.                   IGHashKeySet             (INumber
  126.                                                numberOfElements = 100,
  127.                                                IBoundIndicator =
  128.                                                IUnbounded);
  129.  
  130.                   IGHashKeySet             (IGHashKeySet < Element, Key, ElementOps > const&);
  131.  
  132.    IGHashKeySet < Element, Key, ElementOps >&    operator =                  (IGHashKeySet < Element, Key, ElementOps > const&);
  133.  
  134.                  ~IGHashKeySet             ();
  135.  
  136.    Boolean        add                         (Element const&);
  137.  
  138.    Boolean        add                         (Element const&,
  139.                                                ICursor&);
  140.  
  141.    void           addAllFrom                  (IGHashKeySet < Element, Key, ElementOps > const&);
  142.  
  143.    Element const& elementAt                   (ICursor const&) const;
  144.  
  145.    Element&       elementAt                   (ICursor const&);
  146.  
  147.    Element const& anyElement                  () const;
  148.  
  149.    void           removeAt                    (ICursor const&);
  150.  
  151.    INumber        removeAll                   (Boolean (*property)
  152.                                                (Element const&, void*),
  153.                                                void* additionalArgument = 0);
  154.  
  155.    void           replaceAt                   (ICursor const&,
  156.                                                Element const&);
  157.  
  158.    void           removeAll                   ();
  159.  
  160.    Boolean        isBounded                   () const;
  161.  
  162.    INumber        maxNumberOfElements         () const;
  163.  
  164.    INumber        numberOfElements            () const;
  165.  
  166.    Boolean        isEmpty                     () const;
  167.  
  168.    Boolean        isFull                      () const;
  169.  
  170.    ICursor*       newCursor                   () const;
  171.  
  172.    Boolean        setToFirst                  (ICursor&) const;
  173.  
  174.    Boolean        setToNext                   (ICursor&) const;
  175.  
  176.    Boolean        allElementsDo               (Boolean (*function)
  177.                                                   (Element&, void*),
  178.                                                void* additionalArgument = 0);
  179.  
  180.    Boolean        allElementsDo               (IIterator <Element>&);
  181.  
  182.    Boolean        allElementsDo               (Boolean (*function)
  183.                                                (Element const&, void*),
  184.                                                void* additionalArgument = 0)
  185.                                                const;
  186.  
  187.    Boolean        allElementsDo               (IConstantIterator
  188.                                                   <Element>&) const;
  189.  
  190.    Key const&     key                         (Element const&) const;
  191.  
  192.    Boolean        containsElementWithKey      (Key const&) const;
  193.  
  194.    Boolean        containsAllKeysFrom         (IGHashKeySet < Element, Key, ElementOps > const&) const;
  195.  
  196.    Boolean        locateElementWithKey        (Key const&, ICursor&)
  197.                                                const;
  198.  
  199.    Boolean        replaceElementWithKey       (Element const&);
  200.  
  201.    Boolean        replaceElementWithKey       (Element const&,
  202.                                                ICursor&);
  203.  
  204.    Boolean        locateOrAddElementWithKey   (Element const&);
  205.  
  206.    Boolean        locateOrAddElementWithKey   (Element const&,
  207.                                                ICursor&);
  208.  
  209.    Boolean        addOrReplaceElementWithKey  (Element const&);
  210.  
  211.    Boolean        addOrReplaceElementWithKey  (Element const&,
  212.                                                ICursor&);
  213.  
  214.    Boolean        removeElementWithKey        (Key const&);
  215.  
  216.    Element const& elementWithKey              (Key const&) const;
  217.  
  218.    Element&       elementWithKey              (Key const&);
  219.  
  220. };
  221.  
  222. template < class Element, class Key >
  223. class IHashKeySet :
  224.   public IGHashKeySet < Element, Key, IKEHOps < Element, Key > > {
  225. public:
  226.   IHashKeySet (INumber n = 100, IBoundIndicator b = IUnbounded) :
  227.     IGHashKeySet < Element, Key, IKEHOps < Element, Key > > (n, b) {}
  228. };
  229.  
  230. #include <ihshks.if>
  231.  
  232. #ifdef __IBMCPP__
  233. #include <ihshks.c>
  234. #endif
  235.  
  236. #endif
  237.