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.

238 lines
4.8 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: A R R A Y . H
  7. //
  8. // Contents: Simple array class
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 17 Aug 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. template <class Type>
  17. HRESULT HrTypeAssign(Type & dst, const Type & src)
  18. {
  19. dst = src;
  20. return S_OK;
  21. }
  22. template <class Type>
  23. void TypeTransfer(Type & dst, Type & src)
  24. {
  25. dst = src;
  26. }
  27. template <class Type>
  28. void TypeClear(Type & type)
  29. {
  30. type = Type();
  31. }
  32. const long CUA_NOT_FOUND = -1;
  33. template <class Type>
  34. class CUArray
  35. {
  36. public:
  37. CUArray() : m_pData(NULL), m_nCount(0), m_nReserve(0) {}
  38. ~CUArray()
  39. {
  40. Clear();
  41. }
  42. // Sizing functions
  43. HRESULT HrSetReserve(long nReserve)
  44. {
  45. // Can't ever shrink
  46. if(nReserve < m_nReserve || 0 == nReserve)
  47. {
  48. return E_INVALIDARG;
  49. }
  50. Type * pData = new Type[nReserve];
  51. if(!pData)
  52. {
  53. return E_OUTOFMEMORY;
  54. }
  55. // Copy old data
  56. for(long n = 0; n < m_nCount; ++n)
  57. {
  58. TypeTransfer(pData[n], m_pData[n]);
  59. }
  60. delete [] m_pData;
  61. m_pData = pData;
  62. m_nReserve = nReserve;
  63. return S_OK;
  64. }
  65. HRESULT HrSetCount(long nCount)
  66. {
  67. // Can't ever shrink
  68. if(nCount < m_nCount || 0 == nCount)
  69. {
  70. return E_INVALIDARG;
  71. }
  72. if(nCount > m_nReserve)
  73. {
  74. HRESULT hr = HrSetReserve(nCount);
  75. if(FAILED(hr))
  76. {
  77. return hr;
  78. }
  79. }
  80. m_nCount = nCount;
  81. return S_OK;
  82. }
  83. long GetCount() const
  84. {
  85. return m_nCount;
  86. }
  87. void Clear()
  88. {
  89. delete [] m_pData;
  90. m_pData = NULL;
  91. m_nCount = 0;
  92. m_nReserve = 0;
  93. }
  94. // Data access functions
  95. Type & operator[](long nIndex)
  96. {
  97. Assert(nIndex < m_nCount && nIndex >= 0);
  98. return m_pData[nIndex];
  99. }
  100. const Type & operator[](long nIndex) const
  101. {
  102. Assert(nIndex < m_nCount && nIndex >= 0);
  103. return m_pData[nIndex];
  104. }
  105. Type * GetData()
  106. {
  107. return m_pData;
  108. }
  109. // Insertion function
  110. HRESULT HrPushBack(const Type & type)
  111. {
  112. if(m_nCount == m_nReserve)
  113. {
  114. // Don't thrash on inserts
  115. HRESULT hr = HrSetReserve(m_nReserve + 50);
  116. if(FAILED(hr))
  117. {
  118. return hr;
  119. }
  120. }
  121. HRESULT hr = HrTypeAssign(m_pData[m_nCount], type);
  122. if(SUCCEEDED(hr))
  123. {
  124. ++m_nCount;
  125. }
  126. return hr;
  127. }
  128. HRESULT HrPushBackDefault()
  129. {
  130. HRESULT hr = S_OK;
  131. if(m_nCount == m_nReserve)
  132. {
  133. // Don't thrash on inserts
  134. hr = HrSetReserve(m_nReserve + 50);
  135. }
  136. if(SUCCEEDED(hr))
  137. {
  138. ++m_nCount;
  139. }
  140. return hr;
  141. }
  142. Type & Back()
  143. {
  144. Assert(m_nCount);
  145. return m_pData[0];
  146. }
  147. HRESULT HrPushBackTransfer(Type & type)
  148. {
  149. if(m_nCount == m_nReserve)
  150. {
  151. // Don't thrash on inserts
  152. HRESULT hr = HrSetReserve(m_nReserve + 50);
  153. if(FAILED(hr))
  154. {
  155. return hr;
  156. }
  157. }
  158. TypeTransfer(m_pData[m_nCount], type);
  159. ++m_nCount;
  160. return S_OK;
  161. }
  162. // Removal
  163. HRESULT HrPopBack()
  164. {
  165. if(!m_nCount)
  166. {
  167. return E_UNEXPECTED;
  168. }
  169. --m_nCount;
  170. TypeClear(m_pData[m_nCount]);
  171. return S_OK;
  172. }
  173. HRESULT HrErase(long nIndex)
  174. {
  175. if(nIndex < 0 || nIndex >= m_nCount)
  176. {
  177. return E_INVALIDARG;
  178. }
  179. for(long n = nIndex; n < (m_nCount-1); ++n)
  180. {
  181. TypeTransfer(m_pData[n], m_pData[n+1]);
  182. }
  183. return HrPopBack();
  184. }
  185. // Search
  186. HRESULT HrFind(const Type & type, long & nIndex) const
  187. {
  188. HRESULT hr = E_INVALIDARG;
  189. for(long n = 0; n < m_nCount; ++n)
  190. {
  191. if(type == m_pData[n])
  192. {
  193. nIndex = n;
  194. hr = S_OK;
  195. break;
  196. }
  197. }
  198. return hr;
  199. }
  200. void Swap(CUArray & ref)
  201. {
  202. Type * pData = m_pData;
  203. m_pData = ref.m_pData;
  204. ref.m_pData = pData;
  205. long nCount = m_nCount;
  206. m_nCount = ref.m_nCount;
  207. ref.m_nCount = nCount;
  208. long nReserve = m_nReserve;
  209. m_nReserve = ref.m_nReserve;
  210. ref.m_nReserve = nReserve;
  211. }
  212. private:
  213. CUArray(const CUArray &);
  214. CUArray & operator=(const CUArray &);
  215. Type * m_pData;
  216. long m_nCount;
  217. long m_nReserve;
  218. };