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.

184 lines
4.9 KiB

  1. #pragma once
  2. #include <tlist.h>
  3. //////////////////////////////////////////////////////////////////////////////
  4. // Hash table entry.
  5. //////////////////////////////////////////////////////////////////////////////
  6. template<class T, class V> class THashTableEntry
  7. {
  8. public:
  9. DWORD _dwSig;
  10. HRESULT _hr;
  11. T _tKey;
  12. V _vItem;
  13. DWORD _dwHash;
  14. THashTableEntry(T& tKey, V& vItem);
  15. ~THashTableEntry();
  16. };
  17. //-----------------------------------------------------------------------------
  18. // THashTableEntry ctor
  19. //-----------------------------------------------------------------------------
  20. template<class T, class V> THashTableEntry<T, V>::THashTableEntry(T& tKey, V& vItem)
  21. : _hr(S_OK), _dwSig('RTNE')
  22. {
  23. IF_FAILED_EXIT(_tKey.Assign(tKey));
  24. IF_FAILED_EXIT(_vItem.Assign(vItem));
  25. IF_FAILED_EXIT(_tKey.GetHash(&_dwHash, T::CaseSensitive));
  26. exit:
  27. return;
  28. }
  29. //-----------------------------------------------------------------------------
  30. // THashTableEntry dtor
  31. //-----------------------------------------------------------------------------
  32. template<class T, class V> THashTableEntry<T, V>::~THashTableEntry()
  33. { }
  34. //////////////////////////////////////////////////////////////////////////////
  35. // Hash table class
  36. //////////////////////////////////////////////////////////////////////////////
  37. template<class T, class V> class THashTable
  38. {
  39. public:
  40. THashTable();
  41. THashTable(DWORD nSlots);
  42. ~THashTable();
  43. HRESULT Init(DWORD nSlots);
  44. HRESULT Destruct();
  45. HRESULT Insert(T& tKey, V& vItem);
  46. HRESULT Retrieve(T& tKey, V** ppvItem);
  47. private:
  48. HRESULT _hr;
  49. DWORD _dwSig;
  50. DWORD _nSlots;
  51. TList<THashTableEntry<T, V> *> *_pListArray;
  52. };
  53. //-----------------------------------------------------------------------------
  54. // THashTable ctor
  55. //-----------------------------------------------------------------------------
  56. template<class T, class V> THashTable<T, V>::THashTable()
  57. : _hr(S_OK), _dwSig('HSAH'), _nSlots(0), _pListArray(NULL)
  58. {}
  59. //-----------------------------------------------------------------------------
  60. // THashTable ctor
  61. //-----------------------------------------------------------------------------
  62. template<class T, class V> THashTable<T, V>::THashTable(DWORD nSlots)
  63. : THashTable()
  64. {
  65. Init(nSlots);
  66. }
  67. //-----------------------------------------------------------------------------
  68. // THashTable dtor
  69. //-----------------------------------------------------------------------------
  70. template<class T, class V> THashTable<T, V>::~THashTable(void)
  71. {
  72. Destruct();
  73. SAFEDELETEARRAY(_pListArray);
  74. }
  75. //-----------------------------------------------------------------------------
  76. // THashTable::Init
  77. //-----------------------------------------------------------------------------
  78. template<class T, class V> HRESULT THashTable<T, V>::Init(DWORD nSlots)
  79. {
  80. _nSlots = nSlots;
  81. _pListArray = new TList<THashTableEntry<T, V> * > [_nSlots];
  82. IF_ALLOC_FAILED_EXIT((_pListArray));
  83. exit:
  84. return _hr;
  85. }
  86. //-----------------------------------------------------------------------------
  87. // THashTable::Insert
  88. //-----------------------------------------------------------------------------
  89. template<class T, class V> HRESULT THashTable<T, V>::Insert(T& tKey, V& vItem)
  90. {
  91. _hr = S_OK;
  92. THashTableEntry<T, V> *pEntry = new THashTableEntry<T, V>(tKey, vItem);
  93. IF_ALLOC_FAILED_EXIT(pEntry);
  94. IF_FALSE_EXIT((pEntry->_hr == S_OK), pEntry->_hr);
  95. IF_FAILED_EXIT(_pListArray[(pEntry->_dwHash) % _nSlots].Insert(pEntry));
  96. exit:
  97. return _hr;
  98. }
  99. //-----------------------------------------------------------------------------
  100. // THashTable::Retrieve
  101. //-----------------------------------------------------------------------------
  102. template<class T, class V> HRESULT THashTable<T, V>::Retrieve(T& tKey, V** ppvItem)
  103. {
  104. _hr = S_OK;
  105. DWORD dwHash = 0;
  106. BOOL bFound = FALSE;
  107. THashTableEntry<T, V> **ppEntry = 0;
  108. tKey.GetHash(&dwHash, T::CaseSensitive);
  109. TList_Iter<THashTableEntry<T, V> * > Iterator(_pListArray[dwHash % _nSlots]);
  110. while (ppEntry = Iterator.Next())
  111. {
  112. if (dwHash != ((*ppEntry)->_dwHash))
  113. continue;
  114. IF_FAILED_EXIT(tKey.CompareString((*ppEntry)->_tKey));
  115. if (_hr == S_OK)
  116. {
  117. bFound = TRUE;
  118. break;
  119. }
  120. }
  121. exit:
  122. *ppvItem = bFound ? &((*ppEntry)->_vItem) : NULL;
  123. _hr = bFound ? S_OK : S_FALSE;
  124. return _hr;
  125. }
  126. //-----------------------------------------------------------------------------
  127. // THashTable::Destruct
  128. //-----------------------------------------------------------------------------
  129. template<class T, class V> HRESULT THashTable<T, V>::Destruct()
  130. {
  131. for (DWORD i = 0; i < _nSlots; i++)
  132. _pListArray[i].Destruct();
  133. return S_OK;
  134. }