home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / IHSHKB.H < prev    next >
Text File  |  1993-09-22  |  10KB  |  266 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 _IHSHKB_H
  13. #define _IHSHKB_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 IGHashKeyBag {
  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 IGHashKeyBag < 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.     IGHashKeyBag < Element, Key, ElementOps > const* ivCollection;
  108.     IHashKeyBagImpl::Cursor ivImpl;
  109.     friend class IGHashKeyBag < Element, Key, ElementOps >;
  110.     IBoolean isFor (IGHashKeyBag < Element, Key, ElementOps > const& c) const
  111.     { return ivCollection == &c; }
  112.  
  113.   public:
  114.     Cursor (IGHashKeyBag < 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.  
  125.     IBoolean operator== (Cursor const& cursor) const
  126.     { return ivCollection == cursor.ivCollection &&
  127.              ivImpl == cursor.ivImpl; }
  128.     IBoolean operator!= (Cursor const& cursor) const
  129.     { return ! operator== (cursor); }
  130.   };
  131.  
  132. private:
  133.  
  134.   void                checkNotEmpty        () const;
  135.   void                checkCursorIsForThis (ICursor const&) const;
  136.   void                checkCursor          (ICursor const&) const;
  137.  
  138. public:
  139.  
  140.                   IGHashKeyBag             (INumber
  141.                                                numberOfElements = 100);
  142.  
  143.                   IGHashKeyBag             (IGHashKeyBag < Element, Key, ElementOps > const&);
  144.  
  145.    IGHashKeyBag < Element, Key, ElementOps >&
  146.                   operator =                  (IGHashKeyBag < Element, Key, ElementOps > const&);
  147.  
  148.                  ~IGHashKeyBag             ();
  149.  
  150.    IBoolean       add                         (Element const&);
  151.  
  152.    IBoolean       add                         (Element const&,
  153.                                                ICursor&);
  154.  
  155.    void           addAllFrom                  (IGHashKeyBag < Element, Key, ElementOps > const&);
  156.  
  157.    Element const& elementAt                   (ICursor const&) const;
  158.  
  159.    Element&       elementAt                   (ICursor const&);
  160.  
  161.    Element const& anyElement                  () const;
  162.  
  163.    void           removeAt                    (ICursor const&);
  164.  
  165.    INumber        removeAll                   (IBoolean (*property)
  166.                                                (Element const&, void*),
  167.                                                void* additionalArgument = 0);
  168.  
  169.    void           replaceAt                   (ICursor const&,
  170.                                                Element const&);
  171.  
  172.    void           removeAll                   ();
  173.  
  174.    IBoolean       isBounded                   () const;
  175.  
  176.    INumber        maxNumberOfElements         () const;
  177.  
  178.    INumber        numberOfElements            () const;
  179.  
  180.    IBoolean       isEmpty                     () const;
  181.  
  182.    IBoolean       isFull                      () const;
  183.  
  184.    ICursor*       newCursor                   () const;
  185.  
  186.    IBoolean       setToFirst                  (ICursor&) const;
  187.  
  188.    IBoolean       setToNext                   (ICursor&) const;
  189.  
  190.    IBoolean       allElementsDo               (IBoolean (*function)
  191.                                                   (Element&, void*),
  192.                                                void* additionalArgument = 0);
  193.  
  194.    IBoolean       allElementsDo               (IIterator <Element>&);
  195.  
  196.    IBoolean       allElementsDo               (IBoolean (*function)
  197.                                                (Element const&, void*),
  198.                                                void* additionalArgument = 0)
  199.                                                const;
  200.  
  201.    IBoolean       allElementsDo               (IConstantIterator
  202.                                                   <Element>&) const;
  203.  
  204.    IBoolean       isConsistent                () const;
  205.  
  206.    Key const&     key                         (Element const&) const;
  207.  
  208.    IBoolean       containsElementWithKey      (Key const&) const;
  209.  
  210.    IBoolean       containsAllKeysFrom         (IGHashKeyBag < Element, Key, ElementOps > const&) const;
  211.  
  212.    IBoolean       locateElementWithKey        (Key const&, ICursor&)
  213.                                                const;
  214.  
  215.    IBoolean       replaceElementWithKey       (Element const&);
  216.  
  217.    IBoolean       replaceElementWithKey       (Element const&,
  218.                                                ICursor&);
  219.  
  220.    IBoolean       locateOrAddElementWithKey   (Element const&);
  221.  
  222.    IBoolean       locateOrAddElementWithKey   (Element const&,
  223.                                                ICursor&);
  224.  
  225.    IBoolean       addOrReplaceElementWithKey  (Element const&);
  226.  
  227.    IBoolean       addOrReplaceElementWithKey  (Element const&,
  228.                                                ICursor&);
  229.  
  230.    IBoolean       removeElementWithKey        (Key const&);
  231.  
  232.    Element const& elementWithKey              (Key const&) const;
  233.  
  234.    Element&       elementWithKey              (Key const&);
  235.  
  236.    INumber        numberOfElementsWithKey     (Key const&) const;
  237.  
  238.    IBoolean       locateNextElementWithKey    (Key const&,
  239.                                                ICursor&) const;
  240.  
  241.    INumber        removeAllElementsWithKey    (Key const&);
  242.  
  243.    INumber        numberOfDifferentKeys       () const;
  244.  
  245.    IBoolean       setToNextWithDifferentKey   (ICursor&) const;
  246.  
  247. };
  248.  
  249. template < class Element, class Key >
  250. class IHashKeyBag :
  251.   public IGHashKeyBag < Element, Key, IKEHOps < Element, Key > > {
  252. public:
  253.   IHashKeyBag (INumber n = 100) :
  254.     IGHashKeyBag < Element, Key, IKEHOps < Element, Key > > (n) {}
  255. };
  256.  
  257. #include <ihshkb.if>
  258.  
  259. #ifdef __IBMCPP__
  260. #ifndef __TEMPINC__
  261. #include <ihshkb.c>
  262. #endif
  263. #endif
  264.  
  265. #endif
  266.