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.

304 lines
8.5 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: scmhash.hxx
  7. //
  8. // Contents: Class definitions used for SCM hash table.
  9. //
  10. // History: 20-Jan-93 Ricksa Created from map_kv.h
  11. // 04-Oct-00 JohnDoty Changed memory allocation to use
  12. // PrivAlloc
  13. //
  14. // Notes: The reason for creating this file rather than using the
  15. // original class is that the SCM has different memory allocation
  16. // needs depending on whether it is built for Win95 or NT.
  17. //
  18. // JohnDoty:
  19. // Of course, that's not strictly true, since we aren't
  20. // ever built for Win9x anymore...
  21. //
  22. //--------------------------------------------------------------------------
  23. #ifndef __SCMHASH_HXX__
  24. #define __SCMHASH_HXX__
  25. // Forward declaration
  26. class CScmHashIter;
  27. //+-------------------------------------------------------------------------
  28. //
  29. // Class: CScmHashEntry (she)
  30. //
  31. // Purpose: Base of hash table entries
  32. //
  33. // Interface: IsEqual - tells whether entry is equal to input key
  34. // GetNext - next next entry after this one
  35. // SetNext - set the next pointer for this etnry
  36. //
  37. // History: 20-Jan-95 Ricksa Created
  38. //
  39. // Notes:
  40. //
  41. //--------------------------------------------------------------------------
  42. class CScmHashEntry : public CPrivAlloc
  43. {
  44. public:
  45. CScmHashEntry(void);
  46. virtual ~CScmHashEntry(void);
  47. virtual BOOL IsEqual(LPVOID pKey, UINT cbKey) = 0;
  48. CScmHashEntry * GetNext(void);
  49. void SetNext(CScmHashEntry *);
  50. private:
  51. // Points to next hash entry if there is one
  52. // for the hash bucket.
  53. CScmHashEntry * _sheNext;
  54. };
  55. //+-------------------------------------------------------------------------
  56. //
  57. // Member: CScmHashEntry::CScmHashEntry
  58. //
  59. // Synopsis: Initalize base of hash entry
  60. //
  61. // History: 20-Jan-95 Ricksa Created
  62. //
  63. //--------------------------------------------------------------------------
  64. inline CScmHashEntry::CScmHashEntry(void) : _sheNext(NULL)
  65. {
  66. // Header does all the work
  67. }
  68. //+-------------------------------------------------------------------------
  69. //
  70. // Member: CScmHashEntry::GetNext
  71. //
  72. // Synopsis: Get next entry in the collision list
  73. //
  74. // History: 20-Jan-95 Ricksa Created
  75. //
  76. //--------------------------------------------------------------------------
  77. inline CScmHashEntry *CScmHashEntry::GetNext(void)
  78. {
  79. return _sheNext;
  80. }
  81. //+-------------------------------------------------------------------------
  82. //
  83. // Member: CScmHashEntry::SetNext
  84. //
  85. // Synopsis: Set the next entry in the collision list.
  86. //
  87. // History: 20-Jan-95 Ricksa Created
  88. //
  89. //--------------------------------------------------------------------------
  90. inline void CScmHashEntry::SetNext(CScmHashEntry *sheNew)
  91. {
  92. _sheNext = sheNew;
  93. }
  94. //+-------------------------------------------------------------------------
  95. //
  96. // Class: CScmHashTable (sht)
  97. //
  98. // Purpose: Hash table class
  99. //
  100. // Interface: InitOK - whether initialization succeeded
  101. // GetCount - Get count of items in the list
  102. // IsBucketEmpty - get whether collision list is empty
  103. // GetBucketList - get collision list for entry
  104. // Lookup - lookup an entry in the hash table
  105. // SetAt - add entry to hash table
  106. // RemoveEntry - remove entry from the hash table
  107. //
  108. // History: 20-Jan-95 Ricksa Created
  109. //
  110. // Notes:
  111. //
  112. //--------------------------------------------------------------------------
  113. class CScmHashTable : public CPrivAlloc
  114. {
  115. public:
  116. CScmHashTable(DWORD nHashSize = 17);
  117. ~CScmHashTable();
  118. // Tell whether object got correctly initialized
  119. BOOL InitOK(void) const;
  120. // Get count of entries in the table.
  121. DWORD GetCount(void) const;
  122. // Reports whether a particular buck is empty
  123. BOOL IsBucketEmpty(DWORD dwHash) const;
  124. // Gets list associated with the bucket. This
  125. // is used if there is some special search of the
  126. // list required.
  127. CScmHashEntry * GetBucketList(DWORD dwHash) const;
  128. // Lookup - return pointer to entry if found
  129. CScmHashEntry * Lookup(
  130. DWORD dwHash,
  131. LPVOID pKey,
  132. UINT cbKey) const;
  133. // Add new entry
  134. void SetAt(
  135. DWORD dwHash,
  136. CScmHashEntry *psheToAdd);
  137. // removing existing entry
  138. BOOL RemoveEntry(
  139. DWORD dwHash,
  140. CScmHashEntry *psheToRemove);
  141. private:
  142. friend CScmHashIter;
  143. CScmHashEntry ** _apsheHashTable;
  144. DWORD _ndwHashTableSize;
  145. DWORD _ndwCount;
  146. };
  147. //+-------------------------------------------------------------------------
  148. //
  149. // Member: CScmHashTable::CScmHashTable
  150. //
  151. // Synopsis: Create an empty hash table
  152. //
  153. // History: 20-Jan-95 Ricksa Created
  154. //
  155. //--------------------------------------------------------------------------
  156. inline CScmHashTable::CScmHashTable(DWORD nHashSize)
  157. : _ndwHashTableSize(nHashSize), _ndwCount(0)
  158. {
  159. DWORD dwSize = nHashSize * sizeof(CScmHashEntry *);
  160. _apsheHashTable = (CScmHashEntry **) PrivMemAlloc(dwSize);
  161. if (_apsheHashTable != NULL)
  162. {
  163. memset(_apsheHashTable, 0, dwSize);
  164. }
  165. }
  166. //+-------------------------------------------------------------------------
  167. //
  168. // Member: CScmHashTable::InitOK
  169. //
  170. // Synopsis: Determine whether the constructor succeeded
  171. //
  172. // History: 20-Jan-95 Ricksa Created
  173. //
  174. //--------------------------------------------------------------------------
  175. inline BOOL CScmHashTable::InitOK(void) const
  176. {
  177. return _apsheHashTable != NULL;
  178. }
  179. //+-------------------------------------------------------------------------
  180. //
  181. // Member: CScmHashTable::GetCount
  182. //
  183. // Synopsis: Get the number of entries in the table
  184. //
  185. // History: 20-Jan-95 Ricksa Created
  186. //
  187. //--------------------------------------------------------------------------
  188. inline DWORD CScmHashTable::GetCount(void) const
  189. {
  190. return _ndwCount;
  191. }
  192. //+-------------------------------------------------------------------------
  193. //
  194. // Member: CScmHashTable::IsBucketEmpty
  195. //
  196. // Synopsis: Determine whether a particular hash bucket is empty
  197. //
  198. // History: 20-Jan-95 Ricksa Created
  199. //
  200. //--------------------------------------------------------------------------
  201. inline BOOL CScmHashTable::IsBucketEmpty(DWORD dwHash) const
  202. {
  203. return _apsheHashTable[dwHash] == NULL;
  204. }
  205. //+-------------------------------------------------------------------------
  206. //
  207. // Member: CScmHashTable::GetBucketList
  208. //
  209. // Synopsis: Get a list of entries associated with a particular hash
  210. //
  211. // History: 20-Jan-95 Ricksa Created
  212. //
  213. //--------------------------------------------------------------------------
  214. inline CScmHashEntry *CScmHashTable::GetBucketList(DWORD dwHash) const
  215. {
  216. return _apsheHashTable[dwHash];
  217. }
  218. //+-------------------------------------------------------------------------
  219. //
  220. // Class: CScmHashIter (shi)
  221. //
  222. // Purpose: Iterate through list of hash entries sequentially
  223. //
  224. // Interface: GetNext - get next entry in the list
  225. //
  226. // History: 20-Jan-95 Ricksa Created
  227. //
  228. // Notes:
  229. //
  230. //--------------------------------------------------------------------------
  231. class CScmHashIter
  232. {
  233. public:
  234. CScmHashIter(CScmHashTable *psht);
  235. CScmHashEntry * GetNext(void);
  236. private:
  237. CScmHashEntry * FindNextBucketWithEntry(void);
  238. CScmHashTable * _psht;
  239. DWORD _dwBucket;
  240. CScmHashEntry * _psheNext;
  241. };
  242. //+-------------------------------------------------------------------------
  243. //
  244. // Member: CScmHashIter::CScmHashIter
  245. //
  246. // Synopsis: Initialize iteration
  247. //
  248. // History: 20-Jan-95 Ricksa Created
  249. //
  250. //--------------------------------------------------------------------------
  251. inline CScmHashIter::CScmHashIter(CScmHashTable *psht)
  252. : _psht(psht), _dwBucket(0), _psheNext(NULL)
  253. {
  254. FindNextBucketWithEntry();
  255. }
  256. #endif // __SCMHASH_HXX__