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.

245 lines
4.1 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1994.
  5. //
  6. // File: strhash.hxx
  7. //
  8. // Contents: A hash table for strings
  9. //
  10. // History: 7-Nov-94 BruceFo Created
  11. //
  12. //----------------------------------------------------------------------------
  13. #ifndef __STRHASH_HXX__
  14. #define __STRHASH_HXX__
  15. //////////////////////////////////////////////////////////////////////////////
  16. #define HASH_DEFAULT_NUM_BUCKETS 47
  17. //////////////////////////////////////////////////////////////////////////////
  18. // Forward definitions
  19. class CStrHashBucketElem;
  20. class CStrHashBucket;
  21. class CStrHashTable;
  22. //////////////////////////////////////////////////////////////////////////////
  23. // Class definitions
  24. #define ITERATE_END ((DWORD)-1)
  25. class CIterateData
  26. {
  27. friend class CStrHashTable;
  28. public:
  29. CIterateData(
  30. VOID
  31. )
  32. :
  33. m_pCurrentElem(NULL),
  34. m_CurrentBucket(0)
  35. {
  36. }
  37. private:
  38. CStrHashBucketElem* m_pCurrentElem;
  39. DWORD m_CurrentBucket;
  40. };
  41. //////////////////////////////////////////////////////////////////////////////
  42. class CStrHashBucketElem
  43. {
  44. DECLARE_SIG;
  45. friend class CStrHashBucket;
  46. friend class CStrHashTable;
  47. public:
  48. CStrHashBucketElem(
  49. IN const WCHAR* pszKey
  50. );
  51. ~CStrHashBucketElem();
  52. BOOL
  53. IsEqual(
  54. IN const WCHAR* pszKey
  55. );
  56. #if DBG == 1
  57. VOID Print()
  58. {
  59. appAssert(NULL != m_pszKey);
  60. appDebugOut((DEB_TRACE,"%ws\n", m_pszKey));
  61. }
  62. #endif DBG == 1
  63. private:
  64. CStrHashBucketElem* m_next;
  65. const WCHAR* m_pszKey;
  66. };
  67. //////////////////////////////////////////////////////////////////////////////
  68. class CStrHashBucket
  69. {
  70. DECLARE_SIG;
  71. friend class CStrHashTable;
  72. public:
  73. CStrHashBucket(
  74. VOID
  75. );
  76. ~CStrHashBucket();
  77. HRESULT
  78. Insert(
  79. IN const WCHAR* pszKey
  80. );
  81. BOOL
  82. Remove(
  83. IN const WCHAR* pszKey
  84. );
  85. BOOL
  86. IsMember(
  87. IN const WCHAR* pszKey
  88. );
  89. #if DBG == 1
  90. VOID Print()
  91. {
  92. for (CStrHashBucketElem* x = m_head; NULL != x; x = x->m_next)
  93. {
  94. x->Print();
  95. }
  96. }
  97. #endif DBG == 1
  98. private:
  99. CStrHashBucketElem* m_head;
  100. };
  101. //////////////////////////////////////////////////////////////////////////////
  102. class CStrHashTable
  103. {
  104. DECLARE_SIG;
  105. public:
  106. CStrHashTable(
  107. IN DWORD cNumBuckets = HASH_DEFAULT_NUM_BUCKETS
  108. );
  109. // Returns the status of the object after construction: S_OK or an error
  110. // code.
  111. HRESULT
  112. QueryError(
  113. VOID
  114. );
  115. ~CStrHashTable();
  116. // Three basic operations: insert, remove, and check membership. The
  117. // strings may not be NULL.
  118. HRESULT
  119. Insert(
  120. IN const WCHAR* pszKey
  121. );
  122. HRESULT
  123. Remove(
  124. IN const WCHAR* pszKey
  125. );
  126. BOOL
  127. IsMember(
  128. IN const WCHAR* pszKey
  129. );
  130. DWORD
  131. Count(
  132. VOID
  133. );
  134. // Iteration routines
  135. CIterateData*
  136. IterateStart(
  137. VOID
  138. );
  139. VOID
  140. IterateGetNext(
  141. IN OUT CIterateData* pCurrent
  142. );
  143. VOID
  144. IterateEnd(
  145. IN CIterateData* pCurrent
  146. );
  147. BOOL
  148. IterateDone(
  149. IN CIterateData* pCurrent
  150. );
  151. const WCHAR*
  152. IterateGetData(
  153. IN OUT CIterateData* pCurrent
  154. );
  155. #if DBG == 1
  156. VOID Print()
  157. {
  158. appAssert(NULL != m_ht);
  159. for (DWORD i = 0; i < m_cNumBuckets; i++)
  160. {
  161. appDebugOut((DEB_TRACE,"======== bucket %d\n",i));
  162. m_ht[i].Print();
  163. }
  164. }
  165. #endif DBG == 1
  166. private:
  167. VOID
  168. IterateGetBucket(
  169. IN OUT CIterateData* pCurrent
  170. );
  171. DWORD
  172. HashFunction(
  173. IN const WCHAR* pszKey
  174. );
  175. HRESULT
  176. CheckRehash(
  177. VOID
  178. );
  179. HRESULT
  180. Rehash(
  181. IN DWORD cNumBuckets
  182. );
  183. CStrHashBucket* m_ht;
  184. DWORD m_cMinNumBuckets;
  185. DWORD m_cNumBuckets;
  186. DWORD m_cElems;
  187. };
  188. #endif // __STRHASH_HXX__