home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c221 / 6.ddi / MWHC.006 / P2 < prev    next >
Encoding:
Text File  |  1992-06-07  |  3.2 KB  |  112 lines

  1. #ifndef __RWTVHSET_H__
  2. #define __RWTVHSET_H__
  3. pragma push_align_members(64);
  4.  
  5. /*
  6.  * RWTValHashSet<T>:  A Set of values of type T, using a hashed lookup
  7.  *
  8.  * $Header:   E:/vcs/rw/tvhset.h_v   1.2   17 Mar 1992 12:00:44   KEFFER  $
  9.  *
  10.  ****************************************************************************
  11.  *
  12.  * Rogue Wave Software, Inc.
  13.  * P.O. Box 2328
  14.  * Corvallis, OR 97339
  15.  *
  16.  * Copyright (C) 1992. This software is subject to copyright 
  17.  * protection under the laws of the United States and other countries.
  18.  *
  19.  ***************************************************************************
  20.  *
  21.  * This class implements a parameterized Set of types T.  In a Set,
  22.  * only one instance of an object of a given value can be inserted into
  23.  * the collection.
  24.  *
  25.  * The implementation uses a hash table.
  26.  *
  27.  * Example use of this class:
  28.  *
  29.  *   #include <rw/cstring.h>
  30.  *   #include <rw/tvhset.h>
  31.  *   
  32.  *   unsigned myHash(const RWCString& s){ return s.hash(); }
  33.  *   
  34.  *   RWTValHashSet<RWCString> set(myHash);    // A Set of RWCStrings
  35.  *   
  36.  *   set.insert("a string");    // Type conversion: char* to RWCString happens
  37.  *   set.insert("another string");
  38.  *   set.insert("a string");    // Rejected (already exists in collection)
  39.  *   set.contains("a string");    // Returns true.
  40.  *
  41.  *
  42.  * Note that the constructor for RWTValHashSet<T> takes a function with
  43.  * prototype
  44.  *
  45.  *   unsigned hashFun(const T&);
  46.  *
  47.  * It should return a suitable hashing value for an instance of class T.
  48.  * Usually, the definition for such a function is trivial because hashing
  49.  * functions have been defined for all Rogue Wave supplied classes.
  50.  *
  51.  ***************************************************************************
  52.  *
  53.  * $Log:   E:/vcs/rw/tvhset.h_v  $
  54.  * 
  55.  *    Rev 1.2   17 Mar 1992 12:00:44   KEFFER
  56.  * 
  57.  *    Rev 1.1   04 Mar 1992 10:16:46   KEFFER
  58.  * 
  59.  *    Rev 1.0   02 Mar 1992 16:10:54   KEFFER
  60.  * Initial revision.
  61.  */
  62.  
  63. //$DECLARE_TEMPLATE
  64.  
  65. #include "rw/tvhasht.h"
  66.  
  67. /****************************************************************
  68.  *                                *
  69.  *        Declarations for RWTValHashSet<T>        *
  70.  *                                *
  71.  ****************************************************************/
  72.  
  73. template <class T> class RWExport RWTValHashSet : public RWTValHashTable<T> {
  74.  
  75. public:
  76.  
  77.   RWTValHashSet(unsigned (*hashFun)(const T&), unsigned size = RWDEFAULT_CAPACITY) :
  78.     RWTValHashTable<T>(hashFun, size) { }
  79.  
  80.   // Member functions:
  81.   void            insert(T val);
  82.   unsigned        occurrencesOf(T val) const;
  83. };
  84.  
  85. //$IMPLEMENT_TEMPLATE
  86.  
  87. /****************************************************************
  88.  *                                *
  89.  *        Definitions for RWTValHashSet<T>        *
  90.  *                                *
  91.  ****************************************************************/
  92.  
  93. template <class T> void
  94. RWTValHashSet<T>::insert(T val)
  95. {
  96.   int idx = (int)hashIndex(val);
  97.   if (_table(idx)==rwnil) _table(idx) = new CHAIN;
  98.   else if (_table(idx)->contains(val)) return;
  99.   _table(idx)->insert(val);
  100.   _nitems++;
  101. }
  102.  
  103. template <class T> unsigned
  104. RWTValHashSet<T>::occurrencesOf(T val) const
  105. {
  106.   // This works because only one instance is allowed ---
  107.   return contains(val) ? 1 : 0;
  108. }
  109.  
  110. pragma pop_align_members();
  111. #endif    /* __RWTVHSET_H__ */
  112.