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.

207 lines
4.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: T A B L E . H
  7. //
  8. // Contents: Simple templated table class
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 17 Aug 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #include "Array.h"
  17. template <class Key, class Value>
  18. class CTable
  19. {
  20. public:
  21. typedef CUArray<Key> KeyArray;
  22. typedef CUArray<Value> ValueArray;
  23. CTable() {}
  24. ~CTable() {}
  25. // Insertion
  26. HRESULT HrInsert(const Key & key, const Value & value)
  27. {
  28. Value * pValue = Lookup(key);
  29. if(pValue)
  30. {
  31. return HrTypeAssign(*pValue, value);
  32. }
  33. HRESULT hr;
  34. hr = m_keys.HrPushBack(key);
  35. if(SUCCEEDED(hr))
  36. {
  37. hr = m_values.HrPushBack(value);
  38. if(FAILED(hr))
  39. {
  40. m_keys.HrPopBack();
  41. }
  42. }
  43. return hr;
  44. }
  45. HRESULT HrInsertTransfer(Key & key, Value & value)
  46. {
  47. HRESULT hr = S_OK;
  48. Value * pValue = Lookup(key);
  49. if(pValue)
  50. {
  51. TypeTransfer(*pValue, value);
  52. return hr;
  53. }
  54. hr = m_keys.HrPushBackTransfer(key);
  55. if(SUCCEEDED(hr))
  56. {
  57. hr = m_values.HrPushBackTransfer(value);
  58. if(FAILED(hr))
  59. {
  60. m_keys.HrPopBack();
  61. }
  62. }
  63. return hr;
  64. }
  65. HRESULT HrAppendTableTransfer(CTable<Key, Value> & table)
  66. {
  67. HRESULT hr = S_OK;
  68. long n;
  69. long nCount = table.Keys().GetCount();
  70. // Transfer items
  71. for(n = 0; n < nCount && SUCCEEDED(hr); ++n)
  72. {
  73. hr = HrInsertTransfer(table.m_keys[n], table.m_values[n]);
  74. }
  75. // Remove items if we failed
  76. if(FAILED(hr))
  77. {
  78. nCount = n;
  79. for(n = 0; n < nCount; ++n)
  80. {
  81. m_keys.HrPopBack();
  82. m_values.HrPopBack();
  83. }
  84. }
  85. // Clear the passed in table
  86. table.Clear();
  87. return hr;
  88. }
  89. // Data access
  90. Value * Lookup(const Key & key)
  91. {
  92. long nCount = m_keys.GetCount();
  93. for(long n = 0; n < nCount; ++n)
  94. {
  95. if(key == m_keys[n])
  96. {
  97. return &m_values[n];
  98. }
  99. }
  100. return NULL;
  101. }
  102. const Value * Lookup(const Key & key) const
  103. {
  104. long nCount = m_keys.GetCount();
  105. for(long n = 0; n < nCount; ++n)
  106. {
  107. if(key == m_keys[n])
  108. {
  109. return &m_values[n];
  110. }
  111. }
  112. return NULL;
  113. }
  114. HRESULT HrLookup(const Key & key, Value ** ppValue)
  115. {
  116. if(!ppValue)
  117. {
  118. return E_POINTER;
  119. }
  120. HRESULT hr = E_INVALIDARG;
  121. *ppValue = Lookup(key);
  122. if(*ppValue)
  123. {
  124. hr = S_OK;
  125. }
  126. return hr;
  127. }
  128. HRESULT HrLookup(const Key & key, const Value ** ppValue) const
  129. {
  130. if(!ppValue)
  131. {
  132. return E_POINTER;
  133. }
  134. HRESULT hr = E_INVALIDARG;
  135. *ppValue = Lookup(key);
  136. if(*ppValue)
  137. {
  138. hr = S_OK;
  139. }
  140. return hr;
  141. }
  142. // Cleanup
  143. void Clear()
  144. {
  145. m_keys.Clear();
  146. m_values.Clear();
  147. }
  148. // Removal
  149. HRESULT HrErase(const Key & key)
  150. {
  151. long nCount = m_keys.GetCount();
  152. for(long n = 0; n < nCount; ++n)
  153. {
  154. if(key == m_keys[n])
  155. {
  156. HRESULT hr;
  157. hr = m_keys.HrErase(n);
  158. Assert(SUCCEEDED(hr));
  159. if(SUCCEEDED(hr))
  160. {
  161. hr = m_values.HrErase(n);
  162. }
  163. return hr;
  164. }
  165. }
  166. return E_INVALIDARG;
  167. }
  168. // Iteration
  169. const KeyArray & Keys() const
  170. {
  171. return m_keys;
  172. }
  173. const ValueArray & Values() const
  174. {
  175. return m_values;
  176. }
  177. // Special
  178. void Swap(CTable<Key, Value> & table)
  179. {
  180. m_keys.Swap(table.m_keys);
  181. m_values.Swap(table.m_values);
  182. }
  183. private:
  184. CTable(const CTable &);
  185. CTable & operator=(const CTable &);
  186. KeyArray m_keys;
  187. ValueArray m_values;
  188. };