home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IHSHKS.H < prev    next >
Text File  |  1993-09-22  |  10KB  |  254 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 _IHSHKS_H
  13. #define _IHSHKS_H
  14.  
  15. #include <iglobals.h>
  16. #include <icursor.h>
  17. #include <stdlib.h>
  18. #include <iihshkb.h>
  19.  
  20. template < class Element, class Key, class ElementOps >
  21. class IGHashKeySet {
  22.  
  23.   static ElementOps elementOps ()
  24.   { ElementOps ops;
  25.     return ops;
  26.   }
  27.  
  28.   class Node;
  29.   class Operations;
  30.   class Cursor;
  31.  
  32.   friend class Node;
  33.   friend class Operations;
  34.   friend class Cursor;
  35.  
  36.   class Node : public IHashKeyBagImpl::Node {
  37.     friend class IGHashKeySet < Element, Key, ElementOps >;
  38.     friend class Cursor;
  39.     friend class Operations;
  40.  
  41.     Element ivElement;
  42.  
  43.                      Node                (Element const& element)
  44.                      : ivElement (element) {};
  45.  
  46. #if defined (__IBMCPP__) && defined (__DEBUG_ALLOC__)
  47.     void*            operator new        (size_t s, char const* fn, size_t ln)
  48.                      { return elementOps().allocate (s, fn, ln); }
  49.  
  50.     void             operator delete     (void* p, char const* fn, size_t ln)
  51.                      { elementOps().deallocate (p, fn, ln); }
  52. #else
  53.     void*            operator new        (size_t s)
  54.                      { return elementOps().allocate (s); }
  55.  
  56.     void             operator delete     (void* p)
  57.                      { elementOps().deallocate (p); }
  58. #endif // __IBMCPP__ && __DEBUG_ALLOC__
  59.   };
  60.  
  61.   class Operations : public IHashKeyBagImpl::Operations {
  62.     IHashKeyBagImpl::Node*
  63.                 newNode                   (void const* element) const;
  64.     IHashKeyBagImpl::Node*
  65.                 copyNode                  (void const* node) const;
  66.     void        deleteNode                (void* node) const;
  67.  
  68.     void*       newBlock                  (size_t size) const;
  69.     void        removeBlock               (void*, size_t size) const;
  70.  
  71.     INumber     getHashvalue              (void const* key, INumber) const;
  72.  
  73.     void*       elementAt                 (void *node) const;
  74.     void const* keyAt                     (void const* node) const;
  75.     IBoolean    isKeyEqualToElement       (void const* node,
  76.                                            void const* element) const;
  77.     IBoolean    isKeyEqualToKey           (void const* node,
  78.                                            void const* key) const;
  79.     IBoolean    isEqualTo                 (void const* node1,
  80.                                            void const* node2) const;
  81.     IBoolean    isEqualToElement          (void const* node,
  82.                                            void const* element) const;
  83.     void        copyFrom                  (void* node,
  84.                                            void const* element) const;
  85.  
  86.     IBoolean    constantFunctionIteration (void *function,
  87.                                            void* env,
  88.                                            void const* node);
  89.     IBoolean    functionIteration         (void *function,
  90.                                            void* env,
  91.                                            void* node);
  92.     IBoolean    constantIteratorIteration (void* iterator,
  93.                                            void const* node);
  94.     IBoolean    iteratorIteration         (void* iterator,
  95.                                            void* node);
  96.   };
  97.  
  98.   // ivOps must be initialized (and therefore declared) before ivImpl
  99.   // since the constructor of ivImpl uses ivOps.
  100.   Operations          ivOps;
  101.   IHashKeyBagImpl      ivImpl;
  102.  
  103. public:
  104.  
  105.   class Cursor : public ICursor {
  106.   protected:
  107.     IGHashKeySet < Element, Key, ElementOps > const* ivCollection;
  108.     IHashKeyBagImpl::Cursor ivImpl;
  109.     friend class IGHashKeySet < Element, Key, ElementOps >;
  110.     IBoolean isFor (IGHashKeySet < Element, Key, ElementOps > const& c) const
  111.     { return ivCollection == &c; }
  112.  
  113.   public:
  114.     Cursor (IGHashKeySet < Element, Key, ElementOps > const& c)
  115.     : ivCollection (&c) {};
  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 operator== (Cursor const& cursor) const
  125.     { return ivCollection == cursor.ivCollection &&
  126.              ivImpl == cursor.ivImpl; }
  127.     IBoolean operator!= (Cursor const& cursor) const
  128.     { return ! operator== (cursor); }
  129.   };
  130.  
  131. private:
  132.  
  133.   void                checkNotEmpty        () const;
  134.   void                checkCursorIsForThis (ICursor const&) const;
  135.   void                checkCursor          (ICursor const&) const;
  136.  
  137. public:
  138.  
  139.                   IGHashKeySet             (INumber
  140.                                                numberOfElements = 100);
  141.  
  142.                   IGHashKeySet             (IGHashKeySet < Element, Key, ElementOps > const&);
  143.  
  144.    IGHashKeySet < Element, Key, ElementOps >&
  145.                   operator =                  (IGHashKeySet < Element, Key, ElementOps > const&);
  146.  
  147.                  ~IGHashKeySet             ();
  148.  
  149.    IBoolean       add                         (Element const&);
  150.  
  151.    IBoolean       add                         (Element const&,
  152.                                                ICursor&);
  153.  
  154.    void           addAllFrom                  (IGHashKeySet < Element, Key, ElementOps > const&);
  155.  
  156.    Element const& elementAt                   (ICursor const&) const;
  157.  
  158.    Element&       elementAt                   (ICursor const&);
  159.  
  160.    Element const& anyElement                  () const;
  161.  
  162.    void           removeAt                    (ICursor const&);
  163.  
  164.    INumber        removeAll                   (IBoolean (*property)
  165.                                                (Element const&, void*),
  166.                                                void* additionalArgument = 0);
  167.  
  168.    void           replaceAt                   (ICursor const&,
  169.                                                Element const&);
  170.  
  171.    void           removeAll                   ();
  172.  
  173.    IBoolean       isBounded                   () const;
  174.  
  175.    INumber        maxNumberOfElements         () const;
  176.  
  177.    INumber        numberOfElements            () const;
  178.  
  179.    IBoolean       isEmpty                     () const;
  180.  
  181.    IBoolean       isFull                      () const;
  182.  
  183.    ICursor*       newCursor                   () const;
  184.  
  185.    IBoolean       setToFirst                  (ICursor&) const;
  186.  
  187.    IBoolean       setToNext                   (ICursor&) const;
  188.  
  189.    IBoolean       allElementsDo               (IBoolean (*function)
  190.                                                   (Element&, void*),
  191.                                                void* additionalArgument = 0);
  192.  
  193.    IBoolean       allElementsDo               (IIterator <Element>&);
  194.  
  195.    IBoolean       allElementsDo               (IBoolean (*function)
  196.                                                (Element const&, void*),
  197.                                                void* additionalArgument = 0)
  198.                                                const;
  199.  
  200.    IBoolean       allElementsDo               (IConstantIterator
  201.                                                   <Element>&) const;
  202.  
  203.    IBoolean       isConsistent                () const;
  204.  
  205.    Key const&     key                         (Element const&) const;
  206.  
  207.    IBoolean       containsElementWithKey      (Key const&) const;
  208.  
  209.    IBoolean       containsAllKeysFrom         (IGHashKeySet < Element, Key, ElementOps > const&) const;
  210.  
  211.    IBoolean       locateElementWithKey        (Key const&, ICursor&)
  212.                                                const;
  213.  
  214.    IBoolean       replaceElementWithKey       (Element const&);
  215.  
  216.    IBoolean       replaceElementWithKey       (Element const&,
  217.                                                ICursor&);
  218.  
  219.    IBoolean       locateOrAddElementWithKey   (Element const&);
  220.  
  221.    IBoolean       locateOrAddElementWithKey   (Element const&,
  222.                                                ICursor&);
  223.  
  224.    IBoolean       addOrReplaceElementWithKey  (Element const&);
  225.  
  226.    IBoolean       addOrReplaceElementWithKey  (Element const&,
  227.                                                ICursor&);
  228.  
  229.    IBoolean       removeElementWithKey        (Key const&);
  230.  
  231.    Element const& elementWithKey              (Key const&) const;
  232.  
  233.    Element&       elementWithKey              (Key const&);
  234.  
  235. };
  236.  
  237. template < class Element, class Key >
  238. class IHashKeySet :
  239.   public IGHashKeySet < Element, Key, IKEHOps < Element, Key > > {
  240. public:
  241.   IHashKeySet (INumber n = 100) :
  242.     IGHashKeySet < Element, Key, IKEHOps < Element, Key > > (n) {}
  243. };
  244.  
  245. #include <ihshks.if>
  246.  
  247. #ifdef __IBMCPP__
  248. #ifndef __TEMPINC__
  249. #include <ihshks.c>
  250. #endif
  251. #endif
  252.  
  253. #endif
  254.