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.

252 lines
5.1 KiB

  1. //
  2. // ptrmap.h
  3. //
  4. #ifndef PTRMAP_H
  5. #define PTRMAP_H
  6. #ifdef __cplusplus
  7. #include "private.h"
  8. #define PM_HASHSIZE 31
  9. template<class TKey, class TPtr>
  10. class CPMEntry
  11. {
  12. public:
  13. TKey key;
  14. TPtr *ptr;
  15. CPMEntry<TKey, TPtr> *next;
  16. };
  17. template<class TKey, class TPtr>
  18. class CPtrMap
  19. {
  20. public:
  21. CPtrMap() { memset(_HashTbl, 0, sizeof(_HashTbl)); }
  22. ~CPtrMap();
  23. BOOL _Set(TKey key, TPtr *ptr);
  24. TPtr *_Find(TKey key);
  25. TPtr *_Remove(TKey key);
  26. BOOL _Remove(TPtr *ptr);
  27. BOOL _FindKey(TPtr *ptr, TKey *pkeyOut);
  28. private:
  29. UINT _HashFunc(TKey key) { return (UINT)((UINT_PTR)key % PM_HASHSIZE); }
  30. CPMEntry<TKey, TPtr> **_FindEntry(TKey key);
  31. CPMEntry<TKey, TPtr> *_HashTbl[PM_HASHSIZE];
  32. };
  33. //+---------------------------------------------------------------------------
  34. //
  35. // dtor
  36. //
  37. //----------------------------------------------------------------------------
  38. template<class TKey, class TPtr>
  39. CPtrMap<TKey, TPtr>::~CPtrMap()
  40. {
  41. CPMEntry<TKey, TPtr> *pe;
  42. CPMEntry<TKey, TPtr> *peTmp;
  43. int i;
  44. // free anything left in the hashtbl
  45. for (i=0; i<ARRAYSIZE(_HashTbl); i++)
  46. {
  47. pe = _HashTbl[i];
  48. while (pe != NULL)
  49. {
  50. peTmp = pe->next;
  51. cicMemFree(pe);
  52. pe = peTmp;
  53. }
  54. }
  55. }
  56. //+---------------------------------------------------------------------------
  57. //
  58. // _Set
  59. //
  60. //----------------------------------------------------------------------------
  61. template<class TKey, class TPtr>
  62. BOOL CPtrMap<TKey, TPtr>::_Set(TKey key, TPtr *ptr)
  63. {
  64. UINT uIndex;
  65. CPMEntry<TKey, TPtr> *pe;
  66. CPMEntry<TKey, TPtr> **ppe;
  67. BOOL fRet;
  68. fRet = TRUE;
  69. if (ppe = _FindEntry(key))
  70. {
  71. // already in hash tbl
  72. (*ppe)->ptr = ptr;
  73. }
  74. else if (pe = (CPMEntry<TKey, TPtr> *)cicMemAlloc(sizeof(CPMEntry<TKey, TPtr>)))
  75. {
  76. // new entry
  77. uIndex = _HashFunc(key);
  78. pe->key = key;
  79. pe->ptr = ptr;
  80. pe->next = _HashTbl[uIndex];
  81. _HashTbl[uIndex] = pe;
  82. }
  83. else
  84. {
  85. // out of memory
  86. fRet = FALSE;
  87. }
  88. return fRet;
  89. }
  90. //+---------------------------------------------------------------------------
  91. //
  92. // _Find
  93. //
  94. //----------------------------------------------------------------------------
  95. template<class TKey, class TPtr>
  96. TPtr *CPtrMap<TKey, TPtr>::_Find(TKey key)
  97. {
  98. CPMEntry<TKey, TPtr> **ppe;
  99. if (ppe = _FindEntry(key))
  100. {
  101. return (*ppe)->ptr;
  102. }
  103. return NULL;
  104. }
  105. //+---------------------------------------------------------------------------
  106. //
  107. // _Remove
  108. //
  109. //----------------------------------------------------------------------------
  110. template<class TKey, class TPtr>
  111. TPtr *CPtrMap<TKey, TPtr>::_Remove(TKey key)
  112. {
  113. CPMEntry<TKey, TPtr> *pe;
  114. CPMEntry<TKey, TPtr> **ppe;
  115. TPtr *ptr;
  116. if (ppe = _FindEntry(key))
  117. {
  118. pe = *ppe;
  119. ptr = pe->ptr;
  120. *ppe = pe->next;
  121. cicMemFree(pe);
  122. return ptr;
  123. }
  124. return NULL;
  125. }
  126. //+---------------------------------------------------------------------------
  127. //
  128. // _Remove
  129. //
  130. //----------------------------------------------------------------------------
  131. template<class TKey, class TPtr>
  132. BOOL CPtrMap<TKey, TPtr>::_Remove(TPtr *ptr)
  133. {
  134. int i;
  135. CPMEntry<TKey, TPtr> *pe;
  136. CPMEntry<TKey, TPtr> **ppe;
  137. for (i = 0; i < PM_HASHSIZE; i++)
  138. {
  139. ppe = &_HashTbl[i];
  140. while (*ppe)
  141. {
  142. if ((*ppe)->ptr == ptr)
  143. {
  144. pe = *ppe;
  145. *ppe = pe->next;
  146. cicMemFree(pe);
  147. }
  148. else
  149. {
  150. ppe = &(*ppe)->next;
  151. }
  152. }
  153. }
  154. return TRUE;
  155. }
  156. //+---------------------------------------------------------------------------
  157. //
  158. // _FindKey
  159. //
  160. //----------------------------------------------------------------------------
  161. template<class TKey, class TPtr>
  162. BOOL CPtrMap<TKey, TPtr>::_FindKey(TPtr *ptr, TKey *ptkeyOut)
  163. {
  164. int i;
  165. CPMEntry<TKey, TPtr> **ppe;
  166. *ptkeyOut = NULL;
  167. for (i = 0; i < PM_HASHSIZE; i++)
  168. {
  169. ppe = &_HashTbl[i];
  170. while (*ppe)
  171. {
  172. if ((*ppe)->ptr == ptr)
  173. {
  174. *ptkeyOut = (*ppe)->key;
  175. return TRUE;
  176. }
  177. else
  178. {
  179. ppe = &(*ppe)->next;
  180. }
  181. }
  182. }
  183. return FALSE;
  184. }
  185. //+---------------------------------------------------------------------------
  186. //
  187. // _FindEntry
  188. //
  189. //----------------------------------------------------------------------------
  190. template<class TKey, class TPtr>
  191. CPMEntry<TKey, TPtr> **CPtrMap<TKey, TPtr>::_FindEntry(TKey key)
  192. {
  193. CPMEntry<TKey, TPtr> **ppe;
  194. ppe = &_HashTbl[_HashFunc(key)];
  195. while (*ppe)
  196. {
  197. if ((*ppe)->key == key)
  198. {
  199. return ppe;
  200. }
  201. ppe = &(*ppe)->next;
  202. }
  203. return NULL;
  204. }
  205. #endif // __cplusplus
  206. #endif // PTRMAP_H