Leaked source code of windows server 2003
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.

261 lines
5.4 KiB

  1. /*++
  2. FHash.h
  3. This file contains a template class for a hash table.
  4. The template has two arguments, the type of the Data Elements and the
  5. type of the Key.
  6. The Data type must support the following :
  7. class Data {
  8. Data* m_pNext ;
  9. KEYREF GetKey( ) ;
  10. int MatchKey( KEYREF otherkey) ; /* NOTE : MatchKey returns non-zero on equality
  11. } ;
  12. DWORD (* m_pfnHash)( KEYREF k ) ;
  13. --*/
  14. #ifndef _FHASHEX_H_
  15. #define _FHASHEX_H_
  16. //------------------------------------------------------------
  17. template< class Data, /* This is the item that resides in the hashtable */
  18. class Key, /* This is the type of the Key */
  19. class KEYREF /* This is the type used to point or reference items in the cache*/
  20. >
  21. class TFHashEx {
  22. //
  23. // This class defines a Hash table which can grow dynamically to
  24. // accomodate insertions into the table. The table only grows, and
  25. // does not shrink.
  26. //
  27. public :
  28. //
  29. // Define the subtypes we need !
  30. //
  31. //
  32. // This is a member pointer to a pointer to Data -
  33. // i.e. this is the offset in the class Data where we
  34. // will hold the next pointers for our hash buckets !
  35. //
  36. typedef Data* Data::*NEXTPTR ;
  37. //
  38. // This is a member function pointer to a function which
  39. // will retrieve the key we are to use !
  40. //
  41. typedef KEYREF (Data::*GETKEY)( ) ;
  42. //
  43. // This is a member function pointer of the type that will
  44. // compare keys for us !
  45. //
  46. typedef int (Data::*MATCHKEY)( KEYREF key ) ;
  47. private :
  48. //
  49. // An array of pointer to buckets.
  50. //
  51. Data** m_ppBucket ;
  52. //
  53. // Member Pointer - points to where the pointer is that
  54. // we should use for chaining buckets together !
  55. //
  56. NEXTPTR m_pNext ;
  57. //
  58. // Member Pointer - will get the key out of the object for us !
  59. //
  60. GETKEY m_pGetKey ;
  61. //
  62. // Member Pointer - will compare the key in the item for us !
  63. //
  64. MATCHKEY m_pMatchKey ;
  65. //
  66. // A counter that we use to determine when to grow the
  67. // hash table. Each time we grow the table we set this
  68. // to a large positive value, and decrement as we insert
  69. // elements. When this hits 0 its time to grow the table !
  70. //
  71. long m_cInserts ;
  72. //
  73. // The function we use to compute hash values.
  74. // (Provided by the Caller of Init())
  75. //
  76. DWORD (* m_pfnHash)( KEYREF k ) ;
  77. //
  78. // Number of Buckets used in index computation
  79. //
  80. int m_cBuckets ;
  81. //
  82. // Number of Buckets we are actually using
  83. // Assert( m_cBuckets >= m_cActiveBuckets ) always true.
  84. //
  85. int m_cActiveBuckets ;
  86. //
  87. // Number of Buckets we have allocated
  88. // Assert( m_cNumAlloced >= m_cActiveBuckets ) must
  89. // always be true.
  90. //
  91. int m_cNumAlloced ;
  92. //
  93. // The amount we should grow the hash table when we
  94. // decide to grow it.
  95. //
  96. int m_cIncrement ;
  97. //
  98. // The number of CBuckets we should allow in each
  99. // collision chain (on average).
  100. //
  101. int m_load ;
  102. //
  103. // The function we use to compute the
  104. // position of an element in the hash table given its
  105. // Hash Value.
  106. //
  107. DWORD
  108. ComputeIndex( DWORD dw ) ;
  109. public :
  110. TFHashEx( ) ;
  111. ~TFHashEx( ) ;
  112. BOOL
  113. Init( NEXTPTR pNext,
  114. int cInitial,
  115. int cIncrement,
  116. DWORD (* pfnHash)( KEYREF ),
  117. int,
  118. GETKEY,
  119. MATCHKEY
  120. ) ;
  121. //
  122. // Check that the hash table is in a valid state
  123. // if fCheckHash == TRUE we will walk all the buckets and check that
  124. // the data hashes to the correct value !
  125. //
  126. BOOL
  127. IsValid( BOOL fCheckHash = FALSE ) ;
  128. //
  129. // Insert a piece of Data into the Hash Table
  130. //
  131. Data*
  132. InsertDataHash( DWORD dw,
  133. Data& d
  134. ) ;
  135. //
  136. // Insert a piece of Data into the Hash Table
  137. // We take a pointer to the Data object.
  138. //
  139. Data*
  140. InsertDataHash( DWORD dw,
  141. Data* pd
  142. ) ;
  143. //
  144. // Insert a piece of Data into the Hash Table
  145. //
  146. Data*
  147. InsertData( Data& d ) ;
  148. //
  149. // Search for a given Key in the Hash Table - return a pointer
  150. // to the Data within our Bucket object
  151. //
  152. Data*
  153. SearchKeyHash( DWORD dw,
  154. KEYREF k
  155. ) ;
  156. //
  157. // Search for a given Key in the Hash Table - return a pointer
  158. // to the Data within our Bucket object
  159. //
  160. Data*
  161. SearchKey( KEYREF k ) ;
  162. //
  163. // Search for a given Key in the Hash Table and remove
  164. // the item if found. We will return a pointer to the item.
  165. //
  166. Data*
  167. DeleteData( KEYREF k,
  168. Data* pd = 0
  169. ) ;
  170. //
  171. // Insert the given block of data into the hash table.
  172. // We will make a copy of the Data Object and store it in one
  173. // of our bucket objects.
  174. //
  175. BOOL
  176. Insert( Data& d ) ;
  177. //
  178. // Insert the given block of data into the hash table.
  179. // We take a pointer to the Data Object and store it in one
  180. // of our bucket objects.
  181. //
  182. BOOL
  183. Insert( Data* pd ) ;
  184. //
  185. // Find the given key in the table and copy the Data object into
  186. // the out parameter 'd'
  187. //
  188. BOOL
  189. Search( KEYREF k,
  190. Data &d
  191. ) ;
  192. //
  193. // Delete the key and associated data from the table.
  194. //
  195. BOOL
  196. Delete( KEYREF k ) ;
  197. //
  198. // Discards any memory we have allocated - after this, you must
  199. // call Init() again!
  200. //
  201. void Clear( ) ;
  202. //
  203. // Removes all of the items in the hash table. Does not call "delete"
  204. // on them.
  205. //
  206. void Empty( ) ;
  207. //
  208. // Function to compute hash value of a key for callers
  209. // who don't keep track of the hash function
  210. //
  211. DWORD
  212. ComputeHash( KEYREF k ) ;
  213. } ;
  214. #include "fhashex.inl"
  215. #endif // _FHASHEX_H_