home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / BOCOLE.PAK / HASHTBL.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  2.2 KB  |  90 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectComponents
  3. // Copyright (c) 1991, 1996 by Borland International, All Rights Reserved
  4. //
  5. // $Revision:   2.2  $
  6. //----------------------------------------------------------------------------
  7. #ifndef _HASHTBL_H
  8. #define _HASHTBL_H
  9.  
  10. #include <bole.h>
  11.  
  12. //------------------------------ IHashable -------------------------
  13. // An ABC for objects which can be linked into a hash table
  14.  
  15. typedef unsigned int HashIndex;
  16.  
  17. template <class T> class _ICLASS IHashable : public IUnknown {
  18. public:
  19.     virtual HashIndex Hash(HashIndex modulo) const = 0;
  20.     virtual BOOL operator==(const T *) const =0;
  21.     virtual BOOL operator!=(const T *) const =0;
  22.     virtual IHashable<T> *&Next() = 0;
  23. };
  24.  
  25.  
  26. template <class T, HashIndex SIZE> class    _ICLASS HashTable {
  27. protected:
  28.     IHashable<T> *    aBuckets[SIZE];
  29.     IHashable<T> *&    Insert(IHashable<T> *&rpFound, IHashable<T> *pKey);
  30.     IHashable<T> *&    Delete(IHashable<T> *&rpFound);
  31.     IHashable<T> *&    Find(IHashable<T> *hashKey);
  32.  
  33. public:
  34.     ~HashTable();
  35.     // Add  returns existing instance or adds
  36.     BOOL    Add(IHashable<T> **pPat);
  37.     // Remove takes key out of HashTable
  38.     void    Remove(IHashable<T> *pKey);
  39. };
  40.  
  41.  
  42. template <class T, HashIndex SIZE> IHashable<T> *& HashTable<T, SIZE>::
  43.     Insert(IHashable<T> *&rpFound, IHashable<T> *pKey)
  44. {
  45.     pKey->Next()=rpFound,
  46.     pKey->AddRef(),
  47.     rpFound=pKey;
  48.     return rpFound;
  49. }
  50.  
  51. template <class T, HashIndex SIZE> IHashable<T> *& HashTable<T, SIZE>::
  52.     Delete(IHashable<T> *&rpFound)
  53. {
  54.     assert(rpFound != 0L);
  55.     IHashable<T> **ppFoundNext = &rpFound->Next();
  56.     IHashable<T> *pFoundNext = *ppFoundNext;
  57.     *ppFoundNext=0L;
  58.     rpFound->Release();
  59.     return rpFound = pFoundNext;
  60. }
  61.  
  62. template <class T, HashIndex SIZE>
  63. BOOL    HashTable<T, SIZE>::Add(IHashable<T> **ppPat)
  64. {
  65.     IHashable<T> *&pSrc = Find(*ppPat);
  66.     if (pSrc) {
  67.         (*ppPat)->Release();
  68.         *ppPat = pSrc;
  69.     }
  70.     else
  71.         Insert(pSrc, *ppPat) ;
  72.     
  73.     (*ppPat) ->AddRef();
  74.     return TRUE;
  75. }
  76.  
  77. //    (pSrc) ?
  78. //        pPat->Release(),
  79. //            pPat = pSrc :
  80. //        Insert(pSrc, pPat) ;
  81.  
  82. template <class T, HashIndex SIZE>
  83. void    HashTable<T, SIZE>::Remove(IHashable<T> *pKey)
  84. {
  85.     IHashable<T> *&pFound =Find(pKey);
  86.     pFound ? Delete(pFound) : 0L;
  87. }
  88.  
  89. #endif
  90.