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.

234 lines
4.6 KiB

  1. /*++
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. rss_hash.hxx
  5. Abstract:
  6. Template for a hash table class.
  7. Author:
  8. Ran Kalach
  9. Revision History:
  10. 04/22/2002 rankala Copying from vss project nt\drivers\storage\volsnap\vss\server\inc\vs_hash.hxx
  11. 06/03/2000 aoltean Porting it from ATL code in order to add string comparison
  12. --*/
  13. #ifndef _RSSHASH_
  14. #define _RSSHASH_
  15. ////////////////////////////////////////////////////////////////////////////////////////
  16. // Utilities
  17. //
  18. inline BOOL RssHashAreKeysEqual( const LPCWSTR& lhK, const LPCWSTR& rhK )
  19. {
  20. return (::wcscmp(lhK, rhK) == 0);
  21. }
  22. inline BOOL RssHashAreKeysEqual( const LPWSTR& lhK, const LPWSTR& rhK )
  23. {
  24. return (::wcscmp(lhK, rhK) == 0);
  25. }
  26. template < class KeyType >
  27. inline BOOL RssHashAreKeysEqual( const KeyType& lhK, const KeyType& rhK )
  28. {
  29. return lhK == rhK;
  30. }
  31. ////////////////////////////////////////////////////////////////////////////////////////
  32. // Definitions
  33. //
  34. /*++
  35. Class:
  36. CRssSimpleMap
  37. Description:
  38. Intended for small number of simple types or pointers.
  39. Adapted from the ATL class to work with strings also.
  40. --*/
  41. template <class TKey, class TVal>
  42. class CRssSimpleMap
  43. {
  44. public:
  45. TKey* m_aKey;
  46. TVal* m_aVal;
  47. int m_nSize;
  48. // Construction/destruction
  49. CRssSimpleMap() : m_aKey(NULL), m_aVal(NULL), m_nSize(0)
  50. { }
  51. ~CRssSimpleMap()
  52. {
  53. RemoveAll();
  54. }
  55. // Operations
  56. int GetSize() const
  57. {
  58. return m_nSize;
  59. }
  60. BOOL Add(TKey key, TVal val)
  61. {
  62. TKey* pKey;
  63. pKey = (TKey*)realloc(m_aKey, (m_nSize + 1) * sizeof(TKey));
  64. if(pKey == NULL)
  65. return FALSE;
  66. m_aKey = pKey;
  67. TVal* pVal;
  68. pVal = (TVal*)realloc(m_aVal, (m_nSize + 1) * sizeof(TVal));
  69. if(pVal == NULL)
  70. return FALSE;
  71. m_aVal = pVal;
  72. m_nSize++;
  73. SetAtIndex(m_nSize - 1, key, val);
  74. return TRUE;
  75. }
  76. BOOL Remove(TKey key)
  77. {
  78. int nIndex = FindKey(key);
  79. if(nIndex == -1)
  80. return FALSE;
  81. // Call destructors in all cases (Bug 470439)
  82. m_aKey[nIndex].~TKey();
  83. m_aVal[nIndex].~TVal();
  84. if(nIndex != (m_nSize - 1))
  85. {
  86. memmove((void*)(m_aKey + nIndex), (void*)(m_aKey + nIndex + 1), (m_nSize - (nIndex + 1)) * sizeof(TKey));
  87. memmove((void*)(m_aVal + nIndex), (void*)(m_aVal + nIndex + 1), (m_nSize - (nIndex + 1)) * sizeof(TVal));
  88. }
  89. TKey* pKey;
  90. pKey = (TKey*)realloc(m_aKey, (m_nSize - 1) * sizeof(TKey));
  91. if(pKey != NULL || m_nSize == 1)
  92. m_aKey = pKey;
  93. TVal* pVal;
  94. pVal = (TVal*)realloc(m_aVal, (m_nSize - 1) * sizeof(TVal));
  95. if(pVal != NULL || m_nSize == 1)
  96. m_aVal = pVal;
  97. m_nSize--;
  98. return TRUE;
  99. }
  100. void RemoveAll()
  101. {
  102. if(m_aKey != NULL)
  103. {
  104. for(int i = 0; i < m_nSize; i++)
  105. {
  106. m_aKey[i].~TKey();
  107. m_aVal[i].~TVal();
  108. }
  109. free(m_aKey);
  110. m_aKey = NULL;
  111. }
  112. if(m_aVal != NULL)
  113. {
  114. free(m_aVal);
  115. m_aVal = NULL;
  116. }
  117. m_nSize = 0;
  118. }
  119. BOOL SetAt(TKey key, TVal val)
  120. {
  121. int nIndex = FindKey(key);
  122. if(nIndex == -1)
  123. return FALSE;
  124. SetAtIndex(nIndex, key, val);
  125. return TRUE;
  126. }
  127. TVal Lookup(TKey key) const
  128. {
  129. int nIndex = FindKey(key);
  130. if(nIndex == -1)
  131. return NULL; // must be able to convert
  132. return GetValueAt(nIndex);
  133. }
  134. TKey ReverseLookup(TVal val) const
  135. {
  136. int nIndex = FindVal(val);
  137. if(nIndex == -1)
  138. return NULL; // must be able to convert
  139. return GetKeyAt(nIndex);
  140. }
  141. TKey& GetKeyAt(int nIndex) const
  142. {
  143. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  144. return m_aKey[nIndex];
  145. }
  146. TVal& GetValueAt(int nIndex) const
  147. {
  148. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  149. return m_aVal[nIndex];
  150. }
  151. // Implementation
  152. template <typename T>
  153. class Wrapper
  154. {
  155. public:
  156. Wrapper(T& _t) : t(_t)
  157. {
  158. }
  159. template <typename _Ty>
  160. void *operator new(size_t, _Ty* p)
  161. {
  162. return p;
  163. }
  164. template <typename _Ty>
  165. void operator delete(void * /*pMem*/, _Ty* /*p*/)
  166. {
  167. }
  168. T t;
  169. };
  170. void SetAtIndex(int nIndex, TKey& key, TVal& val)
  171. {
  172. ATLASSERT(nIndex >= 0 && nIndex < m_nSize);
  173. new(m_aKey + nIndex) Wrapper<TKey>(key);
  174. new(m_aVal + nIndex) Wrapper<TVal>(val);
  175. }
  176. int FindKey(TKey& key) const
  177. {
  178. for(int i = 0; i < m_nSize; i++)
  179. {
  180. // [aoltean] Comparing strings also
  181. if(::RssHashAreKeysEqual(m_aKey[i], key))
  182. return i;
  183. }
  184. return -1; // not found
  185. }
  186. int FindVal(TVal& val) const
  187. {
  188. for(int i = 0; i < m_nSize; i++)
  189. {
  190. // [aoltean] Comparing strings also
  191. if(::RssHashAreKeysEqual(m_aVal[i], val))
  192. return i;
  193. }
  194. return -1; // not found
  195. }
  196. };
  197. #endif // _RSSHASH_