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.

302 lines
6.8 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. CharVector.h
  5. Abstract:
  6. A light-weight vector implementation.
  7. Created:
  8. 03/14/2000 robkenny
  9. Modified:
  10. 06/19/2000 robkenny Converted Resize() and Append() to return BOOL to know if malloc failed.
  11. 08/14/2001 robkenny Inserted inside the ShimLib namespace.
  12. --*/
  13. #pragma once
  14. #include <new> // for in-place new
  15. namespace ShimLib
  16. {
  17. /*++
  18. Vector template
  19. This vector is extremely fast and simple.
  20. --*/
  21. template <class ClassType> class VectorT
  22. {
  23. public:
  24. typedef VectorT<ClassType> MyType;
  25. protected:
  26. ClassType * vectorList;
  27. int nVectorListMax; // Current size of vectorList array
  28. int nVectorList; // Number of entries in the vectorList array
  29. bool bListIsLocked; // Are we allowed to modify the array?
  30. inline void ValidateIndex(int index) const
  31. {
  32. #if DBG
  33. if (index < 0 || index >= nVectorList)
  34. {
  35. DebugPrintf("VectorT", eDbgLevelError, "VectorT: index %d out of bounds.\n", index);
  36. }
  37. if (nVectorListMax < 0 || nVectorList > nVectorListMax)
  38. {
  39. DebugPrintf("VectorT", eDbgLevelError, "VectorT: invalid nVectorListMax(%d) nVectorList(%d).\n", nVectorListMax, nVectorList);
  40. }
  41. #endif
  42. }
  43. public:
  44. VectorT()
  45. {
  46. // Init
  47. vectorList = NULL;
  48. nVectorListMax = 0;
  49. nVectorList = 0;
  50. bListIsLocked = false;
  51. }
  52. ~VectorT()
  53. {
  54. Erase();
  55. }
  56. // A Copy constructor
  57. VectorT(const MyType & copyMe)
  58. {
  59. // Init
  60. vectorList = NULL;
  61. nVectorListMax = 0;
  62. nVectorList = 0;
  63. bListIsLocked = false;
  64. Duplicate(copyMe);
  65. }
  66. // Assignment operator, this gets a copy of other
  67. MyType & operator = (const MyType & other)
  68. {
  69. if (this != & other)
  70. Duplicate(other);
  71. return *this;
  72. }
  73. // Copy copyMe into this
  74. void Duplicate(const MyType & copyMe)
  75. {
  76. Erase();
  77. // Copy class data
  78. Resize(copyMe.Size()); // Allocates space
  79. nVectorListMax = copyMe.nVectorListMax;
  80. nVectorList = copyMe.nVectorList;
  81. bListIsLocked = copyMe.bListIsLocked;
  82. // Copy array data
  83. size_t nBytes = nVectorListMax * sizeof(ClassType);
  84. memcpy(vectorList, copyMe.vectorList, nBytes);
  85. }
  86. void CopyElement(int index, const ClassType & element)
  87. {
  88. // Use memcpy to avoid any assignment operators.
  89. void * dest = & Get(index);
  90. const void * src = & element;
  91. memcpy(dest, src, sizeof(ClassType));
  92. }
  93. // Allow this to be treated like an array.
  94. ClassType & operator [] (int index)
  95. {
  96. ValidateIndex(index);
  97. return vectorList[index];
  98. }
  99. // return the value of the index member
  100. ClassType & Get(int index)
  101. {
  102. ValidateIndex(index);
  103. return vectorList[index];
  104. }
  105. // return the const value of the index member
  106. const ClassType & Get(int index) const
  107. {
  108. ValidateIndex(index);
  109. return vectorList[index];
  110. }
  111. BOOL Resize(int size)
  112. {
  113. if (size > nVectorListMax)
  114. {
  115. size_t newVectorListSize = size * sizeof(ClassType);
  116. ClassType * newVectorList = (ClassType *)malloc(newVectorListSize);
  117. if (newVectorList)
  118. {
  119. size_t origSize = nVectorListMax * sizeof(ClassType);
  120. memcpy(newVectorList, vectorList, origSize);
  121. free(vectorList);
  122. vectorList = newVectorList;
  123. nVectorListMax = size;
  124. }
  125. }
  126. // We were successful if there is enough space in the array
  127. return nVectorListMax >= size;
  128. }
  129. // return the number of entries in the list
  130. int Size() const
  131. {
  132. return nVectorList;
  133. }
  134. // return the current MAXIMUM number of entries in the list
  135. int MaxSize() const
  136. {
  137. return nVectorListMax;
  138. }
  139. // Lock the list (prevent further additions)
  140. void Lock(bool lock = true)
  141. {
  142. bListIsLocked = lock;
  143. }
  144. // return true if the list is locked.
  145. bool IsLocked() const
  146. {
  147. return bListIsLocked;
  148. }
  149. // Reset number of entries in the list to 0
  150. void Reset()
  151. {
  152. nVectorList = 0;
  153. }
  154. // Remove all entries in the list
  155. void Erase()
  156. {
  157. Reset();
  158. if (vectorList)
  159. free(vectorList);
  160. vectorList = NULL;
  161. nVectorListMax = 0;
  162. }
  163. // Search for the member in the list, return index or -1
  164. int Find(const ClassType & member) const
  165. {
  166. for (int i = 0; i < Size(); ++i)
  167. {
  168. if (Get(i) == member)
  169. return i;
  170. }
  171. return -1;
  172. }
  173. // Add this item to the end of the list
  174. BOOL Append(const ClassType & member)
  175. {
  176. if (!bListIsLocked)
  177. {
  178. // Increase array size
  179. if (Resize(nVectorList + 1))
  180. {
  181. nVectorList += 1;
  182. CopyElement(nVectorList-1, member);
  183. return TRUE;
  184. }
  185. }
  186. return FALSE;
  187. }
  188. // Append this to the list, if it does not already exist
  189. // Return FALSE if any allocation failed.
  190. BOOL AppendUnique(const ClassType & member)
  191. {
  192. if (!bListIsLocked)
  193. {
  194. int index = Find(member);
  195. if (index == -1)
  196. {
  197. return Append(member);
  198. }
  199. }
  200. return TRUE;
  201. }
  202. // Add this item to the end of the list,
  203. // Use the assignment operator to set the new member.
  204. BOOL AppendConstruct(const ClassType & member)
  205. {
  206. if (!bListIsLocked)
  207. {
  208. // Increase array size
  209. if (Resize(nVectorList + 1))
  210. {
  211. // Must increase the size of the array before calling Get()
  212. // otherwise we'll over index the array.
  213. nVectorList += 1;
  214. ClassType & last = Get(nVectorList-1);
  215. new (&last) ClassType; // inplace new
  216. last = member;
  217. return TRUE;
  218. }
  219. }
  220. return FALSE;
  221. }
  222. // remove this index from the list. This does not keep the list order.
  223. void Remove(int index)
  224. {
  225. if (!bListIsLocked)
  226. {
  227. if (index >= 0 && index < Size())
  228. {
  229. // Remove the entry by copying the last entry over this index
  230. // Only move if this is not the last entry.
  231. if (index < Size() - 1)
  232. {
  233. CopyElement(index, Get(Size() - 1));
  234. }
  235. nVectorList -= 1;
  236. }
  237. }
  238. }
  239. };
  240. /*++
  241. Char Vector type class.
  242. --*/
  243. class CharVector : public VectorT<char *>
  244. {
  245. };
  246. }; // end of namespace ShimLib