Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
4.1 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name :
  4. hashtab.hxx
  5. Abstract:
  6. Declares a stand alone multiple use hash table.
  7. Author:
  8. Murali R. Krishnan ( MuraliK ) 02-Oct-1996
  9. Environment:
  10. Win32 - User Mode
  11. Project:
  12. Internet Server DLL
  13. Revision History:
  14. --*/
  15. # ifndef _HASHTAB_HXX_
  16. # define _HASHTAB_HXX_
  17. /************************************************************
  18. * Include Headers
  19. ************************************************************/
  20. # include <windows.h>
  21. # if !defined(dllexp)
  22. # define dllexp __declspec( dllexport)
  23. # endif
  24. /************************************************************
  25. * Type Definitions
  26. ************************************************************/
  27. /*++
  28. HT_ELEMENT
  29. o base class for hashable elements.
  30. All the hash table elements should be derived from this base object
  31. and should define the abstract virtual functions.
  32. The data that the hash table elements should maintain include:
  33. the key for hashing (LPCSTR), key length,
  34. reference count -- usually a LONG value
  35. + any additional data specific to the object.
  36. --*/
  37. class dllexp HT_ELEMENT {
  38. public:
  39. virtual ~HT_ELEMENT( VOID) {}
  40. virtual LPCSTR QueryKey(VOID) const = 0;
  41. virtual DWORD QueryKeyLen(VOID) const = 0;
  42. virtual LONG Reference( VOID) = 0;
  43. virtual LONG Dereference( VOID) = 0;
  44. virtual BOOL IsMatch( IN LPCSTR pszKey, IN DWORD cchKey) const = 0;
  45. virtual VOID Print( VOID) const = 0;
  46. }; // class HT_ELEMENT()
  47. // forward declaration
  48. class dllexp HASH_TABLE_BUCKET;
  49. /*++
  50. HT_ITERATOR - is used for iterating across the elements stored
  51. in the hash table. The structure contains state information to
  52. control the iteration. The user of the iterator should not
  53. modify any data member here.
  54. --*/
  55. struct HT_ITERATOR {
  56. PVOID nChunkId; // pointer for the bucket chunk
  57. DWORD nBucketNumber;
  58. DWORD nPos;
  59. };
  60. /*++
  61. HASH_TABLE
  62. o reusable hash table object - which contains state information
  63. for each instance of the table.
  64. Only LPCSTR keys are supported. Complex keys are not supported now.
  65. The object is templatable using C++, but not done for now.
  66. --*/
  67. class dllexp HASH_TABLE {
  68. public:
  69. HASH_TABLE( IN DWORD nBuckets,
  70. IN LPCSTR pszIdentifier,
  71. IN DWORD dwHashTableFlags
  72. );
  73. virtual ~HASH_TABLE(VOID) { Cleanup(); }
  74. virtual
  75. DWORD CalculateHash( IN LPCSTR pszKey, IN DWORD cchKey) const;
  76. virtual
  77. DWORD CalculateHash( IN LPCSTR pszKey) const
  78. { return ( CalculateHash( pszKey, strlen( pszKey))); }
  79. VOID Cleanup( VOID);
  80. BOOL IsValid( VOID) const { return ( NULL != m_prgBuckets); }
  81. HT_ELEMENT * Lookup( IN LPCSTR pszKey, IN DWORD cchKey);
  82. HT_ELEMENT * Lookup( IN LPCSTR pszKey)
  83. { return ( Lookup( pszKey, strlen(pszKey))); }
  84. BOOL Insert( HT_ELEMENT * phte,
  85. IN BOOL fCheckBeforeInsert = TRUE
  86. );
  87. BOOL Delete( HT_ELEMENT * phte);
  88. DWORD FlushElements(VOID) { return ( ERROR_CALL_NOT_IMPLEMENTED);}
  89. DWORD InitializeIterator( IN HT_ITERATOR * phti);
  90. DWORD FindNextElement( IN HT_ITERATOR * phti,
  91. OUT HT_ELEMENT ** pphte);
  92. DWORD CloseIterator( IN HT_ITERATOR * phti);
  93. VOID Print( IN DWORD level = 0);
  94. private:
  95. DWORD m_nBuckets;
  96. HASH_TABLE_BUCKET * m_prgBuckets;
  97. // Statistics about Hash table usage
  98. DWORD m_nLookups;
  99. DWORD m_nHits;
  100. DWORD m_nInserts;
  101. DWORD m_nEntries;
  102. DWORD m_nFlushes;
  103. // Identification and Control Information
  104. DWORD m_dwFlags;
  105. CHAR m_rgchId[8];
  106. };
  107. typedef HASH_TABLE * PHASH_TABLE;
  108. inline VOID
  109. DerefAndKillElement( IN HT_ELEMENT * phte)
  110. {
  111. // if we are the last user, kill the object.
  112. // if not the last user who does a deref has to kill the object
  113. if ( phte->Dereference() == 0) {
  114. // we are the last user. kill the object
  115. delete phte;
  116. }
  117. } // DerefAndKillElement()
  118. # endif // _HASHTAB_HXX_
  119. /************************ End of File ***********************/