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.

245 lines
4.9 KiB

  1. #ifndef __HASHTABLE_CPP
  2. #define __HASHTABLE_CPP
  3. /*
  4. * Class:
  5. *
  6. * WmiAllocator
  7. *
  8. * Description:
  9. *
  10. * Provides abstraction above heap allocation functions
  11. *
  12. * Version:
  13. *
  14. * Initial
  15. *
  16. * Last Changed:
  17. *
  18. * See Source Depot for change history
  19. *
  20. */
  21. #if 0
  22. #include <precomp.h>
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <HashTable.h>
  26. #endif
  27. /******************************************************************************
  28. *
  29. * Name:
  30. *
  31. *
  32. * Description:
  33. *
  34. *
  35. *****************************************************************************/
  36. template <class WmiKey,class WmiElement,ULONG HashSize>
  37. WmiHashTable <WmiKey,WmiElement,HashSize> :: WmiHashTable (
  38. WmiAllocator &a_Allocator
  39. ) : m_Allocator ( a_Allocator ) , m_Buckets ( NULL )
  40. {
  41. }
  42. /******************************************************************************
  43. *
  44. * Name:
  45. *
  46. *
  47. * Description:
  48. *
  49. *
  50. *****************************************************************************/
  51. template <class WmiKey,class WmiElement,ULONG HashSize>
  52. WmiHashTable <WmiKey,WmiElement,HashSize> :: ~WmiHashTable ()
  53. {
  54. WmiStatusCode t_StatusCode = UnInitialize () ;
  55. }
  56. /******************************************************************************
  57. *
  58. * Name:
  59. *
  60. *
  61. * Description:
  62. *
  63. *
  64. *****************************************************************************/
  65. template <class WmiKey,class WmiElement,ULONG HashSize>
  66. WmiStatusCode WmiHashTable <WmiKey,WmiElement,HashSize> :: Initialize ()
  67. {
  68. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  69. if ( ! m_Buckets )
  70. {
  71. t_StatusCode = m_Allocator.New (
  72. ( void ** ) & m_Buckets ,
  73. sizeof ( WmiBasicTree <WmiKey,WmiElement> ) * HashSize
  74. ) ;
  75. if ( t_StatusCode == e_StatusCode_Success )
  76. {
  77. for ( ULONG t_Index = 0 ; t_Index < HashSize ; t_Index ++ )
  78. {
  79. :: new ( ( void * ) & m_Buckets [ t_Index ] ) WmiBasicTree <WmiKey,WmiElement> ( m_Allocator ) ;
  80. }
  81. }
  82. else
  83. {
  84. m_Buckets = NULL ;
  85. }
  86. }
  87. return t_StatusCode ;
  88. }
  89. /******************************************************************************
  90. *
  91. * Name:
  92. *
  93. *
  94. * Description:
  95. *
  96. *
  97. *****************************************************************************/
  98. template <class WmiKey,class WmiElement,ULONG HashSize>
  99. WmiStatusCode WmiHashTable <WmiKey,WmiElement,HashSize> :: UnInitialize ()
  100. {
  101. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  102. if ( m_Buckets )
  103. {
  104. for ( ULONG t_Index = 0 ; t_Index < HashSize ; t_Index ++ )
  105. {
  106. m_Buckets [ t_Index ].WmiBasicTree <WmiKey,WmiElement> :: ~WmiBasicTree <WmiKey,WmiElement> () ;
  107. }
  108. t_StatusCode = m_Allocator.Delete (
  109. ( void * ) m_Buckets
  110. ) ;
  111. m_Buckets = NULL;
  112. }
  113. return t_StatusCode ;
  114. }
  115. /******************************************************************************
  116. *
  117. * Name:
  118. *
  119. *
  120. * Description:
  121. *
  122. *
  123. *****************************************************************************/
  124. template <class WmiKey,class WmiElement,ULONG HashSize>
  125. WmiStatusCode WmiHashTable <WmiKey,WmiElement,HashSize> :: Insert (
  126. const WmiKey &a_Key ,
  127. const WmiElement &a_Element
  128. )
  129. {
  130. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  131. if ( m_Buckets )
  132. {
  133. ULONG t_Hash = Hash ( a_Key ) % HashSize ;
  134. WmiBasicTree <WmiKey,WmiElement> *t_Tree = &m_Buckets [ t_Hash ] ;
  135. WmiBasicTree <WmiKey,WmiElement> :: Iterator t_Iterator ;
  136. t_StatusCode = t_Tree->Insert ( a_Key , a_Element ,t_Iterator ) ;
  137. }
  138. else
  139. {
  140. t_StatusCode = e_StatusCode_NotInitialized ;
  141. }
  142. return t_StatusCode ;
  143. }
  144. /******************************************************************************
  145. *
  146. * Name:
  147. *
  148. *
  149. * Description:
  150. *
  151. *
  152. *****************************************************************************/
  153. template <class WmiKey,class WmiElement,ULONG HashSize>
  154. WmiStatusCode WmiHashTable <WmiKey,WmiElement,HashSize> :: Delete (
  155. const WmiKey &a_Key
  156. )
  157. {
  158. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  159. if ( m_Buckets )
  160. {
  161. ULONG t_Hash = Hash ( a_Key ) % HashSize ;
  162. WmiBasicTree <WmiKey,WmiElement> *t_Tree = &m_Buckets [ t_Hash ] ;
  163. t_StatusCode = t_Tree->Delete ( a_Key ) ;
  164. }
  165. else
  166. {
  167. t_StatusCode = e_StatusCode_NotInitialized ;
  168. }
  169. return t_StatusCode ;
  170. }
  171. /******************************************************************************
  172. *
  173. * Name:
  174. *
  175. *
  176. * Description:
  177. *
  178. *
  179. *****************************************************************************/
  180. template <class WmiKey,class WmiElement,ULONG HashSize>
  181. WmiStatusCode WmiHashTable <WmiKey,WmiElement,HashSize> :: Find (
  182. const WmiKey &a_Key ,
  183. WmiElement &a_Element
  184. )
  185. {
  186. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  187. if ( m_Buckets )
  188. {
  189. ULONG t_Hash = Hash ( a_Key ) % HashSize ;
  190. WmiBasicTree <WmiKey,WmiElement> *t_Tree = &m_Buckets [ t_Hash ] ;
  191. WmiBasicTree <WmiKey,WmiElement> :: Iterator a_Iterator ;
  192. t_StatusCode = t_Tree->Find ( a_Key , a_Iterator ) ;
  193. a_Element = a_Iterator.GetElement () ;
  194. }
  195. else
  196. {
  197. t_StatusCode = e_StatusCode_NotInitialized ;
  198. }
  199. return t_StatusCode ;
  200. }
  201. #endif __HASHTABLE_CPP